45 We ave a abit in writing articles publised in scientiþc journals to make te work as Þnised as possible, to cover up all te tracks, to not worry about te blind alleys or describe ow you ad te wrong idea Þrst, and so on. So tere isn t any place to publis, in a digniþed manner, wat you actually did in order to get to do te work. Ricard Pilips Feynman (1918-1988) Capter 7 Numerical Differentiation and Integration Agivensetof(n+1) data points (x i,y i ), i =0, 1,,...,nis assumed to represent some function y = y(x). Te data can come from some experiment or statistical study, were y = y(x) is unknown, or te data can be generated from a known function y = y(x). We assume te data points are equally spaced along te x-axis so tat x i+1 x i = is a constant for i =0, 1,,...,n 1. In tis capter we develop ways to approximate te derivatives of y = y(x) given only te data points. We also develop ways to integrate te function y = y(x) based solely upon te data points given. Numerical Approximation for Derivative To approximate te derivative function y! (x), evaluated at one of te given data points (x i,y i ),sayatx = x m, x 0 <x m <x n, we assume tat te function y(x) as a Taylor series expansion about te point x m given by eiter of te forms or y(x m + ) =y(x m )+y! (x m ) + y!! (x m )! + y!!! (x n ) 3 3! + (7.1) y(x m ) =y(x m ) y! (x m ) + y!! (x m )! y!!! (x m ) 3 + (7.) 3! By solving te equation (7.1) for te Þrst derivative one obtains te forward derivative approximation y! (x m )= y(x m + ) y(x m ) + O(). (7.3) Solving te equation (7.) for te Þrst derivative gives te backward derivative approximation y! (x m )= y(x m) y(x m ) + O(). (7.4)
46 Subtracting te equation (7.) from te equation (7.1) gives y(x m + ) y(x m ) =y! (x m )+y!!! (x m ) 3 3! fromwiconecanobtaintecentralderivativeapproximation y! (x m )= y(x m + ) y(x m ) + (7.5) + O( ) (7.6) wic is more accurate tan te results from equations (7.3) or (7.4). By using Taylor series expansions one can develop a variety of derivative approximations. One can derive a derivative approximation for any order derivative. Consider an approximation for te jt derivative d j y dx j x=x i = y (j) (x i ), (7.7) were j a positive integer. Te derivative can be approximated by assuming te derivative can be represented in te form y (j) (x i )= 1 j [β my(x i m)+β m 1 y(x i (m 1))+ + β 1 y(x i ) α 0 y(x i )+ + α n 1 y(x i +(n 1))+α n y(x i + n)] + O( N ) involving (m + n +1) data points, were β m, β m 1,...,β 1, α 0, α 1,...,α n and N are constants to be determined. Let y i+j = y(x i + j) for te index j ranging over te values j = m, (m 1),...,(n 1),n and expand tese terms in a Taylor series wic are ten substitute into te equation (7.8). One can ten collect like terms and force te rigt-and side of equation (7.8) to equal te left-and side of equation (7.8) by setting certain coefficients equal to eiter zero or one. Tis will produce a system of equations were te coefficients β m,...,β 1, α 0,...,α n and te order N of te error term can be determined. Example 7-1. (Derivative formula) Derive a formula for te Þrst derivative of te form (7.8) y! (x m )= 1 [α 0y m + α 1 y m+1 + α y m+ ]+O( N ) (7.9) were α 0, α 1, α and N are constants to be determined. Solution: Substitute te Taylor series expansions y m+1 = y(x m + ) =y(x m )+y! (x m ) + y!! (x m )! + y m+ = y(x m +) =y(x m )+y! (x m )()+y!! (x m ) ()! + (7.10)
47 into te assumed form for te derivative to obtain y m! = 1 "!α 0 y m + α 1 y m + y m! + # " y!! m + + α y m + y m! () ()+y!! m We collect like terms and write te above equation in te form y! m = 1! (α 0 + α 1 + α )y m +(α 1 + α ())y m! +(α $ 1 + α ( ))y m!! + O(3 ) #$ +. (7.11) In order tat te rigt-and side of equation (7.11) reduce to y! m we require te unknown coefficients to satisfy te equations α 0 + α 1 + α =0 α 1 +α =1 (1/) α 1 +α =0. (7.1) We solve tis system of equations and Þnd α 0 = 3/, α 1 =and α = 1/. Tis gives te derivative formula y! (x m )= 3y m +4y m+1 y m+ + O( ) (7.13) were te 1/ factor as simpliþed te error term in equation (7.11). By including more terms in te expansions above one can determine te exact form for te error term. Example 7-. (Derivative formula) Derive an approximation formula for te second derivative of te form y!! (x m )= 1 [β 1y m 1 + α 0 y m + α 1 y m+1 ]+O( N ) (7.14) were β 1, α 0, α 1 and N are constants to be determined. Solution: Substitute te Taylor series expansions given by equations (7.1) and (7.) into te equation (7.14) and ten combine like terms to obtain y!! (x m )= 1 [(β 1 + α 0 + α 1 )y m 1 +(α 0 + α 1 ())y! m +(α 0 /+α 1 ( ))y m!! +(α 1 β 1 )y m!!! 3 3! +(α 1 + β 1 )y m (iv) 4 $ 4! +. (7.15) In order for te rigt-and side of tis equation to reduce to y!! m we require te coefficients to satisfy te conditions β 1 + α 0 +α 1 =0 α 0 +α 1 =0 (1/) α 0 +α 1 =1
48 We solve tis system of equations and Þnd β 1 =1, α 0 = and α 1 =1. Observe tat tese values for β 1, α 0, α 1 make te y m!!! coefficient zero and so one obtains te second derivative approximation y!! (x m )= y m 1 y m + y m+1 + O( ). (7.16) Derivative approximations of a function can also be derived by differentiating a polynomial approximation of te function. For example, one can use a polynomial approximations suc as te Newton forward, Newton backward or Stirling polynomial approximations for y(x) and ten one can differentiate te polynomial approximation and use tat as an approximation for te derivative. For te Þrst derivative one obtains te approximation dy dp n(s) ds, were s = x x 0 dx x=x0 ds dx x=x0 (7.17) Approximations for iger derivatives can be obtain by taking iger order derivatives of te approximating polynomials. Tis gives te approximation d m y dx m 1 d m P n (s) m ds m (7.18) for m =1,, 3,... Differentiation is a rougening process and so one sould expect to obtain large errors wen using collocation polynomials to approximate a derivative. Te error term associated wit a derivative of an interpolating polynomial is obtained by differentiating te error term of te interpolating polynomial. Example 7-3. (Derivative formula) Obtain approximations for te derivatives y! (x 0 ),y!! (x 0 ) and y!!! (x 0 ) by differentiation of te Stirling polynomial approximation wic we obtain from te lozenge diagram of Þgure 4-1 % s+1 & % + s & " # s y0 + y 1 y(x) P n (x) =y 0 + + 1 " # s +1 3 y 1 + 3 y + + 3 % s+ 4 y 1 & % + s+1 & 4 4 y + (7.19) were s = x x 0 takes on integer values at x 0,x 1,... Solution: We use cain rule differentiation to differentiate te approximating
49 polynomial and ten use tese derivatives to approximate te derivatives of y(x). Expanding te equation (7.19) we Þnd " y0 + y 1 y(x) y 0 + s wit derivatives + 1 4 (s4 s ) 4 y + # + 1 s y 1 + 1 6 (s3 s) " 3 y 1 + 3 # y y! (x) 1!" # y0 + y 1 + s y 1 + 1 " 3 y 1 + 3 # y 6 (3s 1) + 1 $ 4 (4s3 s) 4 y + y!! (x) 1 "! 3 y 1 + 3 # y y 1 + s + 1 $ 1 (6s 1) 4 y + y!!! (x) 1!" 3 y 1 + 3 # $ y 3 + s 4 y + At te point x = x 0 we ave s =0and so we obtain te approximations y! (x 0 ) 1!" # y0 + y 1 1 " 3 y 1 + 3 #$ y 6 y!! (x 0 ) 1! y 1 1 $ 1 4 y y!!! (x 0 ) 1!" 3 y 1 + 3 #$ y 3 Tese same results can be obtain by differentiating te equation (4.95) considered earlier. Note also tat derivative approximations can be obtained from te appropriate values of a difference table. Alternatively, te differences can be expressed in terms of ordinate values and so te above derivative formulas can also be expressed in terms of ordinate values. Error Terms for Derivative Approximations To derive error terms associated wit numerical differentiation or integration we will need te following results. (1.) If F (x) is a continuous function over te interval a x b, tentere exists at least one point c suc tat a c b and for positive constants α and β. αf (a)+βf (b) =(α + β)f (c) (7.0)
50 (.) A generalization of te above result is te following. For F (x) continuous over te interval a<x<b,witpointsx i satisfying a x i b for i =1,...,n ten one can write F (x 1 )+F (x )+ + F (x n )=nf (ξ) (7.1) for some value ξ lying in te interval [a, b]. Te result (7.0) follows from te inequalities, tat if F (a) F (b), ten for positive weigts α and β or (α + β)f (a) αf (a)+βf (b) (α + β)f (b) F (a) αf (a)+βf (b) α + β F (b). Hence, if F (x) is continuous over te interval [a, b], ten tere exists at least one point c suc tat αf (a)+βf (b) α + β = F (c). Te result (7.1) is obtained by similar arguments. Te error associated wit an it derivative approximation evaluated at a point x 0 is deþned Error = y (i) (x 0 ) y (i) (x 0 ) approx (7.) Most error terms can be obtained by truncation of appropriate Taylor series expansions. For example, to Þnd an error term associated wit te forward derivative approximation y 0! = y 1 y 0 we truncate te Taylor series expansion and write y 1 = y(x 0 + ) =y(x 0 )+y! (x 0 ) + y!! (ξ)!, x 0 < ξ <x 0 + (7.3) Te error term is ten found from te relation " # y! y(x0 + ) y(x 0 ) (x 0 ) = Error (7.4) Substituting te Taylor series expansion for y(x 0 + ) gives Error = y! 0 1!y 0 + y!0 $!! + y(ξ)! y 0 = y!! (ξ). (7.5)
51 Sometimes it is necessary to use Taylor series expansions on one or more terms in a derivative approximation. For example, to Þnd an error term associated wit te central difference approximation for te second derivative y 0!! = y 1 y 0 + y 1 we use truncated Taylor series expansions from equations (7.1) and (7.) to obtain wic simpliþes to " Error =y 0!! y 1 y 0 + y 1 Error =y 0!! 1! y 0 y 0! + y 0!! +y 0 + y 0! + y!! 0 # + y!!! 0 y!!! 0 3 6 + y(iv) 0 (ξ 1 ) 4 $ 3 6 + y(iv) 0 (ξ ) 4 4 4 y 0 (7.6) Error = 1 y(iv) 0 (ζ) (7.7) To derive te result given by equation (7.7) we ave made te assumption tat te derivative y (iv) 0 (x) is a continuous function so tat 4 4 y(iv) 0 (ξ 1 )+ 4 4 y(iv) 0 (ξ )= " 4 4 + 4 4 # y (iv) 0 (ζ) wic is a special case of te result (7.0) previously cited. Note also we ad to go to fourt order terms in te expansions because te tird order terms added to zero. Metod of Undetermined Coefficients One can assume a derivative formula for f! (x) involving undetermined coef- Þcients and ten select te coefficients so tat te assumed derivative representation is exact wen te function f(x) is a polynomial. For example, wit equal spacing were x i+1 = x i +, one can assume a derivative formula f! (x i )=β 0 f(x i )+β 1 f(x i+1 )+β f(x i+ ) (7.8) were β 0, β 1, β are undetermined coefficients, and ten require tat tis formula be exact for te cases f(x) =1, f(x) =x x i and f(x) =(x x i ). In tis way one obtains tree equations from wic te tree unknowns β 0, β 1, β can be determined. We ave: For f(x) =1, te equation (7.8) becomes 0=β 0 + β 1 + β For f(x) =x x i, te equation (7.8) becomes 1= β 1 + β (7.9) For f(x) =(x x i ), te equation (7.8) becomes 0= β 1 + β 4
5 We solve te equations (7.9) and Þnd β 0 = 3/, β 1 =4/, andβ = 1/. Tis gives te Þrst derivative formula f! (x i )= 1 [ 3f(x i)+4f(x i+1 ) f(x i+ )] (7.30) Te error term associated wit tis formula can be obtained from equation (7.) togeter wit appropriate Taylor series expansions. Te metod of undetermined coefficients is applicable for determining bot derivative and integration formulas. Numerical Integration In tis section we develop integration formulas and associated error terms wic can be used for evaluating integrals of te form I 1 = ' b a f(x) dx or I = ' b a w(x)f(x) dx (7.31) were w(x) is called a weigt function. Integration formulas are also referred to as quadrature formulas. Te term quadrature coming from te ancient practice of constructing squares wit area equivalent to tat of a given plane surface. Te integrands f(x) or w(x)f(x) in equations (7.31) are assumed to be continuous wit known values over te interval a x b. Te interval [a, b], over wic te integral is desired, is divided by (n+1) points into sections wit a = x 0 <x 1 <x <... < x n = b. Tis is called partitioning te interval into n-panels. Tese panels can be of equal lengts or unequal lengts as illustrated in te Þgure 7-1. Figure 7-1. Partition of interval [a, b] into n-panels.
53 By developing integration formulas for te area under te curve associated wit one or more panels, one can repeat te integration formula until te area associated wit all panels is calculated. We begin by developing a one-panel formula. Assume tat te interval [a, b] is partitioned wit equal spacing wit a = x 0, b = x n, = b a n, x j = x 0 + j, for j =0, 1,,...,n. (7.3) Teareaundertecurve y = f(x) between and x i is approximated by constructing a straigt line interpolating polynomial troug te points (,y i 1 ) and (x i,y i ) andtenintegrating tis interpolation function. We use y i 1 = f( ) and y i = f(x i ) and obtain from te lozenge diagram of Þgure 4-1, wit appropriate notation cange, te straigt line P 1 (x) =y i 1 + s y i 1 + s(s 1) f!! (ξ(x)), were s = x (7.33) and y i 1 = y i y i 1. Te area associated wit one-panel is ten approximated by f(x) dx P 1 (x) dx = $!y i 1 + s y i 1 + s(s 1) f!! (ξ(x)) dx. (7.34) We use te cange of variable for s given by equation (7.33) and integrate te Þrst two terms of equation (7.34) to obtain f(x) dx = $ 1!sy i 1 + s y i 1 = 0 [y i 1 + y i ] (7.35) wic is known as te trapezoidal rule since te area of a trapezoid is te average eigt times te base. Alternatively te trapezoidal rule can be derived by integrating te Lagrange interpolating polynomial from to x i. P 1 (x) = x x i x i f( )+ x x i f(x i )
54 Te last integral in equation (7.34) represents an integral of te error term accompanying te approximation polynomial. In order to evaluate tis last integral we use te mean value teorem ' xn x m ' xn f(x)g(x) dx = f(ζ) g(x) dx, x m < ζ <x n, (7.36) x m were it is assumed tat te functions f, g are continuous in te interval [x m,x n ] and g(x) remains of one sign over te interval. i.e. Eiter g(x) 0 or g(x) 0 over te interval. By integrating te error term of te straigt line approximating polynomial one obtains te local error term associated wit te trapezoidal rule formula. Tis integration produces local error = (s s) f!! (ξ(x)) dx, s = x local error = 3 f!! (ζ) ' 1 local error = 3 1 f!! (ζ). 0 (s s) ds = 3 f!! (ζ)! s 3 3 s, ds = dx $ 1 0 (7.37) Te one-panel trapezoidal formula wit local error term can be written in eiter of te forms or f(x) dx = [y i 1 + y i ] 3 1 f!! (ζ) f(x) dx = [f()+f(x i )] 3 1 f!! (ζ) By partitioning an interval [x 0,x n ] into n +1 points one can write ' xn x 0 f(x) dx = n( j=1 ' xj (7.38) x j 1 f(x) dx. (7.39) Now one can apply te trapezoidal rule to eac of te n-panels. Te sum tat results gives a representation of te integral over te interval [x 0,x n ]. Tis representation is called te extended trapezoidal rule or composite trapezoidal rule and can be represented for equal or unequal panel spacing. For unequal panel spacing te extended trapezoidal rule is written ' xn x 0 f(x) dx = 1 (y 0 + y 1 )+ (y 1 + y )+ + n (y n 1 + y n )+global error (7.40)