Mathematics |
Finding Zeros of Functions
The fzero
function attempts to find a zero of one equation with one variable. You can call this function with either a one-element starting point or a two-element vector that designates a starting interval. If you give fzero
a starting point x0, fzero
first searches for an interval around this point where the function changes sign. If the interval is found, then fzero
returns a value near where the function changes sign. If no such interval is found, fzero
returns NaN
. Alternatively, if you know two points where the function value differs in sign, you can specify this starting interval using a two-element vector; fzero
is guaranteed to narrow down the interval and return a value near a sign change.
Use fzero
to find a zero of the humps
function near -0.2
For this starting point, fzero
searches in the neighborhood of -0.2
until it finds a change of sign between -0.10949 and -0.264. This interval is then narrowed down to -0.1316. You can verify that -0.1316 has a function value very close to zero using
Suppose you know two places where the function value of humps
differs in sign such as x = 1
and x = -1
. You can use
Then you can give fzero
this interval to start with and fzero
then returns a point near where the function changes sign. You can display information as fzero
progresses with
options = optimset('Display','iter'); a = fzero(@humps,[-1 1],options) Func-count x f(x) Procedure 1 -1 -5.13779 initial 1 1 16 initial 2 -0.513876 -4.02235 interpolation 3 0.243062 71.6382 bisection 4 -0.473635 -3.83767 interpolation 5 -0.115287 0.414441 bisection 6 -0.150214 -0.423446 interpolation 7 -0.132562 -0.0226907 interpolation 8 -0.131666 -0.0011492 interpolation 9 -0.131618 1.88371e-07 interpolation 10 -0.131618 -2.7935e-11 interpolation 11 -0.131618 8.88178e-16 interpolation 12 -0.131618 -9.76996e-15 interpolation a = -0.1316
The steps of the algorithm include both bisection and interpolation under the Procedure
column. If the example had started with a scalar starting point instead of an interval, the first steps after the initial function evaluations would have included some search steps while fzero
searched for an interval containing a sign change.
You can specify a relative error tolerance using optimset
. In the call above, passing in the empty matrix causes the default relative error tolerance of eps
to be used.
Setting Minimization Options | Tips |