Civil Engineering Computation Numerical Integration I always wanted to be somebody, but I should have been more specific. Jane Wagner Contents Basic approach Trapezoid rule Truncation errors Simpson s 1/3 rule Simpson s 3/8 rule Richardson extrapolation Romberg integration Integration of data 2 Numerical Methods of Integration 1 The Problem Consider definite integration b ∫ f ( x)dx a Basic idea: integral represents area under the curve Approximate integral by approximating area Numerical Methods of Integration 3 Geometry of Integration Area 4 Numerical Methods of Integration 2 Rectangular Approximation If n rectangular areas are placed under the curve: Δx = (b-a) / n 5 Individual area defined by Ai = f(xi)* Δx Numerical Methods of Integration Trapezoid Rule 6 But rectangles miss curved area tops So add triangle to top of each rectangle Numerical Methods of Integration 3 Then Ai = 0.5*(f(xi+1) + f(xi))* Δx Fill area under curve with these areas and add For example if we wanted to find the area under the curve shown below bounded by the limits x=3 and x=6 y = 3.2x 2 + 2.5x + 3 7 Numerical Methods of Integration a b n Δx 3 6 4 0.75 x f(x) 3 39.3 3.75 57.375 4.5 79.05 5.25 104.325 6 133.2 y = 3.2x 2 + 2.5x + 3 8 Numerical Methods of Integration 4 Ai = 0.5*(f(xi+1) + f(xi))* Δx a b n Δx 3 6 4 0.75 x 3 3.75 4.5 5.25 6 f(x) 39.3 57.375 79.05 104.325 133.2 Ai 36.253125 51.159375 68.765625 89.071875 y = 3.2x 2 + 2.5x + 3 9 Numerical Methods of Integration Ai = 0.5*(f(xi+1) + f(xi))* Δx a b n Δx 3 6 4 0.75 x 3 3.75 f(x) 39.3 57.375 Ai 36.253125 51.159375 4.5 5.25 6 79.05 104.325 133.2 68.765625 89.071875 245.25 The area under the curve is the sum of the areas under all the segments (delta x). y = 3.2x 2 + 2.5x + 3 10 Numerical Methods of Integration 5 If we consider n rectangles, we can develop a general formula for using the trapezoid rule. f ( a ) + f ( a + !x ) !x 2 f ( a + !x ) + f ( a + 2!x ) A2 = !x 2 f ( a + 2!x ) + f ( a + 3!x ) A3 = !x 2 ! A1 = An = 11 f ( a + ( n "1) !x ) + f ( a + n!x ) 2 !x Numerical Methods of Integration And with some manipulation f ( a ) + f ( a + !x ) !x 2 f ( a + !x ) + f ( a + 2!x ) A2 = !x 2 f ( a + 2!x ) + f ( a + 3!x ) A3 = !x 2 ! A1 = An = 12 f ( a + ( n "1) !x ) + f ( b) 2 !x Numerical Methods of Integration 6 Summing up the incremental areas f ( a ) + f ( a + !x ) f ( a + !x ) + f ( a + 2!x ) !x + !x 2 2 f ( a + ( n "1) !x ) + f ( b) f ( a + 2!x ) + f ( a + 3!x ) + !x +! + !x 2 2 Area = 13 Numerical Methods of Integration Some of the terms appear twice so we can rewrite the expression Area = f ( a ) + 2 f ( a + !x ) + 2 f ( a + 2!x ) +!2 f ( a + ( n "1) !x ) + f ( b) 2 f ( a ) + f ( b) $ n"1 ' Area = !x & # f ( a + i!x )) 2 % i=1 ( 14 !x Numerical Methods of Integration 7 The summation expression is very easy to program using a loop f (a ) + 2 f (a + Δx ) + 2 f (a + 2Δx ) + 2 f (a + (n − 1)Δx ) + f (b ) Δx 2 a 3 f (a ) + f (b ) ⎡ n −1 ⎤ b 6 Area = Δx + ⎢∑ f (a + iΔx )⎥ Δx n 4 2 ⎣ i =1 ⎦ Δx 0.75 Area = x 3 3.75 4.5 5.25 6 f(x) 39.3 57.375 79.05 104.325 133.2 f(a)+f(b) /2 Sum 172.5 86.25 240.75 327 245.25 Ai 36.253125 51.159375 68.765625 89.071875 245.25 15 Numerical Methods of Integration <mes delta x Two forms of 2nd Derivative Estimate the 2nd derivative in 2 ways: 1. maximum value, in magnitude 2. average value: f"avg = [f'(b) - f'(a)] / (b-a) (This can be justified by applying the Mean Value Theorem to the second derivative or by integrating it over the interval) 16 Numerical Methods of Integration 8 Maximum Error f ( x ) = 3.2x 2 + 2.5x + 3 f ! ( x ) = 6.4x + 2.5 f !! ( x ) = 6.4 For 3 <= x <= 6 max f"(x) is the same across the interval f"(3) =6.4 If the second derivative was a function rather than a number, you would need to find the x value where the second derivative was a maximum. 17 Numerical Methods of Integration Maximum Error ( "x )2 * ( b ! a) * f ## ( x ) =! ETrun 12 where x is a value between a and b such that f ##(x) is a maximum ETrun ( 0.75)2 * ( 6 ! 3) * 6.4 = !0.9 =! f ( x ) = 3.2x 2 + 2.5x + 3 f ! ( x ) = 6.4x + 2.5 f !! ( x ) = 6.4 12 Minus sign means estimate greater than correct value Correct value is 244.35 and our estimate is 245.25 18 Numerical Methods of Integration 9 Average Error If we were not so lucky with the second derivative, we could use the average of the second derivative across the interval This would be the change in the first derivative across the interval divided by the length of the interval 19 Numerical Methods of Integration Average Error So the calculation for the average 2nd derivative would be f !! ( x )avg = 20 f ! ( b) " f ! ( a) b"a Numerical Methods of Integration 10 Average Error And then you can substitute this expression for the second derivative into the expression for the truncation error ETrun = ! f ## ( x )avg ETrun = ! ETrun = ! ETrun = ! 21 ( "x )2 * ( b ! a) * f ## ( x ) 12 f # ( b) ! f # ( a) = b!a f# b ! f# a ( "x )2 * ( b ! a) * ( ) ( ) b!a ( "x )2 12 12 ( f # ( b ) ! f # ( a )) ( 0.75)2 * ( 40.9 ! 21.7) = !2.4 12 Numerical Methods of Integration Average Error The value found for the this typically is less conservative than that found using the maximum second derivative over the interval ETrun = ! f ## ( x )avg ETrun = ! ETrun = ! ETrun = ! 22 ( "x )2 * ( b ! a) * f ## ( x ) 12 f # ( b) ! f # ( a) = b!a f# b ! f# a ( "x )2 * ( b ! a) * ( ) ( ) b!a ( "x )2 12 12 ( f # ( b ) ! f # ( a )) ( 0.75)2 * ( 40.9 ! 21.7) = !2.4 12 Numerical Methods of Integration 11 Trapezoid Rule Error Form Reasonable Trapezoid rule based on connecting adjacent points with straight line Resulting error formula supports this —exact for linear f(x) since f"(x) zero for linear f(x) Better result expected if a quadratic fit used for adjacent points 23 Numerical Methods of Integration Simpson s 1/3 Rule I = Δx*[f(a) + 4*f(a+Δx) +2*f(a+2*Δx) + 4*f(a+3*Δx) …+2*f(b-2*Δx) + 4*f(bΔx)+f(b)]/3 First and last function values multiplied by 1, interior function values multiplied by 4 and 2, alternating n must be even for Simpson s 1/3 rule 24 Numerical Methods of Integration 12 Simpson s 1/3 Rule Truncation Error ET = 53.28214 – 53.27472 = 0.00742 Estimate Simpson s 1/3 rule truncation error ETRUN = - Δx 4 * (b - a) * f IV (x) 180 Simpson s 1/3 rule was derived with quadratic fit between adjacent points on the curve But error formula is exact for a cubic integrand since fIV(x) would be 0 Thus have better result than expected 25 Numerical Methods of Integration Simpson s 3/8 Rule Use Simpson s 3/8 rule (and 1/3 rule) for an odd number of intervals Simpson s 3/8 rule is used when n = 3, Not any odd number, only 3 But any odd number 3 or greater can be broken into 3 + an even number (b − a) * [ f (a) + 3 * f (a + Δx) + 3 * f (a + 2Δx) + f (b)] 8 26 Numerical Methods of Integration 13 Richardson Extrapolation, a Clever Idea Truncation error can be reduced by increasing n But increasing n increases number of calculations and hence round-off error Eventually round-off error increase can overwhelm truncation error decrease Want method to improve accuracy without increasing n too much 27 Numerical Methods of Integration Let Δx1 = 2* Δx2 i.e. interval is halved Solving for C and substituting obtain: I = [4*I2-I1] / 3 a weighted average of two evaluations of integral using trapezoid rule I2 is more accurate since uses smaller Δx 28 Numerical Methods of Integration 14 Richardson Example With n = 4, Δx = 1 I = 54.95879 With n = 8, Δx= 0.5 I = 53.70091 Then I = [4*53.70091 – 54.95879]/3 = 53.28162 ET = 53.28214 – 53.28162 = 0.00052 a very good result! 29 Numerical Methods of Integration Romberg Integration 30 Can combine multiple Richardson evaluations k=1 k=2 n Δx trapezoid Richardson 1 4 80.47190 53.19066 2 2 60.01097 53.27473 4 1 54.95879 53.28160 8 0.5 53.70091 Numerical Methods of Integration 15 Richardson results combined further using: Ir,k = [16*Ir+1,k-1 – Ir,k-1] / 15 I1,3 = [16*53.27473 – 53.19066]/15 = 53.28034 Generalize the formula as Ir,k = [4k-1*Ir+1,k-1 – Ir,k-1]/(4k-1 -1) 31 Truncation error changes as Δ2k Numerical Methods of Integration 16