7.1. Zero Finding

Reading Assignment

Please read chapter 6 of Physical Modeling in MATLAB, by Allen B. Downey [DOWNEY11].

7.1.1. Fzero

Chapter 6 of Physical Modeling in MATLAB presents how MATLAB can help us solve potentially difficult math problems using numerical methods. Numerical methods use the computational speed of computers along with some clever algorithms to find numerical answers to math problems. In math classes, you are taught analytic methods for solving various problems. Analytic solutions provide exact equations. But some problems are very difficult, if not impossible, to solve with analytic methods. In engineering, we often want a numeric solutions to equations with specific values, thus numerical methods can be a good alternative to analytic methods.

The numerical method discussed here is finding the root of a function. That is, where is the function equal to zero.

In the exercises at the end of the chapter, he suggests using the function ezplot to display a function over a range. However, the current MATLAB documentation tells us that ezplot is deprecated and we should try fplot instead.

Some sample code follows. Note that the algorithm is most efficient when the second argument to fzero is a two value vector. List those values such that f(x) = 0 occurs once between the values of x given. The function should be positive at one of the vector values and negative at the other value. It may help to plot the function to determine what values to use. With some effort, a logical expression could be also be used. Dr. Downey gives a very good description of the method and how MATLAB’s fzero function works. The algorithm that fzero uses is a combination of the bisection method and the secant method. The bisection method repeatedly cuts the range passed to fzero in half keeping the zero crossing point between in the range. The secant method finds the slope of the function at a specified point as pointer towards the zero crossing.

%% Test of fplot and fzero

f = @(x) x.^2 - 3*x - 2;  % just a made up test function

fplot(f, [-5 10]);
grid on;
%% First root
z1 = fzero(f, [-1, 0]);
fprintf("First root = %.4f\n", z1);
%% Second root
z2 = fzero(f, [2, 4]);
fprintf("Second root = %.4f\n", z2);

See also

MATLAB documentation for fzero and fplot.

7.1.2. Fminsearch

We sometimes want to find a minimum value of a function, especially when the function expresses the error between a desired solution and that resulting from a function.

When a closed form solution is not available, a numeric search algorithm can be used. MATLAB has a function called fminsearch() that uses the Nelder-Mead simplex search method. The math behind how the algorithm works is a little complicated, but you can read about it in the MATLAB documentation and articles found by searching the Internet. To use fminsearch(), one first defines a function that returns the error between the result yielded by a possible solution and the desired result. Then fminsearch() tries various possible solutions searching for a local minimum error.