fzero

Finds a root of a function of one variable.

Category: Mathematics > Optimization

Syntax

x = fzero(fun, x0)

Searches from the scalar x0 for a point where fun changes sign.

x = fzero(fun, [a b])

Searches inside an interval whose end values have opposite signs.

x = fzero(fun, x0, options)

Same, with settings created by optimset.

[x, fval, exitflag, output] = fzero(___)

Also returns the function value, a status flag and run statistics.

Description

fzero combines bisection, the secant method and inverse quadratic interpolation. Bisection guarantees progress and the faster methods are used whenever they stay inside the bracket.

It finds points where the function crosses zero. A root where the function only touches zero without changing sign, such as x^2 at 0, cannot be bracketed and is usually missed. It returns one root per call; to find others, use a different starting point or interval.

Input arguments

fun
Function handle that takes a scalar and returns a scalar.
x0
Starting guess. The method expands a bracket around it until it finds a sign change.
[a b]
Bracketing interval. fun(a) and fun(b) must have different signs, otherwise it is an error.

Output arguments

x
The root found.
fval
fun(x), close to zero.
exitflag
1 when a root was found.
output
Struct with intervaliterations, iterations, funcCount, algorithm and message.

Example

Root in an interval and from a guess

x = fzero(@cos, [1 2]);
fprintf('%.6f\n', x)
[r, fval, flag] = fzero(@(t) t^2 - 2, 1);
fprintf('%.6f %.1e %d\n', r, abs(fval), flag)
Output
1.570796
1.414214 4.4e-16 1

See also

fminbnd fminsearch roots optimset fplot