MthSc 365: Numerical methods for engineers Lecture 17 Pietro Belotti Dept. of Mathematical Sciences Clemson University December 4, 2012 Numerical integration Rectangle 1: compute f on lower bound of [xk , xk+1 ], i.e., xk . Z b N X f (x)dx ≈ f (xk )(xk+1 − xk ). a k=1 Rectangle 2: compute f on upper bound of [xk , xk+1 ], i.e., xk+1 . Z b N X f (xk+1 )(xk+1 − xk ). f (x)dx ≈ a k=1 Rectangle 3: compute f on mid bound of [xk , xk+1 ], i.e., Z b N X xk+1 + xk f f (x)dx ≈ (xk+1 − xk ). 2 a k=1 Trapezoidal method: Z b N X f (xk+1 ) + f (xk ) (xk+1 − xk ). f (x)dx ≈ 2 a k=1 xk+1 +xk . 2 Gauß quadrature Let’s generalize the above methods. Rb In general, a f (x)dx is approximated as follows: 1. Define a set of points x1 , x2 . . . , N 2. Compute appropriate coefficients Ck , for k = 1, 2, . . . , N 3. Compute the integral as Z b f (x)dx = a N X Ck f (xk ) k=1 Numerical integration Z Rectangle 1: b a f (x)dx ≈ PN k=1 f (xk )(xk+1 N X Ck f (xk ) k=1 − xk ) means Ck = xk+1 − xk = h Rectangle 2 and 3: Same as R1 (different choice of xk ): Ck = h. Trapezoidal method: ◮ C1 = CN = 2h . ◮ Ck = h for k = 2, 3, . . . , N − 1. Simpson’s 1/3 method: h 3 ◮ C1 = CN = ◮ For k even, Ck = 43 h ◮ For k odd and 1 6= k 6= N, Ck = 32 h Gauß quadrature Now the question is: “Are there other ways to select Ck ’s?” Problem: Find x1 , x2 , . . . , xN and C1 , C2 , . . . , CN to approximate Z b a f (x)dx ≈ N X Ck f (xk ) k=1 This clearly depends on f , but we can’t adapt this for every f . ◮ Idea: f can be approximated by a Taylor series expansion 1 f (x) = f (x0 ) + f ′ (x0 )(x − x0 ) + f ′′ (x0 )(x − x0 )2 + . . . 2 ⇒ Find the appropriate xk , Ck for 1, x, x2 , x3 . . . Gauß quadrature Solve a slightly simpler problem: Z 1 f (x)dx −1 And set N = 2. Then find Z 1 f (x)dx = C1 f (x1 ) + C2 f (x2 ), −1 at least for the following four functions: f (x) = 1 f (x) = x2 f (x) = x f (x) = x3 Gauß quadrature R1 f (x) dx = C1 f (x1 ) + C2 f (x2 ) −1 R1 1 R−1 1 x −1 R1 x2 −1 R1 3 x −1 ⇐⇒ = = = = C1 C1 C1 C1 1 x1 x1 2 x1 3 2 = C1 0 = C1 2 3 = C1 0 = C1 1 x1 x1 2 x1 3 dx dx dx dx + + + + + + + + C2 C2 C2 C2 C2 C2 C2 C2 1 x2 x2 2 x2 3 1 x2 x2 2 x2 3 A system of 4 nonlinear equations in 4 variables C1 , C2 , x1 , x2 . Gauß quadrature We need four functions because there are four variables. Its solution is C1 = C2 = 1, x1 = − √13 , x2 = √1 3 Hence from now on we can approximate Z 1 −1 1 f (x)dx = 1 · f − √3 + 1 · f √13 for any function f . (Results may vary.) Gauß quadrature For N = 3, then we have six variables ⇒ six functions: f (x) = 1 f (x) = x3 R1 R−1 1 −1 R1 R−1 1 R−1 1 R−1 1 −1 1 x x2 x3 x4 x5 dx dx dx dx dx dx f (x) = x2 f (x) = x5 f (x) = x f (x) = x4 = = = = = = C1 C1 C1 C1 C1 C1 + + + + + + 1 x1 x1 2 x1 3 x1 4 x1 5 C2 C2 C2 C2 C2 C2 1 x2 x2 2 x2 3 x2 4 x2 5 + + + + + + C3 C3 C3 C3 C3 C3 1 x3 x3 2 x3 3 x3 4 x3 5 Gauß quadrature 2 = C1 0 = C1 2 3 = C1 0 = C1 2 5 = C1 0 = C1 Solution: ◮ ◮ + + + + + + 1 x1 x1 2 x1 3 x1 4 x1 5 5 8 5 9, 9, 9 C2 C2 C2 C2 C2 C2 1 x2 x2 2 x2 3 x2 4 x2 5 + + + + + + C3 C3 C3 C3 C3 C3 1 x3 x3 2 x3 3 x3 4 x3 5 (C1 , C2 , C3 ) = q q (x1 , x2 , x3 ) = − 35 , 0, 35 We can approximate q Z 1 f (x)dx = 59 · f − 35 + −1 8 9 · f (0) + 5 9 ·f q 3 5 Gauß quadrature for the general case: [a, b] Easy! Just create a variable t such that ◮ ◮ x = a ⇔ t = −1 x = b ⇔ t = +1 ⇒ x = a + 12 (b − a)(t + 1) ◮ b−a 2 dt Note: dx = From Calculus we know that Z 1 Z b b−a 1 dt f a + (b − a)(t + 1) f (x)dx = 2 2 −1 a So we simply create g(t) = f a + 12 (b − a)(t + 1) Z b f (x)dx = a Z 1 g(t)dt = −1 5 9 q · g − 35 + 8 9 b−a 2 · g (0) + 5 9 and . . . ·g q 3 5 Errors in numerical integration Consider the first technique we’ve seen to estimate an integral: Z b a f (x)dx ≈ N X k=1 f (xk )(xk+1 − xk ). Let’s restrict to the k-th interval: Z xk+1 f (x)dx ≈ f (xk )(xk+1 − xk ) = f (xk )(xk+1 − xk ). xk The error of this estimate is Z xk+1 f (x)dx = xk where ξk ∈ [xi , xk+1 ] f (xk )h + 1 ′ f (ξk )h2 2 ξi who? So 1 2 f ′ (ξi )h2 is the error for some ξi ∈ [xi , xi+1 ]. We don’t care what ξi is. The total error is E= N X 1 k=1 2 ′ 2 f (ξi )h Note: The average f ′ on [a, b] is f ′ = N h2 X ′ = f (ξi ) 2 k=1 1 N 1 E = h2 Nf ′ 2 Now h = b−a N , so E = 1 b−a 2 N · Nhf ′ = PN k=1 f b−a ′ 2 hf ′ (ξ i ). Hence, ∈ O(h) Errors in numerical integration For the rectangular method that uses f (xi+1 ), the error is similar. However, for the mid-point this changes substantially: E= b − a ′′ 2 f h ∈ O(h2 ), 24 much better (h2 tends to zero much faster for h ↓ 0). Similarly for the trapezoidal method: E= b − a ′′ 2 f h ∈ O(h2 ). 12 Simpson’s 1/3 and 3/8 methods behave better: E1/3 = E3/8 = b−a IV 4 180 f h b−a IV 4 80 f h ∈ O(h4 ) ∈ O(h4 ) Simpson’s 3/8 is better than 1/3: h3/8 = b−a 3N and h1/3 = b−a 2N .