0% found this document useful (0 votes)
104 views

Chapra - Applied Numerical Methods MATLAB Engineers Scientists 3rd-Halaman-182-191

This document describes several numerical methods for finding the roots of functions, including Newton-Raphson, secant methods, and Brent's method. Newton-Raphson uses the function's derivative to quickly find roots but requires evaluating the derivative, while secant methods approximate the derivative to avoid this, using previous function values. Brent's method hybridizes bracketing and open methods, using a fast open method like secant until it fails to converge, then switching to the reliable bracketing bisection method. These root-finding techniques are useful for solving equations numerically when derivatives are difficult to obtain or the functions may be complex programs.

Uploaded by

Juwita Exo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Chapra - Applied Numerical Methods MATLAB Engineers Scientists 3rd-Halaman-182-191

This document describes several numerical methods for finding the roots of functions, including Newton-Raphson, secant methods, and Brent's method. Newton-Raphson uses the function's derivative to quickly find roots but requires evaluating the derivative, while secant methods approximate the derivative to avoid this, using previous function values. Brent's method hybridizes bracketing and open methods, using a fast open method like secant until it fails to converge, then switching to the reliable bracketing bisection method. These root-finding techniques are useful for solving equations numerically when derivatives are difficult to obtain or the functions may be complex programs.

Uploaded by

Juwita Exo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

cha01102_ch06_151-181.

qxd 12/17/10 8:05 AM Page 161

6.3 SECANT METHODS 161

function [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,varargin)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% uses Newton-Raphson method to find the root of func
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations

if nargin<3,error('at least 3 input arguments required'),end


if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter = 0;
while (1)
xrold = xr;
xr = xr - func(xr)/dfunc(xr);
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
if ea <= es | iter >= maxit, break, end
end
root = xr;

FIGURE 6.7
An M-file to implement the Newton-Raphson method.

We should mention that although this derivative is not difficult to evaluate in principle, it
involves a bit of concentration and effort to arrive at the final result.
The two formulas can now be used in conjunction with the function newtraph to
evaluate the root:
>> y = @m sqrt(9.81*m/0.25)*tanh(sqrt(9.81*0.25/m)*4)-36;
>> dy = @m 1/2*sqrt(9.81/(m*0.25))*tanh((9.81*0.25/m) ...
^(1/2)*4)-9.81/(2*m)*sech(sqrt(9.81*0.25/m)*4)^2;
>> newtraph(y,dy,140,0.00001)
ans =
142.7376

6.3 SECANT METHODS


As in Example 6.4, a potential problem in implementing the Newton-Raphson method is
the evaluation of the derivative. Although this is not inconvenient for polynomials and
many other functions, there are certain functions whose derivatives may be difficult or
162 ROOTS: OPEN METHODS

inconvenient to evaluate. For these cases, the derivative can be approximated by a back-
ward finite divided difference:

f (xi−1 ) − f (xi )
f  (xi ) ∼
=
xi−1 − xi

This approximation can be substituted into Eq. (6.6) to yield the following iterative
equation:

f (xi )(xi−1 − xi )
xi+1 = xi − (6.8)
f (xi−1 ) − f (xi )

Equation (6.8) is the formula for the secant method. Notice that the approach requires two
initial estimates of x. However, because f (x) is not required to change signs between the
estimates, it is not classified as a bracketing method.
Rather than using two arbitrary values to estimate the derivative, an alternative ap-
proach involves a fractional perturbation of the independent variable to estimate f  (x),

f (xi + δxi ) − f (xi )


f  (xi ) ∼
=
δxi

where δ = a small perturbation fraction. This approximation can be substituted into


Eq. (6.6) to yield the following iterative equation:

δxi f (xi )
xi+1 = xi − (6.9)
f (xi + δxi ) − f (xi )

We call this the modified secant method. As in the following example, it provides a nice
means to attain the efficiency of Newton-Raphson without having to compute derivatives.

EXAMPLE 6.5 Modified Secant Method


