Solving ODEs Euler Method & RK2/4 Major: All Engineering Majors Authors: Autar Kaw, Charlie Barker http://numericalmethods.eng.usf.edu Transforming Numerical Methods Education for STEM 02/11/10 http://numericalmethods.eng.usf.edu 1 Euler Method http://numericalmethods.eng.usf.edu Euler’s Method y True value Slope x0,y0 Φ y1, Predicted value Step size, h x Figure 1 Graphical interpretation of the first step of Euler’s method 3 http:// numericalmethods.eng.usf.edu Euler’s Method y True Value yi+1, Predicted value Φ yi h Step size xi xi+1 x Figure 2. General graphical interpretation of Euler’s method 4 http:// numericalmethods.eng.usf.edu How to write Ordinary Differential Equation How does one write a first order differential equation in the form of Example is rewritten as In this case 5 http:// numericalmethods.eng.usf.edu Example A ball at 1200K is allowed to cool down in air at an ambient temperature of 300K. Assuming heat is lost only due to radiation, the differential equation for the temperature of the ball is given by Find the temperature at seconds using Euler’s method. Assume a step size of seconds. 6 http:// numericalmethods.eng.usf.edu Solution Step 1: is the approximate temperature at 7 http:// numericalmethods.eng.usf.edu Solution Cont Step 2: For is the approximate temperature at 8 http:// numericalmethods.eng.usf.edu Solution Cont The exact solution of the ordinary differential equation is given by the solution of a non-linear equation as The solution to this nonlinear equation at t=480 seconds is 9 http:// numericalmethods.eng.usf.edu Comparison of Exact and Numerical Solutions 1500,0000 θ(K) Temperature, 1125,0000 Exact Solution 750,0000 h=240 375,0000 0 0 125 250 375 500 Time, t(sec) Figure 3. Comparing exact and Euler’s method 10 http:// numericalmethods.eng.usf.edu Effect of step size Table 1. Temperature at 480 seconds as a function of step size, h 11 Step, h θ(480) Et |єt|% 480 240 120 60 30 −987.8 1 110.32 546.77 614.97 1635.4 537.26 100.80 32.607 14.806 252.54 82.964 15.566 5.0352 2.2864 (exact) http:// numericalmethods.eng.usf.edu Comparison with exact results 1500,0000 Exact solution Temperature, θ(K) 1125,0000 750,0000 h=120 h=240 375,0000 0 h=480 -375,0000 -750,0000 -1125,0000 0 125 250 375 500 Time, t (sec) Figure 4. Comparison of Euler’s method with exact solution for different step sizes 12 http:// numericalmethods.eng.usf.edu Effects of step size on Euler’s Method 750,0000 Temperature, θ(K) 500,0000 250,0000 0 -250,0000 -500,0000 -750,0000 -1000,0000 0 125 250 375 500 Step size, h (s) Figure 5. Effect of step size in Euler’s method. 13 http:// numericalmethods.eng.usf.edu Errors in Euler’s Method It can be seen that Euler’s method has large errors. This can be illustrated using Taylor series. As you can see the first two terms of the Taylor series are the Euler’s method. The true error in the approximation is given by 14 http:// numericalmethods.eng.usf.edu Runge 2nd Order Method Major: All Engineering Majors Authors: Autar Kaw, Charlie Barker http://numericalmethods.eng.usf.edu Transforming Numerical Methods Education for STEM Undergraduates 02/11/10 http://numericalmethods.eng.usf.edu 1 Runge-Kutta nd 2 Order Method http://numericalmethods.eng.usf.edu Runge-Kutta 2nd Order Method For Runge Kutta 2nd order method is given by where 3 http:// numericalmethods.eng.usf.edu Heun’s Method Heun’s method y Here a2=1/2 is chosen resulting in yi+1, predicted yi xi where 4 xi+1 x Figure 1 Runge-Kutta 2nd order method (Heun’s method) http:// numericalmethods.eng.usf.edu Midpoint Method Here is chosen, giving resulting in where 5 http:// numericalmethods.eng.usf.edu Ralston’s Method Here is chosen, giving resulting in where 6 http:// numericalmethods.eng.usf.edu How to write Ordinary Differential Equation How does one write a first order differential equation in the form of Example is rewritten as In this case 7 http:// numericalmethods.eng.usf.edu Example A ball at 1200K is allowed to cool down in air at an ambient temperature of 300K. Assuming heat is lost only due to radiation, the differential equation for the temperature of the ball is given by Find the temperature at seconds using Heun’s method. Assume a step size of seconds. 8 http:// numericalmethods.eng.usf.edu Solution Step 1: 9 http:// numericalmethods.eng.usf.edu Solution Cont Step 2: 10 http:// numericalmethods.eng.usf.edu Solution Cont The exact solution of the ordinary differential equation is given by the solution of a non-linear equation as The solution to this nonlinear equation at t=480 seconds is 11 http:// numericalmethods.eng.usf.edu Comparison with exact results Figure 2. Heun’s method results for different step sizes 12 http:// numericalmethods.eng.usf.edu Effect of step size Table 1. Temperature at 480 seconds as a function of step size, h Step size, h θ(480) Et |єt|% 480 240 120 60 30 1041.4 63.304 −3.7762 −2.3406 −0.63219 160.82 9.7756 0.58313 0.36145 0.097625 −393.87 584.27 651.35 649.91 648.21 (exact) 13 http:// numericalmethods.eng.usf.edu Effects of step size on Heun’s Method Figure 3. Effect of step size in Heun’s method 14 http:// numericalmethods.eng.usf.edu Comparison of Euler and RungeKutta 2nd Order Methods Table 2. Comparison of Euler and the Runge-Kutta methods Step size, h 480 240 120 60 30 15 θ(480) Euler Heun Midpoint Ralston −987.84 110.32 546.77 614.97 632.77 −393.87 584.27 651.35 649.91 648.21 1208.4 976.87 690.20 654.85 649.02 (exact) 449.78 690.01 667.71 652.25 648.61 http:// numericalmethods.eng.usf.edu Comparison of Euler and RungeKutta 2nd Order Methods Table 2. Comparison of Euler and the Runge-Kutta methods Step size, h Euler Heun 480 240 120 60 30 252.54 82.964 15.566 5.0352 2.2864 160.82 9.7756 0.58313 0.36145 0.097625 Midpoin t 86.612 50.851 Ralston 6.5823 3.1092 1.1239 0.7229 9 0.22353 30.544 6.5537 0.1594 (exact) 16 http:// numericalmethods.eng.usf.edu Comparison of Euler and RungeKutta 2nd Order Methods 1200,0000 Midpoint Temperature, θ(K) 1025,0000 Ralston Heun 850,0000 Analytical 675,0000 Euler 500,0000 0 125 250 375 500 Time, t (sec) Figure 4. Comparison of Euler and Runge Kutta 2nd order methods with exact results. http:// 17 numericalmethods.eng.usf.edu Additional Resources For all resources on this topic such as digital audiovisual lectures, primers, textbook chapters, multiple-choice tests, worksheets in MATLAB, MATHEMATICA, MathCad and MAPLE, blogs, related physical problems, please visit http://numericalmethods.eng.usf.edu/topics/ runge_kutta_2nd_method.html Runge th 4 Order Method Major: All Engineering Majors Authors: Autar Kaw, Charlie Barker http://numericalmethods.eng.usf.edu Transforming Numerical Methods Education for STEM Undergraduates 02/11/10 http://numericalmethods.eng.usf.edu 1 Runge-Kutta th 4 Order Method http://numericalmethods.eng.usf.edu Runge-Kutta 4th Order Method For Runge Kutta 4th order method is given by where 3 http:// numericalmethods.eng.usf.edu How to write Ordinary Differential Equation How does one write a first order differential equation in the form of Example is rewritten as In this case 4 http:// numericalmethods.eng.usf.edu Example A ball at 1200K is allowed to cool down in air at an ambient temperature of 300K. Assuming heat is lost only due to radiation, the differential equation for the temperature of the ball is given by Find the temperature at Assume a step size of 5 seconds using Runge-Kutta 4th order method. seconds. http:// numericalmethods.eng.usf.edu Solution Step 1: 6 http:// numericalmethods.eng.usf.edu Solution Cont is the approximate temperature at 7 http:// numericalmethods.eng.usf.edu Solution Cont Step 2: 8 http:// numericalmethods.eng.usf.edu Solution Cont θ2 is the approximate temperature at 9 http:// numericalmethods.eng.usf.edu Solution Cont The exact solution of the ordinary differential equation is given by the solution of a non-linear equation as The solution to this nonlinear equation at t=480 seconds is 10 http:// numericalmethods.eng.usf.edu Comparison with exact results Figure 1. Comparison of Runge-Kutta 4th order method with exact solution 11 http:// numericalmethods.eng.usf.edu Effect of step size Table 1. Temperature at 480 seconds as a function of step size, h Step size, θ (480) h 480 −90.278 240 594.91 120 646.16 60 647.54 30 647.57 Et |єt|% 737.85 52.660 1.4122 0.033626 0.00086900 113.94 8.1319 0.21807 0.0051926 0.00013419 (exact) 12 http:// numericalmethods.eng.usf.edu Effects of step size on RungeKutta 4th Order Method Figure 2. Effect of step size in Runge-Kutta 4th order method 13 http:// numericalmethods.eng.usf.edu Comparison of Euler and RungeKutta Methods Figure 3. Comparison of Runge-Kutta methods of 1st, 2nd, and 4th order. 14 http:// numericalmethods.eng.usf.edu THE END