You are on page 1of 31

# SE301: Numerical Methods

Topic 8
Ordinary Differential Equations (ODEs)
Lecture 28-36

KFUPM
(Term 101)
Section 04

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

 Assess the error level when using Euler method. CISE301_Topic8L2 4 .  Improve Euler method using higher-order Taylor Series.  Appreciate different types of errors in the numerical solution of ODEs.Learning Objectives of Lesson 2  Derive Euler formula using the Taylor series expansion.  Solve the first order ODEs using Euler method.

y( x0  3h).. y ( x0 )  y0 dx Estimates of the solution at different base points: y( x0  h).Taylor Series Method The problem to be solved is a first order ODE: dy ( x)  f ( x. are computed using the truncated Taylor series expansions. CISE301_Topic8L2 5 .. y ). y( x0  2h).. .

CISE301_Topic8L2 6 .  dx x  x0 .Taylor Series Expansion Truncated Taylor Series Expansion h k  d k y n   y ( x0  h)   k !  dx k  k 0  x  x0 . n! dx n x  x0 . y  y0  dy h2 d 2 y hn d n y  y ( x0 )  h  2  ... 0 y  y0 y  y0 The nth order Taylor series method uses the nth order Truncated Taylor series expansion. y y 2! dx x  x0 .

Euler Method  First order Taylor series method is known as Euler Method.  The error due to the use of the truncated Taylor series is of order O(h2). CISE301_Topic8L2 7 .  Only the constant term and linear term are used in the Euler method.

