Chapter 7 Numerical Ordinary Differential Equations (ODEs)
Chapter 7 Numerical Ordinary Differential Equations (ODEs)
Methods
Chapter 7
Numerical Ordinary Differential
Equations (ODEs)
Objectives of Lecture 7
◼ Solve Ordinary Differential Equations
(ODEs).
◼ Appreciate the importance of numerical
methods in solving ODEs.
◼ Assess the reliability of the different
techniques.
◼ Select the appropriate method for any
particular problem.
Outline of Lecture 7
◼ Introduction to ODEs
◼ Taylor series methods (Focus on Euler Method)
◼ Runge-Kutta 2nd order methods
• Heun’s, Midpoint, and Ralston method
◼ Runge-Kutta 4th order methods
Introduction to ODEs
Learning Objectives
◼ Recall basic definitions of ODEs:
• Order
• Linearity
• Initial conditions
• Solution
◼ Classify ODEs based on:
• Order, linearity, and conditions.
◼ Classify the solution methods.
Derivatives
Derivatives
Partial Derivatives
Ordinary Derivatives
dv u
dt y
u is a function of
v is a function of one
more than one
independent variable
independent variable
Differential Equations
Differential
Equations
+ 6tv = 1 − = 0
dt 2 y 2
x 2
dv c
= 9 .8 − v
dt M
M : mass
c : drag coefficient
v : velocity
Definitions
Ordinary
differential
dv c equation
= 9 .8 − v
dt M
Definitions (Cont.)
dv c
= 9 .8 − v (Dependent
dt M variable)
unknown
function to be
determined
Definitions (Cont.)
dv c
= 9 .8 − v
dt M
(independent variable)
the variable with respect to which
other variables are differentiated
Order of a Differential Equation
The order of an ordinary differential equation is the order
of the highest order derivative.
Examples :
dx(t )
− x(t ) = e t First order ODE
dt
d 2 x(t ) dx(t )
2
−5 + 2 x(t ) = cos(t ) Second order ODE
dt dt
3
d x(t )
2
dx(t )
2
− + 2 x 4 (t ) = 1 Second order ODE
dt dt
Solution of a Differential Equation
A solution to a differential equation is a function that
satisfies the equation.
Example: −t
Solution x(t ) = e
dx(t )
+ x(t ) = 0 Proof :
dt
dx(t )
= −e −t
dt
dx(t )
+ x(t ) = −e −t + e −t = 0
dt
Linear ODE
An ODE is linear if:
-The unknown function and its derivatives appear to power one AKA a
Degree of 1.
- No product of the unknown function and/or its derivatives
Examples:
dx(t )
− x(t ) = e t
dt Linear ODE
d 2 x(t ) dx(t )
2
− 5 + 2t 2
x(t ) = cos(t ) Linear ODE
dt dt
3
d 2 x(t ) dx(t )
2
− + x(t ) = 1 Non-linear ODE
dt dt
Solutions of Ordinary
Differential Equations
x(t ) = cos(2t )
is a solution to the ODE
2
d x(t )
2
+ 4 x(t ) = 0
dt
Is it unique?
All functions of the form x(t ) = cos(2t + c)
(where c is a real constant) are solutions.
Uniqueness of a Solution
d 2 x (t )
+ 4 x (t ) = 0 Second order ODE
dt 2
x ( 0) = a Two conditions are
x (0) = b needed to uniquely
specify the solution
Auxiliary Conditions
Auxiliary Conditions
18
Boundary-Value and
Initial value Problems
Initial-Value Problems Boundary-Value Problems
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.
How to write Ordinary Differential
Equation
How does one write a first order differential equation in the form of:
= f ( x, y )
dy
dx
Example
In this case,
f (x, y ) = 1.3e − x − 2 y
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 + + ... +
dx xy==xy0 , 2! dx2 x = x0 , n! dxn x = x0 ,
0 y = y0 y = y0
= f ( x, y ), y (0 ) = y 0
dy True value
dx
y1, Predicted
value
Rise Φ
=
x0,y0
Slope
Run
y1 − y0
= Step size, h
x1 − x0
x
= f ( x0 , y 0 )
y1 = y0 + f (x0 , y 0 )(x1 − x0 )
Figure 1 Graphical interpretation of the first step of Euler’s method
= y0 + f (x0 , y0 )h
Euler’s Method
y
Φ
yi
h
Step size
x
xi xi+1
Euler Method :
y0 = y ( x0 )
yi +1 = yi + h f ( xi , yi ) for i = 1,2,...
Interpretation of Euler Method
y2
y1
y0
x0 x1 x2 x
Interpretation of Euler Method
Slope=f(x0,y0)
y1
y1=y0+hf(x0,y0)
hf(x0,y0)
y0
x0 x1 x2 x
h
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
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
d
dt
( )
= −2.206710−12 4 − 81108 , (0) = 1200K
Find the temperature at t = 480 seconds using Euler’s method. Assume a step size of
h = 240 seconds.
Solution
Step 1:
d
dt
(
= −2.2067 10−12 4 − 81 108 )
(
f (t , ) = −2.206710−12 4 − 81108 )
i +1 = i + f (ti , i )h
1 = 0 + f (t0 , 0 )h
= 1200 + f (0,1200)240
( (
= 1200 + − 2.2067 10−12 12004 − 81 108 240 ))
= 1200 + (− 4.5579)240
= 106.09K
1 is the approximate temperature at t = t1 = t0 + h = 0 + 240 = 240
(240) 1 = 106.09K
Solution Cont.
Step 2: For i = 1, t1 = 240, 1 = 106.09
2 = 1 + f (t1 , 1 )h
= 106.09 + f (240,106.09)240
( (
= 106.09 + − 2.206710−12 106.094 − 81108 240 ))
= 106.09 + (0.017595)240
= 110.32K
(480) = 647.57K
Comparison of Exact and
Numerical Solutions
1400
1200
Temperature, θ(K)
1000
Exact Solution
800
600
400
h=240
200
0
0 100 200 300 400 500
Time, t(sec)
1500
500
h=120
h=240
0
0 100 200 300 400 500
-500
Tim e, t (sec) h=480
-1000
-1500
Figure 4. Comparison of Euler’s method with exact solution for different step sizes
Effects of step size on Euler’s
Method
800
400
Temperature,θ(K)
0
0 100 200 300 400 500
-400
Step size, h (s)
-800
-1200
1 d2y 1 d3y
yi +1 = yi +
dy
(xi +1 − xi ) + (xi +1 − xi ) 2
+ (xi +1 − xi )3 + ...
dx xi , yi 2! dx 2 xi , yi
3! dx 3 xi , yi
2! 3!
As you can see the first two terms of the Taylor series
dy
y ( x0 + h) = y ( x0 ) + h + O(h 2 )
dx xy==xy0 ,
0
Notation:
xn = x0 + nh, yn = y ( xn ),
dy
= f ( xi , yi )
dx xy==xyi ,
i
Euler Method
yi +1 = yi + h f ( xi , yi )
Runge 2nd Order Method
Runge-Kutta 2nd Order Method
Includes only three terms of the Taylor series as follows:
= y i + f ( xi , y i )h + f ( xi , y i )h 2
1
y i +1 (1)
2!
dy
For = f ( x, y ), y (0) = y0
dx
Runge Kutta 2nd order method is given by
yi +1 = yi + (a1k1 + a2 k 2 )h (2)
where
k1 = f (xi , yi )
k2 = f (xi + p1h, yi + q11k1h )
Equating Equation (1) and (2) , gives the following three equations:
a1 + a2 = 1
1
a 2 p1 =
2
1
a 2 q11 =
2
Heun’s Method
Here a2=1/2 is chosen, giving
1
a1 =
2
p1 = 1
q11 = 1
resulting in
1 1
yi +1 = yi + k1 + k2 h
2 2
where
k1 = f (xi , y i )
k 2 = f (xi + h, yi + k1 h )
Midpoint Method
Here a2 = 1 is chosen, giving
a1 = 0
1
p1 =
2
1
q11 =
2
resulting in
yi +1 = yi + k2h
where
k1 = f (xi , yi )
1 1
k 2 = f x i + h , y i + k1 h
2 2
Ralston’s Method
Here a2 = 2 is chosen, giving
3
1
a1 =
3
3
p1 =
4
3
q11 =
4
resulting in
1 2
yi +1 = yi + k1 + k 2 h
3 3
where
k1 = f (xi , yi )
3 3
k 2 = f x i + h , y i + k1 h
4 4
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
d
dt
( )
= −2.2067 10−12 4 − 81 108 , (0 ) = 1200K
Find the temperature at t = 480 seconds using Heun’s method. Assume a step size of
h = 240 seconds.
d
dt
(
= −2.2067 10−12 4 − 81 108 )
(
f (t , ) = −2.206710−12 4 − 81108 )
1 1
i +1 = i + k1 + k 2 h
2 2
Solution
1 1
2 = 1 + k1 + k 2 h
2 2
1
= 655.16 + (− 0.38869) + (− 0.20206)240
1
2 2
= 655.16 + (− 0.29538)240
= 584.27K
Solution Cont.
− 300
0.92593ln − 1.8519 tan −1 (0.0033333 ) = −0.22067 10−3 t − 2.9282
+ 300
(480) = 647.57K
Comparison with exact results
1200
Temperature, θ(K)
Exact h=120
800
h=240
400
h=480
0
0 100 200 300 400 500
-400
Time, t(sec)
600
400
200
0
0 100 200 300 400 500
-200 Step size, h
-400
t %
Step size,
h Euler Heun Midpoint Ralston
1100
Temperature, θ(K)
1000 Midpoint
900 Ralston
800 Heun
700
Analytical
600
Euler
500
0 100 200 300 400 500 600
Time, t (sec)
Figure 4. Comparison of Euler and Runge Kutta 2nd order methods with
exact results.
Runge 4th Order Method
Runge-Kutta 4th Order Method
- Includes only five terms of the Taylor series as follows:
- Equating Equation (1) and (2), one of the popular solutions is:
Find the temperature at t = 480 seconds using Runge-Kutta 4th order method.
( )
k1 = f (t0 ,o ) = f (0,1200) = −2.206710−12 12004 − 81108 = −4.5579
k 2 = f t0 + h, 0 + k1h = f 0 + (240),1200 + (− 4.5579)240
1 1 1 1
2 2 2 2
( )
= f (120,653.05) = −2.2067 10−12 653.054 − 81 108 = −0.38347
k3 = f t0 + h, 0 + k 2 h = f 0 + (240),1200 + (− 0.38347)240
1 1 1 1
2 2 2 2
( )
= f (120,1154.0 ) = 2.2067 10−12 1154.0 4 − 81 108 = −3.8954
t = t1 = t0 + h = 0 + 240 = 240
(240) 1 = 675.65K
Solution Cont.
Step 2: i = 1, t1 = 240,1 = 675.65K
( )
k1 = f (t1 ,1 ) = f (240,675.65) = −2.206710−12 675.654 − 81108 = −0.44199
k 2 = f t1 + h, 1 + k1h = f 240 + (240), 675.65 + (− 0.44199)240
1 1 1 1
2 2 2 2
( )
= f (360,622.61) = −2.206710−12 622.614 − 81 108 = −0.31372
k3 = f t1 + h, 1 + k 2 h = f 240 + (240),675.65 + (− 0.31372)240
1 1 1 1
2 2 2 2
( )
= f (360, 638.00) = 2.2067 10−12 638.004 − 81108 = −0.34775
(480) 2 = 594.91K
Solution Cont
(480) = 647.57K
Comparison with exact results
1600
Temperature, θ(K)
1200
h=120
800 Exact
h=240
400
h=480
0
0 200 400 600
-400
Time,t(sec)
600
400
200
0
0 100 200 300 400 500
-200
Step size, h
1200
4th order
1000
800
Exact
600
Heun
400
200 Euler
0
0 100 200 300 400 500
Time, t(sec)
= f ( x, y ), y (0) = y0
dy
(1)
dx
dny d n −1 y
+ ao y = f (x )
dy
a n n + a n −1 n −1 + + a1 (2)
dx dx dx
y = z1 (3.1)
dy dz1
= = z2 (3.2)
dx dx
Higher Order Equations
ODE
d 2 y dz2
2
= = z3 (3.3)
dx dx
d n −1 y dzn −1
n −1
= = zn (3.n)
dx dx
d n y dzn
=
dxn dx
1 d n −1 y
− a0 y + f ( x )
dy
= − an −1 n −1 − a1
an dx dx
(3.n+1)
=
1
(− a n−1 z n − a1 z 2 − a0 z1 + f (x ))
an
Higher Order Equations
ODE
The Equations from (3.1) to (3.n+1) represent n first order differential
equations as follows
dz1
= z 2 = f1 (z1 , z 2 ,, x ) (4.1)
dx
dz2
= z 3 = f 2 (z1 , z 2 ,, x ) (4.2)
dx
dzn
=
1
(− an−1 z n − a1 z 2 − a0 z1 + f (x )) (4.n)
dx an
Each of the first order ordinary differential equations are accompanied by one
initial condition. These first order ordinary differential equations are simultaneous
in nature but can be solved by the methods used for solving first order ordinary
differential equations that we have already learned.
Example
Rewrite the following differential equation as a set of first order differential
equations.
d2y
3 2 + 2 + 5 y = e − x , y (0) = 5, y(0) = 7
dy
dx dx
Solution
The ordinary differential equation would be rewritten as follows. Assume
dy
= z,
dx
Then
d 2 y dz
2
=
dx dx
Substituting this in the given second order ordinary differential equation gives
= z, y (0) = 5
dy
dx
Solution Cont.
= (e − 2 z − 5 y ), z (0) = 7
dz 1 − x
dx 3
Now one can apply any of the numerical methods used for solving first order
ordinary differential equations.
Example
Given
d2y
2
+ 2
dy
+ y = e −t
, y (0 ) = 1,
dy
(0) = 2 , find by Euler’s method
dt dt dt
(a) y(0.75)
(b) the absolute relative true error for part(a), if y(0.75) exact = 1.668
(c)
dy
(0.75)
dt
dy
=z
dt
then
dz
+ 2 z + y = e −t
dt
dz
= e −t − 2 z − y
dt
Solution Cont.
So the two simultaneous first order differential equations are
= z = f1 (t,y,z ), y (0) = 1
dy
(E2.1)
dt
= e −t − 2 z − y = f 2 (t , y , z ), z (0) = 2
dz
(E2.2)
dt
yi +1 = yi + f1 (t i , yi , z i )h (E2.3)
z i +1 = z i + f 2 (t i , yi , z i )h (E2.4)
Solution Cont.
a) To find the value of y(0.75) and since we are using a step size of 0.25 and
starting at t = 0 , we need to take three steps to find the value of y(0.75).
For i = 0, t0 = 0, y0 = 1, z0 = 2
y1 = y 0 + f1 (t 0 , y 0 , z 0 )h
= 1 + f1 (0,1,2)(0.25)
= 1 + 2(0.25)
= 1.5
Solution Cont.
y1 is the approximate value of y at
t = t1 = t 0 + h = 0 + 0.25 = 0.25
y1 = y(0.25) 1.5
z1 = z 0 + f 2 (t 0 , y 0 , z 0 )h
= 2 + f 2 (0,1,2)(0.25)
(
= 2 + e −0 − 2(2) − 1 (0.25) )
=1
dy
Z1 is the approximate value of Z (same as ) at t =0.25
dt
z1 = z(0.25) 1
Solution Cont.
For i = 1, t1 = 0.25, y1 = 1.5, z1 = 1
= 1.5 + f1 (0.25,1.5,1)(0.25)
= 1.5 + (1)(0.25)
= 1.75
y2 is the approximate value of y at
y2 = y(0.5) 1.75
Solution Cont.
From Equation (E2.4)
z 2 = z1 + f 2 (t1 , y1 , z1 )h
= 1 + f 2 (0.25,1.5,1)(0.25)
= 1 + (e −0.25 − 2(1) − 1.5)(0.25)
= 1 + (− 2.7211)(0.25)
= 0.31970
t = t 2 = 0.5
z2 = z(0.5) 0.3197
Solution Cont.
For i = 2, t2 = 0.5, y2 = 1.75, z2 = 0.31970
y3 = y 2 + f1 (t 2 , y 2 , z 2 )h
= 1.75 + f1 (0.50,1.75,0.31970)(0.25)
= 1.75 + (0.31970)(0.25)
= 1.8299
y3 = y(0.75) 1.8299
Solution Cont.
From Equation (E2.4)
z 3 = z 2 + f 2 (t 2 , y 2 , z 2 )h
= 0.31972+ f 2 (0.50,1.75,0.31970)(0.25)
= 0.31972+ (− 1.7829)(0.25)
= −0.1260
Z3 is the approximate value of Z at
t = t 3 = 0.75
z 3 = z (0.75) −0.12601
y (0.75) y3 = 1.8299
Solution Cont.
b) The exact value of y(0.75) is
The absolute relative true error in the result from part (a) is
1.668 − 1.8299
t = 100
1.668
= 9.7062%
c)
dy
(0.75) = z 3 −0.12601
dx
The End