# max/min di funzioni di una variabile # funzione "optimize" # HELP: # The function optimize searches the interval from "lower" to "upper" for a minimum or maximum of the function f with respect to its first argument. # Usage # # optimize(f, interval, ..., lower = min(interval), upper = max(interval), maximum = FALSE, tol = .Machine$double.eps^0.25) # Arguments # # f: the function to be optimized. The function is either minimized or maximized over its first argument depending on the value of maximum. # interval: a vector containing the end-points of the interval to be searched for the minimum. # lower: the lower end point of the interval to be searched. # upper: the upper end point of the interval to be searched. # maximum: logical. Should we maximize or minimize (the default)? # tol: the desired accuracy. # Note that arguments after ... must be matched exactly. # # The method used is a combination of golden section search and successive parabolic interpolation, and was designed for use with continuous functions. Convergence is never much slower than that for a Fibonacci search. If f has a continuous second derivative which is positive at the minimum (which is not at lower or upper), then convergence is superlinear, and usually of the order of about 1.324. # # The function f is never evaluated at two points closer together than eps * |x_0| + (tol/3), where eps is approximately sqrt(.Machine$double.eps) and x_0 is the final abscissa optimize()$minimum. # If f is a unimodal function and the computed values of f are always unimodal when separated by at least eps * |x| + (tol/3), then x_0 approximates the abscissa of the global minimum of f on the interval lower,upper with an error less than eps * |x_0|+ tol. # If f is not unimodal, then optimize() may approximate a local, but perhaps non-global, minimum to the same accuracy. # # The first evaluation of f is always at x_1 = a + (1-phi)(b-a) where (a,b) = (lower, upper) and phi = (sqrt(5) - 1)/2 = 0.61803.. is the golden section ratio. Almost always, the second evaluation is at x_2 = a + phi(b-a). Note that a local minimum inside [x_1,x_2] will be found as solution, even when f is constant in there, see the last example. # # f will be called as f(x, ...) for a numeric value of x. # # The argument passed to f has special semantics and used to be shared between calls. The function should not copy it. # 1) trovare max/min di f(x) = sqrt(x*(1-x)) [max in 0.5, min in 0 or 1] # 2) trovare max/min di x ^ 2 * (x - 1); max rel in 0, min rel in 2/3 # 3) trovare il max della densita normale (in corrispondenza alla media) # 5) f(x) = exp(-1 / |x - 1|) se -1 < x < 4, 10 altrimenti (funzione discontinua in -1 e 4) # definita per continuita' in 1 (valore 0) # usare ifelse(test, yes, no) # funzione costante al di fuori di (-1, 4) e quasi costante intorno al p. di minimo # 6) f(x) = sen(x) / x per x \= 0, 1 per x = 0 # max in 0, min in 4.493409 e -4.493409 # la derivata e' f'(x) = (x * cos(x) - sen(x)) / x ^ 2 # trovare lo zero equivale a risolvere l'equazione tan(x) = x # 7) trovare min di f(x) = 10 * sin(0.3 * x) * sin(1.3 * x ^ 2) + 0.00001 * x ^ 4 + 0.2 * x + 80 # funzione estremamente irregolare, min assoluto a -15.81515 # multipli max e min relativi # risoluzione di un'equazione in dimensione 1 del tipo f(x) = 0 # funzione: "uniroot" # The function uniroot searches the interval from lower to upper for a root (i.e., zero) of the function f with respect to its first argument. # Setting extendInt to a non-"no" string, means searching for the correct interval = c(lower,upper) if sign(f(x)) does not satisfy the requirements at the interval end points; see the 'Details' section. # Usage # uniroot(f, interval, ..., lower = min(interval), upper = max(interval), f.lower = f(lower, ...), f.upper = f(upper, ...), extendInt = c("no", "yes", "downX", "upX"), check.conv = FALSE, tol = .Machine$double.eps^0.25, maxiter = 1000, trace = 0) # Arguments # f: the function for which the root is sought. # interval a vector containing the end-points of the interval to be searched for the root. # ...: additional named or unnamed arguments to be passed to f # lower, upper: the lower and upper end points of the interval to be searched. # f.lower, f.upper: the same as f(upper) and f(lower), respectively. Passing these values from the caller where they are often known is more economical as soon as f() contains non-trivial computations. # extendInt: character string specifying if the interval c(lower,upper) should be extended or directly produce an error when f() does not have differing signs at the endpoints. The default, "no", keeps the search interval and hence produces an error. Can be abbreviated. # check.conv: logical indicating whether a convergence warning of the underlying uniroot should be caught as an error and if non-convergence in maxiter iterations should be an error instead of a warning. # tol: the desired accuracy (convergence tolerance). # maxiter: the maximum number of iterations. # trace: integer number; if positive, tracing information is produced. Higher values giving more details. # 8) risolvere cos(x) = x: ## 9) risolvere exp(x) + x = 0: # se la funzione ha lo stesso valore agli estremi # se si sa se la funzione e' crescente o decrescente # 10) trovare TIR del coupon bond che paga cedole pari a 2 ogni 6 mesi per 10 anni, prezzo 110 oggi # 11) pericolo nell'estensione dell'intervallo: # zero di una funzione positiva f # numericamente, f diventa zero: ## Convergence checking : # zero in pi + 2 * k * pi, k = +-1, +-2, +-3, ...