Products & Services Industries Academia Support User Community Company

Learn more about Optimization Toolbox   

fzero - Find root of continuous function of one variable

Syntax

x = fzero(fun,x0)
x = fzero(fun,x0,options)
x = fzero(problem)
[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. fun is a function handle for either an M-file function or an anonymous function. 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 that fzero returns a value near a point where fun changes sign. An interval x0 must be finite; it cannot contain ±Inf.

x = fzero(fun,x0,options) solves the equation with the optimization options specified in the structure options. Use optimset to set these options.

x = fzero(problem) solves problem, where problem is a structure described in Input Arguments.

Create the structure problem by exporting a problem from Optimization Tool, as described in Exporting to the MATLAB Workspace.

[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.

Input Arguments

Function Arguments contains general descriptions of arguments passed into fzero. This section provides function-specific details for fun, options, and problem:

fun

The function whose zero is to be computed. fun is a function handle for a function that accepts a scalar x and returns a scalar, the objective function evaluated at x. The function fun can be specified as a function handle for an M-file function

x = fzero(@myfun,x0)

where myfun is a MATLAB function such as

function f = myfun(x)
f = ...            % Compute function value at x

fun can also be a function handle for an anonymous function.

x = fzero(@(x)sin(x*x),x0);

options

Optimization options. You can set or change the values of these options using the optimset function. fzero uses these options structure fields:

 

Display

Level of display:

  • 'off' displays no output.

  • 'iter' displays output at each iteration.

  • 'final' displays just the final output

  • 'notify' (default) displays output only if the function does not converge.

 

FunValCheck

Check whether objective function values are valid. 'on' displays an error when the objective function returns a value that is complex, Inf, or NaN. The default, 'off', displays no error.

 

OutputFcn

Specify one or more user-defined functions that an optimization function calls at each iteration, either as a function handle or as a cell array of function handles. The default is none ([]). See Output Function.

 

PlotFcns

Plots various measures of progress while the algorithm executes, select from predefined plots or write your own. Pass a function handle or a cell array of function handles. The default is none ([]).

  • @optimplotx plots the current point

  • @optimplotfval plots the function value

 

TolX

Termination tolerance on x, a positive scalar. The default is eps, 2.2204e-16.

problem

objective

Objective function

x0

Initial point for x, scalar or 2–dimensional vector

solver

'fzero'

options

Options structure created with optimset

Output Arguments

Function Arguments contains general descriptions of arguments returned by fzero. This section provides function-specific details for exitflag and output:

exitflag

Integer identifying the reason the algorithm terminated. The following lists the values of exitflag and the corresponding reasons the algorithm terminated.

 

1

Function converged to a solution x.

 

-1

Algorithm was terminated by the output function.

 

-3

NaN or Inf function value was encountered during search for an interval containing a sign change.

 -4

Complex function value was encountered during search for an interval containing a sign change.

 -5

Algorithm might have converged to a singular point.

output

Structure containing information about the optimization. The fields of the structure are

 intervaliterations

Number of iterations taken to find an interval containing a root

 iterations

Number of zero-finding iterations

 funcCount

Number of function evaluations

 algorithm

Optimization algorithm used

 message

Exit message

Examples

Calculate π by finding the zero of the sine function near 3.

x = fzero(@sin,3)
x =
    3.1416

To find the zero of cosine between 1 and 2, enter

x = fzero(@cos,[1 2])
x =
   1.5708

Note that cos(1) and cos(2) differ in sign.

To find a zero of the function

f(x) = x3 – 2x – 5,

write an M-file called f.m.

function y = f(x)
y = x.^3-2*x-5;

To find the zero near 2, enter

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

Algorithm

The fzero 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

The fzero command finds a point where the function changes sign. If the function is continuous, this is also a point where the function has a value near zero. If the function is not continuous, fzero may return values that are discontinuous points instead of zeros. For example, fzero(@tan,1) returns 1.5708, a discontinuous point in tan.

Furthermore, the fzero 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.

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.

See Also

@ (function_handle), \ (matrix left division), fminbnd, fsolve, optimset, optimtool, roots, Anonymous Functions

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS