Se301: Numerical Methods: Ordinary Differential Equations (Odes)

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

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
n
h k  d k y 

y ( x0  h )  
k !  dx k

k 0
 x  x0 , y  y 0 

dy h2 d 2 y hn d n y
 y ( x0 )  h  2
 ... 
dx x  x0 ,
y y
2! dx x  x0 , n! dx n x  x0 ,
0 y  y0 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  O(h 2 )
dx x  x0 ,
y  y0

Notation :
xn  x0  nh, yn  y ( xn ),
dy
 f ( xi , yi )
dx x  xi ,
y  yi

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

Euler Method :
y0  y ( x0 )
yi 1  yi  h f ( xi , yi ) for i  1,2,...

CISE301_Topic8L2 9
Interpretation of Euler Method
y2

y1

y0

x0 x1 x2 x

CISE301_Topic8L2 10
Interpretation of Euler Method

Slope=f(x0,y0)
y1
y1=y0+hf(x0,y0)
hf(x0,y0)
y0

x0 x1 x2 x
h

CISE301_Topic8L2 11
Interpretation of Euler Method
y2 y2=y1+hf(x1,y1)
Slope=f(x1,y1)
hf(x1,y1)
Slope=f(x0,y0)

y1 y1=y0+hf(x0,y0)
hf(x0,y0)
y0

x0 x1 x2 x
h h

CISE301_Topic8L2 12
Example 1
Use Euler method to solve the ODE:

dy 2
 1 x , y (1)  4
dx
to determine y(1.01), y(1.02) and y(1.03).

CISE301_Topic8L2 13
Example 1
2
f ( x, y )  1  x , 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


 
Step 2 : y2  y1  h f ( x1 , y1 )  3.98  0.01 1  1.01  3.9598
2

Step3 :  
y3  y2  h f ( x2 , y2 )  3.9598  0.01 1  1.02  3.9394
2

CISE301_Topic8L2 14
Example 1
2
f ( x, y )  1  x , 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
2
f ( x, y )  1  x , x0  1, y0  4 , h  0.01
Comparison with true value:
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

CISE301_Topic8L2 16
Example 1
2
f ( x, y )  1  x , 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  2
 O(h )
dx 2! dx
2
d y
2
needs to be derived analytically.
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  2
 3
 O(h )
dx 2! dx 3! dx
2 3
d y d y
2
and 3 need to be derived analytically.
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  2
 ....  n
 O(h )
dx 2! dx n! dx
2 3 n
d y d y d y
2
, 3 ,....., n need to be derived analytically.
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, use h  0.01
dt

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
 1 2x2  t
dt
d 2 x(t ) dx 2
2
 0  4 x  1   4 x (1  2 x  t ) 1
dt dt
2
2 h 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
2 h 2
xi 1  xi  h(1  2 xi  ti )  (  1  4 xi (1  2 xi  ti ))
2
Step 1 :

x1  1  0.01(1  2(1) 2
 0) 
0.01
2
(1  4(1)(1  2  0))  0.9901
2
Step 2 :

x2  0.9901  0.01(1  2(0.9901) 2


 0.01) 
0.01
2
( 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 2  t , t0  0, x0  1, h  0.01
Summary of the results:
i ti xi
0 0.00 1
1 0.01 0.9901
2 0.02 0.9807
3 0.03 0.9716

CISE301_Topic8L2 26
Programming Euler Method
Write a MATLAB program to implement
Euler method to solve:

dv 2
 1  2v  t . v(0)  1
dt

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 Definition of the ODE

t=0
Initial condition
v=1
T(1)=t;
V(1)=v;
for i=1:100 Main loop
v=v+h*f(t,v)
t=t+h; Euler method
T(i+1)=t;
V(i+1)=v; Storing information
end

CISE301_Topic8L2 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

You might also like