178 Chapter 9: Numerical Differetiatio Numerical Differetiatio Formulatio of equatios for physical problems ofte ivolve derivatives (rate-of-chage quatities, such as velocity ad acceleratio). Numerical solutio of such problems ivolves umerical evaluatio of the derivatives. Oe method for umerically evaluatig derivatives is to use Fiite DIffereces: From the defiitio of a first derivative dy ----- = y' ( x) dx we ca take a fiite approximatio as y' ( x) which is called Forward DIfferece Approximatio. Similarly, we could use the Backward Differece Approximatio: ( yx + ) y( x) lim ---------------------------------------- 0 y( x + ) y( x) ---------------------------------------- y( x) y( x ) y' ( x) --------------------------------------- Ad, i either case, we ca reduce a roudoff errors i the computatios by choosig to be a small value that ca be exactly represeted i biary (for example ot 0.1).
179 Also, i additio to a x differece that ca be exactly represeted i biary, we ca get a somewhat better approximatio to the first derivative with a Cetral Fiite Differece: y' ( x) y( x + ) y( x ) ---------------------------------------------------- 2 Example: Give a expressio for the fuctio f(x, y) i the equatio: dy ----- = f ( x, y) dx we ca umerically approximate y values over the rage of x, with the differece equatio: y( x + ) y( x) = f ( x, y) Assumig we kow the iitial value y 0 ad we subdivide the x rage from x 0 to x ito equal itervals, we ca solve for each successive y value as y k = y k 1 + f ( x k 1, y k 1 ), k = 1, 2,, I geeral, such problems ca be solved with ouiform x subitervals ad with kow iitial or boudary coditos.
180 Usig similar methods, we ca evaluate equatios ivolvig partial derivatives, ad we ca set up fiite differece approximatios for higher order derivatives. For istace, for secod derivatives, we ca use the approximatio y' ( x) y( x + ) 2y( x ) -------------------------------------------------------- 2 Derivative Fuctios i Mathematica Numerical evaluatio of differet equatios accomplished with NDSolve[ eqs, y, {x, ymi, xmax]} - umerically solves eqs (which iclude iitial or boudary equatios) for y with idepedet variable x i the rage xmi to xmax. NDSolve[ eqs, {y1, y2,...}, {x, xmi, xmax}] - solves the set of eqs for values y1, y2, etc. Example: NDSolve[{y [x]==exp[x],y[0]==1},y,{x,0,5}] {{y -> IterpolatigFuctio[{0., 5.}, <>]}} Solutio the stored as a iterpolatio table, ad we retrieve values with statemets such as y[1.0 /. % {2.71829}
181 Also, we ca use statemets such as Plot[Evaluate[y[x] /. %], {x,xmi, xmax}] to plot the umeric solutio. Symbolic differece performed with D[, x] - evaluates first derivative of with respect to x. (Ca also be applied to f(x1, x2,...).) D[, {x, } ] - evaluates th derivative of with respect to x. Examples: (Also ca use D fuctio to obtai partial derivatives.) D[Exp[x],x] E x D[x^3, x] 3 x 2 D[x^3, {x, 2}] 6 x {secod derivative D[x^2 y, x] {partial derivative 2 x y
182 Chapter 9: Numerical Itegratio Numerical Itegratio Most itegrals arisig from solutios of problems i egieerig ad sciece caot be represeted i closed form - they must be evaluated umerically. For fuctio of a sigle variable, we seek a approximatio to the area uder the curve: Area = b a f ( x) dx a b x For fuctio of several variables, we seek approximatio to the size of the volume for the specified itegratio regio. We will cosider oly algorithms for fuctios of a sigle variable:.
183 Numerical evaluatio procedures for a fuctio of a sigle variable ivolve summatio of small subareas, usig simple fuctios to approximate. For example, by defiitio of itegral: b f ( x ) dx = lim f ( x k ) k a a k = 1 with k = x k x k 1 We ca approximate itegral with a fiite umber of subitervals usig a costat value for i each subiterval: f ( x) = f ( x 1 ) x k 1 x x k, k = 1, 2,, x 0 =a x 1 x 2 x 3 x -1 x =b I this case, we approximate itegral by addig the areas of a set of approximatig rectagles.
184 Example: f ( x) = 1 x 2, a = 1, b = 1 A = 1 1 g( x) dx -1 0 1 x Selectig equal width itervals with with = 5, we have b a 2 k = ----------- = -- = 0.4 k 5 The area is the approximated as A k = 1 with x k = x k 1 + k ad x 0 = -1. f ( x k ) k -1 -.6-2.2.6 1 x The A f ( x k ) 0.4( 0.8 + 0.98 + 0.98 + 0.8 + 0.0) k = 1 1.42 Actual size of area: A -------- πr2 1.57 2
185 We ca improve umerical estimate of itegral by Icreasig umber of itervals. Usig a more accurate approximatio of i each iterval. Commo Approximatio Methods: Liear Quadratic or Higher-Order Polyomial Trapezoidal Rule Approximate with liear fuctio over each subiterval. Area is the calculated as the sum of the areas of resultig trapezoids: Takig equal-width itervals: b a = ----------- x 0 = a x 1 x 2 x 3... Thus, or Area of each trapezoid is A k f ( x k 1) + f ( x k ) = -----------------------------------------, 2 k = 1, 2,, b f ( x) dx ----- [ f ( x 2 0 ) + f ( x 1 ) ] + [ f ( x 1 ) + f ( x 2 ) ] + a + [ f ( x 1 ) + f ( x )] } b f ( x) x 1 1 2 -- [ f ( a d ) + f ( b) ] + f ( x k ) a k = 1
186 Example: Trapezoidal Rule to evaluate Width of each iterval is 1 ( 1) = ------------------- = 0.5 5 Itegral is the evaluated as 1 1 x 2 x, = 5 1 x 0 = 1, x k = x k 1 +, k = 1, 2, 3, 4, 5 1 f ( x) x -------------------------------- f ( 1) + f ( 1) 4 d + 2 f ( x ) k 1 k = 1 0.4( 0.80 + 0.98 + 0.98 + 0.80) 1.42 (Same result as obtaied usig rectagular areas. Why?)
187 Simpso s Rule Approximates with a quadratic fuctio. More accurate evaluatio of itegral tha Trapezoidal Rule (a liear approximatio). Simpso s Rule (parabola) Trapezoidal Rule (liear) x k-1 x k 1 + x k ------------------------ 2 x k I this case, the itegral is approximated as x k b f ( x) dx ( α 0 + α 2 x + α 2 x 2 ) dx a k = 1 x k 1 To evaluate the summatio, we eed to determie the parabola coefficiets α 0, α 1, α 2 i each of the subdivisios. We ca accomplish this with three boudary coditios that require the parabola to itersect at the edpoits ad at the midpoit of each subdidivisio.
188 Applyig Simpso s Rule: 1. Divide iterval (a, b) ito equal subitervals, with a multiple of 2. 2. Approximate with parabola across every two subitervals. α 0 + α 1 x + α 2 x 2 x k-1 x k x k+1 Obtai coefficiets α 0, α 1, α 2 by solvig 2 α 0 + α 1 x k 1 + α 2 x k 1 = f ( x k 1 ) 2 α 0 + α 1 x k + α 2 x k = f ( x k ) 2 α 0 + α 1 x k + 1 + α 2 x k + 1 = f ( x k + 1 ) over each of the itervals (x k-1, x k+1 ).
189 We the approximate itegral as x k + 1 f x dx 1 ( α 0 + α 1 x + α 2 x 2 ) dx a k = 1 x k 1 kodd 1 k = 1 kodd α 0 x + a 1 x 2 x 3 ---- + α 2 2 ---- 3 x k + 1 x k 1 Substitutig the solutio for α 0, α 1, α 2 over itervals (x k-1, x k+1 ), we obtai b s 1 1 f ( x) dx -- [ f ( x 3 k 1 ) + 4 f ( x k ) + f ( x k + 1 )] k = 1 kodd where = x k+1 - x k = x k - x k-1 {Note: A easy way to derive this result is to cosider a parabols specified over the iterval (- x, x ). Hece, b f ( x) dx ----- [( f 3 0 + 4 f 1 + f 2 ) + ( f 2 + 4 f 3 + f 4 ) + ] a Collectig terms, we have Simpso s Rule: b a f ( x) dx ----- f ( a) + f b + 3 1 ( ) 4 f ( x) + 2 f ( x k ) k = 1 k = 2 ( odd) ( eve) with each successive x value calculated as x k = x k 1 + ( k = 1, 2,, ) 2
190 Example: Simpso s Rule Evaluatio of 1 1 x 2 d x, = 4 The, Ad 1 1 ( 1) = ------------------- = 0.5 4 x 0 = 1, x k = x k = 1 +, k = 1, 2, 3, 4 1 3 2 f ( x) dx ----- f ( 1) + f ( 1) + 4 f ( x 3 k ) + 2 f ( x k ) 1 k = 1 k = 2 ( odd) ( eve) ----- { 4[ f ( x 3 1 ) + f ( x 3 )] + 2 f ( x 2 )} 1 -- [ 4( 0.87 + 0.87) + 2( 1) ] 6 1 -- ( 8.96) 6 1 1.49 where f ( x) d x 1.57 Thus Simplso with =4 is better tha Trapezoidal with =5. 1
191 Psuedocode Algorithm - Simpso s Rule Iput f, a, b, (* fc, iteger limits, o. of itervals *) dx = (b-a)/ dx2 = dx + dx odds = 0.0; eves = 0.0 x = a Do k=2, -2, 2 x = x + dx2 eves = eves + Edofdo x = a - dx Do k = 1, -1, 2 x = x + dx2 odds = odds + Edofdo simpsoarea=(dx/3.0)*(f(a)+f(b)+4.0*odds+2.0*eves) Summary of Itegratio Methods Approximate = f(x k ) (costat) over each iterval A b a ----------- f ( x ) k k = 1 x 0 = a, x = b x k = x k = 1 + ( b a) Trapezoidal Rule: Liear Approximatio A b ----------- a f ( a) + f ( b) 1 ---------------------------- + 2 f ( x ) k k = 1 Simpso s Rule: Quadratic Approximatio, eve A 1 2 ----- f ( a) + f ( b) + 4 f ( x 3 k ) + 2 f ( x k ) k = 1 k = 2 = (b-a)/ ( odd) ( eve)
192 Numerical Itegratio Errors For small, error is primarily due to approx. of (width of subitervals large). For large, roudoff errors become sigificat; i.e., area of each subiterval small, ad this value ca get lost i precisio of larger accumulated area: accumulated area ext subiterval a b x Error Magitude Plot: error roudoff error approximatio error (due to width of subitervals) ideal umber of subitervals Ideal value for depeds o: Form of Itegratio method used Computer System
193 Numerical Itegratio Improvemets Repeatedly icrease umber of subitervals util area estimate is withi specified tolerace. A method for accomplishig this is to start with large subitervals ad the double the umber of itervals at each step: Iput compute: area oldarea = area = 2 compute: area relerror = (oldarea - area) o relerror < tolerace yes retur Also, could use higher-order polyomials to approximate over each of the subitervals. Ad we could use higher precisio i calculatios to reduce roudoff errors. (Tradeoff: more calculatio time.)
194 Numerical Evaluatio of Ifiite Itegrals Divide itegral ito parts ad apply a umerical itegratio techique to each part util cotributios ted to zero. For example: a f ( x) dx = a 1 a 1 f ( x) dx + a 2 a 1 f ( x) dx + a 1 a 1 f ( x) dx + If itegral is fiite, the we must have f ( x) 0 as x, That is, we ca compute the itegral as a fiite umber of terms: a f ( x) dx = f ( x) dx + ε a a k 1 k = 1 whter the error term is less tha some specified tolerace.
195 Mote Carlo Methods Examples: Use of radom umbers to solve determiistic problems. This approach useful for umerically evaluatig complex itegrals. Multiple Itegratio b 1 b b 1 b f ( x 1, x 2,, x ) x 1 Fuctios with High-Frequecy Oscillatios b b d d x 2 dx a b x Basic Approach: Geerate uiformly distributed radom poits i a area cotaiig ad the iterval (a, b) -- where area is ot too large compared to itegral area. The compute: h a b x b f ( x) dx a ----------------------- h( b a) = lim umber of poits uder ----------------------------------------------------------------- where (b-a) may be positive or egative, ad we might have < 0 over part or all of the iterval (a, b).
196 Algorithm: Mote Carlo Itegratio 1. Geerate two uiformly distributed radom umbers r 1, r 2 i the iterval (0,1). x = a + r 1 ( b a) 2. Assumig 0, calculate coordiate positios (x,y) as: y = r 2 h h f(x,y) a b x Cout poit (x,y) as uder if 0 y f ( x) f ( x) 0 3. If < 0 for a value of x i the iterval (a,b), calculate coordiate positio as h = y max y mi y max x = a + r 1 ( b a) a f(x,y) b y = y mi + r 2 h y mi Cout poit (x,y) as uder if 0 y f ( x) for f ( x) < 0 I this case, we subtract 1 from the poit cout; i.e., ptcout =, - where is the umber of poits below the x axis.
197 Pseudocode Algorithm - Mote Carlo Itegratio Iput f, a, b, h, (* assume > 0; how width = b - a ca this be modified ptcout = 0 for < 0 also? *) Do k = 1, x = a + width * Radom( ) y = h * Radom( ) If (y < ) The ptcout = ptcout + 1 Edofif Edofdo area = h * width * ptcout / 1 Example: 1 x 2 dx 1 y mi = 0, y max = 1 a = 1, b = 1, h = 1, = 10 y -1 0 1 1 x r 1 r 2 x = -1 + 2r 1 y = r 2 = 1 - x 2 ptcout1 1 0.972 0.466 0.944 0.466 0.330 0 2 0.069 0.001-0.862 0.001 0.507 1 3 0.038 0.395-0.924 0.395 0.382 1 4 0.740 0.267 0.480 0.267 0.877 2 5 0.152 0.948-0.696 0.948 0.718 2 6 0.805 0.662 0.610 0.662 0.792 3
198 r 1 r 2 x = -1 + 2r 1 y = r 2 = 1 - x 2 ptcout1 7 0.227 0.034-0.546 0.034 0.838 4 8 0.607 0.835 0.214 0.835 0.977 5 9 0.212 0.463 0.576 0.463 0.817 6 10 0.670 0.624 0.340 0.624 0.940 7 Itegratio Fuctios i Mathematica Itegrate[, x] - idefiite itegral of Itegrate[, {x, xmi, xmax}] - defiite itegral of with itegratio limits xmi ad xmax Itegrate[f(x, y), {x, xmi, xmax}, {y, ymi, ymax}] - multiple itegral of fuctio f(x, y) Nitegrate[... ] - umerically evaluate itegral Examples: Itegrate[x^3,x] x 4 ---- 4 Itegrate[x^3,{x,a,b}] x 4 b 4 -------- + ----- 4 4 Itegrate[x^3,{x,0,1}] 1 -- 4
199 Itegrate[Sqrt[1-x^2],{x,-1,1}] Pi ---- 2 NItegrate[Sqrt[1-x^2],{x,-1,1}] 1.5708 f[x_, y_1] := x y^2 Itegrate[f[x,y], {x,0,a}, {y,0,b}] a 2 b 3 ----------- 6 Itegrate[f[x,y], {x,0,3}, {y,0,2}] 12 NItegrate[f[x,y], {x,0,3}, {y,0,2}] 12.000000000000000001 (roudoff error)