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

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