Non-Linear Numerical Functions

fminbnd

Minimum of a function.

Syntax

x = fminbnd(fun, x0)
x = fminbnd(fun, [xlow,xhigh])
x = fminbnd(..., options)
x = fminbnd(..., options, ...)

Description

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.

Examples

Minimum of a sine near 2, displayed with 15 digits:

fprintf('%.15g\n', fminbnd('sin', 2));
  4.712389014989218

To find the minimum of c*exp(x)-sin(x) between -1 and 10 with c=0.1, the expression is written as an inline function stored in variable fun:

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

See also

optimset, fzero, inline

fzero

Zero of a function.

Syntax

x = fzero(fun,x0)
x = fzero(fun,[xlow,xhigh])
x = fzero(...,options)
x = fzero(...,options,...)

Description

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.

Examples

Zero of a sine near 3, displayed with 15 digits:

fprintf('%.15g\n', fzero('sin', 3));
  3.141592653589793

To find the solution of exp(x)=c+sqrt(x) between 0 and 100 with c=10, a function f whose zero gives the desired solution is written:

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

See also

optimset, fminbnd, inline, roots

ode23 ode45

Ordinary differential equation integration.

Syntax

(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,...)

Description

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.

Example

Let us integrate the following ordinary differential equation (Van Der Pol equation), parameterized by mu:

x" = mu (1 - x^2) x' - x

Let y1=x and y2=x'; their derivatives are

y1' = y2

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 x(0)=2 and x'(0)=0, with mu=1:

(t,y)=ode45('f',[0,1],[2;0],[],1)

See also

odeset, quad, inline, expm

odeset

Options for ordinary differential equation integration.

Syntax

options = odeset
options = odeset(name1, value1, ...)
options = odeset(options0, name1, value1, ...)

Description

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"):

NameDefaultMeaning
RelTol1e-3maximum relative error
AbsTol1e-6maximum 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)
Statsfalsestatistics 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.

Examples

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');

See also

ode23, ode45, optimset

optimset

Options for minimization and zero finding.

Syntax

options = optimset
options = optimset(name1, value1, ...)
options = optimset(options0, name1, value1, ...)

Description

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"):

NameDefaultMeaning
TolX[]maximum relative error (default: eps, sqrt(eps) for fminbnd)
MaxIter500maximum number of iterations
MaxFunEvals1000maximum number of evaluations
Displayfalsedetailed display

Examples

Default options:

optimset
  {.TolX [],
   .MaxIter 500,.MaxFunEvals 1000,
   .Display false}

Display of the steps performed to find the zero of cos(x) between 1 and 2:

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

See also

fzero, fminbnd, odeset

quad

Numerical integration.

Syntax

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, ...)

Description

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.

Example

Integration of t*exp(-t) between 0 and 2:

quad(inline('t*exp(-t)'), 0, 2)
  0.5940

See also

sum, ode45, inline


Copyright 2000-2001, Calerga.

All rights reserved.