Problem Statement. Use the modified secant method to determine the mass of the
bungee jumper with a drag coefficient of 0.25 kg/m to have a velocity of 36 m/s after 4 s of
free fall. Note: The acceleration of gravity is 9.81 m/s2. Use an initial guess of 50 kg and a
value of 10−6 for the perturbation fraction.

Solution. Inserting the parameters into Eq. (6.9) yields


First iteration:
x0 = 50 f (x0 ) = −4.57938708
x0 + δx0 = 50.00005 f (x0 + δx0 ) = −4.579381118

10−6 (50)(−4.57938708)
x1 = 50 −
−4.579381118 − (−4.57938708)
= 88.39931(|εt | = 38.1%; |εa | = 43.4%)
cha01102_ch06_151-181.qxd 12/17/10 8:05 AM Page 163

6.4 BRENT’S METHODS 163

Second iteration:
x1 = 88.39931 f (x1 ) = −1.69220771
x1 + δx1 = 88.39940 f (x1 + δx1 ) = −1.692203516
10−6 (88.39931)(−1.69220771)
x2 = 88.39931 −
−1.692203516 − (−1.69220771)
= 124.08970(|εt | = 13.1%; |εa | = 28.76%)
The calculation can be continued to yield

i xi |εt|, % |εa|, %

0 50.0000 64.971
1 88.3993 38.069 43.438
2 124.0897 13.064 28.762
3 140.5417 1.538 11.706
4 142.7072 0.021 1.517
5 142.7376 4.1 × 10−6 0.021
6 142.7376 3.4 × 10−12 4.1 × 10−6

The choice of a proper value for δ is not automatic. If δ is too small, the method can be
swamped by round-off error caused by subtractive cancellation in the denominator of
Eq. (6.9). If it is too big, the technique can become inefficient and even divergent. How-
ever, if chosen correctly, it provides a nice alternative for cases where evaluating the
derivative is difficult and developing two initial guesses is inconvenient.
Further, in its most general sense, a univariate function is merely an entity that returns
a single value in return for values sent to it. Perceived in this sense, functions are not
always simple formulas like the one-line equations solved in the preceding examples in this
chapter. For example, a function might consist of many lines of code that could take a sig-
nificant amount of execution time to evaluate. In some cases, the function might even rep-
resent an independent computer program. For such cases, the secant and modified secant
methods are valuable.

6.4 BRENT’S METHOD


Wouldn’t it be nice to have a hybrid approach that combined the reliability of bracketing
with the speed of the open methods? Brent’s root-location method is a clever algorithm that
does just that by applying a speedy open method wherever possible, but reverting to a reli-
able bracketing method if necessary. The approach was developed by Richard Brent (1973)
based on an earlier algorithm of Theodorus Dekker (1969).
The bracketing technique is the trusty bisection method (Sec. 5.4), whereas two dif-
ferent open methods are employed. The first is the secant method described in Sec. 6.3. As
explained next, the second is inverse quadratic interpolation.
164 ROOTS: OPEN METHODS

6.4.1 Inverse Quadratic Interpolation


Inverse quadratic interpolation is similar in spirit to the secant method. As in Fig. 6.8a, the
secant method is based on computing a straight line that goes through two guesses. The
intersection of this straight line with the x axis represents the new root estimate. For this
reason, it is sometimes referred to as a linear interpolation method.
Now suppose that we had three points. In that case, we could determine a quadratic
function of x that goes through the three points (Fig. 6.8 b). Just as with the linear secant
method, the intersection of this parabola with the x axis would represent the new root esti-
mate. And as illustrated in Fig. 6.8b, using a curve rather than a straight line often yields a
better estimate.
Although this would seem to represent a great improvement, the approach has a fun-
damental flaw: it is possible that the parabola might not intersect the x axis! Such would be
the case when the resulting parabola had complex roots. This is illustrated by the parabola,
y  f(x), in Fig. 6.9.
The difficulty can be rectified by employing inverse quadratic interpolation. That is,
rather than using a parabola in x, we can fit the points with a parabola in y. This amounts to
reversing the axes and creating a “sideways” parabola [the curve, x  f(y), in Fig. 6.9].
If the three points are designated as (xi–2 , yi–2 ), (xi–1 , yi–1 ), and (xi, yi), a quadratic
function of y that passes through the points can be generated as

