• Tidak ada hasil yang ditemukan

ROOTS OF EQUATIONS

6.4 BRENT’S METHOD

Now suppose that we had three points. In that case, we could determine a quadratic function of xthat goes through the three points (Fig. 6.10b). Just as with the linear secant method, the intersection of this parabola with the x axis would represent the new root estimate. And as illustrated in Fig. 6.10b, 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 fundamental 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.11.

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.11).

If the three points are designated as (xi2,yi2), (xi1,yi1), and (xi,yi), a quadratic function of ythat passes through the points can be generated as

g(y)= (yyi1)(yyi)

(yi2yi1)(yi2yi)xi2+ (yyi2)(yyi) (yi1yi2)(yi1yi)xi1

+(yyi2)(yyi1)

(yiyi2)(yiyi1)xi (6.9) f(x)

x

(a) (b)

f(x)

x

FIGURE 6.10

Comparison of (a) the secant method and (b) inverse quadratic interpolation. Note that the dark parabola passing through the three points in (b) is called “inverse” because it is written in y rather than in x.

6.4 BRENT’S METHOD 161

As we will learn in Sec. 18.2, this form is called a Lagrange polynomial.The root, xi+1, corresponds to y=0, which when substituted into Eq. (6.9) yields

xi+1= yi1yi

(yi2yi1)(yi2yi)xi2+ yi2yi

(yi1yi2)(yi1yi)xi1 + yi2yi1

(yiyi2)(yiyi1)xi (6.10)

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

EXAMPLE 6.9 Inverse Quadratic Interpolation

Problem Statement. Develop quadratic equations in both xand yfor the data points depicted in Fig. 6.11: (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 quadratic interpolation (Eq. 6.10) to determine the root estimate.

5 Root 3

1 2

0 2 4 6 y

x = f(y)

y = f(x)

x

FIGURE 6.11

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 xaxis. In contrast, if the variables are reversed, and the parabola developed as x= f(y), the function does intersect the xaxis.

Solution. By reversing thex’s andy’s, Eq. (6.9) can be used to generate a quadratic inxas f(x)=(x−2)(x−4)

(1−2)(1−4)2+(x−1)(x−4)

(2−1)(2−4)1+(x−1)(x−2) (4−1)(4−2)5 or collecting terms

f(x)=x2−4x+5

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

x =4±"

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

2 =2±i

Equation (6.9) can be used to generate the quadratic in yas g(y)= (y−1)(y−5)

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

(1−2)(1−5)2+(y−2)(y−1) (5−2)(5−1)4 or collecting terms

g(y)=0.5x2−2.5x+4

Finally, Eq. (6.10) can be used to determine the root as xi+1= −1(−5)

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

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

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

Before proceeding to Brent’s algorithm, we need to mention one more case where in- verse quadratic interpolation does not work. If the three yvalues are not distinct (that is, yi2=yi1or yi1 =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 xi1and xi. If yi1=yi, we use xi2and xi1.

6.4.2 Brent’s Method Algorithm

The general idea behind the Brent’s root finding methodis 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.12 presents pseudocode for the algorithm based on a MATLAB M-file devel- oped by Cleve Moler (2005). It represents a stripped down version of the fzerofunction

6.4 BRENT’S METHOD 163

FIGURE 6.12

Pseudocode for Brent’s root finding algorithm based on a MATLAB m-file developed by Cleve Moler (2005).

Function fzerosimp(xl, xu) eps "2.22044604925031E-16 tol "0.000001

a "xl: b "xu: fa "f(a): fb "f(b) c "a: fc "fa: d "b 'c: e "d DOIF fb "0 EXIT

IF fa )fb THEN (If necessary, rearrange points) a "c: fa "fc: d "b 'c: e "d

ENDIF

IF |fa| !|fb| THEN c "b: b "a: a "c fc "fb: fb "fa: fa "fc ENDIF

m "0.5 * (a 'b) (Termination test and possible exit)

tol "2 * eps * max(|b|, 1)

IF |m| *tol Or fb "0. THEN ENDIFEXIT

(Choose open methods or bisection) IF |e| &tol And |fc| )|fb| THEN

s "fb / fc

IF a "c THEN (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)

ENDIF

IF p )0 THEN q " 'q ELSE p " 'p

IF 2 * p < 3 * m * q '|tol * q| AND p !|0.5 * e * q| THEN e "d: d "p / q

ELSEd "m: e "m ENDIF

ELSE (Bisection) d "m: e "m

ENDIF

c "b: fc "fb

IF |d| )tol THEN b "b #d Else b "b 'Sgn(b 'a) * tol fb "f(b)

ENDDO

fzerosimp "b END fzerosimp

which is the professional 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 fzerosimpfunction is passed two initial guesses that must bracket the root.

After assigning values for machine epsilon and a tolerance, the three variables defining the search interval (a , b , c) are initialized, and fis evaluated 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 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.

Note that Sec. 7.7.2 presents an application of Brent’s method where we illustrate how the MATLAB’s fzerofunction works. In addition, it is employed in Case Study 8.4 to determine the friction factor for air flow through a tube.