chap6

40
Chapter 6 Root Finding We describe and analyze several techniques for finding a real root of a general nonlinear real function of one variable. We apply one of these methods to the special important problem of calculating square roots. Then, we discuss how to calculate real roots in the special case of a polynomial. A root of a function f is a point, x = x * , where the equation f (x)=0 is satisfied. So, for example x * = 0 is a root of f (x)= x, f (x)= x 2 , f (x)= x and f (x) = ln (x + 1). A root x * is simple if f (x * ) = 0, if the function f exists everywhere on an open interval containing x * , and if f (x * ) = 0. So, x * = 0 is a simple root of f (x)= x and f (x) = ln(x + 1) (choose the open interval to be (- 1 2 , 1 2 )). But, x * = 0 is not a simple root of f (x)= x n ,n> 1 because f (0) = 0 Also, x * = 0 is not a simple root of f (x)= x because there is no open interval containing x * = 0 in which f (x) exists everywhere. Observe that, at a simple root x * , the graph of f (x) crosses the x-axis. However, the graph of f (x) can also cross the x-axis at a root which is not simple; consider, for example the root x * =0 of f (x)= x 3 , or any other odd power of x. Suppose we want to compute a simple root of a function f that is continuous on the closed interval [c, d]; that is, the value f (x) is defined at each point x [c, d] and has no jumps (discontinuities) there. If f (a) · f (b) < 0, then, from the Intermediate Value Theorem (see Problem 6.0.1), there is a point x * [c, d] where f (x * ) = 0. That is, if the function f is continuous and changes sign on an interval, then it must have at least one root in that interval. We will exploit this property in later sections of this chapter but we begin with a related problem. Root-finding problems often occur in fixed–point form; that is, the problem is to find a point x = x * that satisfies the equation x = g(x) This type of problem sometimes arises directly when finding an equilibrium of some process. We can convert a problem written in the form to solve the equation x = g(x) to the standard form of solving the equation f (x) = 0 simply by setting f (x) x - g(x) = 0. But, first we consider this problem in its natural fixed–point form. We need some theorems from the calculus of a single variable which we now state as problems. We follow these problems with some that apply the theorems. Problem 6.0.1. The Intermediate Value Theorem. Let the function f be continuous on the closed interval [a, b]. Show that for any number v between the values f (a) and f (b) there exists at least one point c (a, b) such that f (c)= v. Problem 6.0.2. Consider the function f (x)= x - e -x . Use the Intermediate Value Theorem (see Problem 6.0.1) to prove that the equation f (x)=0 has a solution x * (0, 1). Problem 6.0.3. The Mean Value Theorem. Let the function f be continuous and differentiable on the closed interval [a, b]. Show that there exists a point c (a, b) such that f (c)= f (b) - f (a) b - a 159

Upload: uzma-azam

Post on 09-Nov-2014

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: chap6

Chapter 6

Root Finding

We describe and analyze several techniques for finding a real root of a general nonlinear real functionof one variable. We apply one of these methods to the special important problem of calculating squareroots. Then, we discuss how to calculate real roots in the special case of a polynomial.

A root of a function f is a point, x = x!, where the equation

f(x) = 0

is satisfied. So, for example x! = 0 is a root of f(x) = x, f(x) = x2, f(x) =!

x and f(x) = ln (x + 1).A root x! is simple if f(x!) = 0, if the function f " exists everywhere on an open interval

containing x!, and if f "(x!) "= 0. So, x! = 0 is a simple root of f(x) = x and f(x) = ln (x + 1)(choose the open interval to be (# 1

2 , 12 )). But, x! = 0 is not a simple root of f(x) = xn, n > 1

because f "(0) = 0 Also, x! = 0 is not a simple root of f(x) =!

x because there is no open intervalcontaining x! = 0 in which f "(x) exists everywhere.

Observe that, at a simple root x!, the graph of f(x) crosses the x-axis. However, the graph off(x) can also cross the x-axis at a root which is not simple; consider, for example the root x! = 0of f(x) = x3, or any other odd power of x.

Suppose we want to compute a simple root of a function f that is continuous on the closed interval[c, d]; that is, the value f(x) is defined at each point x $ [c, d] and has no jumps (discontinuities)there. If f(a) · f(b) < 0, then, from the Intermediate Value Theorem (see Problem 6.0.1), there is apoint x! $ [c, d] where f(x!) = 0. That is, if the function f is continuous and changes sign on aninterval, then it must have at least one root in that interval. We will exploit this property in latersections of this chapter but we begin with a related problem.

Root-finding problems often occur in fixed–point form; that is, the problem is to find a pointx = x! that satisfies the equation

x = g(x)This type of problem sometimes arises directly when finding an equilibrium of some process. We canconvert a problem written in the form to solve the equation x = g(x) to the standard form of solvingthe equation f(x) = 0 simply by setting f(x) % x # g(x) = 0. But, first we consider this problemin its natural fixed–point form. We need some theorems from the calculus of a single variable whichwe now state as problems. We follow these problems with some that apply the theorems.

Problem 6.0.1. The Intermediate Value Theorem. Let the function f be continuous on the closedinterval [a, b]. Show that for any number v between the values f(a) and f(b) there exists at least onepoint c $ (a, b) such that f(c) = v.

Problem 6.0.2. Consider the function f(x) = x# e#x. Use the Intermediate Value Theorem (seeProblem 6.0.1) to prove that the equation f(x) = 0 has a solution x! $ (0, 1).

Problem 6.0.3. The Mean Value Theorem. Let the function f be continuous and di!erentiable onthe closed interval [a, b]. Show that there exists a point c $ (a, b) such that

f "(c) =f(b)# f(a)

b# a

159

Page 2: chap6

160 CHAPTER 6. ROOT FINDING

0.2 0.4 0.6 0.8 1

-1

-0.75

-0.5

-0.25

0.25

0.5

Figure 6.1: Graph of f(x) = x# e#x

Problem 6.0.4. Consider the function f(x) = x# e#x on the interval [a, b] = [0, 1]. Use the MeanValue Theorem (see Problem 6.0.3) to find a point c $ (0, 1) where

f "(c) =f(b)# f(a)

b# a

.

Problem 6.0.5. Consider the functions f(x) = (x# 1)(x + 2)2 and F (x) = x!

x + 2.

1. What are the roots of f?

2. What are the simple roots of f?

3. What are the roots of F?

4. What are the simple roots of F?

Problem 6.0.6. Consider the equation x = cos!!

2x". Convert this equation into the standard

form f(x) = 0 and hence show that it has a solution x! $ (0, 1). [Hint: Use the Intermediate ValueTheorem (see Problem 6.0.1) on f(x).]

6.1 Fixed–Point Iteration

A commonly used technique for computing a solution x! of an equation x = g(x) is to define asequence {xn}$n=0 of approximations of x! according to the iteration

xn+1 = g(xn), n = 0, 1, . . .

Here x0 is an initial estimate of x! and g is the fixed–point iteration function. If the function g iscontinuous and the sequence {xn}$n=0 converges to x!, then x! satisfies x! = g(x!).

In practice, to find a good starting point x0 for the fixed–point iteration we may estimate x! froma graph of f(x) = x#g(x). For example, consider the function f(x) = x#e#x in Fig. 6.1. From theIntermediate Value Theorem (Problem 6.0.1) there is a root of the function f in the interval (0, 1)because f(x) is continuous on [0, 1] with f(0) = #1 < 0 and f(1) = 1# e#1 > 0 so f(0) · f(1) < 0.The graph of the derivative f "(x) = 1+e#x in Fig. 6.2 suggests that this root is simple because f "(x)is continuous and positive for all x $ [0, 1]. Alternatively, the root of f(x) can be characterized asthe intersection of the line y = x and the curve y = g(x) % e#x as in Fig. 6.3. Either way, x0 = 0.5

Page 3: chap6

6.1. FIXED–POINT ITERATION 161

0.2 0.4 0.6 0.8 1

0.5

1

1.5

2

2.5

Figure 6.2: Graph of f "(x) = 1 + e#x

0.2 0.4 0.6 0.8 1

0.2

0.4

0.6

0.8

1

Figure 6.3: Graphs of y = x (dashed) and y = e#x (solid)

Page 4: chap6

162 CHAPTER 6. ROOT FINDING

Iteration Functione#x # ln(x)

n xn xn

0 0.5000 0.50001 0.6065 0.69312 0.5452 0.36653 0.5797 1.00374 0.5601 -0.00375 0.5712 ******6 0.5649 ******7 0.5684 ******8 0.5664 ******9 0.5676 ******10 0.5669 ******11 0.5673 ******12 0.5671 ******

Table 6.1: Computing the root of x#e#x = 0 by fixed–point iteration. The asterisks indicate valuesthat are not defined when using only real arithmetic. Note, though, if complex arithmetic is used,these values are computed as complex numbers.

Iteration Function3!

x + 1 x3 # 1n xn xn

0 1.000 1.0001 1.260 0.0002 1.312 -1.0003 1.322 -2.0004 1.324 -9.0005 1.325 -730.06 1.325 HUGE

Table 6.2: Computing the real root of x3 # x# 1 = 0 by fixed–point iteration.

is a reasonable initial estimate to x!. Of course we are relying rather too much on graphical evidencehere. As we saw in Section 2.3.1 graphical evidence can be misleading. This is especially true ofthe evidence provided by low resolution devices such as graphing calculators. But we can provethere is a unique simple root of the equation x = e#x easily. The curve y = x is strictly monotonicincreasing and continuous everywhere and the curve y = e#x is strictly monotonic decreasing andcontinuous everywhere. We have already seen these curves intersect so there is at least one root andthe monotonicity properties show that the root is simple and unique.

If we take natural logarithms of both sides of the equation x = e#x and rearrange we obtainx = # ln(x) as an alternative fixed–point equation. Table 6.1 lists the iterates computed usingeach of the fixed–point iteration functions g(x) = e#x, i.e. using the iteration xn+1 = e#xn , andg(x) = # ln(x), i.e. using the iteration xn+1 = # ln(xn), each working to 4 decimal digits andstarting from x0 = 0.5. (The asterisks in the table are used to indicate that the logarithm of anegative number, x4, is not defined when using real arithmetic.) The iterates computed using theiteration function g(x) = e#x converge to x! = 0.567143 · · · while iterates computed using theiteration function g(x) = # ln(x) diverge. An analysis of these behaviors is given in the next section.

As another example, consider the cubic polynomial f(x) = x3 # x# 1. Its three roots compriseone real root and a complex conjugate pair of roots. Solving the equation f(x) % x3 # x # 1 = 0

Page 5: chap6

6.1. FIXED–POINT ITERATION 163

0.5 1 1.5 2

-1

1

2

3

4

5

Figure 6.4: Graph of f(x) = x3 # x# 1

for x3 then taking the cube root suggests the fixed–point iteration function g(x) = 3!

x + 1, whilerearranging directly for x suggests the iteration function g(x) = x3 # 1. From the plot of the valuesf(x) = x3 # x # 1 in Fig. 6.4, we see that x! = 1.3 · · · . Starting from the point x0 = 1.0 for eachiteration function we compute the results in Table 6.2. That is the results in the first column arefrom the iteration xn+1 = 3

!xn + 1 and in the second column from the iteration xn+1 = x3

n#1. [Theentry HUGE in the table corresponds to a number clearly too large to be of interest; the iterationultimately overflows on any computer or calculator!] The iterates generated using the iterationfunction g(x) = 3

!x + 1 converge to the root x! = 1.3247 · · · , while the iterates generated using the

iteration function g(x) = x3 # 1 diverge.

Problem 6.1.1. Assume that the function g is continuous and that the sequence {xn}$n=0 convergesto a fixed point x!. Employ a limiting argument to show that x! satisfies x! = g(x!).

Problem 6.1.2. Consider the function f(x) = x # e#x. The equation f(x) = 0 has a root x! in(0, 1). Use Rolle’s Theorem (see Problem 4.1.22) to prove that this root x! of f(x) is simple.

Problem 6.1.3. 1. Show algebraically by rearrangement that the equation x = # ln(x) has thesame roots as the equation x = e#x

2. Show algebraically by rearrangement that the equation x = 3!

x + 1 has the same roots as theequation x3 # x# 1 = 0

3. Show algebraically by rearrangement that the equation x = x3 # 1 has the same roots as theequation x3 # x# 1 = 0

Problem 6.1.4. Prove that the equation x = # ln(x) has a unique simple real root.

Problem 6.1.5. Prove that the equation x3 # x# 1 = 0 has a unique simple real root.

Problem 6.1.6. For f(x) = x3#x# 1, the show that rearrangement x3#x# 1 = x(x2# 1)# 1 = 0

leads to a fixed–point iteration function g(x) =1

x2 # 1, and show that the rearrangement x3#x#1 =

(x#1)(x2 +x+1)#x = 0 leads to the iteration function g(x) = 1+x

x2 + x + 1. How do the iterates

generated by each of these fixed–point functions behave? [In each case compute a few iterates startingfrom x0 = 1.5.]

Page 6: chap6

164 CHAPTER 6. ROOT FINDING

6.1.1 Analysis of Convergence

Given an initial estimate x0 of the root x! of the equation x = g(x), we need two properties of theiteration function g to ensure that the sequence of iterates {xn}$n=1 defined by

xn+1 = g(xn), n = 0, 1, . . . ,

converges to x!:

• x! = g(x!); that is, x! is a fixed–point of the iteration function g.

• For any n, let’s define en = xn # x! to be the error in xn considered as an approximation tox!. Then the sequence of absolute errors {|en|}$n=0 decreases monotonically to 0; that is, foreach value of n, the iterate xn+1 is closer to the point x! than was the previous iterate xn.

Assuming that the derivative g" is continuous wherever we need it to be, from the Mean ValueTheorem (Problem 6.0.3), we have

en+1 = xn+1 # x!= g(xn)# g(x!)= g"("n) · (xn # x!)= g"("n) · en

where "n is some (unknown) point between x! and xn.Summarizing, the key equation

en+1 = g"("n) · en

states that the new error en+1 is a factor g"("n) times the old error en.Now, the new iterate xn+1 is closer to the root x! than is the old iterate xn if and only if