(y − yi−1 )(y − yi ) (y − yi−2 )(y − yi )


g(y) = xi−2 + xi−1
(yi−2 − yi−1 )(yi−2 − yi ) (yi−1 − yi−2 )(yi−1 − yi )
(y − yi−2 )(y − yi−1 )
+ xi (6.10)
(yi − yi−2 )(yi − yi−1 )

FIGURE 6.8
Comparison of (a) the secant method and (b) inverse quadratic interpolation. Note that the
approach in (b) is called “inverse” because the quadratic function is written in y rather than in x.

f (x) f (x)

x x

(a) (b)
cha01102_ch06_151-181.qxd 12/17/10 8:05 AM Page 165

6.4 BRENT’S METHODS 165

y
6

x  f(y)
4
y  f(x)

2
Root

0
2 4 x

FIGURE 6.9
Two parabolas fit to three points. The parabola written as a function of x, y  f(x), has complex
roots and hence does not intersect the x axis. In contrast, if the variables are reversed, and the
parabola developed as x  f(y), the function does intersect the x axis.

As we will learn in Sec. 18.2, this form is called a Lagrange polynomial. The root, xi1, cor-
responds to y  0, which when substituted into Eq. (6.10) yields
yi−1 yi yi−2 yi
xi+1 = xi−2 + xi−1
(yi−2 − yi−1 )(yi−2 − yi ) (yi−1 − yi−2 )(yi−1 − yi )
yi−2 yi−1
+ xi (6.11)
(yi − yi−2 )(yi − yi−1 )

As shown in Fig. 6.9, such a “sideways” parabola always intersects the x axis.

EXAMPLE 6.6 Inverse Quadratic Interpolation


Problem Statement. Develop quadratic equations in both x and y for the data points
depicted in Fig. 6.9: (1, 2), (2, 1), and (4, 5). For the first, y  f(x), employ the quadratic
formula to illustrate that the roots are complex. For the latter, x  g(y), use inverse qua-
dratic interpolation (Eq. 6.11) to determine the root estimate.

Solution. By reversing the x’s and y’s, Eq. (6.10) can be used to generate a quadratic in x
as
(x − 2)(x − 4) (x − 1)(x − 4) (x − 1)(x − 2)
f (x) = 2+ 1+ 5
(1 − 2)(1 − 4) (2 − 1)(2 − 4) (4 − 1)(4 − 2)
or collecting terms
f (x) = x 2 − 4x + 5
166 ROOTS: OPEN METHODS

This equation was used to generate the parabola, y  f(x), in Fig. 6.9. The quadratic for-
mula can be used to determine that the roots for this case are complex,

4 ± (−4)2 − 4(1)(5)
x= =2±i
2
Equation (6.10) can be used to generate the quadratic in y as

(y − 1)(y − 5) (y − 2)(y − 5) (y − 2)(y − 1)


g(y) = 1+ 2+ 4
(2 − 1)(2 − 5) (1 − 2)(1 − 5) (5 − 2)(5 − 1)

or collecting terms:
g(y) = 0.5x 2 − 2.5x + 4
Finally, Eq. (6.11) can be used to determine the root as

−1(−5) −2(−5) −2(−1)


xi+1 = 1+ 2+ 4=4
(2 − 1)(2 − 5) (1 − 2)(1 − 5) (5 − 2)(5 − 1)

Before proceeding to Brent’s algorithm, we need to mention one more case where
inverse quadratic interpolation does not work. If the three y values are not distinct (i.e.,
yi–2  yi–1 or yi–1  yi), an inverse quadratic function does not exist. So this is where the
secant method comes into play. If we arrive at a situation where the y values are not distinct,
we can always revert to the less efficient secant method to generate a root using two of
the points. If yi2  yi1, we use the secant method with xi–1 and xi. If yi–1  yi, we use xi–2
and xi–1.

6.4.2 Brent’s Method Algorithm


