Minimum of a function.
x = fminbnd(fun, x0) x = fminbnd(fun, [xlow,xhigh]) x = fminbnd(..., options) x = fminbnd(..., options, ...)
fminbnd(fun,...) finds numerically a local minimum of function fun. fun is either specified by its name or given as an inline function. It has at least one input argument x, and it returns one output argument, also a real number. fminbnd finds the value x such that fun(x) is minimized.
Second argument tells where to search; it can be either a starting point or a pair of values which must bracket the minimum.
The optional third argument may contain options. It is either the empty array [] for the default options, or the result of optimset (the use of a number for the relative tolerance is deprecated).
Remaining input arguments of fminbnd, if any, are given as additional input arguments to function fun. They permit to parameterize the function. For example fminbnd('fun',x0,[],2,5) calls fun as fun(x,2,5) and minimizes its value with respect to x.
Minimum of a sine near 2, displayed with 15 digits:
fprintf('%.15g\n', fminbnd('sin', 2)); 4.712389014989218
To find the minimum of
fun = inline('c*exp(x)-sin(x)', 'x', 'c');
Then fminbnd is used, with the value of c passed as an additional argument:
x = fminbnd(fun,[-1,10],[],0.1) x = 1.2239
Zero of a function.
x = fzero(fun,x0) x = fzero(fun,[xlow,xhigh]) x = fzero(...,options) x = fzero(...,options,...)
fzero(fun,...) finds numerically a zero of function fun. fun is either specified by its name or given as an inline function or function reference. It has at least one input argument x, and it returns one output argument, also a real number. fzero finds the value x such that fun(x)==0, up to some tolerance.
Second argument tells where to search; it can be either a starting point or a pair of values xlow and xhigh which must bracket the zero, such that fun(xlow) and fun(xhigh) have opposite sign.
The optional third argument may contain options. It is either the empty array [] for the default options, or the result of optimset (the use of a number for the relative tolerance is deprecated).
Additional input arguments of fzero are given as additional input arguments to the function specified by fun. They permit to parameterize the function.
Zero of a sine near 3, displayed with 15 digits:
fprintf('%.15g\n', fzero('sin', 3)); 3.141592653589793
To find the solution of
function y = f(x,c) y = exp(x) - c - sqrt(x);
Then fsolve is used, with the value of c passed as an additional argument:
x = fzero('f',[0,100],[],10) x = 2.4479 f(x,10) 1.9984e-15
optimset, fminbnd, inline, roots
Ordinary differential equation integration.
(t,y) = ode23(fun,[t0,tend],y0) (t,y) = ode23(fun,[t0,tend],y0,options) (t,y) = ode23(fun,[t0,tend],y0,options,...) (t,y) = ode45(fun,[t0,tend],y0) (t,y) = ode45(fun,[t0,tend],y0,options) (t,y) = ode45(fun,[t0,tend],y0,options,...)
ode23(fun,[t0,tend],y0) and ode45(fun,[t0,tend],y0) integrate numerically an ordinary differential equation (ODE). Both functions are based on a Runge-Kutta algorithm with adaptive time step; ode23 is low-order and ode45 high-order. In most cases for non-stiff equations, ode45 is the best method. The function to be integrated is either specified by its name or given as an inline function. It should have at least two input arguments and exactly one output argument:
function yp = f(t,y)
The function calculates the derivative yp of the state vector y at time t.
Integration is performed over the time range specified by the second argument [t0,tend], starting from the initial state y0.
The optional fourth argument may contain options. It is either the empty array [] for the default options, or the result of odeset (the use of a vector of option values is deprecated.)
Additional input arguments of ode45 are given as additional input arguments to the function specified by fun. They permit to parameterize the ODE.
Let us integrate the following ordinary differential equation (Van Der Pol equation),
parameterized by
Let
y2' = mu (1 - y1^2) y2 - y1
and may be computed by the following function:
function yp = f(t,y,mu) yp = [y(2); mu*(1-y(1)^2)*y(2)-y(1)];
The following ode45 call integrates the Van Der Pol equation from
0 to 1 with the default options, starting from
(t,y)=ode45('f',[0,1],[2;0],[],1)
Options for ordinary differential equation integration.
options = odeset options = odeset(name1, value1, ...) options = odeset(options0, name1, value1, ...)
odeset(name1,value1,...) creates the option argument used by ode23 and ode45. Options are specified with name/value pairs, where the name is a string which must match exactly the names in the table below. Case is significant. Options which are not specified have a default value. The result is a structure whose fields correspond to each option. Without any input argument, odeset creates a structure with all the default options. Note that ode23 and ode45 also interpret the lack of an option argument, or the empty array [], as a request to use the default values.
When its first input argument is a structure, odeset adds or changes fields which correspond to the name/value pairs which follow.
Here is the list of permissible options (empty arrays mean "automatic"):
Name | Default | Meaning |
---|---|---|
RelTol | 1e-3 | maximum relative error |
AbsTol | 1e-6 | maximum absolute error |
MinStep | [] | minimum time step (default: time range/1e6) |
MaxStep | [] | maximum time step (default: time range/10) |
InitialStep | [] | initial time step (default: 10*MinStep) |
Refine | [] | refinement factor (default: 1, 4 for ode45) |
Stats | false | statistics display |
Most options control how the time step is tuned during the numerical integration. The errors are calculated for each element of y separately. If the requirements cannot be satisfied, for instance if the system is stiff and requires a too small integration step, integration is aborted. 'Refine' specifies how many points are added to the result for each integration step. When it is larger than 1, additional points are interpolated, which is faster than reducing MaxStep.
Default options:
odeset {.RelTol 1e-3,.AbsTol 1e-6, .MinStep [],.MaxStep [],.InitialStep [], .Refine [],.Stats false}
ode45 is typically able to use large time steps to achieve the requested tolerance. When plotting the output, however, interpolating it with straight lines produces visual artifacts. This is why ode45 inserts 3 interpolated points for each calculated point, based on the fifth-order approximation calculated for the integration (Refine is 4 by default). In the following code, curves with and without interpolation are compared. Note that the numbers of evaluations of the function being integrated are the same.
fun = inline('[y(2); mu*(1-y(1)^2)*y(2)-y(1)]', 't', 'y', 'mu'); (t, y) = ode45(fun, [0,10], [2;0], odeset('Refine',1,'Stats',true), 1); Number of function evaluations: 523 Successful steps: 69 Failed steps (error too large): 18 size(y) 70 2 (ti, yi) = ode45(fun, [0,10], [2;0], odeset('Stats',true), 1); Number of function evaluations: 523 Successful steps: 69 Failed steps (error too large): 18 size(yi) 277 2 plot(ti', yi', 'g'); plot(t', y');
Options for minimization and zero finding.
options = optimset options = optimset(name1, value1, ...) options = optimset(options0, name1, value1, ...)
optimset(name1,value1,...) creates the option argument used by fminbnd and fzero. Options are specified with name/value pairs, where the name is a string which must match exactly the names in the table below. Case is significant. Options which are not specified have a default value. The result is a structure whose fields correspond to each option. Without any input argument, optimset creates a structure with all the default options. Note that fminbnd and fzero also interpret the lack of an option argument, or the empty array [], as a request to use the default values.
When its first input argument is a structure, optimset adds or changes fields which correspond to the name/value pairs which follow.
Here is the list of permissible options (empty arrays mean "automatic"):
Name | Default | Meaning |
---|---|---|
TolX | [] | maximum relative error (default: eps, sqrt(eps) for fminbnd) |
MaxIter | 500 | maximum number of iterations |
MaxFunEvals | 1000 | maximum number of evaluations |
Display | false | detailed display |
Default options:
optimset {.TolX [], .MaxIter 500,.MaxFunEvals 1000, .Display false}
Display of the steps performed to find the zero of
fzero(@cos, [1,2], optimset('Display',true)) Checking lower bound Checking upper bound Inverse quadratic interpolation 2,1.5649,1 Inverse quadratic interpolation 1.5649,1.571,2 Inverse quadratic interpolation 1.571,1.5708,1.5649 Inverse quadratic interpolation 1.5708,1.5708,1.571 Inverse quadratic interpolation 1.5708,1.5708,1.571 ans = 1.5708
Numerical integration.
y = quad(fun, a, b) y = quad(fun, a, b, tol) y = quad(fun, a, b, tol, trace) y = quad(fun, a, b, tol, trace, ...)
quad(fun,a,b) integrates numerically function fun between a and b. fun is either specified by its name or given as an inline function.
The optional fourth argument is the requested relative tolerance of the result. It is either a positive real scalar number or the empty matrix (or missing argument) for the default value, which is sqrt(eps). The optional fourth argument, if true or nonzero, makes quad displays information at each step.
Additional input arguments of quad are given as additional input arguments to function fun. They permit to parameterize the function.
quad(inline('t*exp(-t)'), 0, 2) 0.5940