7.1. Zero FindingΒΆ

Reading Assignment

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

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.