The general idea behind the Brent’s root-finding method is whenever possible to use one of
the quick open methods. In the event that these generate an unacceptable result (i.e., a root
estimate that falls outside the bracket), the algorithm reverts to the more conservative
bisection method. Although bisection may be slower, it generates an estimate guaranteed to
fall within the bracket. This process is then repeated until the root is located to within an
acceptable tolerance. As might be expected, bisection typically dominates at first but as the
root is approached, the technique shifts to the faster open methods.
Figure 6.10 presents a function based on a MATLAB M-file developed by Cleve
Moler (2004). It represents a stripped down version of the fzero function which is the pro-
fessional root-location function employed in MATLAB. For that reason, we call the
simplified version: fzerosimp. Note that it requires another function f that holds the
equation for which the root is being evaluated.
The fzerosimp function is passed two initial guesses that must bracket the root.
Then, the three variables defining the search interval (a,b,c) are initialized, and f is eval-
uated at the endpoints.
A main loop is then implemented. If necessary, the three points are rearranged to satisfy
the conditions required for the algorithm to work effectively. At this point, if the stopping
cha01102_ch06_151-181.qxd 12/17/10 8:05 AM Page 167

6.4 BRENT’S METHODS 167

function b = fzerosimp(xl,xu)
a = xl; b = xu; fa = f(a); fb = f(b);
c = a; fc = fa; d = b - c; e = d;
while (1)
if fb == 0, break, end
if sign(fa) == sign(fb) %If needed, rearrange points
a = c; fa = fc; d = b - c; e = d;
end
if abs(fa) < abs(fb)
c = b; b = a; a = c;
fc = fb; fb = fa; fa = fc;
end
m = 0.5*(a - b); %Termination test and possible exit
tol = 2 * eps * max(abs(b), 1);
if abs(m) <= tol | fb == 0.
break
end
%Choose open methods or bisection
if abs(e) >= tol & abs(fc) > abs(fb)
s = fb/fc;
if a == c %Secant method
p = 2*m*s;
q = 1 - s;
else %Inverse quadratic interpolation
q = fc/fa; r = fb/fa;
p = s * (2*m*q * (q - r) - (b - c)*(r - 1));
q = (q - 1)*(r - 1)*(s - 1);
end
if p > 0, q = -q; else p = -p; end;
if 2*p < 3*m*q - abs(tol*q) & p < abs(0.5*e*q)
e = d; d = p/q;
else
d = m; e = m;
end
else %Bisection
d = m; e = m;
end
c = b; fc = fb;
if abs(d) > tol, b=b+d; else b=b-sign(b-a)*tol; end
fb = f(b);
end

FIGURE 6.10
Function for Brent’s root-finding algorithm based on a MATLAB M-file developed by Cleve Moler
(2005).
168 ROOTS: OPEN METHODS

criteria are met, the loop is terminated. Otherwise, a decision structure chooses among the
three methods and checks whether the outcome is acceptable. A final section then evaluates
f at the new point and the loop is repeated. Once the stopping criteria are met, the loop
terminates and the final root estimate is returned.

6.5 MATLAB FUNCTION: fzero


The fzero function is designed to find the real root of a single equation. A simple repre-
sentation of its syntax is
fzero(function,x0)

where function is the name of the function being evaluated, and x0 is the initial guess.
Note that two guesses that bracket the root can be passed as a vector:
fzero(function,[x0 x1])

where x0 and x1 are guesses that bracket a sign change.


Here is a simple MATLAB session that solves for the root of a simple quadratic: x2 − 9.
Clearly two roots exist at −3 and 3. To find the negative root:
>> x = fzero(@(x) x^2-9,-4)
x =
-3

If we want to find the positive root, use a guess that is near it:
>> x = fzero(@(x) x^2-9,4)
x =
3

If we put in an initial guess of zero, it finds the negative root:


>> x = fzero(@(x) x^2-9,0)

x =
-3

If we wanted to ensure that we found the positive root, we could enter two guesses as in
>> x = fzero(@(x) x^2-9,[0 4])

x =
3

Also, if a sign change does not occur between the two guesses, an error message is displayed
>> x = fzero(@(x) x^2-9,[-4 4])
??? Error using ==> fzero
The function values at the interval endpoints must ...
differ in sign.
cha01102_ch06_151-181.qxd 12/17/10 8:05 AM Page 169