|en+1| < |en|. This occurs if and only if |g"("n)| < 1. So, informally, if |g"("n)| < 1 for each value of"n arising in the iteration, the absolute errors tend to zero and the iteration converges.

More specifically:

• If for some constant 0 < G < 1, |g"(x)| & G (the tangent line condition) for all x $I = [x! # r, x! + r] then, for every starting point x0 $ I, the sequence {xn}$n=0 is welldefined and converges to the fixed–point x!. (Note that, as xn ' x!, g"(xn) ' g"(x!) and sog"("n)' g"(x!) since "n is “trapped” between xn and x!.)

• The smaller the value of |g"(x!)|, the more rapid is the ultimate rate of convergence. Ultimately(that is, when the iterates xn are so close to x! that g"("n) ( g"(x!)) each iteration reducesthe magnitude of the error by a factor of about g"(x!).

• If g"(x!) > 0, convergence is ultimately one–sided. Because, when xn is close enough to x!then g"("n) will have the same sign as g"(x!) > 0 since we assume that g"(x) is continuous nearx!. Hence the error er+1 will have the same sign as the error er for all values of r > n.

• By a similar argument, if g"(x!) < 0, ultimately (that is, when the iterates xn are close enoughto x!) convergence is two–sided because the signs of the errors er and er+1 will be oppositefor each value of r > n.

We have chosen the interval I to be centered at x!. This is to simplify the theory. Then, if x0

lies in the interval I so do all future iterates xn. The disadvantage of this choice is that we don’tknow the interval I until we have calculated the answer x!! However, what this result does tell usis what to expect if we start close enough to the solution. If |g"(x!)| & G < 1, g"(x) is continuous ina neighborhood of x!, and x0 is close enough to x! then the fixed point iteration

xn+1 = g(xn), n = 0, 1, . . . ,

converges to x!. The convergence rate is linear if g"(x!) "= 0 and superlinear if g"(x!) = 0; that is,ultimately

en+1 ( g"(x!)en

Page 7: chap6

6.1. FIXED–POINT ITERATION 165

n xn en en/en#1

0 0.5000 -0.0671 —1 0.6065 0.0394 -0.5872 0.5452 -0.0219 -0.5563 0.5797 0.0126 -0.5734 0.5601 -0.0071 -0.5645 0.5712 0.0040 -0.569

Table 6.3: Errors and error ratios for fixed–point iteration with g(x) = e#x

n xn en en/en#1

0 1.000 -0.325 —1 1.260 -0.065 0.2002 1.312 -0.012 0.1923 1.322 -0.002 0.1904 1.324 -0.000 0.190

Table 6.4: Errors and error ratios for fixed–point iteration with g(x) = 3!

x + 1

Formally, if the errors satisfy

limn%$

|en+1||ep

n|= K

where K is a positive constant then the convergence rate is p. The case p = 1 is labelled linear, p = 2quadratic, p = 3 cubic, etc.; any case with p > 1 is superlinear. The following examples illustratethis theory.

Example 6.1.1. Let g(x) = e#x, then x! = 0.567143 · · · and g"(x) = #e#x. Note, if x > 0 then#1 < g"(x) < 0. For all x $ (0,)), |g"(x)| < 1. Choose x0 in the interval I ( [0.001, 1.133] whichis approximately centered symmetrically about x!. Since g"(x!) = #0.567 · · · < 0, convergence isultimately two–sided. Ultimately, the error at each iteration is reduced by a factor of approximately|# 0.567| as we see in the last column of Table 6.3.

Example 6.1.2. Let g(x) = 3!

x + 1, then x! = 1.3247 · · · and g"(x) =1

3(x + 1)2/3. Note, if x * 0

then x+1 * 1 and hence 0 & 1x + 1

& 1. Therefore 0 & g"(x) =1

3(x + 1)2/3& 1

3. So |g"(x)| & 1

3for

x $ [0,)). Choose x0 in the interval I ( [0, 2.649] which is approximately symmetrically centeredaround x!. Since g"(x!) ( 0.189, convergence is ultimately one–sided. The error at each iteration isultimately reduced by a factor of approximately 0.189, see the last column of Table 6.4.

Problem 6.1.7. For the divergent sequences shown in Tables 6.1 and 6.2 above, argue why

|x! # xn+1| > |x! # xn|

whenever xn is su"ciently close, but not equal, to x!.

Problem 6.1.8. Analyze the convergence, or divergence, of the fixed–point iteration for each of theiteration functions

1. g(x) % 1x2 # 1

2. g(x) % 1 +x

x2 + x + 1

Page 8: chap6

166 CHAPTER 6. ROOT FINDING

See Problem 6.1.6 for the derivation of these iteration functions. Does your analysis reveal thebehavior of the iterations that you computed in Problem 6.1.6?

Problem 6.1.9. Consider the fixed–point equation x = 2 sinx for which there is a root x! ( 1.89.Why would you expect the fixed–point iteration xn+1 = 2 sin xn to converge from a starting point x0

close enough to x!? Would you expect convergence to be ultimately one–sided or two–sided? Why?Use the iteration xn+1 = 2 sin xn starting (i) from x0 = 2.0 and (ii) from x0 = 1.4. In each case

continue until you have 3 correct digits in the answer. Discuss the convergence behavior of theseiterations in the light of your observations above.

6.2 The Newton Iteration

A systematic procedure for constructing iteration functions g given the function f is to choose theNewton Iteration Function

g(x) = x# f(x)f "(x)

leading to the well–known Newton Iteration

xn+1 = xn #f(xn)f "(xn)

How does this choice of iteration function arise? Here are two developments:

A. Expand f(y) around x using Taylor series:

f(y) = f(x) + (y # x)f "(x) + . . .

Now terminate the Taylor series after the terms shown. Setting y = xn+1, f(xn+1) = 0 andx = xn and rearranging gives the Newton iteration. Note, this is “valid” because we assumexn+1 is much closer to x! than is xn, so, usually, f(xn+1) is much closer to zero than is f(xn).

B. The curve y = f(x) crosses the x-axis at x = x!. In point–slope form, the tangent to y = f(x)at the point (xn, f(xn)) is given by y # f(xn) = f "(xn)(x # xn). Let xn+1 be defined as thevalue of x where this tangent crosses the x-axis (see Fig. 6.5). Rearranging, we obtain theNewton iteration.

A short computation shows that, for the Newton iteration function g,

g"(x) = 1# f "(x)f "(x)

+f(x)f ""(x)

f "(x)2=

f(x)f ""(x)f "(x)2

If we assume that the root x! is simple, then f(x!) = 0 and f "(x!) "= 0, so

g"(x!) =f(x!)f ""(x!)

f "(x!)2= 0

From the previous section we know that, when xn is close enough to x!, then en+1 ( g"(x!)en. Sog"(x!) = 0 implies en+1 is very much smaller in magnitude than en.

In Table 6.5 we give the iterates, errors and error ratios for the Newton iteration applied to thefunction f(x) = x#e#x. See Table 6.6 for the iterates, errors and error ratios for the function f(x) =x3#x#1. In these tables the correct digits are underlined. At each Newton iteration the number ofcorrect digits approximately doubles: this behavior is typical for quadratically convergent iterations.Can we confirm theoretically that the Newton iteration approximately doubles the number of correctdigits at each iteration? From Taylor series,

en+1 = xn+1 # x!= g(xn)# g(x!)= g(x! + en)# g(x!)

= eng"(x!) +e2n

2g""(x!) + . . .

Page 9: chap6

6.2. THE NEWTON ITERATION 167

xn xn+1

x−axis

x*

(xn , f(xn))

y = f(x)

tangent line

Figure 6.5: Illustration of Newton’s method. xn+1 is the location where the tangent line throughthe point (xn, f(xn)) crosses the x-axis.

n xn en en/en#1 en/e2n#1

0 0.500000000000000 -6.7E-02 — —1 0.566311003197218 -8.3E-04 1.2E-02 0.18462 0.567143165034862 -1.3E-07 1.5E-04 0.18093 0.567143290409781 2.9E-13 -2.3E-06 -0.1716

Table 6.5: Errors and error ratios for the Newton iteration applied to f(x) = x# e#x.

n xn en en/en#1

0 1.00000000000000 -3.2E-01 —1 1.50000000000000 1.8E-01 -5.4E-012 1.34782608695652 2.3E-02 1.3E-013 1.32520039895091 4.8E-04 2.1E-024 1.32471817399905 2.2E-07 4.5E-045 1.32471795724479 4.4E-14 2.0E-07

Table 6.6: Errors and error ratios for the Newton iteration applied to f(x) = x3 # x# 1.

Page 10: chap6

168 CHAPTER 6. ROOT FINDING

Neglecting the higher order terms not shown here, since g" (x!) = 0, assuming g"" (x!) "= 0 as it is inmost cases, we obtain the error equation

en+1 (g""(x!)

2· e2

n

(For example, for the iteration in Table 6.5en+1

e2n

( g""(x!)2

( 0.1809 and we have quadratic conver-

gence.)Now, let’s check that this error equation implies ultimately doubling the number of correct digits.

Dividing both sides of the error equation by x! leads to the relation

rn+1 (x! g""(x!)

2· r2

n

where the relative error rn in en is defined by rn %en

x!. Suppose that xn is correct to about t decimal

digits, so that rn ( 10#t and choose d to be an integer such that

x!g""(x!)2

( ±10d

where the sign of ± is chosen according to the sign of the left hand side. Then,

rn+1 (x! g""(x!)

2· r2

n ( ±10d · 10#2t = ±10d#2t

As the iteration converges, t grows and, ultimately, 2t will be much larger than d so then rn+1 (±10#2t. Hence, for su!ciently large values t, when rn is accurate to about t decimal digits thenrn+1 is accurate to about 2t decimal digits. This approximate doubling of the number of correctdigits at each iteration is evident in Tables 6.5 and 6.6. When the error en is su!ciently small thatthe error equation is accurate then en+1 has the same sign as g""(x!). Then, convergence for theNewton iteration is one–sided: from above if g""(x!) > 0 and from below if g""(x!) < 0.

Summarizing — for a simple root x! of f :

1. The Newton iteration converges when started from any point x0 that is “close enough” to theroot x! assuming g"(x) is continuous in a closed interval containing x!, because the Newtoniteration is a fixed point iteration with |g"(x!)| < 1

2. When the error en is su!ciently small that the error equation is accurate, the number ofcorrect digits in xn approximately doubles at each Newton iteration.

3. When the error en is su!ciently small that the error equation is accurate, the Newton iterationconverges from one side.

Example 6.2.1. Consider the equation f(x) % x20#1 = 0 with real roots ±1. If we use the Newtoniteration the formula is

xn+1 = xn #x20

n # 120x19

n

Starting with x0 = 0.5, which we might consider to be “close” to the root x! = 1 we computex1 = 26213, x2 = 24903, x3 = 23658 to five digits, that is the first iteration took an enormous leapacross the root then the iterations “creep” back towards the root. So, we didn’t start “close enough”to get quadratic convergence; the iteration is converging linearly. If, instead, we start from x0 = 1.5,to five digits we compute x1 = 1.4250, x2 = 1.3538 and x3 = 1.2863, and convergence is again linear.If we start from x0 = 1.1, to five digits we compute x1 = 1.0532, x2 = 1.0192, x3 = 1.0031, andx4 = 1.0001. We observe quadratic convergence in this last set of iterates.

Problem 6.2.1. Construct the Newton iteration by deriving the tangent equation to the curvey = f(x) at the point (xn, f(xn)) and choosing the point xn+1 as the place where this line crossesthe x-axis. (See Fig. 6.5.)

Page 11: chap6

6.2. THE NEWTON ITERATION 169

Problem 6.2.2. Assuming that the root x! of f is simple, show that g""(x!) =f ""(x!)f "(x!)

. Hence,

show thaten+1

e2n

( # f ""(x!)2f "(x!)

Problem 6.2.3. For the function f(x) = x3#x#1 write down the Newton iteration function g andcalculate g"(x) and g""(x). Table 6.6 gives a sequence of iterates for this Newton iteration function.In Table 6.6 compute the ratios

en

e2n#1

. Do the computed ratios behave in the way that you would

predict?

Problem 6.2.4. Consider computing the root x! of the equation x = cos(x), that is computing thevalue x! such that x! = cos(x!). Use a calculator for the computations required below.

1. Use the iteration xn+1 = cos(xn) starting from x0 = 1.0 and continuing until you have com-puted x! correctly to three digits. Present your results in a table. Observe that the itera-tion converges. Is convergence from one side? Viewing the iteration as being of the formxn+1 = g(xn), by analyzing the iteration function g show that you would expect the iterationto converge and that you can predict the convergence behavior.

2. Now use a Newton iteration to compute the root x! of x = cos(x) starting from the samevalue of x0. Continue the iteration until you obtain the maximum number of digits of accuracyavailable on your calculator. Present your results in a table, marking the number of correctdigits in each iterate. How would you expect the number of correct digits in successive iteratesto behave? Is that what you observe? Is convergence from one side? Is that what you wouldexpect and why?

Problem 6.2.5. Some computers perform the divisionN

Dby first computing the reciprocal

1D

and

then computing the productN

D= N · 1

D. Consider the function f(x) % D # 1

x. What is the root

of f(x)? Show that, for this function f(x), the Newton iteration function g(x) = x # f(x)f "(x)

can

be written so that evaluating it does not require divisions. Simplify this iteration function so thatevaluating it uses as few adds, subtracts, and multiplies as possible.

Use the iteration that you have derived to computeN

D=

23

without using any divisions. To dothis, first use the Newton iteration for the reciprocal of D = 3 starting from x0 = 0.5; iterate until

you have computed the reciprocal to 10 decimal places. Next, computeN

D.

6.2.1 Calculating Square Roots

For any real value a > 0, the function f(x) = x2 # a has two square roots, x! = ±!

a. The Newtoniteration applied to f(x) gives

xn+1 = xn #f(xn)f "(xn)

= xn #x2

n # a

2xn

=

#xn +

a

xn

$

2

Note, the (last) algebraically simplified expression is less expensive to compute than the originalexpression because it involves just one divide, one add, and one division by two whereas the originalexpression involves a multiply, two subtractions, a division and a multiplication by two. [When usingIEEE Standard floating–point arithmetic, multiplication or division by two of a machine floating–point number involves simply increasing or reducing, respectively, that number’s exponent by one.This is a low cost operation relative to the standard arithmetic operations.]

Page 12: chap6

170 CHAPTER 6. ROOT FINDING

From the iteration xn+1 =12

#xn +

a

xn

$we observe that xn+1 has the same sign as xn for all n.

Hence, if the Newton iteration converges, it converges to that root with the same sign as x0. Nowconsider the error in the iterates.

en % xn #!

a

Algebraically,

en+1 = xn+1 #!

a =

#xn +

a

xn

$

2#!

a

=(xn #

!a)2

2xn=

e2n

2xn

From this formula, we see that when x0 > 0 we have e1 = x1 #!

a > 0 so that x1 >!

a > 0. Itfollows similarly that en * 0 for all n * 1. Hence, if xn converges to

!a then it converges from

above. To prove that the iteration converges, note that

0 & en

xn=

xn #!

a

xn # 0=

distance from xn to!

a

distance from xn to 0< 1

for n * 1, so

en+1 =e2n

2xn=

#en

xn

$en

2<

en

2

Hence, for all n * 1, the error en is reduced by a factor of at least12

at each iteration. Therefore theiterates xn converge to

!a. Of course, “quadratic convergence rules!” as this is the Newton iteration,

so when the error is su!ciently small the number of correct digits in xn approximately doubles witheach iteration. However, if x0 is far from the root we may not have quadratic convergence initially,and, at worst, the error may only be approximately halved at each iteration.

Not surprisingly, the Newton iteration is widely used in computers’ and calculators’ mathematicalsoftware libraries as a part of the built-in function for determining square roots. Clearly, for thisapproach to be useful a method is needed to ensure that, from the start of the iteration, the erroris su!ciently small that “quadratic convergence rules!”. Consider the floating–point number

a = S(a) · 2e(a)

where the significand, S(a), satisfies 1 & S(a) < 2. (As we saw in Chapter 2, any positive floatingpoint number may be written this way.) If the exponent e(a) is even then

!a =

%S(a) · 2e(a)/2

Hence, we can compute!

a from this expression by computing the square root of the significand S(a)and halving the exponent e(a). Similarly, if e(a) is odd then e(a)#1 is even, so a = {2S(a)} ·2e(a)#1

and !a =

%2S(a) · 2(e(a)#1)/2

Hence, we can compute!

a from this expression by computing the square root of twice the signifi-cand, 2S(a), and halving the adjusted exponent, e(a)# 1.

Combining these two cases we observe that the significand is in the range

1 & S(a) < 2 & 2S(a) < 4

So, for all values a > 0 computing!

a is reduced to simply modifying the exponent and computing!b for a value b in the interval [1, 4); here b is either S(a) or it is 2S(a) depending on whether the

exponent e(a) of a is even or odd, respectively.This process of reducing the problem of computing square roots of numbers a defined in the semi-

infinite interval (0,)) to a problem of computing square roots of numbers b defined in the (small)finite interval [1,4) is a form of range reduction. Range reduction is used widely in simplifying

Page 13: chap6

6.2. THE NEWTON ITERATION 171

the computation of mathematical functions because, generally, it is easier (and more accurate ande!cient) to find a good approximation to a function at all points in a small interval than at allpoints in a larger interval.

The Newton iteration to compute!

b is

Xn+1 =

#Xn +

b

Xn

$

2, n = 0, 1, . . .

where X0 is chosen carefully in the interval [1, 2) to give convergence in a small (known) numberof iterations. On many computers a seed table is used to generate the initial iterate X0. Giventhe value of a, the value of b =(S(a) or 2S(a)) is computed, then the arithmetic unit supplies theleading bits (binary digits) of b to the seed table which returns an estimate for the square root of thenumber described by these leading bits. This provides the initial estimate X0 of

!b. Starting from

X0, the iteration to compute!

b converges to machine accuracy in a known maximum number ofiterations (usually 1, 2, 3 or 4 iterations). The seed table is constructed so the number of iterationsneeded to achieve machine accuracy is fixed and known for all numbers for which each seed fromthe table is used as the initial estimate. Hence, there is no need to check whether the iteration hasconverged thus saving computing the iterate needed for a check!

Problem 6.2.6. Use the above range reduction strategy (based on powers of 2) combined with theNewton iteration for computing each of

!5 and

!9. [Hint: 5 = 5

4 · 22] (In each case compute b

then start the Newton iteration for computing!

b from X0 = 1.0). Continue the iteration until youhave computed the maximum number of correct digits available on your calculator. Check for (a)the predicted behavior of the iterates and (b) quadratic convergence.

Problem 6.2.7. Consider the function f(x) = x3#a which has only one real root, namely x = 3!

a.Show that a simplified Newton iteration for this function may be derived as follows

xn+1 = xn #f(xn)f "(xn)

= xn #13

#x3

n # a

x2n

$

=13

#2xn +

a

x2n

$

Problem 6.2.8. In the cube root iteration above show that the second (algebraically simplified)expression for the cube root iteration is less expensive to compute than the first. Assume that the

value of13

is precalculated and in the iteration a multiplication by this value is used where needed.

Problem 6.2.9. For a > 0 and the cube root iteration

1. Show thaten+1 =

(2xn + 3!

a)3x2

n

e2n

when x0 > 0

2. Hence show that e1 * 0 and x1 > 3!

a and, in general, that xn * 3!

a for all n * 1

3. When xn * 3!

a show that

0 & (2xn + 3!

a)en

3x2n

< 1

4. Hence, show that the error is reduced at each iteration and that the Newton iteration convergesfrom above to 3

!a from any initial estimate x0 > 0.

Problem 6.2.10. We saw that!

x can be computed for any x > 0 by range reduction from values of!X for X $ [1, 4). Develop a similar argument that shows how 3

!x can be computed for any x > 0

by range reduction from values of 3!

X for X $ [1, 8). What is the corresponding range of values forX for computing 3

!x when x < 0?

Page 14: chap6

172 CHAPTER 6. ROOT FINDING

Problem 6.2.11. Use the range reduction derived in the previous problem followed by a Newtoniteration to compute each of the values 3

!10 and 3

!#20; in each case, start the iteration from

X0 = 1.0.

Problem 6.2.12. Let a > 0. Write down the roots of the function

f(x) = a# 1x2

Write down the Newton iteration for computing the positive root of f(x) = 0. Show that the iterationformula can be simplified so that it does not involve divisions. Compute the arithmetic costs periteration with and without the simplification; recall that multiplications or divisions by two (exponentshifts) do not count as arithmetic. How would you compute

!a from the result of the iteration,

without using divisions or using additional iterations?

Problem 6.2.13. Let a > 0. Write down the real root of the function

f(x) = a# 1x3

Write down the Newton iteration for computing the real root of f(x) = 0. Show that the iterationformula can be simplified so that it does not involve a division at each iteration. Compute the arith-metic costs per iteration with and without the simplification; identify any parts of the computationthat can be computed a priori, that is they can be computed just once independently of the numberof iterations. How would you compute 3

!a from the result of the iteration, without using divisions

or using additional iterations?

Calculator and Software Note

The algorithm for calculating square roots on many calculators and in many mathematical softwarelibraries is essentially what has been described in this section.

6.3 Secant Iteration

The major disadvantage of the Newton iteration is that the function f " must be evaluated at eachiteration. This derivative may be di!cult to derive or may be far more expensive to evaluate thanthe function f . One quite common circumstance where it is di!cult to derive f " is when f isdefined by a program. Recently, the technology of Automatic Di"erentiation has made computingautomatically the values of derivatives defined in such complex ways a possibility.

When the cost of evaluating the derivative is the problem, this cost can often be reduced byusing instead the iteration

xn+1 = xn #f(xn)mn

approximating f "(xn) in the Newton iteration by mn which is chosen as a di!erence quotient

mn =f(xn)# f(xn#1)

xn # xn#1

This di"erence quotient uses the already–computed values f(x) from the current and the previousiterate. It yields the secant iteration:

xn+1 = xn #f(xn)#

f(xn)# f(xn#1)xn # xn#1

$

= xn ##

f(xn)f(xn)# f(xn#1)

$(xn # xn#1) [preferred computational form]

=xnf(xn#1)# xn#1f(xn)

f(xn#1)# f(xn)[computational form to be avoided]

Page 15: chap6

6.3. SECANT ITERATION 173

This iteration requires two starting values x0 and x1. Of the three expressions for the iterate xn+1,the second expression

xn+1 = xn ##

f(xn)f(xn)# f(xn#1)

$(xn # xn#1), n = 1, 2, . . .

provides the “best” implementation. It is written as a correction of the most recent iterate in termsof a scaling of the di"erence of the current and the previous iterate. All three expressions su"erfrom cancellation; in the last, cancellation can be catastrophic.

When the error en is small enough and when the secant iteration converges, it can be shown that

the error ultimately behaves like |en+1| ( K|en|p where p =1 +

!5

2( 1.618 and K is a positive

constant. (Derivation of this result is not trivial.) Since p > 1, the error exhibits superlinear conver-gence but at a rate less than that exemplified by quadratic convergence. However, this superlinearconvergence property has a similar e"ect on the behavior of the error as the quadratic convergenceproperty of the Newton iteration. As long as f ""(x) is continuous and the root x! is simple, we havethe following properties:

• If the initial iterates x0 and x1 are both su!ciently close to the root x! then the secant iterationis guaranteed to converge to x!.

• When the iterates xn and xn#1 are su!ciently close to the root x! then the secant convergessuperlinearly; that is, the number of correct digits increases faster than linearly at each iter-ation. Ultimately the number of correct digits increases by a factor of approximately 1.6 periteration.

Example 6.3.1. Consider solving the equation f(x) % x3 # x # 1 = 0 using the secant iterationstarting with x0 = 1 and x1 = 2. Observe in Table 6.7 that the errors are tending to zero atan increasing rate but not as fast as in quadratic convergence where the number of correct digits

doubles at each iteration. The ratio |ei|/|ei#1|p where p =1 +

!5

2is tending to a limit until limiting

precision is encountered.

n xn en |en|/|en#1|p0 1.0 -0.32471795724475 –1 2.0 0.67528204275525 –2 1.16666666666666 -0.15805129057809 0.2983 1.25311203319502 -0.07160592404973 1.4174 1.33720644584166 0.01248848859691 0.8905 1.32385009638764 -0.00086786085711 1.0436 1.32470793653209 -0.00001002071266 0.9017 1.32471796535382 0.00000000810907 0.9958 1.32471795724467 -0.00000000000008 0.9849 1.32471795724475 -0.00000000000000 –

Table 6.7: Secant iterates, errors and error ratios

Problem 6.3.1. Derive the secant iteration geometrically as follows: Write down the formula forthe straight line joining the two points (xn#1, f(xn#1)) and (xn, f(xn)), then for the next iterate xn+1

choose the point where this line crosses the x-axis. (The figure should be very similar to Fig. 6.5.)

Problem 6.3.2. In the three expressions for the secant iteration above, assume that the calculationof f(xn) is performed just once for each value of xn. Write down the cost per iteration of eachform in terms of arithmetic operations (adds, multiplies, subtracts and divides) and function, f ,evaluations.

Page 16: chap6

174 CHAPTER 6. ROOT FINDING

5 10 15 20 25 30

5

10

15

20

25

30

Figure 6.6: Plot of # log2(en) versus n — linear on average convergence of bisection.

6.4 Root Bracketing Methods

The methods described so far in this chapter choose an initial estimate for the root of f(x), then usean iteration which converges in appropriate circumstances. However, normally there is no guaranteethat these iterations will converge from an arbitrary initial estimate. Next we discuss iterationswhere the user must supply more information but the resulting iteration is guaranteed to converge.

6.4.1 Bisection

The interval [a, b] is a bracket for a root of f(x) if f(a) · f(b) & 0. Clearly, when f is continuousa bracket contains a root of f(x). For, if f(a) · f(b) = 0, then either a or b or both is a root off(x) = 0. And, if f(a) · f(b) < 0, then f(x) starts with one sign at a and ends with the oppositesign at b, so by continuity f(x) = 0 must have a root between a and b. (Mathematically, this lastargument appeals to the Intermediate Value Theorem, see Problem 6.0.1.)

Let [a, b] be a bracket for a root of f(x). The bisection method refines this bracket (that is, it

makes it shorter) as follows. Let m =a + b

2; this divides the interval [a, b] into two subintervals

[a,m] and [m, b] each of half of the length of [a, b]. We know that at least one of these subintervalsmust be a bracket (see Problem 6.4.2), so the bisection process refines an initial bracket to a bracketof half its length. This process is repeated with the newly computed bracket replacing the previousone. On average, the bisection method converges linearly to a root. As described in Problem 6.4.4,this (approximate) linear convergence is illustrated in Fig. 6.6. That is, the data points in the figurelie approximately on a straight line of slope one through the origin.

Example 6.4.1. Consider solving the equation f(x) % x3 # x# 1 = 0 using the bisection methodstarting with the bracket [a, b] where a = 1 and b = 2 and terminated when the bracket is smallerthan 10#4. Observe in Table 6.8 that the errors are tending to zero but irregularly. The ratioen/en#1 is not tending to a limit.

Problem 6.4.1. Consider using the bisection method to compute the root of f(x) = x#e#x startingwith initial bracket [0, 1] and finishing with a bracket of length 2#10. Predict how many iterationswill be needed?

Problem 6.4.2. Let the interval [a, b] be a bracket for a root of the function f , and let m =a + b

2be

the midpoint of [a, b]. Show that if neither [a,m] nor [m, b] is a bracket, then neither is [a, b]. [Hint:If neither [a,m] nor [m, b] is a bracket, then f(a) · f(m) > 0 and f(m) · f(b) > 0. Conclude that

Page 17: chap6

6.4. ROOT BRACKETING METHODS 175

xn en en/en#1

1.0000000000000000 -0.3247179572447501 –2.0000000000000000 0.6752820427552499 –1.5000000000000000 0.1752820427552499 0.2601.2500000000000000 -0.0747179572447501 -0.4261.3750000000000000 0.0502820427552499 -0.6731.3125000000000000 -0.0122179572447501 -0.2431.3437500000000000 0.0190320427552499 -1.5581.3281250000000000 0.0034070427552499 0.1791.3203125000000000 -0.0044054572447501 -1.2931.3242187500000000 -0.0004992072447501 0.1131.3261718750000000 0.0014539177552499 -2.9121.3251953125000000 0.0004773552552499 0.3281.3247070312500000 -0.0000109259947501 -0.0231.3249511718750000 0.0002332146302499 -21.3451.3248291015625000 0.0001111443177499 0.4771.3247680664062500 0.0000501091614999 0.451

Table 6.8: Bisection iterates, errors and error ratios

f(a) · f(b) > 0; that is, conclude that [a, b] is not a bracket.] Now, proceed to use this contradictionto show that one of the intervals [a,m] and [m, b] must be a bracket for a root of the function f .

Problem 6.4.3. (Boundary Case) Let [a, b] be a bracket for a root of the function f . In the definitionof a bracket, why is it important to allow for the possibility that f(a)·f(b) = 0? In what circumstancemight we have f(a) ·f(b) = 0 without having either f(a) = 0 or f(b) = 0? Is this circumstance likelyto occur in practice? How would you modify the algorithm to avoid this problem?

Problem 6.4.4. Let {[an, bn]}$n=0 be the sequence of intervals (brackets) produced when bisection isapplied to the function f(x) % x# e#x starting with the initial interval (bracket) [a0, b0] = [0, 1]. Letx! denote the root of f(x) in the interval [0, 1], and let en % max(x!# an, bn# x!) be the maximumdeviation of the endpoints of the nth interval [an, bn] from the root x!. Fig. 6.6 depicts a plotof # log2(en) versus n. Show that this plot is consistent with the assumption that, approximately,en = c · 2#n for some constant c.

6.4.2 False Position Iteration

The method of bisection makes no use of the magnitudes of the values f(x) at the endpoints ofeach bracket, even though they are normally computed. In contrast, the method of false positionuses these magnitudes. As in bisection, in false position we begin with a bracket [a, b] containingthe root and the aim is to produce successively shorter brackets that still contain the root. Ratherthan choose the interior point t as the midpoint m of the current bracket [a, b] as in bisection, themethod of false position chooses as t the point where the secant line through the points (a, f(a))and (b, f(b)) intersects the x#axis. So

t = a##

f(a)f(a)# f(b)

$(a# b)

is the new approximation to the root of f(x). Then, the new bracket is chosen by replacing one ofthe endpoints a or b by this value of t using the sign of f(t) so as to preserve a bracket, just like inthe bisection method. As the bracket [a, b] is refined, the graph of f(x) for points inside the bracketbehaves increasingly like a straight–line. So we might expect that this “straight line” approximationwould accurately estimate the position of the root.

Is this false position algorithm useful? Probably not. Consider the case where the function f isconcave up on the bracket [a, b] with f(a) < 0 and f(b) > 0. The secant line through the points

Page 18: chap6

176 CHAPTER 6. ROOT FINDING

xi ei ei/ei#1

1.0000000000000000 -0.3247179572447501 –2.0000000000000000 0.6752820427552499 –1.1666666666666665 -0.1580512905780835 -0.2341.2531120331950207 -0.0716059240497293 0.4531.2934374019186834 -0.0312805553260667 0.4371.3112810214872344 -0.0134369357575157 0.4301.3189885035664628 -0.0057294536782873 0.4261.3222827174657958 -0.0024352397789542 0.4251.3236842938551607 -0.0010336633895893 0.4241.3242794617319507 -0.0004384955127994 0.4241.3245319865800917 -0.0001859706646583 0.4241.3246390933080365 -0.0000788639367135 0.4241.3246845151666697 -0.0000334420780803 0.4241.3247037764713760 -0.0000141807733740 0.4241.3247119440797213 -0.0000060131650288 0.4241.3247154074520977 -0.0000025497926524 0.4241.3247168760448742 -0.0000010811998759 0.4241.3247174987790529 -0.0000004584656972 0.424

Table 6.9: False position iterates, errors and error ratios

(a, f(a)) and (b, f(b)) cuts the x-axis at a point t to the left of the root x! of f(x) = 0. Hence, thevalues f(t) and f(a) have the same sign. Hence the left endpoint a will be moved to the positionof t. This is fine, but what is not fine is that it is also the left endpoint that is moved on all futureiterations! That is, the false position algorithm never moves the right endpoint when the function fis concave up on the bracket! If, instead, f is concave down on the bracket, by a similar argumentthe left endpoint is never moved by the false position algorithm. Almost all functions are eitherconcave up or concave down on short enough intervals containing a simple root, so the false positionmethod is a poor algorithm for refining a bracket in almost all cases.

We can “improve” the false position method in many ways so as to ensure that convergence isnot one–sided and that the length of the bracket tends to zero. One such method is to keep track ofthe brackets and when it is clear that the iterates are one–sided to insert a bisection point in placeof a false position point as the next iterate. Such an algorithm is described in the pseudocode inFig. 6.7. In this algorithm two iterations of one–sided behavior are permitted before a bisectioniteration is inserted. In reality far more sophisticated algorithms are used.

Example 6.4.2. Consider solving the equation f(x) % x3 # x # 1 = 0 using the false positionmethod defined in Fig. 6.7 starting with a = 1 and b = 2 and terminating when the error is smallerthan 10#6. Observe in Table 6.9 that the errors are tending to zero linearly; the ratio |ei|/|ei#1| istending to a constant. Convergence is one-sided (the errors are all negative).

Example 6.4.3. Consider solving the equation f(x) % x3 # x # 1 = 0 using the false position-bisection method starting with the bracket [a, b] where a = 1 and b = 2 and terminated whenthe bracket is smaller than 10#4. Observe in Table 6.10 that the errors are tending to zero buterratically. Convergence is not longer one-sided but the error is much smaller for the right-hand endof the bracket.

Problem 6.4.5. Show that t is the point where the straight line through the points (a, f(a)) and(b, f(b)) intersects the x#axis.

Problem 6.4.6. Consider any function f that is concave down. Argue that the false positionalgorithm will never move the left endpoint.

Page 19: chap6

6.5. QUADRATIC INVERSE INTERPOLATION 177

Let [a, b] be a bracket for a root of f(x)The requirement is to reduce the length of the bracket to a length no greater than tolThe counters ka and kb keep track of the changes in the endpoints

ka := 0; kb := 0fa := f(a); fb := f(b)if sign(fa) + sign(fb) * 0 then return (no bracket on root)while |b# a| > tol do

if ka = 2 or kb = 2 then!Use bisection

t := a + 0.5(b# a)ka := 0; kb := 0

else!Use false position

t := a + fa(fb# fa)/(b# a)endifft := f(t)if ft = 0 then return (exact root found)if sign(fa) + sign(ft) & 0 then

b := t; fb := ftka := ka + 1; kb := 0

elsea := t; fa := ftka := 0; kb := kb + 1

endifend while

Figure 6.7: Pseudocode False Position – Bisection.

6.5 Quadratic Inverse Interpolation

A rather di"erent approach to root-finding is to use inverse interpolation. We discuss just the mostcommonly used variant, quadratic inverse interpolation. Consider a case where we have three pointsxn#2, xn#1 and xn and we have evaluated the function f at each of these points giving the valuesyn#i = f(xn#i), i = 0, 1, 2. Assume f has an inverse function g then xn#i = g(yn#i), i = 0, 1, 2.Now, make a quadratic interpolant to this inverse data:

P2(y) =(y # yn#1)(y # yn#2)

(yn # yn#1)(yn # yn#2)g(yn)+

(y # yn)(y # yn#2)(yn#1 # yn)(yn#1 # yn#2)

g(yn#1)+(y # yn#1)(y # yn)

(yn#2 # yn#1)(yn#2 # yn)g(yn#2)

Next, evaluate this expression at y = 0 giving

xn+1 = P2(0) =yn#1yn#2

(yn # yn#1)(yn # yn#2)xn+

ynyn#2

(yn#1 # yn)(yn#1 # yn#2)xn#1+

yn#1yn

(yn#2 # yn#1)(yn#2 # yn)xn#2

Discard the point xn#2 and continue with the same process to compute xn+2 using the points xn#1

and xn, and the new point xn+1. Proceeding in this way, we obtain an iteration reminiscent of thesecant iteration for which the error behaves approximately like en+1 = Ke1.44

n . Note that this rateof convergence occurs only when all the xi are close enough to the root x! of the equation f(x) = 0;that is, in the limit quadratic inverse interpolation is a superlinearly convergent method.

Now, the quadratic inverse interpolation iteration is only guaranteed to converge from points closeenough to the root. One way of “guaranteeing” convergence is to use the iteration in a bracketingmethod, rather like false position whose disadvantages quadratic inverse interpolation shares, in thissetting. Consider three points a, b and c and the values A = f(a), B = f(b) and C = f(c). With

Page 20: chap6

178 CHAPTER 6. ROOT FINDING

xi ei

1.0000000000000000 -0.32471795724475012.0000000000000000 0.67528204275524991.1666666666666665 -0.15805129057808351.2531120331950207 -0.07160592404972931.6265560165975104 0.30183805935276031.3074231247616550 -0.01729483248309501.3206623408834519 -0.00405561636129811.4736091787404813 0.14889122149573121.3242063715696741 -0.00051158567507591.3246535623375968 -0.00006439490715331.3991313705390391 0.07441341329428911.3247137081586036 -0.00000424908614651.3247176768803632 -0.00000028036438681.3619245237097011 0.03720656646495101.3247179477646336 -0.00000000948011651.3247179569241898 -0.00000000032056021.3433212403169454 0.01860328307219541.3247179572392584 -0.00000000000549161.3247179572446521 -0.00000000000009791.3340195987807988 0.00930164153604871.3247179572447452 -0.00000000000000491.3247179572447461 -0.0000000000000040

Table 6.10: False position–bisection iterates and errors

appropriate ordering a < b < c there are two possibilities: AB < 0 and AC < 0 so that [a, b] is abracket on the root; and AB > 0 and AC < 0 so that [b, c] is a bracket on the root. We considerjust the first of these; the second follows similarly. We compute

d = P2(0) =BC

(A#B)(A# C)a +

AC

(B #A)(B # C)b +

AB

(C #B)(C #A)c

and we expect d to be close to the root. Indeed, since P2(y) is quadratic, normally we would expectthat d $ [a, b]. If this is the case we can discard the point c and order the points so they have thesame properties as above (or the alternative AC < 0 and BC < 0 which gives the bracket [b, c])then proceed as before. Unfortunately, d may not lie in [a, c] and even if it does it may be very closeto one of the points a, b and c. So, to use this method we need a method for dealing with thesecases where d is not usable. One such approach is to use a secant iteration instead for the interval[a, b] using the values A = f(a) and B = f(b) in those cases when d /$ (a, b) or when d $ [a, b] butd ( a or d ( b. If a secant approximation also delivers an unacceptable point d then bisection maybe used.

Problem 6.5.1. Derive the formula for the error in interpolation associated with the interpolationformula

P2(y) =(y # yn#1)(y # yn#2)

(yn # yn#1)(yn # yn#2)g(yn)+

(y # yn)(y # yn#2)(yn#1 # yn)(yn#1 # yn#2)

g(yn#1)+(y # yn#1)(y # yn)

(yn#2 # yn#1)(yn#2 # yn)g(yn#2)

Write the error term in terms of derivatives of f(x) rather than of g(y), recalling that g is the inverse

of f so that g"(y) =1

f "(x).

Problem 6.5.2. Show graphically how we may get d /$ (a, b). Remember in sketching your graphthat P2(y) is quadratic in y. [Hint: Make the y-axis the horizontal.]

Page 21: chap6

6.6. ROOTS OF POLYNOMIALS 179

6.6 Roots of Polynomials

Specialized versions of the root–finding iterations described in previous sections may be tailored forcomputing the roots of a polynomial

pn(x) = anxn + an#1xn#1 + . . . + a0

of degree n. These methods exploit the fact that we know precisely the number of roots for apolynomial of exact degree n; namely, there are n roots counting multiplicities. In many cases ofinterest some, or all, of these roots are real.

We concentrate on the Newton iteration. To use a Newton iteration to find a root of a function frequires values of both f(x) and its derivative f "(x). For a polynomial pn, it is easy to determine boththese values e!ciently. An algorithm for evaluating polynomials e!ciently, and generally accurately,is Horner’s rule .

The derivation of Horner’s rule begins by noting that a polynomial pn can be expressed as asequence of nested multiplies. For example, for a cubic polynomial, n = 3, and

p3(x) = a3x3 + a2x

2 + a1x + a0

may be written as the following sequence of nested multiplies

p3(x) = ((((a3)x + a2)x + a1)x + a0)

which may be written as the following sequence of operations for evaluating p3(x):

t3(x) = a3

t2(x) = a2 + x · t3(x)t1(x) = a1 + x · t2(x)

p3(x) = t0(x) = a0 + x · t1(x)

where ti(x) denotes the ith temporary function.

Example 6.6.1. Consider evaluating the cubic

p3(x) = 2# 5x + 3x2 # 7x3

= (2) + x · {(#5) + x · [(3) + x · (#7)]}

at x = 2. This nested multiplication form of p3(x) leads to the following sequence of operations forevaluating p3(x) at any point x:

t3(x) = (#7)t2(x) = (3) + x · t3(x)t1(x) = (#5) + x · t2(x)

p3(x) % t0(x) = (2) + x · t1(x)

Evaluating this sequence of operations for x = 2 gives

t3(x) = (#7)t2(x) = (3) + 2 · (#7) = #11t1(x) = (#5) + 2 · (#11) = #27

p3(x) % t0(x) = (2) + 2 · (#27) = #52

which you may check by evaluating the original cubic polynomial directly.

To determine the derivative p"3(x) we di"erentiate this sequence of operations with respect to xto give the derivative sequence

t"3(x) = 0t"2(x) = t3(x) + x · t"3(x)t"1(x) = t2(x) + x · t"2(x)

p"3(x) % t"0(x) = t1(x) + x · t"1(x)

Page 22: chap6

180 CHAPTER 6. ROOT FINDING

Merging alternate lines of the derivative sequence with that for the polynomial produces a sequencefor evaluating the cubic p3(x) and its derivative p"3(x) simultaneously, namely

t"3(x) = 0t3(x) = a3

t"2(x) = t3(x) + x · t"3(x)t2(x) = a2 + x · t3(x)t"1(x) = t2(x) + x · t"2(x)t1(x) = a1 + x · t2(x)

p"3(x) % t"0(x) = t1(x) + x · t"1(x)p3(x) % t0(x) = a0 + x · t1(x)

Example 6.6.2. Continuing with Example 6.6.1

t"3(x) = 0t3(x) = (#7)t"2(x) = t3(x) + x · t"3(x)t2(x) = (3) + x · t3(x)t"1(x) = t2(x) + x · t"2(x)t1(x) = (#5) + x · t2(x)

p"3(x) % t"0(x) = t1(x) + x · t"1(x)p3(x) % t0(x) = (2) + x · t1(x)

and evaluating at x = 2 we have

t"3(x) = 0t3(x) = (#7)t"2(x) = (#7) + 2 · 0 = #7t2(x) = (3) + 2 · (#7) = #11t"1(x) = (#11) + 2 · (#7) = #25t1(x) = (#5) + 2 · (#11) = #27

p"3(x) % t"0(x) = (#27) + 2 · (#25) = #77p3(x) % t0(x) = (2) + 2 · (#27) = #52

Again you may check these values by evaluating the original polynomial and its derivative directlyat x = 2.

Since the current temporary values t"i(x) and ti(x) are used to determine only the next temporaryvalues t"i#1(x) and ti#1(x), we can write the new values t"i#1(x) and ti#1(x) over the old values t"i(x)and ti(x) as follows

tp = 0t = a3

tp = t + x · tpt = a2 + x · t

tp = t + x · tpt = a1 + x · t

p"3(x) % tp = t + x · tpp3(x) % t = a0 + x · t

For a polynomial of degree n the regular pattern of this code segment is captured in the pseudocodein Fig. 6.8.

Problem 6.6.1. Use Horner’s rule to compute the first two iterates x1 and x2 in Table 6.6.

Problem 6.6.2. Write out Horner’s rule for evaluating a quartic polynomial

p4(x) = a4x4 + a3x

3 + a2x2 + a1x + a0

Page 23: chap6

6.6. ROOTS OF POLYNOMIALS 181

tp := 0t := a[n]for i = n# 1 downto 0 do

tp := t + x + tpt := a[i] + x + t

next ip"n(x) := tppn(x) := t

Figure 6.8: Pseudocode – Horner’s rulefor simultaneously evaluating p"n(x) and pn(x)

and its derivative p"4(x). For the polynomial

p4(x) = 3x4 # 2x2 + 1

compute p4(#1) and p"4(#1) using Horner’s scheme.

Problem 6.6.3. What is the cost in arithmetic operations of a single Newton iteration when com-puting a root of the polynomial pn(x) using the code segment in Fig. 6.8 to compute both of thevalues pn(x) and p"n(x).

Problem 6.6.4. Modify the code segment computing the values pn(x) and p"n(x) in Fig. 6.8 so thatit also computes the value of the second derivative p""n(x). (Fill in the assignments to tpp below.)

tpp =?tp = 0t = a[n]for i = n# 1 downto 0 do

tpp =?tp = t + x + tpt = a[i] + x + t

next ip""n(x)= tppp"n(x)= tppn(x)= t

6.6.1 Synthetic Division

Consider a cubic polynomial p3 and assume that we have an approximation # to a root x! of p3(x).In synthetic division, we divide the polynomial

p3(x) = a3x3 + a2x

2 + a1x + a0

by the linear factor x# # to produce a quadratic polynomial

q2(x) = b3x2 + b2x + b1

as the quotient and a constant b0 as the remainder. The defining relation is

p3(x)x# #

= q2(x) +b0

x# #

To determine the constant b0, and the coe!cients b1, b2 and b3 in q2(x), we write this relation inthe form

p3(x) = (x# #)q2(x) + b0

Page 24: chap6

182 CHAPTER 6. ROOT FINDING

Now we substitute in this expression for q2 and expand the product (x # #)q2(x) + b0 as a sum ofpowers of x. As this must equal p3(x) we write the two power series together:

(x# #)q2(x) + b0 = (b3)x3 + (b2 # #b3)x2 + (b1 # #b2)x + (b0 # #b1)p3(x) = a3x3 + a2x2 + a1x + a0

The left hand sides of these two equations must be equal for any value x, so we can equate thecoe!cients of the powers xi on the right hand sides of each equation (here # is considered to befixed). This gives

a3 = b3

a2 = b2 # #b3

a1 = b1 # #b2

a0 = b0 # #b1

Solving for the unknown values bi, we have

b3 = a3

b2 = a2 + #b3

b1 = a1 + #b2

b0 = a0 + #b1

We observe that ti(#) = bi; that is, Horner’s rule and synthetic division are di"erent interpretationsof the same relation. The intermediate values ti(#) determined by Horner’s rule are the coe!cientsbi of the polynomial quotient q2(x) that is obtained when p3(x) is divided by (x # #). The resultb0 is the value t3(#) = p3(#) of the polynomial p3(x) when it is evaluated at the point x = #. So,when # is a root of p3(x) = 0, the value of b0 = 0.

Synthetic division for a general polynomial pn(x), of degree n in x, proceeds analogously. Dividingpn(x) by (x # #) produces a quotient polynomial qn#1(x) of degree n # 1 and a constant b0 asremainder. So,

pn(x)x# #

= qn#1(x) +b0

x# #

whereqn#1(x) = bnxn#1 + bn#1x

n#2 + . . . + b1

Sincepn(x) = (x# #)qn#1(x) + b0,

we find that

(x# #)qn#1(x) + b0 = bnxn + (bn#1 # #bn)xn#1 + . . . + (b0 # #b1)pn(x) = anxn + an#1xn#1 + . . . + a0

Equating coe!cients of the powers xi,

an = bn

an#1 = bn#1 # #bn...

a0 = b0 # #b1

Solving these equations for the constant b0 and the unknown coe!cients bi of the polynomial qn#1(x)leads to

bn = an

bn#1 = an#1 + #bn...

b0 = a0 + #b1

How does the synthetic division algorithm apply to the problem of finding the roots of a polyno-mial? First, from its relationship to Horner’s rule, for a given value of # synthetic division e!ciently

Page 25: chap6

6.6. ROOTS OF POLYNOMIALS 183

determines the value of the polynomial pn(#) (and also of its derivative p"n(#) using the techniquedescribed in the pseudocode given in Fig. 6.8). Furthermore, suppose that we have found a goodapproximation to the root, # ( x!. When the polynomial pn(x) is evaluated at x = # via syntheticdivision the remainder b0 ( pn(x!) = 0. We treat the remainder b0 as though it is precisely zero.Therefore, approximately

pn(x) = (x# x!)qn#1(x)

where the quotient polynomial qn#1 has degree one less than the polynomial pn. The polynomialqn#1 has as its (n#1) roots values that are close to each of the remaining (n#1) roots of pn(x) = 0.We call qn#1 the deflated polynomial obtained by removing a linear factor (x # #) approximating(x# x!) from the polynomial pn. This process therefore reduces the problem of finding the roots ofpn(x) = 0 to

• Find one root # of the polynomial pn(x)

• Deflate the polynomial pn to derive a polynomial of one degree lower, qn#1

• Repeat this procedure with qn#1 replacing pn

Of course, this process can be “recursed” to find all the real roots of a polynomial equation. If thereare only real roots, eventually we will reach a polynomial of degree two. Then, we may stop and usethe quadratic formula to “finish o"”.

To use this approach to find all the real and all the complex roots of a polynomial equation, wemust use complex arithmetic in both the Newton iteration and in the synthetic division algorithm,and we must choose starting estimates for the Newton iteration that lie in the complex plane; thatis, the initial estimates x0 must not be on the real line. Better, related approaches exist whichexploit the fact that, for a polynomial with real coe!cients, the complex roots arise in conjugatepairs. The two factors corresponding to each complex conjugate pair taken together correspond to anirreducible quadratic factor of the original polynomial. These alternative approaches aim to computethese irreducible quadratic factors directly, in real arithmetic, and each irreducible quadratic factormay then be factored using the quadratic formula.

Example 6.6.3. Consider the quartic polynomial

p4(x) = x4 # 5x2 + 4

which has roots ±1 and ±2. Assume that we have calculated the root # = #2 and that we use thesynthetic division algorithm to deflate the polynomial p4(x) = a4x4 + a3x3 + a2x2 + a1x + a0 tocompute the cubic polynomial q3(x) = b4x3 + b3x2 + b2x + b1 and the constant b0. The algorithm is

b4 = a4

b3 = a3 + #b4

b2 = a2 + #b3

b1 = a1 + #b2

b0 = a0 + #b1

Substituting the values of the coe!cients of the quartic polynomial p4 and evaluating at # = #2 wecompute

b4 = (1)b3 = (0) + (#2)(1) = #2b2 = (#5) + (#2)(#2) = #1b1 = (0) + (#2)(#1) = 2b0 = (4) + (#2)(2) = 0

So, b0 = 0 as it should be since # = #2 is exactly a root and we have

q3(x) = (1)x3 + (#2)x2 + (#1)x + (2)

You may check that q3(x) has the remaining roots ±1 and 2 of p4(x).

Page 26: chap6

184 CHAPTER 6. ROOT FINDING

Example 6.6.4. We’ll repeat the previous example but using the approximate root # = #2.001and working to four significant digits throughout. Substituting this value of # we compute

b4 = (1)b3 = (0) + (#2.001)(1) = #2.001b2 = (#5) + (#2.001)(#2.001) = #0.9960b1 = (0) + (#2.001)(#0.9960) = 1.993b0 = (4) + (#2.001)(1.9993) = 0.0120

Given that the error in # is only 1 in the fourth significant digit, the value of b0 is somewhat largerthan we might expect. If we assume that # = #2.001 is close enough to a root, which is equivalentto assuming that b0 = 0, the reduced cubic polynomial is q3(x) = x3# 2.001x2# 0.996x + 1.993. Tofour significant digits the cubic polynomial q3(x) has roots 2.001, 0.998 and #0.998, approximatingthe roots 2, 1 and #1 of p4(x), respectively.

Problem 6.6.5. The cubic polynomial p3(x) = x3 # 7x + 6 has roots 1, 2 and #3. Deflate p3

by synthetic division to remove the root # = #3. This will give a quadratic polynomial q2. Checkthat q2(x) has the appropriate roots. Repeat this process but with an approximate root # = #3.0001working to five significant digits throughout. What are the roots of the resulting polynomial q2(x).

Problem 6.6.6. Divide the polynomial qn#1(x) = bnxn#1 + bn#1xn#2 + . . .+ b1 by the linear factor(x# #) to produce a polynomial quotient rn#2(x) and remainder c1

qn#1(x)x# #

= rn#2(x) +c1

x# #

where rn#2(x) = cnxn#2 + cn#1xn#3 + . . . + c2.

(a) Solve for the unknown coe"cients ci in terms of the values bi.

(b) Derive a code segment, a loop, that interleaves the computations of the bi’s and ci’s.

Problem 6.6.7. By di!erentiating the synthetic division relation pn(x) = (x##)qn#1(x) + b0 withrespect to the variable x, keeping the value of # fixed, and equating the coe"cients of powers of xon both sides of the di!erentiated relation derive Horner’s algorithm for computing the value of thederivative p"n(x) at any point x = #.

6.6.2 Conditioning of Polynomial Roots

So far, we have considered calculating the roots of a polynomial by repetitive iteration and deflation,and we have left the impression that both processes are simple and safe although the computationof Example 6.6.4 should give us pause for thought. Obviously there are errors when the iterationis not completed, that is the root is not computed exactly, and these errors are clearly propagatedinto the deflation process which itself introduces further errors into the remaining roots. The overallprocess is di!cult to analyze but a simpler question which we can in part answer is whether thecomputed polynomial roots are sensitive to these errors. Particularly we might ask the closely relatedquestion “Are the polynomial roots sensitive to small changes in the coe!cients of the polynomial?”That is, is polynomial root finding an inherently ill–conditioned process? In many applications onealgorithmic choice is to reduce a “more complicated” computational problem to one of computingthe roots of a high degree polynomial. If the resulting problem is likely to be ill–conditioned, thisapproach should be considered with caution.

Consider a polynomialpn(x) = a0 + a1x + . . . anxn

Assume that the coe!cients of this polynomial are perturbed to give the polynomial

Pn(x) = A0 + A1x + . . . Anxn

Page 27: chap6

6.6. ROOTS OF POLYNOMIALS 185

Let z0 be a root of pn(x) and Z0 be the “corresponding” root of Pn(x); usually, for su!ciently smallperturbations of the coe!cients ai there will be such a correspondence. By Taylor series

Pn(z0) ( Pn(Z0) + (z0 # Z0)P "n(Z0)

and so, since pn(z0) = 0,

Pn(z0)# pn(z0) ( Pn(Z0) + (z0 # Z0)P "n(Z0)

Observing that Pn(Z0) = 0, we have

z0 # Z0 (&n

i=0(Ai # ai)zi0

P "n(Z0)

So, the error in the root can be large when P "n(Z0) ( 0 such as will occur when Z0 is close to a clusterof roots of Pn(x). We observe that Pn is likely to have a cluster of at least m roots whenever pn hasa multiple root of multiplicity m. [Recall that at a multiple root # of Pn(x) we have P "n(#) = 0.]

One way that the polynomial Pn might arise is essentially directly. That is, if the coe!cients ofthe polynomial pn are not representable as computer numbers at the working precision then theywill be rounded; that is, the di"erence |ai # Ai| ( max{|ai|, |Ai|}$WP. In this case, a reasonableestimate is

z0 # Z0 (Pn(z0)P "n(Z0)

$WP

so that even small perturbations that arise in the storage of computer numbers can lead to asignificantly sized error. The other way Pn(x) might arise is as a consequence of backward erroranalysis. For some robust algorithms for computing roots of polynomials, we can show that theroots computed are the roots of a polynomial Pn(x) whose coe!cients are usually close to thecoe!cients of pn(x)

Example 6.6.5. Consider the following polynomial of degree 12:

p12(x) = (x# 1)(x# 2)2(x# 3)(x# 4)(x# 5)(x# 6)2(x# 7)2(x# 8)(x# 9)= x12 # 60x11 + 1613x10 # 25644x9 + · · ·

If we use, for example, Matlab (see Section 6.7) to compute the roots of the power series form ofp12(x), we obtain:

exact root computed root9 9.000000005610958 7.999999934573067 7.000451893741447 6.999547883074916 6.000403975008416 5.999596275487395 5.000000034911814 3.999999997494643 3.000000000090972 2.000001511196092 1.999998488810361 0.999999999999976

Observe that each of the double roots 2, 6 and 7 of the polynomial p12(x) is approximated by apair of close real roots near the roots 2, 6 and 7 of P12(x). Also note that the double roots areapproximated much less accurately than are the simple roots of similar size, and the larger roots arecalculated somewhat less accurately than the smaller ones.

Page 28: chap6

186 CHAPTER 6. ROOT FINDING

Example 6.6.5 and the preceding analysis indicates that multiple and clustered roots are likelyto be ill–conditioned, though this does not imply that isolated roots of the same polynomial arewell–conditioned. The numerator in the expression for z0 # Z0 contains “small” coe!cients ai #Ai

by definition so, at first glance, it seems that a near-zero denominator gives rise to the only ill–conditioned case. However, this is not so; as a famous example due to Wilkinson shows, the rootsof a polynomial of high degree may be ill–conditioned even when all its roots are well–spaced.

Example 6.6.6. Wilkinson devised an example of a ill-conditioned polynomial of degree 20 withthe integer roots 1, 2, . . . , 20, that is

p20(x) = (x# 1)(x# 2) · · · (x# 20)= x20 # 210x19 + 20615x18 # 1256850x17 + · · ·

To demonstrate ill-conditioning in this case, suppose we use Matlab (see Section 6.7) to constructthe power series form of p20(x), and then compute its roots. Some of the coe!cients are su!cientlylarge integers that they require more digits than are available in a Matlab DP word; so, they arerounded. The computed roots or p20(x) are

exact root computed root20 20.000338392795819 18.997027410935818 18.011785668882317 16.969525653841116 16.050898366879415 14.931918943506414 14.068379375633113 12.947162319145412 12.034506682850411 10.983610393002810 10.00625029750189 8.998318049623608 8.000310357154367 6.999967059353806 6.000000823043035 5.000000230760084 3.999999974231123 3.000000000860402 1.999999999999341 0.999999999999828

Observe that the computed roots are all real but the least accurate di"er from the true (integer) rootsin the fourth decimal place (recall that Matlab DP uses about 15 decimal digits in its arithmetic).A calculation of the residuals (that is, evaluations of the computed polynomial) using the Matlabfunction polyval for the true (integer) roots and for the computed roots shows that both are large.(The true roots have large residuals, when they should mathematically be zero, because of the e"ectsof rounding.) The residuals for the computed roots are about 2 orders of magnitude larger than theresiduals for the true roots. This indicates that Matlab performed satisfactorily – it was responsiblefor losing only about 2 of the available 15 digits of accuracy. So, the errors in the computed roots arethe result mainly of the ill–conditioning of the original polynomial. [Note: The results that we giveare machine dependent though you should get approximately the same values using DP arithmeticwith any compiler on any computer implementing the IEEE Standard.]

Example 6.6.7. Next, we increase the degree of the polynomial to 22 giving p22(x) by addingthe (next two) integer roots 21 and 22. Then, we follow the same computation. This time ourcomputation gives a mixture of real and complex conjugate pairs of roots with much larger errorsthan previously:

Page 29: chap6

6.6. ROOTS OF POLYNOMIALS 187

exact root computed root22 22.004493464158021 20.951418785742820 20.165637875297819 18.5836448660249 + 0.441345126232861i18 18.5836448660249 - 0.441345126232861i17 16.3917246244130 + 0.462094344544644i16 16.3917246244130 - 0.462094344544644i15 15.090596329535614 13.4984691077096 + 0.371867167940926i13 13.4984691077096 - 0.371867167940926i12 11.699070824032111 11.174148878414810 9.958434412611749 9.009824554992108 7.998580753329247 7.000118570147036 5.999999253657785 4.999999001940024 4.000000103891443 2.999999995910732 2.000000000044151 1.00000000000012

Observe that some of the large roots 21, 20, 15, 12, 11 and 10 of p22(x) are each computed to nomore than three correct digits. Worse, each of the pairs of real roots 18 and 19, 16 and 17, and13 and 14 of p22(x) become complex conjugate pairs of roots of P22(x). They are not even close tobeing real!

Problem 6.6.8. Consider the quadratic polynomial p2(x) = x2 # 2x + 1 with double root x = 1.Perturb the quadratic to the form P2(x) = x2#2(1+ $)x+1 where $ is small in magnitude comparedto 1 but may be either positive or negative. Approximately, by how much is the root of the quadraticperturbed? Does the root remain a double root? If not, do the roots remain real?

Page 30: chap6

188 CHAPTER 6. ROOT FINDING

6.7 Matlab Notes

By now our familiarity with Matlab should lead us to suspect that there are built-in functionsthat can be used to compute roots, and it is not necessary to write our own implementations of themethods discussed in this chapter. It is, however, instructive to implement some of the methods;at the very least, such exercises can help to improve our programming skills. We therefore beginthis section by developing implementations of fixed point, Newton and bisection methods. We thenconclude this section with a thorough discussion of the built-in Matlab functions roots (to findroots of polynomials) and fzero (to find a root of a more general function). We emphasize that it istypically best to use one of these built-in functions, which have been thoroughly tested, rather thanone of our own implementations.

6.7.1 Fixed Point Iteration

Recall that the basic fixed point iteration is given by

xn+1 = g(xn), n = 0, 1, 2, ...

The iterates, xn+1, can be computed using a for loop. To begin the iteration, though, we need aninitial guess, x0, and we need a function, g(x). If g(x) is simple, then it is usually best to define it asan anonymous function, and if g(x) is complicated, then it is usually best to define it as a functionm-file.

Example 6.7.1. Suppose we want to compute 12 iterations of g(x) = e#x, using the initial guessx0 = 0.5 (see Table 6.1). Since g(x) is a very simple function, we define it as an anonymous function,and use a for loop to generate the iterates:

g = @(x) exp(-x);x = 0.5;for n = 1:12x = g(x);

end

If we want to test the fixed point iteration for several di"erent g(x), then it would be helpful tohave a Matlab program (that is, a function m-file) that will work for arbitrary g(x). In order towrite this function, we need to consider a few issues:

• In the previous example, we chose, somewhat arbitrarily, to compute 12 iterations. For otherproblems this may be too many or two few iterations. To increase flexibility, we should:

– Stop the iteration if the computed solution is su!ciently accurate. This can be done, forexample, by stopping the iteration if the relative change between successive iterates isless than some specified tolerance. That is, if

|xn+1 # xn| & tol + |xn|.

This can be implemented by inserting the following conditional statement inside the forloop:

if ( abs(x(n+1)-x(n)) <= tol*abs(x(n)) )break

end

A value of tol must be either defined in the code, or specified by the user. The commandbreak terminates execution of the for loop.

Page 31: chap6

6.7. MATLAB NOTES 189

– Allow the user to specify a maximum number of iterations, and execute the loop untilthe above convergence criterion is satisfied, or until the maximum number of iterationsis reached.

• It might be useful to return all of the computed iterations. This can be done easily by creatinga vector containing x0, x1, · · · .

Combining these items with the basic code given in Example 6.7.1 we obtain the following function:

function x = FixedPoint(g, x0, tol, nmax)%% x = FixedPoint(g, x0, tol, nmax);%% Find a fixed point of g(x) using the iteration: x(n+1) = g(x(n))%% Input: g - an anonymous function or function handle,% x0 - an initial guess of the fixed point,% tol - a (relative) stopping tolerance, and% nmax - specifies maximum number of iterations.%% Output: x - a vector containing the iterates, x0, x1, ...%x(1) = x0;for n = 1:nmaxx(n+1) = g(x(n));if ( abs(x(n+1)-x(n)) <= tol*abs(x(n)) )break

endendx = x(:);

The final statement in the function, x = x(:), is used to ensure x is returned as a column vector.This, of course, is not necessary, and is done only for cosmetic purposes.

Example 6.7.2. To illustrate how to use the function FixedPoint, consider g(x) = e#x with theinitial guess x0 = 0.5.

(a) If we want to compute exactly 12 iterations, we can set nmax = 12 and tol = 0. That is, weuse the statements:

>> g = @(x) exp(-x);>> x = FixedPoint(g, 0.5, 0, 12);

The same result could be computed using just one line of code:

>> x = FixedPoint(@(x) exp(-x), 0.5, 0, 12);

(b) If we want to see how many iterations it takes to reach a certain stopping tolerance, say tol =10#8, then we should choose a relatively large value for nmax. For example, we could compute

>> x = FixedPoint(@(x) exp(-x), 0.5, 1e-8, 1000);

Page 32: chap6

190 CHAPTER 6. ROOT FINDING

The number of iterations is then length(x) - 1 (recall the first entry in the vector is theinitial guess). For this example, we find that it takes 31 iterations.

Problem 6.7.1. Implement FixedPoint and use it to:

(a) Produce a table corresponding to Table 6.1 starting from the value x0 = 0.6

(b) Produce a table corresponding to Table 6.2 starting from the value x0 = 0.0

(c) Produce a table corresponding to Table 6.2 starting from the value x0 = 2.0

Recall that it is possible to produced formated tables in Matlab using the sprintf and disp com-mands; see Section 1.3.5.

Problem 6.7.2. Implement FixedPoint and use it to compute 10 iterations using each g(x) givenin Problem 6.1.6, with x0 = 1.5.

Problem 6.7.3. Rewrite the function FixedPoint using while instead of the combination of forand if. Note that it will be necessary to implement a counter to determine when the number ofiterations has reached nmax.

6.7.2 Newton and Secant Methods

The Newton and secant methods are used to find roots of f(x) = 0. Implementation of these methodsis very similar to the fixed point iteration. For example, Newton’s method can be implemented asfollows:

function x = Newton(f, df, x0, tol, nmax)%% x = Newton(f, df, x0, tol, nmax);%% Use Newton’s method to find a root of f(x) = 0.%% Input: f - an anonymous function or function handle for f(x)% df - an anonymous function of function handle for f’(x)% x0 - an initial guess of the root,% tol - a (relative) stopping tolerance, and% nmax - specifies maximum number of iterations.%% Output: x - a vector containing the iterates, x0, x1, ...%x(1) = x0;for n = 1:nmaxx(n+1) = x(n) - f(x(n)) / df(x(n));if ( abs(x(n+1)-x(n)) <= tol*abs(x(n)) )break

endendx = x(:);

Note that in Newton’s method we must input two functions, f(x) and f "(x). It might be prudentto include a check, and print an error message, if df(x(n)) is zero, or smaller than a specified value.

Page 33: chap6

6.7. MATLAB NOTES 191

Example 6.7.3. Suppose we want to find a root of f(x) = 0 where f(x) = x#e#x, with initial guessx0 = 0.5. Then, using Newton, we can compute an approximation of this root using the followingMatlab statements:

>> f = @(x) x - exp(-x);>> df = @(x) 1 + exp(-x);>> x = Newton(f, df, 0.5, 1e-8, 10);

or, in one line of code, using

>> x = Newton(@(x) x - exp(-x), @(x) 1 + exp(-x), 0.5, 1e-8, 10);

For this problem, only 4 iterations of Newton’s method are needed to converge to the specifiedtolerance.

Problem 6.7.4. Implement Newton, and write a similar implementation for the secant iteration.Test both implementations at a simple root of a quadratic polynomial f(x) that is concave up, choos-ing the initial iterates x0 (and, in the case of secant, x1) close to this root. Construct tables con-taining the values of the iterates, xn and corresponding function values, f(xn). Is there any patternto the signs of the consecutive f(x) values?

Problem 6.7.5. Implement Newton, and write a similar implementation for the secant iteration.Use both methods to compute the root of f(x) = x#e#x. For Newton, use the initial guess x0 = 0.5,and for secant use x0 = 0 and x1 = 1. Check the ratios of the errors in successive iterates as inTable 6.7 to determine if they are consistent with the convergence rates discussed in this chapter.Repeat the secant method with initial iterates x0 = 1 and x1 = 0.

6.7.3 Bisection Method

Recall that bisection begins with an initial bracket, [a, b] containing the root, and proceeds as follows:

• First compute the midpoint, m = (a + b)/2.

• Determine if a root is in [a,m] or [m, b]. If the root is in [a,m], rename m as b, and continue.Similarly, if the root is in [m, b], rename m as a and continue.

• Stop if the length of the interval becomes su!ciently small.

Note that we can determine if the root is in [a,m] by checking to see if f(a)f(m) < 0. With theseobservations, an implementation of the bisection method could have the form:

Page 34: chap6

192 CHAPTER 6. ROOT FINDING

function x = Bisection0(f, a, b, tol, nmax)%% x = Bisection0(f, a, b, tol, nmax);%% Use bisection method to find a root of f(x) = 0.%% Input: f - an anonymous function or function handle for f(x)% [a,b] - an initial bracket containing the root% tol - a (relative) stopping tolerance, and% nmax - specifies maximum number of iterations.%% Output: x - a vector containing the iterates, x0, x1, ...%x(1) = a;, x(2) = b;for n = 2:nmaxm = (a+b)/2;, x(n+1) = m;if f(a)*f(m) < 0b = m;

elsea = m;

endif ( abs(b-a) <= tol*abs(a) )break

endendx = x(:);

Note that this implementation requires that we evaluate f(x) twice for every iteration (to computef(a) and f(b)). Since this is typically the most expensive part of the method, it can be helpful toreduce the number of function evaluations as much as possible. The bisection method can beimplemented so that only one function evaluation is needed at each iteration:

Page 35: chap6

6.7. MATLAB NOTES 193

function x = Bisection(f, a, b, tol, nmax)%% x = Bisection(f, a, b, tol, nmax);%% Use bisection method to find a root of f(x) = 0.%% Input: f - an anonymous function or function handle for f(x)% [a,b] - an initial bracket containing the root% tol - a (relative) stopping tolerance, and% nmax - specifies maximum number of iterations.%% Output: x - a vector containing the iterates, x0, x1, ...%x(1) = a;, x(2) = b;, fa = f(a);, fb = f(b);for n = 2:nmaxc = (a+b)/2;, x(n+1) = c;, fc = f(c);if fa*fc < 0b = c;, fb = fc;

elsea = c;, fa = fc;

endif ( abs(b-a) <= tol*abs(a) )break

endendx = x(:);

Problem 6.7.6. Implement Bisection and use it to compute a root of f(x) = x # e#x. Use theinitial bracket [0, 1]. Produce a table of results similar to Table 6.8.

Problem 6.7.7. Implement Bisection, Secant and Newton. Experimentally compare the conver-gence behavior of each method for the function f(x) = x# e#x.

Problem 6.7.8. Use the false position method to compute an approximation to the root of f(x) =x#e#x starting with an initial bracket [0, 1] and finishing when you have converged to four significantfigures. Check the ratios of the errors in successive iterates as in Table 6.9 to determine if they areconsistent with a linear rate of convergence.

Problem 6.7.9. Use the false position – bisection method described in the pseudocode in Fig. 6.7 tocompute a bracket on the root of f(x) = x# e#x starting with an initial bracket [0, 1] and finishingwith a bracket of length 10#4. Produce an output as in Table 6.10.

6.7.4 The roots and fzero Functions

As previously mentioned, Matlab provides two built-in root finding methods:

fzero finds a root of f(x) = 0, where f(x) is a general function of onevariable

roots finds all roots of p(x) = 0, where p(x) is a polynomial

In this section we discuss in more detail how to use these built-in Matlab functions.

Page 36: chap6

194 CHAPTER 6. ROOT FINDING

Computing a solution of f(x) = 0.

The Matlab function fzero is used to find zeros of a general function of one variable The imple-mentation uses a combination of bisection, secant and inverse quadratic interpolation. The basiccalling syntax for fzero is:

x = fzero(fun, x0)

where fun can be an inline or anonymous function, or a handle to a function m-file. The initial guess,x0 can be a scalar (that is, a single initial guess), or it can be a bracket (that is, a vector containingtwo values) on the root. Note that if x0 is a single initial guess, then fzero first attempts to find abracket by sampling on successively wider intervals containing x0 until a bracket is determined. If abracket for the root is known, then it is usually best to supply it, rather than a single initial guess.

Example 6.7.4. Consider the function f(x) = x# e#x. Because f(0) < 0 and f(1) > 0, a bracketfor a root of f(x) = 0 is [0, 1]. This root can be found as follows:

>> x = fzero(@(x) x-exp(-x), [0, 1])

We could have used a single initial guess, such as:

>> x = fzero(@(x) x-exp(-x), 0.5)

but as mentioned above, it is usually best to provide a bracket if one is known.

Problem 6.7.10. Use fzero to find a root of f(x) = 0, with f(x) = sinx. For initial guess, usex0 = 1, x0 = 5, and the bracket [1, 5]. Explain why a di!erent root is computed in each case.

Problem 6.7.11. Consider the function f(x) = 1x # 1, and note that f(1) = 0. Thus, we might

expect that x0 = 0.5 is a reasonable initial guess for fzero to compute a solution of f(x) = 0. Trythis, and see what fzero computes. Can you explain what happens here? Now use the initial guessto be the bracket [0.5, 1.5]. What does fzero compute in this case?

If a root, x!, has multiplicity greater than one, then f "(x!) = 0, and hence the root is not simple.fzero can find roots which have odd multiplicity, but it cannot find roots of even multiplicity, asillustrated in the following example.

Example 6.7.5. Consider the function f(x) = x2ex, which has a root x! = 0 of multiplicity 2.

(a) If we use the initial guess x0 = 1, then

>> f = @(x) x.*x.*exp(x);>> xstar = fzero(f, 1)

computes x ( #925.8190. At first this result may seem completely ridiculous, but if wecompute

>> f(xstar)

the result is 0. Notice that limx%#$

x2ex = 0, thus it appears that fzero ”finds” x ( #).

(b) If we try the initial guess x0 = #1, then

>> f = @(x) x.*x.*exp(x);>> xstar = fzero(f, -1)

Page 37: chap6

6.7. MATLAB NOTES 195

then fzero fails because it cannot find an interval containing a sign change.

(c) We know the root is x! = 0, so we might try the bracket [#1, 1]. However, if we compute

>> f = @(x) x.*x.*exp(x);>> xstar = fzero(f, [-1, 1])

an error occurs because the initial bracket does not satisfy the sign change condition, f(#1)f(1) <0.

Problem 6.7.12. The functions f(x) = x2#4x+4 and f(x) = x3#6x2 +12x#8 satisfy f(2) = 0.Attempt to find this root using fzero. Experiment with a variety of initial guesses. Why does fzerohave trouble finding the root for one of the functions, but not for the other?

It is possible to reset certain default parameters used by fzero (such as stopping tolerance andmaximum number of iterations), and it is possible to obtain more information on exactly what isdone during the computation of the root (such as number of iterations and number of functionevaluations) by using the calling syntax

[x, fval, exitflag, output] = fzero(fun, x0, options)

where

• x is the computed approximation of the root.

• fval is the function value of the computed root, f(x!). Note that if a root is found, then thisvalue should be close to zero.

• exitflag provides information about what condition caused fzero to terminate; see docfzero for more information.

• output is a structure array that contains information such as which algorithms were used (e.g.,bisection, interpolation, secant), number of function evaluations and number of iterations.

• options is an input parameter that can be used, for example, to reset the stopping tolerance,and to request that results of each iteration be displayed in the command window. The optionsare set using the built-in Matlab function optimset.

The following examples illustrate how to use options and output.

Example 6.7.6. Consider f(x) = x#e#x. We know there is a root in the interval [0, 1]. Suppose wewant to investigate how the cost of computing the root is a"ected by the choice of the initial guess.Since the cost is reflected in the number of function evaluations, we use output for this purpose.

(a) Using the initial guess x0 = 0, we can compute:

>> [x, fval, exitflag, output] = fzero(@(x) x-exp(-x), 0);>> output

we see that output displays the information:

intervaliterations: 10iterations: 6funcCount: 27algorithm: ’bisection, interpolation’message: ’Zero found in the interval [-0.64, 0.64]’

Page 38: chap6

196 CHAPTER 6. ROOT FINDING

The important quantity here is funcCount, which indicates that a total of 27 function evalu-ations was needed to compute the root.

(b) On the other hand, if we use x0 = 0.5, and compute:

>> [x, fval, exitflag, output] = fzero(@(x) x-exp(-x), 0.5);>> output

we see that 18 function evaluations are needed.

(c) Finally, if we use the bracket [0, 1], and compute:

>> [x, fval, exitflag, output] = fzero(@(x) x-exp(-x), [0, 1]);>> output

we see that only 8 function evaluations are needed.

Notice from this example that when we provide a bracket, the term intervaliterations, which isreturned by the structure output, is 0. This is because fzero does not need to do an initial searchfor a bracket, and thus the total number of function evaluations is substantially reduced.

Example 6.7.7. Again, consider f(x) = x # e#x, which has a root in [0, 1]. Suppose we want toinvestigate how the cost of computing the root is a"ected by the choice of stopping tolerance onx. To investigate this, we must first use the Matlab function optimset to define the structureoptions.

(a) Suppose we want to set the tolerance to 10#4. Then we can use the command:

>> options = optimset(’TolX’, 1e-4);

If we then execute the commands:

>> [x, fval, exitflag, output] = fzero(@(x) x-exp(-x), [0, 1], options);>> output

we see that only 5 function evaluations are needed.

(b) To increase the tolerance to 10#8, we can use the command:

>> options = optimset(’TolX’, 1e-8);

Executing the commands:

>> [x, fval, exitflag, output] = fzero(@(x) x-exp(-x), [0, 1], options);>> output

shows that 7 function evaluations are needed to compute the approximation of the root.

Example 6.7.8. Suppose we want to display results of each iteration when we compute an approx-imation of the root of f(x) = x# e#x. For this, we use optimset as follows:

>> options = optimiset(’Display’, ’iter’);

If we then use fzero as:

>> x = fzero(@(x) x-exp(-x), [0, 1], options);

then the following information is displayed in the Matlab command window:

Page 39: chap6

6.7. MATLAB NOTES 197

Func-count x f(x) Procedure2 1 0.632121 initial3 0.6127 0.0708139 interpolation4 0.56707 -0.000115417 interpolation5 0.567144 9.44811e-07 interpolation6 0.567143 1.25912e-11 interpolation7 0.567143 -1.11022e-16 interpolation8 0.567143 -1.11022e-16 interpolation

Zero found in the interval [0, 1]

Notice that a total of 8 function evaluations were needed, which matches (as it should) the numberfound in part (c) of Example 6.7.6.

Problem 6.7.13. Note that we can use optimset to reset TolX and to request display of the itera-tions in one command, such as:

options = optimset(’ToxX’, 1e-7, ’Display’, ’iter’);

Use this set of options with f(x) = x # e#x, and various initial guesses (e.g., 0, 1 and [0,1]) infzero and comment on the computed results.

Roots of polynomials

The Matlab function fzero cannot, in general, be e"ectively used to compute roots of polynomials.For example, it cannot compute the roots of f(x) = x2 because x = 0 is a root of even multiplicity.However, there is a special purpose method, called roots, that can be used to compute all roots ofa polynomial.

Recall from our discussion of polynomial interpolation (section 4.5) Matlab assumes polynomialsare written in the canonical form

p(x) = a1xn + a2xn#1 + · · · + anx + an+1,

and that they are represented with a vector (either row or column) containing the coe!cients:

a ='

a1 a2 · · · an an+1

(.

If such a vector is defined, then the roots or p(x) can be computed using the built-in function roots.Because it is beyond the scope of this book, we have not discussed the basic algorithm used by roots(which computes the eigenvalues of a companion matrix defined by the coe!cients of p(x)), but itis a very simple function to use. In particular, execution of the statement

r = roots(a);

produces a (column) vector r containing the roots of the polynomial defined by the coe!cients inthe vector a.

Example 6.7.9. Consider x4 # 5x2 + 4 = 0. The following Matlab commands:

>> a = [1 0 -5 0 4];>> r = roots(a)

compute a vector r containing the roots 2, 1, -2 and -1.

Page 40: chap6

198 CHAPTER 6. ROOT FINDING

The Matlab function poly can be used to create the coe!cients of a polynomial with givenroots. That is, if r is a vector containing the roots of a polynomial, then

a = poly(r);

will construct a vector containing the coe!cients of a polynomial whose roots are given by the entriesin the vector r. Basically, poly multiplies out the factored form

p(x) = (x# r1)(x# r2) · · · (x# rn)

to obtain the canonical power series form

p(x) = xn + a2xn#1 + · · · + anx + an+1.

Example 6.7.10. The roots of (2x + 1)(x# 3)(x + 7) = 0 are obviously # 12 , 3 and #7. Executing

the Matlab statements

>> r = [-1/2, 3, -7];>> a = poly(r)

constructs the vector a = [1, 4.5, -19, -10.5].

Problem 6.7.14. Consider the polynomial given in Example 6.6.5. Use the Matlab function polyto construct the coe"cients of the power series form of p12(x). Then compute the roots using theMatlab function roots. Use the format command so that computed results display 15 digits.

Problem 6.7.15. Consider the polynomial p20(x) given in Example 6.6.6. Compute the resultsshown in that example using the Matlab functions poly and roots. Use the format command sothat computed results display 15 digits.

Problem 6.7.16. Consider the polynomial p22(x) given in Example 6.6.7. Compute the resultsshown in that example using the Matlab functions poly and roots. Use the format command sothat computed results display 15 digits.