Numerical Integration It is sometimes difficult or impossible to find the exact value of a definite integral. For example, it is impossible to evaluate the following integrals exactly: Z 1 Z 1p x2 e dx 1 + x3 dx −1 0 As another example, if a function is determined from collected data, there may be no formula for the function and hence no way to compute a definite integral of the function exactly. When we can’t compute a definite integral exactly, we need to approximate it. The definite integral of f on [a, b] is a limit of Riemann sums of f on [a, b]: Z b n X f (x)dx = lim f (xi )∆x, n→∞ a where ∆x = b−a n , i=1 xi = a + i∆x, and xi is in [xi−1 , xi ]. Any Riemann sum for f on [a, b] is an approximation to the definite integral of f on [a, b]: Z b n X f (x)dx ≈ f (xi )∆x a i=1 For a midpoint Riemann sum, xi = 12 (xi−1 + xi ) = midpoint of [xi−1 , xi ] for i = 1, . . . , n. The approximation obtained using a midpoint Riemann sum is called the Midpoint Rule approximation to Rb a f (x)dx. Midpoint Rule Z b n X xi−1 + xi f (x)dx ≈ M (n) = f ∆x 2 a i=1 x0 + x1 x1 + x2 xn−1 + xn = ∆x f +f + ··· + f 2 2 2 where ∆x = b−a n and xi = a + i∆x for i = 0, . . . , n. 1 2 Rb A Riemann sum approximation to a f (x)dx works by approximating the region between y = f (x), the x-axis, x = a, and x = b by rectangles. Rb We obtain a different approximation to a f (x)dx if we use trapezoids instead of rectangles: Rb The area of the ith trapezoid is 21 (f (xi−1 ) + f (xi ))∆x, so the trapezoid approximation to a f (x)dx is Z b n X f (xi−1 ) + f (xi ) f (x)dx ≈ T (n) = ∆x 2 a i=1 f (x0 ) + f (x1 ) f (x1 ) + f (x2 ) f (xn−1 ) + f (xn ) = ∆x + + ··· + 2 2 2 f (xn−1 ) f (xn−1 ) f (xn−1 ) f (x0 ) f (x1 ) f (x1 ) + + + ··· + + + = ∆x 2 2 2 2 2 2 f (x0 ) f (xn ) = ∆x + f (x1 ) + · · · + f (xn−1 ) + 2 2 Trapezoid Rule Z b f (x)dx ≈ T (n) = a n X f (xi−1 ) + f (xi ) 2 i=1 ∆x 1 1 = ∆x f (x0 ) + f (x1 ) + · · · + f (xn−1 ) + f (xn ) 2 2 where ∆x = b−a n and xi = a + i∆x for i = 0, . . . , n 3 When approximating the region between y = f (x), the x-axis, x = a, and x = b by trapezoids, we used the line segment between f (xi−1 ) and f (xi ) to form the ith trapezoid. In other words, we approximated the graph of f by line segments. We obtain another approximation if we use parabolic arcs instead of line segments to approximate the graph of f : Note that we take an even number of subintervals and approximate y = f (x) by a parabola on each consecutive pair of intervals. Simpson’s Rule Z b ∆x f (x)dx ≈ S(n) = [f (x0 ) + 4f (x1 ) + 2f (x2 ) + 4f (x3 ) + · · · + 2f (xn−2 ) + 4f (xn−1 ) + f (xn )] 3 a where n is even, ∆x = b−a n , and xi = a + i∆x for i = 0, . . . , n. Remark • In a Riemann sum approximation, we are approximating f by a constant function (degree 0 polynomial) on each interval [xi−1 , xi ]. • In the trapezoidal approximation, we are approximating f by a linear function (degree 1 polynomial) on each interval [xi−1 , xi ]. • In Simpson’s approximation, we are approximating f by a quadratic function (degree 2 polynomial) on each interval [xi−1 , xi ]. 4 Z Example Use the trapezoid rule with n = 3 subintervals to approximate 0 3 1 dx. 1 + x3 b−a 3−0 ∆x = = =1 n 3 xi = a + i∆x = 0 + i · 1 = i x0 = 0 x1 = 1 x2 = 2 x3 = 3 1 f (x) = 1 + x3 Therefore Z 3 1 1 1 dx ≈ T (3) = ∆x f (x0 ) + f (x1 ) + f (x2 ) + f (x3 ) 3 2 2 0 1+x 1 1 1 1 1 1 · + + + · =1· 2 1 + 03 1 + 13 1 + 23 2 1 + 33 1 1 1 1 = + + + 2 2 9 56 569 = 504 = 1.1289 . . . (you can stop here on the exams) 5 An approximation is not very useful without some information about its accuracy. Example 3, 3.14, 3.14159, and 10 are all approximations to π. Some of these approximations are more accurate than others. If c is an approximate numerical solution to a problem having an exact solution x, then absolute error = |x − c| relative error = |x − c| |x| (if x 6= 0) When the value of the exact solution x is not be known, an upper bound on the absolute or relative error is very valuable. Example If we know that c = 1.3191 is an approximate solution to the equation ex = x2 + 2 and the absolute error of this approximation satisfies |x − c| ≤ 0.0002, then the exact solution x satisfies c − 0.0002 ≤ x ≤ c + 0.0002 1.3189 ≤ x ≤ 1.3193 Midpoint Rule Error Bound If we can find a number K such that |f 00 (x)| ≤ K for all x in [a, b], then the absoulte error in approxiRb mating a f (x)dx using the Midpoint Rule with n subintervals satisfies Z b K(b − a)3 EM (n) = f (x)dx − M (n) ≤ 24n2 a Trapezoid Rule Error Bound If we can find a number K such that |f 00 (x)| ≤ K for all x in [a, b], then the absoulte error in approxiRb mating a f (x)dx using the Trapezoid Rule with n subintervals satisfies Z b K(b − a)3 f (x)dx − T (n) ≤ ET (n) = 12n2 a Simpson’s Rule Error Bound If we can find a number K such that |f (4) (x)| ≤ K for all x in [a, b], then the absoulte error in Rb approximating a f (x)dx using Simpson’s rule with n subintervals satisfies Z b K(b − a)5 ES(n) = f (x)dx − S(n) ≤ 180n4 a 6 Example How large should we take n in order to guarantee that the Simpson’s rule approximation to Z 2 1 dx is accurate to 0.000000000002 = 2 · 10−12 ? 3 1 x A B C D E 10 100 1000 10000 100000 We want ES(n) Z = 2 1 1 ≤ 2 · 10−12 . dx − S(n) x3 Note that f (x) = x−3 f 0 (x) = −3x−4 f 00 (x) = 12x−5 f (3) (x) = −60x−6 f (4) (x) = 360x−7 Since 0 < 1 ≤ 1 on [1, 2], we have x 360 360 (4) 360 360 = 7 ≤ 7 = 360 on [1, 2]. f (x) = 7 = 7 x |x| x 1 Therefore ES(n) ≤ K(b − a)5 360(2 − 1)5 2 = = 4. 180n4 180n4 n So it suffices to have 2 ≤ 2 · 10−12 n4 n4 ≥ 1012 n ≥ 103 = 1000 Remark Z 1 2 1 3 dx = = 0.375 3 1+x 8 S(10) = 0.3750312659228 . . . (Time (seconds): 0.000 . . .) S(100) = 0.3750000032795 . . . (Time (seconds): 0.031 . . .) S(1000) = 0.3750000000003 . . . (Time (seconds): 0.049 . . .) S(10000) = 0.37500000000000003 . . . S(100000) = 0.375000000000000000003 . . . (Time (seconds): 1.435 . . .) (Time (seconds): 135.674 . . .)