1
School of Chemical Engineering and Advanced Materials
Newcastle University
NUMERICAL SOLUTIONS
OF
ORDINARY DIFFERENTIAL EQUATIONS
School of Chemical Engineering and Advanced Materials
Newcastle University
2
ODEs in Chemical Engineering
•
The ODEs that occur in Chemical Engineering tend to be of the following form:
•
Following the previous steps, the solution is provided by:
•
Numerical methods based on “rectangular” and “trapezoidal” rules cannot be used
Problem:
Solution of x depends on x !
( )
( , )
dx t
f x t
dt
=
( ) ( , ).dx t f x t dt=
( ) ( ) ( , ).x t dx t f x t dt= =
∫ ∫
School of Chemical Engineering and Advanced Materials
Newcastle University
3
Integration via Taylor Series Expansion
•
Alternative methods have to be employed and all are based on the Taylor Series
•
Note: any function can be expanded as a Taylor Series
•
The series can be used to solve
Taylor Series expansion of x(t) about t
0
' 2 ''
0 0 0 0 0
3 ''' 4 (4)'
0 0 0 0
1
( ) ( ) ( ) ( ) ( ) ( )
2
1 1
( ) ( ) ( ) ( )
3! 4!
x t x t t t x t t t x t
t t x t t t x t
= + − + −
+ − + − +K
( )
( , )
dx t
f x t
dt
=
School of Chemical Engineering and Advanced Materials
Newcastle University
4
Example: solution via Taylor Series
t 0 1 2 3 …
x(t) x(0) x(1) x(2) x(3)
…
Given initial conditions:
( , ) '( )
dx
f x t x t
dt
= =
x t x t t t x t t t x t( ) ( ) ( ) ( ) ( ) ( )
' ''
= + − + − +
0 0 0 0
2
0
1
2
' 2 ''
0
1
( ) (0) . (0) . (0) , 0
2
x t x t x t x t= + + + =K
' ''
(0), (0), (0)x x x
School of Chemical Engineering and Advanced Materials
Newcastle University
5
Solution using Taylor Series
•
Accuracy of this solution is dependent on the ‘order’ of the Taylor Series expansion
•
Need to have the higher order differentials of x(t) to maintain accuracy
–
may not be available due to the complexity of the original equation
•
Method is not iterative and thus not amenable for efficient implementation on a computer
•
Nevertheless, the Taylor Series approach is the basis for all numerical ODE solution procedures
x t x t t t x t t t x t( ) ( ) ( ) ( ) ( ) ( )
' ''
= + − + − +
0 0 0 0
2
0
1
2
School of Chemical Engineering and Advanced Materials
Newcastle University
6
Euler’s Method: derivation
•
Euler’s Method is based on the first-order Taylor Series
At t = t
1
, the value of x is:
At t = t
2
, the value of x is:
At t = t
3
, the value of x is:
and so on …
'
0 0 0
ˆ
( ) ( ) ( ) ( ) ( )x t x t x t t t x t≈ = + −
x t x t x t t t x t( )
( ) ( ) ( ) ( )
'
1 1 0 1 0 0
≈ = + −
x t x t x t t t x t( )
( ) ( ) ( ) ( )
'
2 2 0 2 0 0
≈ = + −
x t x t x t t t x t( )
( ) ( ) ( ) ( )
'
3 3 0 3 0 0
≈ = + −
School of Chemical Engineering and Advanced Materials
Newcastle University
7
Euler’s Method: derivation
•
Note that all the values of x have
been calculated using a fixed
reference, t
0
.
x(t)
t
t
0
t
1
t
2
x(t
1
)
x(t
2
)
x’(t
0
)
x(t
0
)
x t x t x t t t x t( )
( ) ( ) ( ) ( )
'
1 1 0 1 0 0
≈ = + −
x t x t x t t t x t( )
( ) ( ) ( ) ( )
'
2 2 0 2 0 0
≈ = + −
x t x t x t t t x t( )
( ) ( ) ( ) ( )
'
3 3 0 3 0 0
≈ = + −
School of Chemical Engineering and Advanced Materials
Newcastle University
8
Euler’s Method: derivation
x(t)
t
t
0
t
1
t
2
x(t
1
)
x(t
2
)
x’(t
0
)
x(t
0
)
•
The gradient at the reference point, t
0
, is used
to project to another value of x
•
Assumes that the function is a straight line over
the interval
x t x t x t t t x t( )
( ) ( ) ( ) ( )
'
1 1 0 1 0 0
≈ = + −
x t x t x t t t x t( )
( ) ( ) ( ) ( )
'
2 2 0 2 0 0
≈ = + −
'
1 0
0
1 0
( ) ( )
( )
( )
x t x t
x t
t t
−
=
−
'
2 0
0
2 0
( ) ( )
( )
( )
x t x t
x t
t t
−
=
−
School of Chemical Engineering and Advanced Materials
Newcastle University
9
Euler’s Method: derivation
•
The use of a fixed reference point can lead to large errors
•
Because of the fixed reference point, the interval keeps on changing and this leads to computational
inefficiency
•
Euler’s Method attempts to overcome these deficiencies by:
–
having a moving reference each time a solution value is calculated
–
the solution evolves over constant intervals
School of Chemical Engineering and Advanced Materials
Newcastle University
10
Euler’s Method: derivation
•
Instead of
•
Use:
etc.
etc.
x t x t x t t t x t( )
( ) ( ) ( ) ( )
'
1 1 0 1 0 0
≈ = + −
x t x t x t t t x t( )
( ) ( ) ( ) ( )
'
2 2 0 2 0 0
≈ = + −
x t x t x t t t x t( )
( ) ( ) ( ) ( )
'
3 3 0 3 0 0
≈ = + −
'
1 1 0 0
ˆ
( ) ( ) ( ) . ( )x t x t x t t x t≈ = + ∆
'
2 2 1 1
ˆ
( ) ( ) ( ) . ( )x t x t x t t x t≈ = + ∆
'
3 3 2 2
ˆ
( ) ( ) ( ) . ( )x t x t x t t x t≈ = + ∆
'
ˆ ˆ
( ) ( ) . ( )x t t x t t x t+ ∆ = + ∆
School of Chemical Engineering and Advanced Materials
Newcastle University
11
Euler’s Method: geometric interpretation
x(t)
t
t
0
t
1
t
2
x(t
1
)
x(t
2
)
x’(t
0
)
x(t
0
)
∆t
∆t
x’(t
1
)
x’(t
2
)
∆t
t
3
x(t
3
)
'
ˆ ˆ
( ) ( ) . ( )x t t x t t x t+ ∆ = + ∆
School of Chemical Engineering and Advanced Materials
Newcastle University
12
Euler’s Method: implementation aspects
•
The formula for Euler’s Method:
is often written as:
•
is always given because it is the
equation that has to be solved
•
is the integration interval and has to be specified
•
Initial conditions are required before the solution can proceed
'
ˆ ˆ
( ) ( ) . ( )x t t x t t x t+ ∆ = + ∆
ˆ ˆ ˆ
( ) ( ) . ( ( ), )x t h x t h f x t t+ = +
( )
( ( ), )
dx t
f x t t
dt
=
h t= ∆
School of Chemical Engineering and Advanced Materials
Newcastle University
13
Euler’s Method: accuracy
•
Euler’s Method can be applied to a wide range of ODEs
•
However, except for the simplest of problems, the integration interval has to be small for accuracy
•
Time to solution becomes longer when integration intervals are too small,
•
There have been many attempts to improve the efficiency and accuracy of the basic Euler’s Method,
resulting in a whole family of algorithms for the numerical solution of ODEs
–
Improved Euler’s Method
–
Modified Euler’s Method
–
Runge-Kutta algorithms
School of Chemical Engineering and Advanced Materials
Newcastle University
14
Improved Euler’s Method
•
The basic Euler’s Method formula is changed to:
•
Problem: requires which has yet to be calculated
Average of 2 gradients
one evaluated at t and the other at t+h
( ) ( )
ˆ ˆ
( ), ( ),
ˆ ˆ
( ) ( )
2
f x t t f x t h t h
x t h x t h
+ + +
+ = +
ˆ
( )x t h+
School of Chemical Engineering and Advanced Materials
Newcastle University
15
Improved Euler’s Method
•
Since is unknown, cannot be evaluated
•
To overcome this problem, perform a first estimate using the basic Euler’s Method
•
Then use in:
( )
( )
( )
( ),x t h x t hf x t t
1
+ = +
( )
( )
( )
( )
( ),
( ),
x t h x t h
f x t t f x t h t h
+ = +
+ + +
1
2
( ) ( )
ˆ ˆ
( ), ( ),
ˆ ˆ
( ) ( )
2
f x t t f x t h t h
x t h x t h
+ + +
+ = +
ˆ
( )x t h+
( )
ˆ
( ),f x t h t h+ +
School of Chemical Engineering and Advanced Materials
Newcastle University
16
Improved Euler’s Method: geometric interpretation
x(t)
t
t
0
x(t
0
)
h=∆t
t
1
( ) ( )
1
ˆ ˆ
( ), ( ),
ˆ ˆ
( ) ( )
2
f x t t f x t h t h
x t h x t h
+ + +
+ = +
( )
1
ˆ ˆ ˆ
( ) ( ) ( ),x t h x t hf x t t+ = +
1 1
ˆ
( )x t
( )
1 1 1
ˆ
( ),f x t t
( )
0 0
( ),f x t t
( ) ( )
0 0 1 1 1
ˆ
( ), ( ),
2
f x t t f x t t+
1
ˆ
( )x t
School of Chemical Engineering and Advanced Materials
Newcastle University
17
Modified Euler’s Method
•
Is another variation of the basic Euler’s Method with the aim of improving accuracy
•
It makes use of a gradient evaluated between integration intervals to generate the solutions to the
underlying ODE
•
Problem: again, the formula requires which has yet to be calculated
ˆ ˆ
( ) ( )
ˆ ˆ
( ) ( ) ,
2 2
x t x t h h
x t h x t hf t
+ +
+ = + +
÷
ˆ
( )x t h+
School of Chemical Engineering and Advanced Materials
Newcastle University
18
Modified Euler’s Method
•
The unknown is first estimated using the basic Euler’s formula:
( )
( )
( )
( )
,x t h x t hf
x t x t h
t
h
+ = +
+ +
+
1
2 2
( )
( )
( )
( ),x t h x t hf x t t
1
+ = +
ˆ ˆ
( ) ( )
ˆ ˆ
( ) ( ) ,
2 2
x t x t h h
x t h x t hf t
+ +
+ = + +
÷
ˆ
( )x t h+
School of Chemical Engineering and Advanced Materials
Newcastle University
19
Modified Euler’s Method: geometric interpretation
x(t)
t
t
0
x(t
0
)
h=∆t
t
1
h/2
( )
1
ˆ ˆ ˆ
( ) ( ) ( ),x t h x t hf x t t+ = +
1 1
ˆ
( )x t
( )
0 0
( ),f x t t
1
ˆ ˆ
( ) ( )
ˆ ˆ
( ) ( ) ,
2 2
x t x t h
h
x t h x t hf t
+ +
+ = + +
÷
0 1
ˆ
( ) ( )
,
2 2
x t x t
h
f t
+
+
÷
0 1 1
ˆ
( ) ( )
2
x t x t+
1
ˆ
( )x t
School of Chemical Engineering and Advanced Materials
Newcastle University
20
Euler’s Methods and variations
Euler’s Method
Improved
Euler’s Method
Modified
Euler’s Method
ˆ ˆ ˆ
( ) ( ) . ( ( ), )x t h x t h f x t t+ = +
( )
( )
( )
( ),x t h x t hf x t t
1
+ = +
( )
( )
( )
( )
( ),
( ),
x t h x t h
f x t t f x t h t h
+ = +
+ + +
1
2
( )
( )
( )
( )
,x t h x t hf
x t x t h
t
h
+ = +
+ +
+
1
2 2
( )
( )
( )
( ),x t h x t hf x t t
1
+ = +
School of Chemical Engineering and Advanced Materials
Newcastle University
21
General form of ODE solution methods
•
New value
= old value + integration interval × function of gradient
•
This is the basis for a family of algorithms used to provide numerical solutions of ODEs
•
A particular class is the Runge-Kutta algorithms
[ ]
ˆ ˆ ˆ
( ) ( ) ( ( ), )x t h x t h function f x t t+ = + ×
School of Chemical Engineering and Advanced Materials
Newcastle University
22
Runge-Kutta (RK) Algorithms
•
approximates the terms
•
are coefficients
•
The order of the solution is given by ‘n’
•
Theoretically, the higher the order of the solution, the more accurate is the method
1
ˆ ˆ
( ) ( )
n
i i
i
x t h x t a k
=
+ = +
∑
, 1, 2, ,
i
k i n= K
hf (.)
, 1, 2, ,
i
a i n= K
School of Chemical Engineering and Advanced Materials
Newcastle University
23
Special cases of RK algorithms
General form of RK algorithms
Basic Euler’s Method
Improved
Euler’s Method
Modified
Euler’s Method
1
ˆ ˆ
( ) ( )
n
i i
i
x t h x t a k
=
+ = +
∑
1 1
ˆ
1, ( ( ), )If a k hf x t t= =
1 2
1 1 1
1
;
2
ˆ ˆ
( ( ), ); ( ( ) , )
If a a
k hf x t t k hf x t k t h
= =
= = + +
1 2
1
1 2
0; 1;
ˆˆ
( ( ), ); ( ( ) , )
2 2
If a a
k
h
k h f x t t k h f x t t
= =
= = + +
School of Chemical Engineering and Advanced Materials
Newcastle University
24
RK4 Algorithm (4
th
order method)
( )
( )
( )x t h x t k k k k+ = + + + +
1
6
2 2
1 2 3 4
( )
k hf x t t
1
=
( ),
k hf x t
k
t
h
2
1
2 2
= + +
( ) ,
k hf x t
k
t
h
3
2
2 2
= + +
( ) ,
( )
k hf x t k t h
4 3
= + +
( ) ,
School of Chemical Engineering and Advanced Materials
Newcastle University
25
Solving sets of 1
st
order ODEs
•
Chemical engineering systems are often described by more than 1 ODE, e.g. dynamic material and
energy balances
•
At each integration interval, the equations are solved one at a time, using the same methods as for
single ODEs,
dx
dt
f t x i n
i
1
1
1 2= =( , , , , )
dx
dt
f t x i n
i
2
2
1 2= =( , , , , )
dx
dt
f t x i n
n
n i
= =( , , , , )1 2