6.5 MATLAB FUNCTION: fzero 169

The fzero function works as follows. If a single initial guess is passed, it first performs
a search to identify a sign change. This search differs from the incremental search described
in Section 5.3.1, in that the search starts at the single initial guess and then takes increasingly
bigger steps in both the positive and negative directions until a sign change is detected.
Thereafter, the fast methods (secant and inverse quadratic interpolation) are used un-
less an unacceptable result occurs (e.g., the root estimate falls outside the bracket). If an
unacceptable result happens, bisection is implemented until an acceptable root is obtained
with one of the fast methods. As might be expected, bisection typically dominates at first
but as the root is approached, the technique shifts to the faster methods.
A more complete representation of the fzero syntax can be written as
[x,fx] = fzero(function,x0,options,p1,p2,...)
where [x,fx] = a vector containing the root x and the function evaluated at the root fx,
options is a data structure created by the optimset function, and p1, p2... are any
parameters that the function requires. Note that if you desire to pass in parameters but not
use the options, pass an empty vector [] in its place.
The optimset function has the syntax
options = optimset('par 1 ',val 1 ,'par 2 ',val 2 ,...)
where the parameter pari has the value vali. A complete listing of all the possible param-
eters can be obtained by merely entering optimset at the command prompt. The parame-
ters that are commonly used with the fzero function are
display: When set to 'iter' displays a detailed record of all the iterations.
tolx: A positive scalar that sets a termination tolerance on x.

EXAMPLE 6.7 The fzero and optimset Functions


Problem Statement. Recall that in Example 6.3, we found the positive root of f (x) =
x 10 − 1 using the Newton-Raphson method with an initial guess of 0.5. Solve the same
problem with optimset and fzero.

Solution. An interactive MATLAB session can be implemented as follows:


>> options = optimset('display','iter');
>> [x,fx] = fzero(@(x) x^10-1,0.5,options)
Func-count x f(x) Procedure
1 0.5 -0.999023 initial
2 0.485858 -0.999267 search
3 0.514142 -0.998709 search
4 0.48 -0.999351 search
5 0.52 -0.998554 search
6 0.471716 -0.999454 search



23 0.952548 -0.385007 search
24 -0.14 -1 search
25 1.14 2.70722 search
170 ROOTS: OPEN METHODS

Looking for a zero in the interval [-0.14, 1.14]


26 0.205272 -1 interpolation
27 0.672636 -0.981042 bisection
28 0.906318 -0.626056 bisection
29 1.02316 0.257278 bisection
30 0.989128 -0.103551 interpolation
31 0.998894 -0.0110017 interpolation
32 1.00001 7.68385e-005 interpolation
33 1 -3.83061e-007 interpolation
34 1 -1.3245e-011 interpolation
35 1 0 interpolation
Zero found in the interval: [-0.14, 1.14].
x =
1
fx =
0

Thus, after 25 iterations of searching, fzero finds a sign change. It then uses interpo-
lation and bisection until it gets close enough to the root so that interpolation takes over and
rapidly converges on the root.
Suppose that we would like to use a less stringent tolerance. We can use the optimset
function to set a low maximum tolerance and a less accurate estimate of the root results:
>> options = optimset ('tolx', 1e-3);
>> [x,fx] = fzero(@(x) x^10-1,0.5,options)
x =
1.0009
fx =
0.0090

6.6 POLYNOMIALS
Polynomials are a special type of nonlinear algebraic equation of the general form

f n (x) = a1 x n + a2 x n−1 + · · · + an−1 x 2 + an x + an+1 (6.12)

where n is the order of the polynomial, and the a’s are constant coefficients. In many (but
not all) cases, the coefficients will be real. For such cases, the roots can be real and/or com-
plex. In general, an nth order polynomial will have n roots.
Polynomials have many applications in engineering and science. For example,
they are used extensively in curve fitting. However, one of their most interesting and
powerful applications is in characterizing dynamic systems—and, in particular, linear
systems. Examples include reactors, mechanical devices, structures, and electrical
circuits.

You might also like