Numerical Solution of Single Odes: Euler Methods Runge-Kutta Methods Multistep Methods Matlab Example
Numerical Solution of Single Odes: Euler Methods Runge-Kutta Methods Multistep Methods Matlab Example
1. Euler methods
2. Runge-Kutta methods
3. Multistep methods
4. Matlab example
Euler Methods
Initial value problem
Often impossible to generate analytical solution y(x)
Instead compute approximate solution at equally spaced
node points
h = step size
Taylor series expansion
0 0
) ( ) , ( y x y y x f
dx
dy
= =
3 3 2 2 1 1
0 3 0 2 0 1
) ( ) ( ) (
3 2
y x y y x y y x y
h x x h x x h x x
+ = + = + =
+ + + = +
2
2 2
) (
! 2
) (
) ( ) (
dx
x y d h
dx
x dy
h x y h x y
Forward Euler Method
First-order Taylor series expansion
Iterative calculation
Approximation error
Local truncation error: O(h
2
)
Global truncation error: O(h) first-order method
) , ( ) (
) (
) ( ) ( y x hf x y
dx
x dy
h x y h x y + = + ~ +
, 2 , 1 , 0 ) , (
) , ( ) , (
1
1 1 1 2 0 0 0 1
= + =
+ = + =
+
n y x hf y y
y x hf y y y x hf y y
n n n n
h x x
dx
y d h
dx
x dy
h x y h x y + s s + + = +
2
2 2
) (
! 2
) (
) ( ) (
Backward Euler Method
Forward Euler
Explicit formula for y
n+1
(explicit method)
Backward Euler
Implicit formula for y
n+1
(implicit method)
Allows larger h values to be used with comparable
errors more stable
Generally preferred to forward Euler
) , ( ) , ( ) , (
) (
1
1
n n n n n n
n n
y x hf y y y x f
h
y y
y x f
dx
x dy
+ = =
=
+
+
) , ( ) , ( ) , (
) (
1 1 1 1 1
1
+ + + + +
+
+ = =
=
n n n n n n
n n
y x hf y y y x f
h
y y
y x f
dx
x dy
Plug-Flow Reactor Example
Ai
N
N A A
Ai A n A n A
n A
n A n A
Ai A A
A
C
q z kA
z C L C
C z C z C
q z kA
z C
z kC
z
z C z C
A
q
C C kC
dz
dC
A
q
|
|
.
|
\
|
A +
= ~
=
A +
=
~ +
A
= = +
+
+
+
1
1
) ( ) (
) ( ) (
1
1
) (
0 ) (
) ( ) (
) 0 ( 0
0 1
1
1
A
k
kC r
B A
=
z
q
i
, C
Ai
q
o
, C
Ao
C
A
(z) Az
0
L
Plug-Flow Reactor Example cont.
Analytical solution
Numerical solution
Convergence formula
Convergence of numerical solution
Ai
N
Ai
N
A
C
Nq kAL
C
q z kA
L C
|
|
.
|
\
|
+
=
|
|
.
|
\
|
A +
~
1
1
1
1
) (
|
|
.
|
\
|
= L
q
kA
C L C
Ai A
exp ) (
|
|
.
|
\
|
=
|
|
.
|
\
|
+
L
q
kA
C C
Nq L kA
Ai Ai
N
N
exp
1
1
lim
a
N
N
e
N a
=
|
|
.
|
\
|
+ 1
1
lim
Improved Euler Method
Predictor-corrector formulation
Approximation error
Local truncation error: O(h
3
)
Global truncation error: O(h
2
) second-order method
Adaptive step size
Large h desirable if dy/dx changing slowly (speed)
Small h necessary if dy/dx changing rapidly (accuracy)
Adjust h to maintain the local error below a prespecified
tolerance
Used in modern ODE solution software
)] , ( ) , ( [ Correction
) , ( Prediction
*
1 1 2
1
1
*
1
+ + +
+
+ + =
+ =
n n n n n n
n n n n
y x f y x f h y y
y x hf y y
Runge-Kutta Methods
Basic Runge-Kutta (RK) method
Global truncation error: O(h
4
) fourth-order method
Runge-Kutta-Fehlberg (RKF) method involves a
combination of two Runge-Kutta formulas (see text)
Comparison of single-step methods
) 2 2 (
) , ( ) , (
) , ( ) , (
4 3 2 1 6
1
1
3 4 2 2
1
2
1
3
1 2
1
2
1
2 1
k k k k y y
k y h x hf k k y h x hf k
k y h x hf k y x hf k
n n
n n n n
n n n n
+ + + + =
+ + = + + =
+ + = =
+
Method Function Evaluations Global Error Local Error
Euler 1 O(h) O(h
2
)
Improved Euler 3 O(h
2
) O(h
3
)
RK 4 O(h
4
) O(h
5
)
RKF 6 O(h
5
) O(h
6
)
Multistep Methods
Definition
Single-step: calculation at current step only uses value at
last step (y
n
y
n+1
)
Multi-step: calculation at current step uses values at
several previous steps
General development
Integrate ODE
Approximate f(x,y) with an interpolation polynomial
Different polynomial produce different methods
} }
+ +
= = =
+
1 1
)] ( , [ ) ( ) ( ) ( ) , (
) (
1
n
n
n
n
x
x
x
x
n n
dx x y x f x y x y x dy y x f
dx
x dy
}
+
+ =
+
1
) (
1
n
n
x
x
n n
dx x p y y
Adams-Bashforth Methods
Utilize cubic polynomial that interpolates
Iterative calculation
Explicit method
Global truncation error: O(h
4
) fourth-order method
Initialization
Must compute y
1
, y
2
, y
3
by another method of comparable
accuracy (e.g. Runge-Kutta)
) , ( ) , (
) , ( ) , (
3 3 3 2 2 2
1 1 1
= =
= =
n n n n n n
n n n n n n
y x f f y x f f
y x f f y x f f
) 9 37 59 55 (
3 2 1 24
1
1 +
+ + =
n n n n n n
f f f f h y y
) 9 37 59 55 (
0 1 2 3 24
1
0 4
f f f f h y y + + =
Adams-Moulton Methods
Utilize cubic polynomial that interpolates
Iterative calculation
Implicit method
Global truncation error: O(h
4
) fourth-order method
Predictor-corrector formulation
) , ( ) , (
) , ( ) , (
2 2 2 1 1 1
1 1 1
+ + +
= =
= =
n n n n n n
n n n n n n
y x f f y x f f
y x f f y x f f
) 5 19 9 (
2 1 1 24
1
1 + +
+ + + =
n n n n n n
f f f f h y y
) 5 19 9 (
) 9 37 59 55 (
2 1
*
1 24
1
1
3 2 1 24
1
*
1
+ +
+
+ + + =
+ + =
n n n n n n
n n n n n n
f f f f h y y
f f f f h y y
Matlab Example
Isothermal CSTR model
Model parameters: q = 2, V = 2, C
af
= 2, k = 0.5
Initial condition: C
A
(0) = 2
Backward Euler formula
Algorithm parameters: h = 0.01, N = 200
) ( 2 ) ( 2
2
A A A Af
A
k
C f kC C C
V
q
dt
dC
B A = =
) ( 2 ) (
, ,
2
, , , 1 , n A n A n A n A Af n A n A
C hf C kC C C
V
q
h C C + =
(
+ =
+
Matlab Implementation: iso_cstr_euler.m
h = 0.01;
N = 200;
Cao = 2;
q = 2;
V = 2;
Caf = 2;
k = 0.5;
t(1) = 0;
Ca(1) = Cao;
for i=1:N
t(i+1) = t(i)+h;
f = q/V*(Caf-Ca(i))-
2*k*Ca(i)^2;
Ca(i+1)= Ca(i)+h*f;
end
plot(t,Ca)
ylabel('Ca (g/L)')
xlabel('Time (min)')
axis([0,2,0.75,2.25])
Euler Solution
>> iso_cstr_euler
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
0.8
1
1.2
1.4
1.6
1.8
2
2.2
C
A
(
g
/
L
)
Time (min)
Solution with Matlab Function
function f = iso_cstr(x)
Cao = 2;
q = 2;
V = 2;
Caf = 2;
k = 0.5;
Ca = x(1);
f(1) = q/V*(Caf-Ca)-2*k*Ca^2;
>> xss = fsolve(@iso_cstr,2)
xss = 1.0000
>> df = @(t,x) iso_cstr(x);
>> [t,x] = ode23(df,[0,2],2);
>> plot(t,x)
>> ylabel('Ca (g/L)')
>> xlabel('Time (min)')
>> axis([0,2,0.75,2.25])
Matlab Function Solution
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
0.8
1
1.2
1.4
1.6
1.8
2
2.2
C
a
(
g
/
L
)
Time (min)
Euler
Matlab