1 Numerical Solution of Ordinary Differential Equations Consider the 1st order ordinary differential equation (ODE) dy f ( x, y ) . dx The initial condition can be taken as y ( x0 ) y 0 . Then we could use a Taylor series about x x0 and obtain the complete solution, or y ( x) y ( x0 ) y ' ( x0 )( x x0 ) 1 1 1 (N ) y ' ' ( x0 )( x x0 ) 2 y ' ' ' ( x0 )( x x0 ) 3 ... y ( x0 )( x x0 ) N ... 2! 3! N! Since y ( x0 ) y 0 and y ' ( x 0 ) f ( x0 , y 0 ) y 0 ' then we can find the first two terms. For the second derivative d2y y ''( x0 ) 2 dx x x0 f f dy f, x ( x0 , y0 ) f, y ( x0 , y0 ) y0 ' . x y dx x x 0 Similarly the third derivative is y '''( x0 ) d3y dx3 f, xx ( x0 , y0 ) 2 f, xy ( x0 , y0 ) y0 ' f, yy ( x0 , y0 ) y0 '2 . x x0 If we truncate at the third derivative y ( x) y ( x 0 ) y ' ( x 0 )( x x 0 ) 1 1 y ' ' ( x 0 )( x x 0 ) 2 y ' ' ' ( x 0 )( x x 0 ) 3 Error , 2! 3! and Error 1 iv y ( )( x x0 ) 4 , where x0 x . 4! 2 Euler’s Method Take the Taylor series to 1st order, and let the interval h x1 x0 , then y1 y ( x0 ) f x0 , y 0 h y ' ' ( ) 2 h . 2 The error for a time step (the local error) is O h 2 . The global error, after many steps, is Oh . Then y1 y0 f ( x0 , y0 )h where y0 y( x0 ) , y2 y1 f ( x1, y1 )h where x1 x0 h , … y N 1 y N f ( x N , y N )h where x N 1 x N h . Example: dy x y , y ( 0) 1 dx The exact solution can be found from dy y x. dx Let y yc y p where dyc yc 0 , or y c Ce rx . Then rCe rx Ce rx 0 for all x , dx or r 1 , and y c Ce x . Since the right hand side is linear in x try y p Ax B . Then dy p dx A and dy p dx y p x becomes A Ax B x which must hold for all x. Hence A 1 , and B=-1, making y p x 1 , and since y yc y p then y Ce x x 1 . But @ x 0 , y 1 or C 1 1 , and C 2 . Making the complete solution y 2e x x 1 . 3 Using Euler’s method and taking h 0.02 y0 1, x0 0 x1 0.02, y1 1 1 0.02 1.02 , since y0 ' 1 . In general, y n 1 y n y n ' h; xn 1 xn h n 0 1 2 3 4 5 xn 0.00 0.02 0.04 0.06 0.08 0.10 yn 1.0000 1.0200 1.0408 1.0624 1.0848 1.1081 yexact 1.0000 1.0204 1.0416 1.0637 1.0866 1.1103 For the error, y Euler y5 y0 0.1081 , y Exact y5 y0 0.1103 , can be defined as Relative Error y Euler y Exact 1 y Euler y Exact 2 0.0022 2% 0.1092 The results plot as It would be better to use the slope at the beginning and end of the increment (e.g., the average at each end), and although we don’t know the slope at the end we can approximate it. 4 Modified Euler Method Let y n ' f ( xn , y n ) . Then an approximation for y at the end of the increment is ~ y n 1 y n y n ' h and an estimate for the slope at the end of the increment is ~ y' f (x ,~ y ). n 1 n 1 n 1 We can now set y n 1 y n 1 y n ' ~ y n' 1 h . 2 The error can be found from y n 1 y n y n' h 1 '' 2 yn h O h3 2 and since y n 1 y n y n' h 1 y n' 1 y n' Oh h 2 O h 3 2 h or Hence the local error is Oh 3 and the global error is Oh 2 . Another way to write our y ' y n' h O h 3 . y n 1 y n n 1 2 results is k1 hf ( xn , y n ) k 2 hf ( xn h, y n k1 ) y n 1 y n 1 k1 k 2 2 The previous example now can include modified Euler n 0 1 2 3 4 5 xn 0.00 0.02 0.04 0.06 0.08 0.10 which is much better. yeuler 1.0000 1.0200 1.0408 1.0624 1.0848 1.1081 ymodified 1.0000 1.0204 1.0416 1.0637 1.0866 1.1104 yexact 1.0000 1.0204 1.0416 1.0637 1.0866 1.1103 5 Runge-Kutta Methods The modified Euler method is actually a two step (second order) Runge-Kutta algorithm. These methods can be readily extended to eight and even tenth order. The derivations follow the same procedure. Assume for the second order method y n 1 y n ak1 bk 2 where k1 hf ( xn , y n ) , and k 2 hf ( xn h, y n k1 ) . The parameters a, b, and are found by comparing to a Taylor series expansion. Recall 1 y n 1 y n hf n h 2 f n, x f n, y f n . 2 But f ( xn h, yn k1 ) f n hf n, x k1 f n, y . Since k1 hf n , f ( xn h, yn k1 ) f n hf n, x hf n f n, y Using y n 1 y n ak1 bk 2 , yn 1 yn ahf n bh f n hf n, x hf n f n, y , or y n 1 y n a b hf n bh 2 f n, x bh 2 f n f n, y Comparing to the Taylor series y n 1 y n hf n 1 2 1 h f n, x h 2 f n f n, y . 2 2 1 1 , b which is three equations in four unknowns. Hence we 2 2 can pick for the fourth equation any equation that is convenient. Then a b 1, b For example we can take or Modified Euler is a 2 / 3, b 1 / 3, and 3 / 2 a 0, b 1, and 1 / 2 . a b 1 / 2, and 1 . 6 Fourth Order Runge-Kutta For fourth order Runge-Kutta the estimates for the changes are k1 hf ( x n , y n ) 1 1 h, y n k1 ) 2 2 1 1 k 3 hf ( x n h, y n k 2 ) 2 2 k 4 hf ( x n h, y n k 3 ) k 2 hf ( x n and the updated value for y is found from y n 1 y n 1 k1 2k 2 2k3 k 4 . 6 Note that all of the algorithms presented are for first order equations with only one dependent variable. These can be readily extended to systems of higher order differential equations. For those cases please see page 7. 7 Higher Order Differential Equations Consider dny n y (n) ( x) f x, y, y' , y ' ' ,..., y n 1 f x, y0 , y1 , y 2 ,..., y n 1 dx and let the vector Y be defined as y0' y ' 1 y1 y 2 y ' y Y ' 2 3 ... y n' 2 ' y n 1 y0 y 1 y where Y 2 ... ... yn 2 y n 1 f y n 1 which is now a vector first order equation, and the first order rules can be applied to a system of first order equations. Systems of First Order Equations Let Y F x, Y Euler: YN 1 YN hFN Modified Euler: where 1 YN 1 YN K1 K 2 2 where K1 hF x N , YN K 2 hF x N h, YN K1 Runge-Kutta (4th order): YN 1 YN 1 K1 2 K 2 2 K3 K 4 6 8 K 1 hF x N , Y N 1 1 K 2 hF x N h, Y N K 1 2 2 1 1 K 3 hF x N h, Y N K 2 2 2 K 4 hF x N h, Y N K 3 9 Problem The equation for a pendulum with a mass, m, at the end of a rod of negligible mass with length, L, is mL2 mgL sin 0 . For the initial conditions take 0 @ t 0 , and 0 @ t 0 , (1) where is the angle from the vertical, () Let d( ) d 2( ) , and () . dt dt 2 g t . The equation becomes L d 2 Now let p d 2 d then since d d 2 d 2 sin 0 . (2) dp dp d dp , p d d d d The equation can be written p dp sin 0 , d and integrating 1 2 p cos E constant . 2 This is the conservation of energy. The initial condition is p hence E cos 0 or d 0 @ 0 , d 2 1 d cos cos 0 2 d (3) is also the governing differential equation. Integrate the equation of motion (2) subject to the initial conditions (1), / 2 @ 0 and 0 @ 0 using Euler, modified Euler, and Runge-Kutta. Use the expression for the energy to check the accuracy of your integration. Integrate for onequarter of a cycle and then determine the period for a complete cycle.