| Optimization Toolbox | Search  Help Desk |
| fzero | Examples See Also |
Zero of a function of one variable
Syntax
x = fzero(fun,x0) x = fzero(fun,x0,options) x = fzero(fun,x0,options,P1,P2,...) [x,fval] = fzero(...) [x,fval,exitflag] = fzero(...) [x,fval,exitflag,output] = fzero(...)
Description
x = fzero(fun,x0)
tries to find a zero of fun near x0 if x0 is a scalar. The value x returned by fzero is near a point where fun changes sign, or NaN if the search fails. In this case, the search terminates when the search interval is expanded until an Inf, NaN, or complex value is found.
If x0 is a vector of length two, fzero assumes x0 is an interval where the sign of fun(x0(1)) differs from the sign of fun(x0(2)). An error occurs if this is not true. Calling fzero with such an interval guarantees fzero will return a value near a point where fun changes sign.
x = fzero(fun,x0,options)
minimizes with the optimization parameters specified in the structure options.
x = fzero(fun,x0,options,P1,P2,...)
provides for additional arguments, P1, P2, etc., which are passed to the objective function, fun. Use options=[] as a placeholder if no options are set.
[x,fval] = fzero(...)
returns the value of the objective function fun at the solution x.
[x,fval,exitflag] = fzero(...)
returns a value exitflag that describes the exit condition.
[x,fval,exitflag,output] = fzero(...)
returns a structure output that contains information about the optimization.
Note:
Arguments
The arguments passed into the function are described in Table 1-1. The arguments returned by the function are described in Table 1-2. Details relevant tofzero are included below for fun, options, exitflag, and output.fun |
The function to be minimized. fun takes a scalar x and returns a scalar value f of the objective function evaluated at x. You can specify fun to be an inline object. For example,
x = fzero(inline('sin(x*x)'),x0)Alternatively, fun can be a string containing the name of a function (an M-file, a built-in function, or a MEX-file). If fun='myfun' then the M-file function myfun.m would have the form
function f = myfun(x) f = ... % Compute function value at x |
options |
Optimization parameter options. You can set or change the values of these parameters using the optimset function. fzero uses these options structure fields:
|
exitflag |
Describes the exit condition:
|
output |
A structure whose fields contain information about the optimization: |
Examples
Calculate
by finding the zero of the sine function near 3.
x = fzero(To find the zero of cosine between 1 and 2:'sin',3) x = 3.1416
x = fzero(Note that'cos',[1 2]) x = 1.5708
cos(1) and cos(2) differ in sign.
To find a zero of the function
f.m.
function y = f(x)
y = x.^3-2*x-5;
To find the zero near 2
z = fzero('f',2)
z =
2.0946
Since this function is a polynomial, the statement roots([1 0 -2 -5]) finds the same real zero, and a complex conjugate pair of zeros.
2.0946 -1.0473 + 1.1359i -1.0473 - 1.1359i
Notes
Callingfzero with an interval (x0 with two elements) is often faster than calling it with a scalar x0.
Algorithm
Thefzero command is an M-file. The algorithm, which was originated by T. Dekker, uses a combination of bisection, secant, and inverse quadratic interpolation methods. An Algol 60 version, with some improvements, is given in [1]. A Fortran version, upon which the fzero M-file is based, is in [2].
Limitations
Thefzero command defines a zero as a point where the function crosses the x-axis. Points where the function touches, but does not cross, the x-axis are not valid zeros. For example, y = x.^2 is a parabola that touches the x-axis at 0. Since the function never crosses the x-axis, however, no zero is found. For functions with no valid zeros, fzero executes until Inf, NaN, or a complex value is detected.
See Also
roots, fminbnd, fsolve, \, inline, optimset
References
[1] Brent, R., Algorithms for Minimization Without Derivatives, Prentice-Hall, 1973. [2] Forsythe, G. E., M. A. Malcolm, and C. B. Moler, Computer Methods for Mathematical Computations, Prentice-Hall, 1976.