You are on page 1of 42

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
Scope
Integration
Numerical Integration
Solution of ODEs
solution via Taylor Series
Eulers Method
improving the accuracy of Eulers Method
higher order solution procedures
solving sets of ODEs
solving high-order ODEs
stability of solutions
Examples
School of Chemical Engineering and Advanced Materials
Newcastle University
3
Integration: concept
Given a function
A pair of limits
Find the area
bounded by the
function and
the limits

f(x)
x
School of Chemical Engineering and Advanced Materials
Newcastle University
4
Error
Error
Numerical Integration: rectangular rule
Use sum of areas
of rectangles to
get approximate
solution
Accuracy
depends on width
of rectangles
(integration
interval/step size)
Large errors if
integration
intervals are large
Integration interval
or
Integration step size
Integration interval
or
Integration step size
f(x)
x
School of Chemical Engineering and Advanced Materials
Newcastle University
5
f(x)
x
Numerical Integration: rectangular rule
Errors can be
reduced by
specifying smaller
integration intervals
Have to use more
rectangles which
means more
calculations
Takes longer to get
answer
Compromise
between accuracy
and efficiency
School of Chemical Engineering and Advanced Materials
Newcastle University
6
Error
Error
Numerical Integration: trapezoidal rule
Uses trapezoids
instead of
rectangles
Approximate
integral is sum of
areas of
trapezoids
Error is reduced
even though
integration
interval is the
same
Integration interval
or
Integration step size
Integration interval
or
Integration step size
f(x)
x
School of Chemical Engineering and Advanced Materials
Newcastle University
7
Integration
( )
( )
dx t
f t
dt
=
( ) ( ). dx t f t dt =
( ) ( ) ( ). x t dx t f t dt = =
} }
Solving an integration problem is equivalent to solving the
underlying ordinary differential equation (ODE)
School of Chemical Engineering and Advanced Materials
Newcastle University
8
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
( )
( , )
dx t
f x t
dt
=
( ) ( , ). dx t f x t dt =
( ) ( ) ( , ). x t dx t f x t dt = =
} }
Problem:
Solution of x depends on x !
School of Chemical Engineering and Advanced Materials
Newcastle University
9
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
' 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
= + +
+ + +
( )
( , )
dx t
f x t
dt
=
Taylor Series expansion of x(t) about t
0
School of Chemical Engineering and Advanced Materials
Newcastle University
10
Example: solution via Taylor Series
( , ) '( )
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 = + + + =
t 0 1 2 3
x(t) x(0) x(1) x(2) x(3)
Given initial conditions:
' ''
(0), (0), (0) x x x
School of Chemical Engineering and Advanced Materials
Newcastle University
11
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
12
Eulers Method: derivation
Eulers Method is based on the first-order Taylor Series
'
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
~ = +
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
School of Chemical Engineering and Advanced Materials
Newcastle University
13
Eulers Method: derivation
Note that all the
values of x have
been calculated
using a fixed
reference, 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
~ = +
x(t)
t
t
0
t
1
t
2
x(t
1
)
x(t
2
)
x(t
0
)
x(t
0
)
School of Chemical Engineering and Advanced Materials
Newcastle University
14
Eulers Method: derivation
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
~ = +
'
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

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

School of Chemical Engineering and Advanced Materials
Newcastle University
15
Eulers 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
Eulers 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
16
Eulers Method: derivation
Instead of




Use:

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 ~ = + A
'
2 2 1 1
( ) ( ) ( ) . ( ) x t x t x t t x t ~ = + A
'
3 3 2 2
( ) ( ) ( ) . ( ) x t x t x t t x t ~ = + A
etc.
etc.
'
( ) ( ) . ( ) x t t x t t x t + A = + A
School of Chemical Engineering and Advanced Materials
Newcastle University
17
Eulers Method: geometric interpretation
x(t)
t t
0
t
1
t
2
x(t
1
)
x(t
2
)
x(t
0
)
x(t
0
)
At At
x(t
1
)
x(t
2
)
At
t
3
x(t
3
)
'
( ) ( ) . ( ) x t t x t t x t + A = + A
School of Chemical Engineering and Advanced Materials
Newcastle University
18
Eulers Method: implementation aspects
The formula for Eulers 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 + A = + A
( ) ( ) . ( ( ), ) x t h x t h f x t t + = +
( )
( ( ), )
dx t
f x t t
dt
=
h t = A
School of Chemical Engineering and Advanced Materials
Newcastle University
19
Eulers Method: accuracy
Eulers 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 Eulers Method, resulting in a
whole family of algorithms for the numerical solution of
ODEs
Improved Eulers Method
Modified Eulers Method
Runge-Kutta algorithms
School of Chemical Engineering and Advanced Materials
Newcastle University
20
Improved Eulers Method
The basic Eulers Method formula is changed to:






