SE301: Numerical Methods Topic 8 Ordinary Differential Equations (ODEs) Lecture 28-36 KFUPM (Term 101) Section 04 Read 25.1-25.4, 26-2, 27-1 CISE301_Topic8L2 1 Outline of Topic 8 Lesson 1: Introduction to ODEs Lesson 2: Taylor series methods Lesson 3: Midpoint and Heun’s method Lessons 4-5: Runge-Kutta methods Lesson 6: Solving systems of ODEs Lesson 7: Multiple step Methods Lesson 8-9: Boundary value Problems CISE301_Topic8L2 2 Lecture 29 Lesson 2: Taylor Series Methods CISE301_Topic8L2 3 Learning Objectives of Lesson 2 Derive Euler formula using the Taylor series expansion. Solve the first order ODEs using Euler method. Assess the error level when using Euler method. Appreciate different types of errors in the numerical solution of ODEs. Improve Euler method using higher-order Taylor Series. CISE301_Topic8L2 4 Taylor Series Method The problem to be solved is a first order ODE: dy ( x) f ( x, y ), y ( x0 ) y0 dx Estimates of the solution at different base points: y( x0 h), y( x0 2h), y( x0 3h), .... are computed using the truncated Taylor series expansions. CISE301_Topic8L2 5 Taylor Series Expansion Truncated Taylor Series Expansion h k d k y y ( x0 h) k k ! dx k 0 n dy y ( x0 ) h dx x x0 , y y 0 h2 d 2 y 2 x x0 , 2 ! dx y y 0 hn d n y ... x x0 , n! dx n y y0 x x0 , y y0 The nth order Taylor series method uses the nth order Truncated Taylor series expansion. CISE301_Topic8L2 6 Euler Method First order Taylor series method is known as Euler Method. Only the constant term and linear term are used in the Euler method. The error due to the use of the truncated Taylor series is of order O(h2). CISE301_Topic8L2 7 First Order Taylor Series Method (Euler Method) dy y ( x0 h) y ( x0 ) h dx x x0 , y y0 O(h 2 ) Notation : xn x0 nh, dy dx x xi , y yi yn y ( xn ), f ( xi , yi ) Euler Method yi 1 yi h f ( xi , yi ) CISE301_Topic8L2 8 Euler Method Problem : Given the first order ODE : y0 y ( x0 ) with the initial condition : Determine : y ( x) f ( x, y ) yi y ( x0 ih ) for i 1,2,... Euler Method : y0 y ( x0 ) yi 1 yi h f ( xi , yi ) CISE301_Topic8L2 for i 1,2,... 9 Interpretation of Euler Method y2 y1 y0 x0 CISE301_Topic8L2 x1 x2 x 10 Interpretation of Euler Method y1 Slope=f(x0,y0) hf(x0,y0) y0 x0 CISE301_Topic8L2 y1=y0+hf(x0,y0) h x1 x2 x 11 Interpretation of Euler Method y2=y1+hf(x1,y1) y2 Slope=f(x1,y1) hf(x1,y1) Slope=f(x0,y0) y1 hf(x0,y0) y0 x0 CISE301_Topic8L2 y1=y0+hf(x0,y0) h x1 h x2 x 12 Example 1 Use Euler method to solve the ODE: dy 2 1 x , dx y (1) 4 to determine y(1.01), y(1.02) and y(1.03). CISE301_Topic8L2 13 Example 1 f ( x, y ) 1 x , 2 x0 1, y0 4 , h 0.01 Euler Method yi 1 yi h f ( xi , yi ) Step1 : y1 y0 h f ( x0 , y0 ) 4 0.01(1 (1) 2 ) 3.98 Step2 : y2 y1 h f ( x1 , y1 ) 3.98 0.01 1 1.01 3.9598 Step3 : 2 y3 y2 h f ( x2 , y2 ) 3.9598 0.01 1 1.02 3.9394 CISE301_Topic8L2 2 14 Example 1 f ( x, y ) 1 x , 2 x0 1, y0 4 , h 0.01 Summary of the result: i xi yi 0 1.00 -4.00 1 1.01 -3.98 2 1.02 -3.9595 3 1.03 -3.9394 CISE301_Topic8L2 15 Example 1 f ( x, y ) 1 x , 2 x0 1, y0 4 , h 0.01 Comparison with true value: CISE301_Topic8L2 i xi yi True value of yi 0 1.00 -4.00 -4.00 1 1.01 -3.98 -3.97990 2 1.02 -3.9595 -3.95959 3 1.03 -3.9394 -3.93909 16 Example 1 f ( x, y ) 1 x , 2 x0 1, y0 4 , h 0.01 A graph of the solution of the ODE for 1<x<2 CISE301_Topic8L2 17 Types of Errors Local truncation error: Error due to the use of truncated Taylor series to compute x(t+h) in one step. Global Truncation error: Accumulated truncation over many steps. Round off error: Error due to finite number of bits used in representation of numbers. This error could be accumulated and magnified in succeeding steps. CISE301_Topic8L2 18 Second Order Taylor Series Methods dy ( x) Given f ( y, x), y ( x0 ) y0 dx Second order Taylor Series method 2 2 dy h d y 3 yi 1 yi h O(h ) 2 dx 2! dx 2 d y needs to be derived analytical ly. 2 dx CISE301_Topic8L2 19 Third Order Taylor Series Methods dy ( x) Given f ( y, x), y ( x0 ) y0 dx Third order Taylor Series method 2 2 3 3 dy h d y h d y 4 yi 1 yi h O(h ) 2 3 dx 2! dx 3! dx 2 3 d y d y and 3 need to be derived analytical ly. 2 dx dx CISE301_Topic8L2 20 High Order Taylor Series Methods dy ( x) Given f ( y, x), y ( x0 ) y0 dx th n order Taylor Series method dy h 2 d 2 y hn d n y n 1 yi 1 yi h .... O(h ) 2 n dx 2! dx n! dx 2 3 n d y d y d y , 3 ,....., n need to be derived analytical ly. 2 dx dx dx CISE301_Topic8L2 21 Higher Order Taylor Series Methods High order Taylor series methods are more accurate than Euler method. But, the 2nd, 3rd, and higher order derivatives need to be derived analytically which may not be easy. CISE301_Topic8L2 22 Example 2 Second order Taylor Series Method Use Second order Taylor Series method to solve : dx 2 2 x t 1, x(0) 1, dt use h 0.01 2 d x(t ) What is : ? 2 dt CISE301_Topic8L2 23 Example 2 Use Second order Taylor Series method to solve : dx 2 2 x t 1, x(0) 1, use h 0.01 dt dx 2 1 2x t dt d 2 x(t ) dx 2 0 4x 1 4 x(1 2 x t ) 1 2 dt dt 2 h 2 2 xi 1 xi h(1 2 xi ti ) ( 1 4 xi (1 2 xi ti )) 2 CISE301_Topic8L2 24 Example 2 f (t , x) 1 2 x 2 t , t0 0, x0 1, h 0.01 2 h 2 2 xi 1 xi h(1 2 xi ti ) ( 1 4 xi (1 2 xi ti )) 2 Step1 : x1 1 0.01(1 2(1) 2 2 0.01 0) (1 4(1)(1 2 0)) 0.9901 2 Step 2 : x2 0.9901 0.01(1 2(0.9901) 2 2 0.01 0.01) (1 4(0.9901)(1 2(0.9901) 2 0.01)) 0.9807 2 Step 3 : x3 0.9716 CISE301_Topic8L2 25 Example 2 f (t , x) 1 2 x t , 2 t0 0, x0 1, h 0.01 Summary of the results: CISE301_Topic8L2 i ti xi 0 0.00 1 1 0.01 0.9901 2 0.02 0.9807 3 0.03 0.9716 26 Programming Euler Method Write a MATLAB program to implement Euler method to solve: dv 2 1 2v t . dt v ( 0) 1 for ti 0.01i, i 1,2,...,100 CISE301_Topic8L2 27 Programming Euler Method f=inline('1-2*v^2-t','t','v') h=0.01 t=0 v=1 T(1)=t; V(1)=v; for i=1:100 v=v+h*f(t,v) t=t+h; T(i+1)=t; V(i+1)=v; end CISE301_Topic8L2 28 Programming Euler Method f=inline('1-2*v^2-t','t','v') h=0.01 t=0 v=1 T(1)=t; V(1)=v; for i=1:100 v=v+h*f(t,v) t=t+h; T(i+1)=t; V(i+1)=v; end CISE301_Topic8L2 Definition of the ODE Initial condition Main loop Euler method Storing information 29 Programming Euler Method Plot of the solution plot(T,V) CISE301_Topic8L2 30 More in This Topic Lesson 3: Midpoint and Heun’s method Provide the accuracy of the second order Taylor series method without the need to calculate second order derivative. Lessons 4-5: Runge-Kutta methods Provide the accuracy of high order Taylor series method without the need to calculate high order derivative. CISE301_Topic8L2 31