However, if you plot y=f(x), it is usually not hard to see approximately where the curve crosses the x axis, this gives an approximate value of xroot. If you would like to know the value of xroot more precisely, and to make the computer do the work rather than relying on your eyeballs looking at a graph, you can use one of the root-finding algorithms presented in NMM Chapter 6.
xmin < xroot < xmax
If the function is continuous, this will certainly be true if f(xmin) and f(xmax) have opposite signs (because somewhere between xmin and xmax the curve y=f(x) must cross the x axis.) There are several algorithms where the computer guesses a number xguess somewhere between xmin and xmax, it evaluates f(xguess), and based on its sign it establishes a tighter bracketing interval (either [xmin, xguess] or [xguess, xmax]). Then the computer makes a new guess inside the new interval, and this process continues until the bracket is smaller than the user-specified tolerance on xroot. The simplest way to choose xguess is to take the midpoint of the interval; this algorithm is called “bisection” (see NMM 6.3).
Here is a graphical demo of how bisection works in detail for NMM Example 6.6:
Click Here to see the demo in a seperate window
The o’s show previously evaluated points. The * indicates the most recently evaluated point (at the midpoint of the interval defined by the two closest o’s).
|xanswer – xroot| < tolerance
Usually an engineer wants the answer to N significant figures, i.e. he or she wants
|xanswer – xroot| < 10-N |xanswer|
i.e.
tolerance = 10-N |xanswer|
This usually works, but if you are unfortunate xanswer might be very close to zero, and then the computer might have a tough time satisfying this condition. A more conservative tolerance (with an escape clause for very small x’s) would be:
tolerance = max (ATOL, RTOL*|xanswer|)
or (almost the same):
tolerance = ATOL + RTOL*|xanswer|
where RTOL = 10-N (this is what you really want to control the tolerance) and ATOL is some tiny positive number (this is the escape clause). Note that ATOL has the same units as x, and should be many orders of magnitude smaller than your guess at the value of xroot.
Sometimes you may want to specify a different type of tolerance; for example in root-finding you could specify a tolerance on |f(xanswer)|, see NMM 6.3.2
>> X_answer=bisect(@sin, [3 4], 1e-6)
which should return an X_answer very close to pi.
You can write your own general Matlab functions which take function names as inputs; the trick is to use feval. ‘feval’ is also discussed in NMM Section 3.6.3
We will not have time to discuss the other root-finding methods presented in Chapter 6 in 10.10. The built-in Matlab root-finding function fzero is discussed in Section 6.6; since it is built in, you may find it convenient to use fzero. This is an example of a hybrid method, which combines the reliability of bisection with the speedy convergence of Newton-like methods. One of the methods fzero uses is the Secant Method, described in Section 6.5. Section 6.2 gives an abstract, but very general mathematical view of root-finding which is applicable to a large number of computational methods which iteratively refine an initial guess. Finally, section 6.7 describes specialized methods for finding the root of polynomials.