yn  y ( xn ). y  y0 Notation : xn  x0  nh. dy  f ( xi .First Order Taylor Series Method (Euler Method) dy y ( x0  h)  y ( x0 )  h  O(h 2 ) dx x  x0 . yi ) CISE301_Topic8L2 8 . yi ) dx x  xi . y  yi Euler Method yi 1  yi  h f ( xi .

y ) with the initial condition : y0  y ( x0 ) Determine : yi  y ( x0  ih ) for i  1. yi ) for i  1....... Euler Method : y0  y ( x0 ) yi 1  yi  h f ( xi . CISE301_Topic8L2 9 .2.2.Euler Method Problem : Given the first order ODE : y ( x)  f ( x.

Interpretation of Euler Method y2 y1 y0 x0 x1 x2 x CISE301_Topic8L2 10 .

y0) y1 y1=y0+hf(x0.Interpretation of Euler Method Slope=f(x0.y0) y0 x0 x1 x2 x h CISE301_Topic8L2 11 .y0) hf(x0.

y1) Slope=f(x0.y0) y1 y1=y0+hf(x0.Interpretation of Euler Method y2 y2=y1+hf(x1.y0) y0 x0 x1 x2 x h h CISE301_Topic8L2 12 .y0) hf(x0.y1) Slope=f(x1.y1) hf(x1.

02) and y(1.01).Example 1 Use Euler method to solve the ODE: dy  1 x .03). 2 y (1)  4 dx to determine y(1. CISE301_Topic8L2 13 . y(1.

2 x0  1.9394 2 CISE301_Topic8L2 14 .02  3. y2 )  3.01(1  (1) 2 )  3. y0  4 . h  0.98   Step2 : y2  y1  h f ( x1 .01 1  1.01 1  1.01 Euler Method yi 1  yi  h f ( xi .98  0. y )  1  x . y0 )  4  0.01  3. yi ) Step1 : y1  y0  h f ( x0 .9598  0. y1 )  3.Example 1 f ( x.9598 2 Step3 :   y3  y2  h f ( x2 .

00 -4.98 2 1. y0  4 .00 1 1.01 Summary of the result: i xi yi 0 1. 2 x0  1.02 -3.03 -3.01 -3. y )  1  x .9394 CISE301_Topic8L2 15 .9595 3 1.Example 1 f ( x. h  0.

9394 -3.01 Comparison with true value: i xi yi True value of yi 0 1.93909 CISE301_Topic8L2 16 .03 -3. 2 x0  1.9595 -3.02 -3.00 -4.95959 3 1.98 -3.00 1 1.00 -4.Example 1 f ( x.01 -3. y0  4 . y )  1  x . h  0.97990 2 1.

y )  1  x . h  0.Example 1 f ( x. y0  4 .01 A graph of the solution of the ODE for 1<x<2 CISE301_Topic8L2 17 . 2 x0  1.

Types of Errors  Local truncation error: Error due to the use of truncated Taylor series to compute x(t+h) in one step.  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 .  Global Truncation error: Accumulated truncation over many steps.

Second Order Taylor Series Methods dy ( x) Given  f ( y. x). dx CISE301_Topic8L2 19 . y ( x0 )  y0 dx Second order Taylor Series method 2 2 dy h d y yi 1  yi  h  2  O(h ) 3 dx 2! dx 2 d y 2 needs to be derived analytical ly.

y ( x0 )  y0 dx Third order Taylor Series method 2 2 3 3 dy h d y h d y yi 1  yi  h  2  3  O(h ) 4 dx 2! dx 3! dx d2y d3y 2 and 3 need to be derived analytical ly. x).Third Order Taylor Series Methods dy ( x) Given  f ( y. dx dx CISE301_Topic8L2 20 .

. x)... n need to be derived analytical ly.High Order Taylor Series Methods dy ( x) Given  f ( y.. 3 ... 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  2  ...  n  O(h ) dx 2! dx n! dx 2 3 n d y d y d y 2 . dx dx dx CISE301_Topic8L2 21 ..

3rd. the 2nd. and higher order derivatives need to be derived analytically which may not be easy.  But. CISE301_Topic8L2 22 .Higher Order Taylor Series Methods  High order Taylor series methods are more accurate than Euler method.

01 dt 2 d x(t ) What is : 2 ? dt CISE301_Topic8L2 23 .Example 2 Second order Taylor Series Method Use Second order Taylor Series method to solve : dx  2 x  t  1. 2 use h  0. x(0)  1.

x(0)  1. use h  0.01 dt dx  1 2x2  t dt d 2 x(t ) dx 2  0  4 x  1   4 x (1  2 x 2  t ) 1 dt dt 2 h xi 1  xi  h(1  2 xi  ti )  (  1  4 xi (1  2 xi  ti )) 2 2 2 CISE301_Topic8L2 24 .Example 2 Use Second order Taylor Series method to solve : dx  2 x 2  t  1.

Example 2 f (t .9716 CISE301_Topic8L2 25 . h  0. t0  0.9901  0. x0  1.9901)(1  2(0.01(1  2(1) 2  0)  0.01)  0.01 2 (1  4(0.01 2 (1  4(1)(1  2  0))  0. x)  1  2 x 2  t .9807 2 Step 3 : x3  0.9901) 2  0.9901) 2  0.01(1  2(0.01))  0.9901 2 Step 2 : x2  0.01 2 h xi 1  xi  h(1  2 xi  ti )  (  1  4 xi (1  2 xi  ti )) 2 2 2 Step1 : x1  1  0.

01 0.02 0.9901 2 0. h  0.03 0.Example 2 f (t . x0  1. x)  1  2 x  t .9716 CISE301_Topic8L2 26 .01 Summary of the results: i ti xi 0 0. 2 t0  0.00 1 1 0.9807 3 0.

. i  1.2..Programming Euler Method Write a MATLAB program to implement Euler method to solve: dv  1  2v  t.01i. 2 v(0)  1 dt for ti  0.100 CISE301_Topic8L2 27 ...

V(1)=v. T(i+1)=t. for i=1:100 v=v+h*f(t.01 t=0 v=1 T(1)=t. V(i+1)=v. end CISE301_Topic8L2 28 .v) t=t+h.'t'.Programming Euler Method f=inline('1-2*v^2-t'.'v') h=0.

Programming Euler Method f=inline('1-2*v^2-t'. Euler method T(i+1)=t.'t'. V(i+1)=v. for i=1:100 Main loop v=v+h*f(t.'v') h=0.01 Definition of the ODE t=0 Initial condition v=1 T(1)=t. V(1)=v. Storing information end CISE301_Topic8L2 29 .v) t=t+h.

Programming Euler Method Plot of the solution plot(T.V) CISE301_Topic8L2 30 .

Lessons 4-5: Runge-Kutta methods Provide the accuracy of high order Taylor series method without the need to calculate high order derivative.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. CISE301_Topic8L2 31 .