Matakuliah Tahun : S0262-Analisis Numerik : 2010 Solving Ordinary Differential Equation Numerically Pertemuan 8 Material Outline • Ordinary differential equation – Euler method – Runge-Kutta method DIFFERENTIAL EQUATIONS (ODE) • Numerical Solution for ODE is absolutely needed for many mathematical or engineering cases because many cases in engineering fields that the analytical solutions do not exist or really hard to be solved analytically. • In this session the ODE’s that will be discussed are first degree ODE that has the general form as follows: dy F ( x, y, y ' ) 0, or y ' f ( x, y ) dx 4 EULER METHOD Euler Method in general is the method that is intended to solve 1st degree ODE . This method is a method of step-by- step with an initial value. The initial value usually is given as a part of the problem. Example: y' f ( x, y), and y( x0 ) y0 5 EULER METHOD The method will be explained by first reviewed the following Taylor series: h2 hn n y ( x h) y ( x) hy ' ( x) y ' ' ( x) y ' ( x) 2! n! In the above eq., if the absolute value of h is very small (h<1h2<<0), then the 3rd term and the following can be neglected and yield the following eq: y(x+h) y(x) + h y’(x) Therefore, the error that is associated with the approximation will depend on the magnitude of h and the type of the function. 6 EULER METHOD The Euler Method can also be derived from limit definition as follow: y ( x h) y ( x ) y ' ( x) lim h 0 h Therefore, the approximation value of the above limit can be written as: y ( x h) y ( x ) y ' ( x) y ( x h) y ( x) hy ' ( x) h The accuracy of the approximation formula given above depends on h, the smaller the h the more accurate the approximation. 7 EULER METHOD If y(x), y(x+h), y(x+2h), … are written as y0, y1, y2, … , then the above approximation formula can be written as: , 1 0 0 , 2 1 1 y y hy y y hy , yn yn 1 hyn 1 Example: Use the Euler method to solve the following ODE : y’(x,y) = x + y, y(0) = y0=0; you may use h=0.2 and compare your result with analytical solution y(x)= ex - x -1 8 Answer: y’(x,y) = x + y, y(0) = y0=0; h=0,2 Euler method: yn+1 yn + 0,2 y’n y1 y0 + 0,2 y’0 =0 + 0,2 y’(0,0)=0+0,2x0=0 y2 y1 + 0.2 y’1 = 0 + 0.2 (0.2) = 0.4 In which: y1 = y(h) y’1= y’(h,y1)=h+ y1= 0,2; etc Note: Analytical solution y(x)= ex - x -1 is found by the theory of Calculus 9 EULER METHOD Solution: n xn Euler yn=yn-1+hy’n-1 Y’n=xn+yn Exact y(x)= ex - x -1 Error O 0 0 (Initial) 0,000 0,000 0.000 1 0,2 0.000 0.200 0,021 0.021 2 0,4 0.040 0.440 0,092 0.052 3 0,6 0.128 0.688 0,222 0.094 4 0,8 0.274 0.946 0,426 0.152 5 1,0 0.489 1.215 0,718 0.229 6 1,2 7 1,4 10 RUNGE-KUTTA METHOD Aside from its simplicity, the numerical error associated with Euler method is relatively high as shown in the previous examples. Runge-Kutta Method is a good method that can improve the weakness of Euler method. As in the previous discussion, the error in the Euler method is due to the omission of the 3rd, 4th ,…. terms from Taylor Series in the approximation formula. 11 The formula in the Runge-Kutta Method is also consists of 2 terms, however, in the 2nd term some additional coefficients will be introduced as given in the following formula: Fourth Order Runge-Kutta Method: 1 yn 1 yn (k1n 2k1n 2k3n k 4 n ) 6 k1n hy ' ( xn , yn ) k 2 n hy ' ( xn 1 2 h, yn 1 2 k1n ) k3n hy ' ( xn 1 2 h, yn 1 2 k 2 n ) k 4 n hy ' ( xn h, yn k3n ) in which , xn 1 xn h x0 nh 12 RUNGE-KUTTA METHOD Example: Use the method of Runge-Kutta to solve the following ODE : y’(x,y) = x + y; y(0) = y0=0 ; take h=0.2 And compare your result to exact solution: y(x)= ex - x –1 Solution: yn 1 yn (k1n 2k 2 n 2k3n k 4 n ) / 6 y1 y0 (k10 2k 20 2k30 k 40 ) / 6 k10 hy ' ( x0 , y0 ) 0.2 0 0 k 20 hy ' ( x0 1 2 h, y0 1 2 k10 ) 0.2 y ' (0.1, 0) 0.02 k30 hy ' ( x0 1 2 h, y0 1 2 k 20 ) 0.2 y ' (0.1,0.01) 0.022 k 40 hy ' ( x0 h, y0 k30 ) 0.2 y ' (0.2,0.022) 0.0444 y1 0 (0 0.04 0.044 0.0444) / 6 0.0214 13 RUNGE-KUTTA METHOD Solution : y’(x,y) = x + y, y(0) = y0=0; h=0.2 Note: Analytical solution y(x)= ex - x -1 is found by the theory of Calculus yn 1 yn (k1n 2k 2 n 2k3n k4 n ) / 6 y2 y1 (k11 2k 21 2k31 k 41 ) / 6 k11 hy ' ( x1 , y1 ) 0.2 y ' (0.2,0.0214) 0.04428 k 21 hy ' ( x1 1 2 h, y1 1 2 k11 ) 0.2 y ' (0.3, 0.04354) 0.068708 k31 hy ' ( x1 1 2 h, y1 1 2 k 21 ) 0.2 y ' (0.3,0.055754) 0.0711508 k 41 hy ' ( x1 h, y1 k31 ) 0.2 y ' (0.4,0.0925508) 0.09851016 y2 0.0214 (0.04428 0.137416 0.1423016 0.09851016) / 6 0.091818 14 RUNGE-KUTTA METHOD Solution : y’(x,y) = x + y, y(0) = y0=0; h=0.2 yn 1 yn (k1n 2k 2 n 2k3n k 4 n ) / 6 y3 y2 (k12 2k 22 2k32 k 42 ) / 6 k12 hy ' ( x2 , y2 ) k 22 hy ' ( x2 1 2 h, y2 1 2 k12 ) k32 hy ' ( x2 1 2 h, y2 1 2 k 22 ) k 42 hy ' ( x2 h, y2 k32 ) y2 0.091818 ( ) / 6 15 Solution : y’(x,y) = x + y, y(0) = y0=0; h=0.2 n xn R.K:yn=yn-1 +(k1+2k2+2k3+k4)/6 Exact: y(x)= Error ex - x -1 0 0 0.0000 0.0000 0.000 1 0.2 0.0214 0.0214 0.000 2 0.4 0.0918 0.0918 0.000 3 0.6 0.2221 0.2221 0.000 4 0.8 0.4255 0.4255 0.000 5 1.0 6 1.2 16 Exercise: Use the method of Runge-Kutta to solve the following ODE: y ' ( x, y ) 2 xy 1 y (0) y0 0 h 0.1 17