Problem: requires which has yet to be calculated
( ) ( )
( ), ( ),
( ) ( )
2
f x t t f x t h t h
x t h x t h
+ + + (
+ = +
(

Average of 2 gradients
one evaluated at t and the other at t+h
( ) x t h +
School of Chemical Engineering and Advanced Materials
Newcastle University
21
Improved Eulers Method
Since is unknown, cannot be
evaluated
To overcome this problem, perform a first estimate using
the basic Eulers 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
22
Improved Eulers Method: geometric interpretation
x(t)
t
( ) ( )
1
( ), ( ),
( ) ( )
2
f x t t f x t h t h
x t h x t h
+ + + (
+ = +
(

t
0
x(t
0
)
h=At
t
1
( )
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
23
Modified Eulers Method
Is another variation of the basic Eulers 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
24
Modified Eulers Method
The unknown is first estimated using the basic
Eulers 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
25
Modified Eulers Method: geometric interpretation
x(t)
t t
0
x(t
0
)
h=At
t
1
( )
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 +
h/2
1
( ) x t
School of Chemical Engineering and Advanced Materials
Newcastle University
26
Eulers Methods and variations
( ) ( ) . ( ( ), ) 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
+ = +
Eulers Method
Improved
Eulers Method
Modified
Eulers Method
School of Chemical Engineering and Advanced Materials
Newcastle University
27
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
28
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 =
hf (.)
, 1, 2, ,
i
a i n =
School of Chemical Engineering and Advanced Materials
Newcastle University
29
Special cases of RK algorithms
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 1
0; 1;
( ( ), ); ( ( ) , )
2 2
If a a
k h
k hf x t t k hf x t t
= =
= = + +
General form of RK algorithms
Basic Eulers Method
Improved
Eulers Method
Modified
Eulers Method
School of Chemical Engineering and Advanced Materials
Newcastle University
30
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
31
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
12 = = ( , , , , )
dx
dt
f t x i n
i
2
2
12 = = ( , , , , )

dx
dt
f t x i n
n
n i
= = ( , , , , ) 12
School of Chemical Engineering and Advanced Materials
Newcastle University
32
Solving higher order ODEs
All numerical ODE solution methods are designed for
applications to 1
st
order ODES
Therefore, the mth order ODE must be reduced,
somehow, to an equivalent set of 1
st
order ODEs
This can be achieved by defining some new variables
( )
d x
dt
f t x x x x x
m
m
m
=

, , ' , ' ' , , ,
( ) ( ) 3 1

mth order ODE


School of Chemical Engineering and Advanced Materials
Newcastle University
33
Formulating a high order ODE problem
Define m new variables:
x t x t
1
( ) ( ) =
x t x t
2
( ) ' ( ) =
x t x t
3
( ) ' ' ( ) =
x t x t
m
m
( ) ( )
( )
=
1
( )
d x
dt
f t x x x x x
m
m
m
=

, , ' , ' ' , , ,
( ) ( ) 3 1

mth order ODE


dx
dt
x t
1
2
= ( )
dx
dt
x t
2
3
= ( )
) (
1
t x
dt
dx
m
m
=

( )
dx
dt
f t x x x x
m
m
= , , , , ,
1 2 3

m 1
st
order ODEs
School of Chemical Engineering and Advanced Materials
Newcastle University
34
Instability of numerical solutions of ODEs
Can occur due to propagation of errors (truncation of
Taylor series)
Stability is improved by use of small step sizes
choose step size to be <10% of smallest time constant
stable solution does not mean accurate solution
Further difficulty when considering sets of ODEs
sets of ODEs can contain states which have vastly different
speeds of responses
known as stiff set of equations
to improve stability of solution
choose step-size to match fastest dynamics
apply adaptive step-size algorithms, e.g. Kutta-Merson
algorithm, Gears method
School of Chemical Engineering and Advanced Materials
Newcastle University
35
Example:
( )
10 ( ) 2 ( )
in
dh t
h t Q t
dt
+ =
where h(t) = level of liquid in the tank at time t
Q
in
(t) = flowrate of liquid into the tank at time t

Initial conditions: h(0) = 10 cm, Q
in
(0)= 5 cm
3
s
-1
The relationship between the level in a
tank and liquid inflow is given by the
following ODE:
Q
in
Q
out
h
School of Chemical Engineering and Advanced Materials
Newcastle University
36
Example: solution procedure
( )
10 ( ) 2 ( )
in
dh t
h t Q t
dt
+ =
2 ( ) ( ) ( )
10
in
Q t h t dh t
dt

=
( )

( ) ( )
dh t
h t t h t t
dt
+ A = + A
2 ( ) ( )

( ) ( )
10
in
Q t h t
h t t h t t

(
+ A = + A
(

Select a value for = 1 (<10% of time-constant) t A
t 0 2 3 .
h(t) h(0) h(1) h(2) h(3) .
t A t A t A
0 10 20 30 40 50
10
11
12
13
14
15
16
17
18
19
20
Plot graph
E.g. Apply Eulers formula
Given
School of Chemical Engineering and Advanced Materials
Newcastle University
37
Example: results using At = 0.5
t Qin Eulers I. Eulers M. Eulers Analytical
4.0000 5.0000 10.0000 10.0000 10.0000 10.0000
4.5000 5.0000 10.0000 10.0000 10.0000 10.0000
5.0000 10.0000 10.5000 10.4875 10.4875 10.0000
5.5000 10.0000 10.9750 10.9512 10.9512 10.4877
6.0000 10.0000 11.4263 11.3924 11.3924 10.9516
6.5000 10.0000 11.8549 11.8120 11.8120 11.3929
7.0000 10.0000 12.2622 12.2111 12.2111 11.8127
48.0000 10.0000 19.8847 19.8707 19.8707 19.8709
48.5000 10.0000 19.8904 19.8770 19.8770 19.8772
49.0000 10.0000 19.8959 19.8830 19.8830 19.8832
49.5000 10.0000 19.9011 19.8887 19.8887 19.8889
School of Chemical Engineering and Advanced Materials
Newcastle University
38
Example: results using At = 0.5
0 5 10 15 20 25 30 35 40 45 50
10
11
12
13
14
15
16
17
18
19
20
t
h
(
t
)
Euler's
Improved Euler's
Modified Euler's
Analytical
School of Chemical Engineering and Advanced Materials
Newcastle University
39
Example: results using At = 2
t Q
in
Eulers I. Eulers M. Eulers Analytical
0 5.0000 10.0000 10.0000 10.0000 10.0000
2.0000 5.0000 10.0000 10.0000 10.0000 10.0000
4.0000 5.0000 10.0000 10.0000 10.0000 10.0000
6.0000 10.0000 12.0000 11.8000 11.8000 12.5918
8.0000 10.0000 13.6000 13.2760 13.2760 13.9347
10.0000 10.0000 14.8800 14.4863 14.4863 15.0341
38.0000 10.0000 19.7748 19.6574 19.6574 19.6980
40.0000 10.0000 19.8199 19.7190 19.7190 19.7528
42.0000 10.0000 19.8559 19.7696 19.7696 19.7976
44.0000 10.0000 19.8847 19.8111 19.8111 19.8343
46.0000 10.0000 19.9078 19.8451 19.8451 19.8643
48.0000 10.0000 19.9262 19.8730 19.8730 19.8889
School of Chemical Engineering and Advanced Materials
Newcastle University
40
Example: results using At = 2
0 5 10 15 20 25 30 35 40 45 50
10
11
12
13
14
15
16
17
18
19
20
t
h
(
t
)


Euler's
Improved Euler's
Modified Euler's
Analytical
School of Chemical Engineering and Advanced Materials
Newcastle University
41
Example: results using At = 5
0 5 10 15 20 25 30 35 40 45
10
11
12
13
14
15
16
17
18
19
20
t
h
(
t
)


Euler's
Improved Euler's
Modified Euler's
Analytical
School of Chemical Engineering and Advanced Materials
Newcastle University
42
Numerical solution of ODEs: summary
All numerical ODE solution methods are based on the
Taylor Series
Eulers Method is the most basic and is a 1
st
order method
Improved and Modified Eulers methods are 2
nd
order
methods
Higher order methods give more accurate solutions
The size of the integration interval also influences the
accuracy of the solutions (should be less than 10% of the
time constant)
Solutions of sets of 1
st
order ODEs are done sequentially
Solutions of higher order ODEs are achieved by
decomposing the problem into a set of 1
st
order ODEs

You might also like