Chapter 4 Numerical Differentiation and Integration 4.3 Elements of Numerical Integration The need often arises for evaluating the definite integral of a function that has no explicit antiderivative or whose antiderivative is not easy to obtain. The basic method b involved in approximating f x dx is called numerical quadrature. It uses a sum a n ai f xi to approximate i 0 b f x dx . a The methods of quadrature in this section are based on the interpolation polynomials given in Chapter 3. We first select a set of distinct nodes x0 , x1, , xn from the interval a, b . Then we integrate the Lagrange interpolating polynomial n Pn x f xi Li x i 0 and its truncation error term over a, b to obtain b b a a i 0 b n f ( n 1) x n f x dx f x L x dx x x n 1! i i i dx a i 0 b n 1 ai f xi x xi f ( n1) x dx n 1! a i 0 i 0 n where x is in a, b for each x and b ai Li x dx , for each i 0,1, ,n. a The quadrature formula is, therefore, b n f x dx ai f xi , i 0 a with error given by E f b n 1 x xi f ( n1) x dx . n 1! a i 0 Before discussing the general situation of quadrature formulas, let us consider formulas produced by using first and second Lagrange polynomials with equally spaced nodes. This gives the Trapezoidal rule and Simpson’s rule, which are commonly introduced in calculus courses. 1 b f x dx , To derive the Trapezoidal rule for approximating let x0 a, x1 b , a h b a and use the linear Lagrange polynomial: P1 x x x1 f x x x0 f x . x0 x1 0 x1 x0 1 Then, b a x1 x x1 x x0 f x dx 1 x1 f x x x x x dx f x dx f x0 0 1 x x1 2 x0 x1 x0 1 x0 0 Since x x0 x x1 does not change sign on x0 , x1 , the Weighted Mean Value Theorem for Integrals can be applied to the error term to give, for some in x0 , x1 , x1 x1 f x x x x x dx f x x x x dx 0 1 0 x0 1 x0 x3 x1 x0 h3 f x0 x1 x f . 3 2 6 x0 x1 Thus, we have b a x1 2 x x1 2 x x0 f x h3 f f x dx f x0 1 2 x1 x0 2 x0 x1 x 12 0 x1 x0 f 2 x0 f x1 h3 f . 12 Since h x1 x0 , we have the following rule: Trapezoidal Rule: b a f x dx h h3 f . f x0 f x1 2 12 This is called the Trapezoidral rule because when f is a function with positive b values, f x dx is approximated by the area in a trapezoid, as shown in Figure 4.3. a Since the error term for the Trapezoidral rule involves f , the rule gives the exact result when applied to any function whose second derivative is identically zero, that is, any polynomial of degree one or less. Simpson’s rule results from integrating over a, b the second Lagrange polynomial 2 with nodes x0 a, x2 b , and x1 a h , where h b a 2 . (See Figure 4.4). Therefore, b a x1 x x1 x x2 x x0 x x2 f x f x dx f x0 x x1 x0 x2 x1 x0 x1 x2 1 x0 0 x x x0 x x1 x x0 x x1 x x3 f (3) x dx. f x2 dx 6 x2 x0 x2 x1 x 3 0 Similarly, we have the following rule: Simpson’s Rule: b f x dx a h h5 (4) f . f x0 4 f x1 f x2 3 90 Since the error term involves the fourth derivative of f , Simpson’s rule gives exact results when applied to any polynomial of degree three or less. 4.4 Composite Numerical Integration Theorem 4.4 Let f C 4 a, b , n be even, h b a n , and x j a jh , for each j 0,1, , n. There exists a a, b for which the Composite Simpson’s rule for n subintervals can be written with its error term as b a n n 1 2 2 b a h 4 (4) h f x dx f a 2 f x2 j 4 f x2 j 1 f b f . 3 180 j 1 j 1 Algorithm 4.1 uses the Composite Simpson’s rule on n subintervals. This is the most frequently used general-purpose quadrature algorithm. Algorithm 4.1 Composite Simpson’s Rule b To approximate the integral I f x dx : a Input: endpoints a , b; even positive integer n . Output: approximation XI to I . Step 1 h b a n Step 2 XI 0 f a f b XI1 0 (*summation of f x2i 1 *) 3 (*summation of f x2i *) XI 2 0 Step 3 DO i 1, n 1 Step 4 Step 5 X a ih If i is even then XI 2 XI 2 f X Else XI1 XI1 f X End If Step 6 XI h( XI 0 2* XI 2 4* XI1) 3 Step 7 Output XI Stop The Algorithm 4.1 can be described in the following SUBROUTINE. SUBROUTINE SIMPSONS(N,A,B,F,XI) C================================================ C ALGORITHM 4.1 Composite Simpson's rule C PURPOSE C Using Composite Simpson's rule, C approximate I = INTEGRAL(f(x) dx)) from A TO B: C INPUT Endpoints A and B; even positive number N; function f C OUTPUT Approximation XI TO I C C-------------------------------------------------------------------------------C INTEGER N REAL A,B,XI, EXTERNAL F REAL H,X,XI0,XI1,XI2 INTEGER I H = (B-A)/N XI0 = F(A)+F(B) C *** Summation of f(x(2*i-1)) *** XI1 = 0.0 4 C *** Summation of f(x(2*i)) *** XI2 = 0.0 DO 10 I = 1,N-1 X = A+I*H IF (I.EQ.(2*(I/2))) THEN XI2 = XI2+F(X) ELSE XI1 = XI1+F(X) END IF CONTINUE XI = H*(XI0+2.0*XI2+4.0*XI1)/3.0 10 WRITE(*,29) A,B,XI FORMAT(1X,'Integral of F from',3X,F4.1,3X,'TO',3X,F10.6,3X,'is' 1 ,3X,F12.6) 29 RETURN END Example 1 Consider approximating sin xdx with an absolute error less than 0 0.00002, using the Composite Simpson’s rule. The Composite Simpson’s rule gives, for some in 0, , n n 1 2 2 h h4 sin x dx sin 0 2 sin x 4 sin x sin 2 j 2 j 1 180 sin . 0 3 j 1 j 1 Since the absolute error to be less than 0.00002, the inequality h4 180 sin h4 180 is used to determine n 5 180n4 and 0.0002 h . Completing these calculations gives n 18 n 17.075 . If n 18 , we use Algorithm 4.1 (see code C4p1.f) b sin x dx a and the formula gives 8 9 2 j 1 j sin 0 2 sin 4 sin 54 18 9 j 1 j 1 sin 2.000010 The exact answer is 2, so Composite Simpson’s rule with n 18 gave an answer well within the required error bound. 5 Code C4p1.f: C***************************************************************** C C Example 1 (pp201): Using Composite Simpson's rule C approximate I = INTEGRAL(sinx dx)) from 0 TO PI: C C Input: Even positive number N C C Outpou: Approximation XI TO I C**************************************************************** C INTEGER N REAL A,B,XI,PI EXTERNAL F OPEN(UNIT=10,FILE='c0.doc',STATUS='UNKNOWN') PI = 3.1415926 A = 0.0 B = PI N = 18 CALL SIMPSONS(N,A,B,F,XI) 29 WRITE(10,29) A,B,XI FORMAT(1X,'Integral of F from',3X,F4.1,3X,'TO',3X,F10.6,3X,'is' 1 ,3X,F12.6) STOP END C REAL FUNCTION F(X) C==================================== C PURPOSE C Find the value of function sin(x) C------------------------------------------------------------C REAL X INTRINSIC SIN F = SIN(X) RETURN END The extension of the Trapezoidal rule (see Figure 4.8) is given. Since the Trapezoidal rule requires only one interval for each application, the integer n can be either odd or even. 6 Theorem 4.5 Let j 0,1, f C 2 a, b , h b a n , and x j a jh , for each , n. There exists a a, b for which the Composite Trapezoidal rule for n subintervals can be written with its error term as b a n 1 b a h2 h f x dx f a 2 f x j f b f . 2 12 j 1 Algorithm 4.1T Composite Trapezoidal Rule b To approximate the integral I f x dx . a Input: endpoints a , b; positive integer N, function f x . Output: approximation XI to I . Step 1 h b a n Step 2 XI 0 f a f b XI1 0 Step 3 (*summation of f xi *) DO i 1, n 1 X a ih Step 4 XI1 XI1 f X Step 5 XI h * XI 0 XI1 / 2 Step 6 Output XI Stop SUBROUTINE TRAPEZOIDAL(N,A,B,F,XI) C================================================= C ALGORITHM 4.1 Composite Trapezoidal rule C PURPOSE C Using Composite Trapezoidal rule C approximate I = INTEGRAL(f(x) dx)) from A TO B. C INPUT Endpoints A and B; positive integer number N; function f C OUTPUT Approximation XI TO I C C----------------------------------------------------------------------------------C INTEGER N 7 REAL A,B,XI EXTERNAL F REAL H,XI0,XI1 INTEGER I H = (B-A)/N XI0 = F(A)+F(B) C *** Summation of f(x(i)) *** XI1 = 0.0 DO 10 I = 1,N-1 X = A+I*H XI1 = XI1+F(X) CONTINUE XI = H*(XI0+2.0*XI1)/2.0 10 WRITE(*,29) A,B,XI FORMAT(1X,'Integral of F from',3X,F4.1,3X,'TO',3X,F10.6,3X,'is' 1 ,3X,F12.6) 29 RETURN END C Example 2 Consider approximating sin xdx with an absolute error less than 0 0.00002, using the Composite Trapezoidal rule. The Composite Trapezoidal rule gives, for some in sin x dx 0 0, , n 1 h2 h sin 0 2 sin x sin sin . j 2 j 1 12 Since the absolute error to be less than 0.00002, the inequality h2 12 sin is used to determine n h2 12 and 3 12n2 0.00002 h . Completing these calculations gives n 360 n 359.434 . If n 360 , we use Algorithm 4.1T (see code C4p1T.f) sin x dx 0 and the formula gives 359 j sin 0 2 sin sin 1.999987 . 720 360 j 1 8 If n 18 , we use Algorithm 4.1T (see code C4p1T.f) sin x dx 0 and the formula gives 17 j sin 0 2 sin sin 1.994920 . 36 18 j 1 From the above two equations, it can be seen that the Composite Trapezoidal rule with n 360 gave an answer well within the required error bound. However, the Composite Trapezoidal rule with n 18 clearly did not. Homework 6 Exercise set 4.4 2 Q7. Determine the values of n and h required to approximate e 2x sin 3 x dx to 0 within 104 and find the approximation. (a) Use the Composite Trapezoidal rule (using Algorithm 4.1T and C4p1T.f). (b) Use the Composite Simpson’s rule (using Algorithm 4.1 and C4p1.f). 9