Chapter 2 Solutions of Equations in One Variable 2.1 The Bisection Method In this section, we consider one of the most basic problems of numerical approximation, the root-finding problem. This process involves finding a root, or solution, of an equation of the form f x 0 , for a given function f . A root of this equation is also called a zero of the function f . The first technique, based on the Intermediate Value Theorem, is called the Bisection, or Binary-search, method. Intermediate Value Theorem (IVT): If f C a, b and K is any number between f a and f b , then there exists a number c in a, b for which f c K . Suppose f is a continuous function defined on the interval a, b , with f a and f b of opposite sign. By the IVT, there exists a number p in a, b with f p 0 . Although the procedure will work when there is more than one root in the interval a, b , we assume for simplicity that the root in this interval is unique. The method calls for a repeated halving of subintervals of a, b and, at each step, locating the half containing p . To begin, set a1 a and b1 b , and let p1 be the midpoint of a, b ; that is p1 a1 b1 a1 a1 b1 . 2 2 If f p1 0 , then p p1 , and we are done. If f p1 0 , then f p1 has the same sign as either f a or f b . When f p1 and f a1 have the same sign, p p1 , b1 , and we set a2 p1 and b2 b1 . When f p1 and f a1 have opposite sign, p a1 , p1 , and we set a2 a1 and b2 p1 . We then reapply the 1 process to the interval a2 , b2 . This produces the method described in Algorithm 2.1. Algorithm 2.1 Bisection PURPOSE: To find a solution to F(x)=0 given the continuous function f on the interval [A,B], where F(A) and F(B) have opposite signs INPUT: endpoints: A,B, tolerance: TOL>0.0, maximum number of iterations N OUTPUT: approximation solution P or message that the algorithm fails Step 1 Set I=1 FA = F(A); FB=F(B) Step 2 While I N do Steps3-6 Step 3 Set P=A+(B-A)/2; FP=F(P). Step 4 IF FP=0 or (B-A)/2<TOL THEN OUTPUT (P) (Procedure completed successfully) STOP END IF Step 5 Set I=I+1 Step 6 IF FA FP 0 THEN Set A=P; FA=FP ELSE Set B=P; FB=FP END IF Step 7 OUTPUT (Method failed after N iteration) STOP We can select a tolerance (TOL>0) and generate p1 , p2 , , pi until one of the following conditions (stopping criteria) is met: (bi ai ) TOL , 2 ( pi pi 1 ) TOL , ( pi pi 1 ) pi TOL , 2 f pi TOL This produces the method (Algorithm 2.1) described in the following SUBROUTINE. SUBROUTINE BISECTION(N,A,B,TOL,P) C===================================================== C ALGORITHM 2.1 Bisection Method C PURPOSE C To find a solution to F(x)=0 given the continuous function f C on the interval [A,B], where F(A) and F(B) have opposite signs C INPUT C endpoints: A,B C tolerance: TOL>0.0 C maximum number of iterations N C OUTPUT C approximation solution P or message that the algorithm fails C------------------------------------------------------------------------------------------C INTEGER N REAL A,B,TOL,P REAL FA,FB,FP EXTERNAL F INTRINSIC ABS C FA = F(A) FB = F(B) IF (FA*FB.GT.0.0) THEN WRITE(*,*) 'FA and FB are the same signs' STOP END IF DO 10 I = 1,N P = A+(B-A)/2.0 FP = F(P) C *** Print the Bisection Algorithm gives the values *** 9 WRITE(10,9) I,A,B,P,FP FORMAT(1X,I5,F14.9,F14.9,F14.9,F14.9) IF ((ABS(FP).LE.1.0E-20).OR.((B-A)/2.0.LT.TOL)) THEN C *** Procedure completed successfully *** RETURN 3 END IF IF (FA*FP.GT.0.0) THEN A= P FA = FP ELSE B=P FB = FP END IF 10 CONTINUE WRITE(*,*) 'Method failed after N iterations' RETURN END Example 1 The equation f x x3 4 x2 10 has a root in [1,2] since f 1 5 and f 2 14 . C ******************************************************************* C Example 1 (Chapter 2.1) Using the Bisection Method to find a solution C accurate to within 0.0002 for f(x)=x^3+x^2-10 on [1,2] C C******************************************************************* * C REAL A,B,TOL INTEGER N A = 1.0 B = 2.0 TOL = 0.0002 N = 30 CALL BISECTION(N,A,B,TOL,P) WRITE(*,9) P 9 FORMAT(1X,'Approximation solution P:',F14.9) STOP END REAL FUNCTION F(X) C============================================================ C PURPOSE C Find the value of function f(x)=x^3+4x^2-10 C----------------------------------------------------------------------------------------------------C F = X*X*X+4.0*X*X-10 4 RETURN END The Bisection Algorithm gives the values in Table 2.1: Table 2.1 -----------------------------------------------------------------i bi ai f pi pi -----------------------------------------------------------------1 1.000000000 2.000000000 1.500000000 2.375000000 2 1.000000000 1.500000000 1.250000000 -1.796875000 3 1.250000000 1.500000000 1.375000000 0.162109375 4 1.250000000 1.375000000 1.312500000 -0.848388672 5 1.312500000 1.375000000 1.343750000 -0.350982666 6 1.343750000 1.375000000 1.359375000 -0.096408844 7 1.359375000 1.375000000 1.367187500 0.032355785 8 1.359375000 1.367187500 1.363281250 -0.032149971 9 1.363281250 1.367187500 1.365234375 0.000072025 10 1.363281250 1.365234375 1.364257812 -0.016046692 11 1.364257812 1.365234375 1.364746094 -0.007989263 12 1.364746094 1.365234375 1.364990234 -0.003959102 13 1.364990234 1.365234375 1.365112305 -0.001943659 -----------------------------------------------------------------Approximation solution P: 1.365112305 The Bisection method, though conceptually clear, has significant drawbacks. It is slow to converge (that is, N may become quite large before pi pi 1 is sufficiently small), and a good intermediate approximation can be inadvertently discarded. However, the method has the important property that it always converges to a solution, and for that reason it is often used as starter for the more efficient methods we will present later in this chapter. Theorem 2.1: Suppose that f C a, b and f a f b 0 . The Bisection method generates a sequence pi i 1 ba , 2i For each n 1, we have ba bn an n 1 2 pi p Proof approximating a zero p of f with when i 1 . and 5 p an , bn . Since pn 1 an bn for all n 1, it follows that 2 1 ba pn p bn an n . 2 2 Thus, the sequence 1 is pn p O n 2 pn n1 1 converges to p with rate of convergence O n ; that 2 . It is important to realize that Theorem 2.1 gives only a bound for approximation error and that this bound may be quite conservative. For example, this boundary applied to the problem in Example 1 ensures only that 2 1 p p9 9 2 103 , 2 but the actual error is much smaller: p p9 1.365230013 1.365234375 4.4 106 . Example 2 To determine the number of iterations necessary to solve f x x3 4 x2 10 =0 with accuracy 0.001 using a1 1 and b1 2 requires finding an integer i that satisfies ba pi p i 2 i 0.001 . 2 To determine i we will use logarithms. Although logarithms to any base would suffice, we will use base-10 logarithms since the tolerance is given as a power of 10. 2i 0.001 implies that log10 2i log10 103 3 , we have i log10 2 3 and i 3 9.96. log10 2 Hence, ten iterations will ensure an approximation accurate to within 0.001. It is important to keep in in mine that the error analysis gives only a bound for the number of iterations, and in many cases this bound is much larger than the actual number required. EXERCISE SET 2.1 Q4. Use the Bisection method to find solutions accurate to within 103 for f x x4 2 x3 4 x2 4 x 4 0 on each interval (a) [-2,-1] (b) [-1,0] (c) [0,2] (d) [2,3]. Q14. Let f x x 1 , 10 pn 1 1 n . Show that p 1 and 6 f pn 103 whenever n 1 but that p pn 103 requires that n 1000 . 7