s Februry 2, 216 1 Exercise 5.2. Apply composite Simpson s rule with m = 1, 2, 4 pnels to pproximte the integrls: () x 2 dx = 1 π/2, (b) cos(x) dx = 1, (c) e x dx = e 1, nd report the errors. () f(x) = x 2. For m = 1 we put h = (1 )/2 = 1/2, x =, x 1 = 1/2, x 2 = 1; y = f(x ) =, y 1 = f(x 1 ) = 1/4, y 2 = f(x 2 ) = 1. The pproximtion is then I h/(y + 4y 1 + y 2 ) = ( + 4 1/4 + 1)/6 = 1/. The error is thus. For m = 2 we hve h = (1 )/4 = 1/4, (x, x 1,..., x 4 ) = (, 1/4, 1/2, /4, 1), (y, y 1,..., y 4 ) = (, 1/16, 1/4, 9/16, 1). The pproximtion is then I h/(y + 4y 1 + 2y 2 + 4y + y 4 ) = ( + 4 1/16 + 2 1/4 + 4 9/16 + 1)/12 = (1/4 + 2/4 + 9/4 + 4/4)/12 = 16/( 4 4) = 1/. The error is thus. One could continue with m =, but the error hs to be zero. Indeed, Simpson s rule is bsed on qudrtic interpoltion polynomils, which mens tht x 2 will be represented exctly by the interpolting polynomil nd the qudrture will be exct. (b) f(x) = cos(x) For m = 1 we put h = (π/2 )/2 = π/4, x =, x 1 = π/4, x 2 = π/2; y = f(x ) = 1, y 1 = f(x 1 ) = 1/ 2, y 2 = f(x 2 ) =. The pproximtion is then I h/(y + 4y 1 + y 2 ) = (1 + 4/ 2 + ) π/12 1.227. The error is thus.227. For m = 2, h = π/8, (x,..., x 4 ) = (, π/8, π/4, π/8, π/2), (y,..., y 4 ) (1,.928795, 1/ 2,.82684, ). I h/(y + 4y 1 + 2y 2 + 4y + y 4 ) 1.15, giving the error.15. For m =, h = π/6, I 1.261, errror.261. (c) f(x) = exp(x) For m = 1 we put h = (1 )/2 = 1/2, x =, x 1 = 1/2, x 2 = 1; y = f(x ) = 1, y 1 = f(x 1 ) = e 1/2, y 2 = f(x 2 ) = e. The pproximtion is then I h/(y + 4y 1 + y 2 ) = (1 + 4 e + e)/6 1.71886115. The error is thus 1.71886115 e + 1 5.79 1 4. For m = 2, h = 1/4, (x,..., x 4 ) = (, 1/4, 1/2, /4, 1),... I h/(y + 4y 1 + 2y 2 + 4y + y 4 ) 1.7181884192175, giving the error.7 1 5. For m =, h = 1/6, I 1.718289169928, errror 7.4 1 6. 2 Exercise 5.2.11 Find the degree of precision of the following pproximtion for f(x) dx: First of ll, let us evlute the integrl of monomil x n, n : [ ] { x x n n+1 x=1, for odd n, dx = = n + 1 2/(n + 1), for even n. x= 1
() f(1) + f(): Let us pply the qudrture to the monomil x n, n : { 1 n + () n, for odd n, = 2, for even n, Therefore the qudrture grees with the integrl for n =, 1 (nd ll odd n), nd its degree of precision is 1. (One could lso observe tht this is trpezoid qudrture on [, 1], which is known to be exct for polynomils up to degree 1.) (b) 2/[f(-1) + f() + f(1)]. We check the qudrture on monomils of incresing degree: 2, for n =, 2/[() n + n + () n ] =, for odd n, 4/, for even n >, which grees with the integrl for n =, 1 (nd odd n). Thus degree of precision is 1. (c) We check the qudrture on monomils of incresing degree: [(/ ) n + (1/ { ) n, for odd n, ] = 2/ n/2, for even n, which grees with the integrl for n =, 1, 2, (nd odd n). Thus degree of precision is. (This is n exmple of Gussin qudrture, which re exct for polynomils of degree up to 2n 1 when n points re used.) Exercise 5.2.16 Use the fct tht the error term of Boole s Rule (see Exercise 5.2.15) is proportionl to f (6) (c) to find the exct error term. We pply the qudrture to the monomil x 6, for which f (6) (c) 6!, for ll c. Thus 4h [ x x 6 7 dx = 7 ] x=4h x= = 47 h 7 7 nd we re interested in finding C. = 2h 45 (7 6 + 2 h 6 + 12 (2h) 6 + 2 (h) 6 + 7 (4h) 6 ) + C6! In [1]: from sympy import * init_printing() h,c=symbols( h C ) integrl = (4*h)**7/7 qudrture = 2*h/45*(2*h**6 + 12*(2*h)**6 + 2*(*h)**6 + 7*(4*h)**6) simplify(integrl-qudrture) Out[1]: Thus C = 28h 7 /(21 6!) = 8h 7 /945 128h7 21 2
4 Exercise 5.4.1 Apply Adptive Qudrture by hnd, using the Trpezoid Rule with tolernce T OL =.5 to pproximte the integrls. Find the pproximtion error. () In this cse, f(x) = x 2, =, b = 1. We begin with n = 1 intervl, =, b = b. We will use S[, b] to denote the Trpezoid qudrture pplied on the intervl [, b]. c = (+b)/2, S[, b] = ( 2 +1 2 )/2 = 1/2, S[, c] = ( 2 +(1/2) 2 )/4 = 1/16, S[c, b] = ((1/2) 2 +1 2 )/4 = 5/16. S[, b] S[, c] S[c, b] = 1/8 =.125 <.5 ((b )/(b )) =.15. Thus we stop with the pproximtion S[, c] + S[c, b] = /8 =.75 nd n error estimte (S[, b] S[, c] S[c, b])/ = /24.417. The ctul integrtion error is x2 /8 = 1/ /8 = /24. Our estimte is exct becuse f is constnt in this cse, thus the pproximte eqution (5.8) in the book is ctully exct. (b) f(x) = cos(x), =, b = π/2. S[, π/2] = (π/2) (1 + )/2 = π/2 1.578, S[, π/4] = (π/4) (1 + 1/ 2)/2.7854, S[π/4, π, 2] = (π/4) (1/ 2 + )/2.27768. S[, b] S[, c] S[c, b].5772.5 (π/2)/(π/2) =.15. Thus we need to split the intervl nd pply the dptive lgorithm recursively. S[, π/4] = (π/4) (1 + 1/ 2)/2.7854, S[, π/8] = (π/8) (1 + cos(π/8))/2.7775, S[π/8, π/4] = (π/8) (cos(π/8) + cos(π/4))/2.224, S[, π/4] S[, π/8] S[π/8, π/4].8741.5 (π/4)/(π/2) =.75. Thus we need to split this intervl gin pply the dptive lgorithm recursively. S[, π/8] = (π/8) (1 + cos(π/8))/2.7775, S[, π/16] = (π/16) (1 + cos(π/16))/2.19446, S[π/16, π/8] = (π/16) (cos(π/16) +cos(π/8))/2.18699, S[, π/8] S[, π/16] S[π/16, π/8].7 <.5 (π/8)/(π/2) =.75. Thus we re done on the intervl [, π/8] with the pproximtion S[, π/16] + S[π/16, π/8].8145. Let us look t the intervl [π/8, π/4] now. S[π/8, π/4] = (π/8) (cos(π/8) + cos(pi/4))/2.224, S[π/8, π/16] = (π/16) (cos(π/8) + cos(π/16))/2.172, S[π/16, π/4] = (π/16) (cos(π/16) + cos(π/4))/2.1515, S[π/8, π/4] S[π/8, π/16] S[π/16, π/4].14 <.5 (π/8)/(π/2) =.75. Thus we re done on the intervl [π/8, π/4] with the pproximtion S[π/8, π/16] + S[π/16, π/4].28. Let us look t the intervl [π/4, π/2] now. S[π/4, π/2] = (π/4) (cos(π/4) + cos(pi/2))/2.27768, S[π/4, π/8] = (π/8) (cos(π/4)+cos(π/8))/2.2198, S[π/8, π/2] = (π/8) (cos(π/8)+cos(π/2))/2.7514, S[π/4, π/2] S[π/4, π/8] S[π/8, π/2].1144 <.5 (π/4)/(π/2) =.75. Thus we re done on the intervl [π/4, π/2] with the pproximtion S[π/4, π/8]+s[π/8, π/2].28912. There re no intervls left, nd therefore the finl pproximtion is.8145 +.28 +.28912 =.9995 wheres the exct integrl is π/2 cos(x) = 1. Thus the error is.9995 1.6 <.5. (c) In this cse, f(x) = exp(x), =, b = 1. S[, 1] = (1 + e)/2 1.85914, S[,.5] = (1 + exp(.5))/4.66218, S[.5, 1] = (exp(.5) + exp(1))/4 1.9175. S[, b] S[, c] S[c, b].152 <.5 ((b )/(b )) =.15. Thus we stop with the pproximtion S[, c] + S[c, b] 1.759 nd n error estimte (S[, b] S[, c] S[c, b])/.5. The ctul integrtion error is exp(x) (S[, c] + S[c, b]).6. Our estimte is quite good in this cse. 5 Exercise 5.4.4 Develop n Adptive Qudrture method for rule (5.28). Let c = ( + b)/2, h = b nd pply the qudrture on [, b], [, c], nd [c, b] (we use the sme nottion s in the book):
c f(x) dx + c f(x) dx = S[, b] + 14h5 45 f (4) (c 1 ), c 1 [, b], f(x) dx = S[, c] + S[c, b] + 14(h/2)5 45 f (4) (c 2 ) + 14(h/2)5 f (4) (c ), 45 c 2 [, c], c [c, b]. Further ssuming tht f (4) (c 1 ) f (4) (c 2 ) f (4) (c ) we obtin: c f(x) dx + c f(x) dx = S[, b] + 14h5 45 f (4) (c 1 ), f(x) dx S[, c] + S[c, b] + 1 14h 5 16 45 f (4) (c 1 ). We cn now subtrct the second eqution from the first to obtin: S[, b] S[, c] S[c, b] 15 1 14h 5 16 45 f (4) (c 1 ). Thus the number S[, b] S[, c] S[c, b] gives n pproximtion to 15 times the error of the qudrture S[, c] + S[c, b]. The rest is exctly the sme s for other qudrtures; one stops subdividing the intervl when S[, b] S[, c] S[c, b] < 15 T OL (b )/(b orig orig ) nd returns S[, c] + S[c, b] s the pproximtion of the integrl over the intervl [,b]. 6 Exercise 5.5.1 Approximte the integrls using n = 2 Gussin Qudrture. Compre with the correct vlue nd give the pproximtion error. Gussin Qudrture with n = 2 is defined by the points x 1,2 = ±1/, w 1,2 = 1. In [2]: x1 = -1/sqrt() x2 = 1/sqrt() w1 = 1 w2 = 1 () In []: from sympy import * init_printing() x=symbols( x ) f=lmbd(x,x**+2*x) Out[]: integrte(f(x),(x,-1,1)) In [4]: # Numericl qudrture 4
Out[4]: Thus the error is, which grees with the theory for Gussin Qudrtures being exct for polynomils up to degree 2n 1. (b) In [5]: f=lmbd(x,x**4) Out[5]: integrte(f(x),(x,-1,1)) In [6]: # Numericl qudrture 2 5 Out[6]: In [7]: # Approximtion error 2/5-2/9 2 9 Out[7]: (c).1777777777777778 In [8]: f=lmbd(x,exp(x)) Out[8]: integrte(f(x),(x,-1,1)) 1 e + e In [9]: # Numericl qudrture Out[9]: e + e In [1]: # Approximtion error N(integrte(f(x),(x,-1,1))-()) Out[1]: (d).776299778724 5
In [11]: f=lmbd(x,cos(pi*x)) Out[11]: integrte(f(x),(x,-1,1)) In [12]: # Numericl qudrture Out[12]: 2 cos ( ) π In [1]: # Approximtion error N(-2*cos(sqrt()*pi/)) Out[1]:.48127298818 7 Exercise 5.5.7 Show tht the Legendre polynomils p 1 (x) = x nd p 2 (x) = x 2 1/ re orthogonl on [, 1]. p 1(x)p 2 (x) dx = (x x/) dx = becuse x x/ is n odd function. 8 Exercise 5.5.8 Compute Legendre polynomils up to degree. Legendre polynomils re given by the expression Thus p i (x) = 1 d 2 i i! dx i [(x2 1) i ]. p (x) = 1, p 1 (x) = 1/2[(x 2 1)] = x, p 2 (x) = 1/(4 2)[(x 2 1) 2 ] = 1/8[x 4 2x 2 + 1] = 1/8[12x 2 4] = /2(x 2 ), p (x) = 1/(8 6)[(x 2 1) ] = 1/48[x 6 x 4 + x 2 1] = 1/48[12x 72x] = 5/2[x /5x]. 9 Exercise 5.5.9 Verify the coefficients c i nd x i in Tble 5.1 for degree n =. We hve computed p (x) = 5/2x(x 2 /5) in Exercise 5.5.8. This immeditly implies tht p () = p (± /5) = confirming the roots listed in Tble 5.1. The coefficients c i re obtined by integrting the Lgrnge polynomils. For exmple, 6
nd therefore Similrly nd therefore L 2 (x) = (x x 1)(x x ) (x 2 x 1 )(x 2 x ) = (x + /5)(x /5) ( + /5)( /5) = 1 5 x2, c 2 = [1 5/x 2 ] dx = [x 5/9x ] x=1 x= = 2 1/9 = 8/9. (x )(x /5) L 1, (x) = ( /5 )( /5 /5) = x(x /5), 2 /5 c 1, = 5 6 1 Computer exercise 5.2.8 Answers: () 1.547866 (b) 1.277978 (c) 1.277978 11 Computer exercise 5.2.1 [x 2 x /5] dx = 5 6 [x / /5x 2 /2] x=1 x= = 5 2 6 = 5 9. See the file uniform refinement.m vilble on the wiki. Adpt the code to use Simpson s rule insted of Trpezoid. 7