Академический Документы
Профессиональный Документы
Культура Документы
Lecture 09!
Analysis of the Runge-Kutta method & !
the Crank-Nicolson method
Stability analysis of 2nd-order Runge-Kutta (page 39 of printed
lecture notes):
to model problem
d
= = f( )
dt
k1 = f ( l , tl )
l l
k2 = f( + tk1 , t + t)
l+1 l 1 1
= + t k1 + k2
2 2
d
= = f( )
dt
l l l l ll ll
k1 = f( , t ) = f( , t ) = = k1
l
k2 = f( + tk1 , tl + t)
l
f( + tk 1, tll + tt) = ( l
+ tk1 )
l
f( + tk1 , tl + t) = ( l
+ t( l )) = k2
= l
k1
k2 = ( l
+ t( l ))
l+1 l 1 1
= + t k1 + k2
2 2
l+1 l 1 l 1 l l
= + t + ( + t )
2 2
l+1 l l 1 2 2 l
= + t + t
2
l+1
= (1 + t + t2 2
/2) l
l+1
= l
= l 1
where =1+ t + t2 2
/2
l+1 l l 1
Prove that = =
1
= Given
2 1
=
3 2 2 1
= =
4 3 3 1
= =
.. .. ..
. = . = .
l+1 l l 1
= =
Hence, for stability
2 2
| | = |1 + t + t /2| 1
2 2
| | = |1 + t + t /2| 1
A complex number
Need to find lt where the above complex number has a
magnitude of 1. Any complex number that has a magnitude 1 can
be represented by
i
|e | = 1
Hence we want to find values of lt that make
2 2
|1 + t + t /2| = 1
1+ t + t2 2
/2 = ei
1+ t + t2 2
/2 ei = 0
2 2 i
1+ t + t /2 e =0
Note that
0 2n
You can follow the steps above and show that the stability region
for the 4th order Runge-Kutta method can be found by solving
2 2 3 3 4 4
t t t i
t+ + + +1 e =0
2 6 24
where
0 8 = 0
Example O09.1: Write MATLAB program to find stability
boundary for the 2nd-order Runge-Kutta method solving the
model problem
!
! d
! = = f( )
! dt
From the theory presented above, we need to solve
1+ t + t2 2
/2 ei = 0
t =p
Hence
2 i
1 +p +p /2 e =0
2 i
1 + p + p /2 e =0
!
plot(real(lamdt),imag(lamdt),'b-','linewidth',5);!
xlabel('\lambda_{Re}\Delta t');! 1
ylabel('\lambda_{Im}\Delta t');!
axis([-4 2 -3 3]);!
= 2 =0
Im t
daspect([1 1 1]); 0
= 4
1
2
= 3
3
4 3 2 1 0 1 2
Re t
Alternatively, we can get MATLAB to plot contours of
| | = |1 + t + t2 2
/2|
function MPO09p1b()!
clear all;!
close all;!
!
relamdt=-4:0.1:4;!
imlamdt=-4:0.1:4;!
[x,y]=meshgrid(relamdt,imlamdt);! (-4,-4) (4,-4)
axis square;!
lamdt=x+i*y;! t = Re( t) + iIm( t)
sig=1+lamdt+lamdt.^2/2; !
contour_levels=[1 2 3 4];! =1+ t + t2 2
/2
contour(x,y,abs(sig),contour_levels,'linewidth
',4);!
xlabel('Re \lambda \Delta t','Fontsize',14);!
contour ||
ylabel('Im \lambda \Delta t','Fontsize',14);! 4
hold on;!
plot([-4 4],[0 0],'k');!
| |
3
Im t 0
| |=4 1
| |=3 2
| |=2 3
| |=1 4
4 3 2 1 0 1 2 3 4
Re t
2.83
1+ h+
2 2
h
eI = 0
4th order Runge Kutta
2
Euler
-2.0
-2.83
2 2 3 3 4 4
h h h
h+ + + +1 eI = 0
2 6 24
Stability region for RK4 is bigger than RK2
In order for your code to be stable, !
you need to pick t such that it falls within !
2 2
h
the stability region of your numerical scheme.
1+ h+ eI = 0
2
d
= 10
dt
-2.79
using Euler, the maximum value of t you !
can use is
2 1
tmax,Euler = = = 0.2
10 5
If you were to use RK4, then the maximum
value of t you can use is
2 2 3 3 4 4
h h h
h+
2
+
6
+
24
+1 eI = 0
2.79
tmax,RK4 = = 0.279
10
Back to this slide again: stability analysis predicts this behaviour
1exp(t) 1exp(t) 1exp(t)
2 2 30
1.8 1.8
20
1.6 1.6
1.4 1.4
10
Explicit Euler
1.2 1.2
1 1 0
0.8 0.8
10
0.6 0.6
0.4 0.4
20
0.2 0.2
0 0 30
0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8
t t t
1.8 1.8
20
1.6 1.6
2nd-order!
1.4 1.4
10
1.2 1.2
1 1 0
Runge-Kutta 0.8
0.6
0.4
0.8
0.6
0.4
10
20
0.2 0.2
0 0 30
0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8
t t t
1.8 1.8
20
1.6 1.6
4th-order!
1.4 1.4
10
1.2 1.2
1 1 0
Runge-Kutta 0.8
0.6
0.4
0.8
0.6
0.4
10
20
0.2 0.2
0 0 30
0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8
t t t
End of Example O09.1
Runge-Kutta methods for a!
System of ODEs
In general, a system of M ODEs can be written as
d 1
= f1 ( 1 , 2 , 3 , . . . . . . , M , t)
dt
d 2
= f2 ( 1 , 2 , 3 , . . . . . . , M , t)
dt
d 3
= f3 ( 1 , 2 , 3 , . . . . . . , M , t)
dt
.. ..
.=.
d M
= fM ( 1 , 2 , 3 , . . . . . . , M , t)
dt
l l
k21 = f1 1 + tk11 , 2 + tk12 , tl + t
l l
k22 = f2 1 + tk11 , 2 + tk12 , tl + t
d 1 d 1
= f1 ( 1 , t)
dt = f1 ( 1, 2, 3 , t)
RK-2 dt
k1 = f( l l d 2
1, t ) = f2 ( 1, 2, 3 , t)
k2 = f( l
1+ tk1 , t +l
t) dt
l+1
= l
+ t
1 1
k1 + k2 d 3 k1 associated with !
1 1
2 2 = f3 ( 1, 2, 3 , t) f1
dt
k2 associated with !
d 1 RK-2 f1
= f1 ( 1, 2 , t)
dt 1 1
l+1
= l
+ t k11 + k21 k2 associated with !
d 2 1 1
f2
= f2 ( 1, 2 , t) 2 2
dt
l+1 l 1 1
2 = 2 + t k12 + k22 k1 associated with !
RK-2 2 2 f2
l+1 l 1 1 k2 associated with !
l+1 l 1 1 = 3 + t k13 + k23
1 = 1 + t k11 + k21 3
2 2 f3
2 2
k1 associated with !
l+1 l 1 1
2 = 2 + t k12 + k22 f3
2 2 l l l l
k11 = f1 ( 1, 2, 3, t )
l l l
k11 = f1 ( 1, 2, t ) l l l l
l l l k12 = f2 ( 1, 2, 3, t )
k12 = f2 ( 1, 2, t )
l l l l
k13 = f3 ( 1, 2, 3, t )
l l
k21 = f1 1 + tk11 , 2 + tk12 , tl + t
l l
k22 = f2 + tk11 , + tk12 , tl + t l l l
1 2 k21 = f1 1 + tk11 , 2 + tk12 , 3 + tk13 , tl + t
l l l
k22 = f2 1 + tk11 , 2 + tk12 , 3 + tk13 , tl + t
l l l
k23 = f3 1 + tk11 , 2 + tk12 , 3 + tk13 , tl + t
Example O09.2: Write a MATLAB program to solve the
nonlinear ODE
!
3 2
! d 1d
! 3
+ 2
=0
! dt 2 dt
!
!
with (0)=0, (d/dt)(0)=0, (d2/dt2)(0)=0.332 using the 4th-
order Runge-Kutta method. Plot d/dt for 0<t<25.
d3 1 d2
+ =0 (9.1)
dt3 2 dt2
d d2
(0) = 0.0 (0) = 0.0 (0) = 0.332 0 t 25
dt dt 2
Let
2 2
1 = , 2 = d /dt, 3 =d /dt
so
d 1 d d 2 d2
= = 2 = 2 = 3
dt dt dt dt
and
1 (0) = 0.0 2 (0) = 0.0 3 (0) = 0.332
xlabel('t');!
ylabel('d\phi/dt');! RK-2
!
l l l l
function dphidt=f(phi)! k11 = f1 ( 1, 2, 3, t )
dphidt=[phi(2) phi(3) -0.5*phi(3)*phi(1)];! k12 = f2 ( l l l l
1, 2, 3, t )
! k13 = f3 ( l l l l
1, 2, 3, t )
l l l
k21 = f1 1 + tk11 , 2 + tk12 , 3 + tk13 , tl + t
l l l
k22 = f2 1 + tk11 , 2 + tk12 , 3 + tk13 , tl + t
l l l
k23 = f3 1 + tk11 , 2 + tk12 , 3 + tk13 , tl + t
l+1 l 1 1
1 = 1 + t k11 + k21
2 2
l+1 l 1 1
2 = 2 + t k12 + k22
2 2
l+1 l 1 1
3 = 3 + t k13 + k23
2 2
function MPO9p2()!
clear all;!
close all;!
!
Delta_t=0.1; !
t=0:Delta_t:25; !
phi=zeros(length(t),3); !
phi(1,:)=[0.0 0.0 0.332]; !
!
for n=1:length(t)-1!
k1=f(phi(n,:));!
k2=f(phi(n,:)+Delta_t*k1);!
phi(n+1,:)=phi(n,:)+Delta_t*(k1/2+k2/2);!
end!
! 1
plot(t,phi(:,2),'ko-') !
hold on! 0.9
xlabel('t');!
0.8
ylabel('d\phi/dt');!
! 0.7
function dphidt=f(phi)!
0.6
dphidt=[phi(2) phi(3) -0.5*phi(3)*phi(1)];!
!
d/dt
0.5
0.4
0.3
0.2
0.1
0
0 5 10 15 20 25
t
End of Example O09.2
CRANK-NICOLSON FORMULA
ODE to solve:
d
(t) = f ( , t)
dt
Apply Crank-Nicolson:
l+1
} El+1local
tl+1)
tl+1)
} l+1
E local
t )
l+1
}
l+1
l
l El+1local l
l+1
t t t
tl
t l+1 tl tl+1 t
l
t l+1
}
}
}
t t t
l+1 l l l l+1 l l+1 l+1 l+1 l 1
= + tf
matic illustrating one step in Figure
( , t )
1.7: Euler
the explicit A schematic =
method. illustrating
+ tf (
The green one stepFigure
, t
in the1.11: )
implicit = illustrating
Euler method. + Thet one step in the Crank-Ni
A schematic 2 l l
e gradient f ( l , tl ) which is used
greento arrow illustrates
step the solution forward. The f ( l+1 , tl+1
the gradient )
greenwhich is
arrows used to step
illustrate the
the
solution
gradients l f (l , t ) and f (
l+1
l+1 l+1
,t )
l+1
computed step to l+1 forward. The pink line illustrates
. The blue line illustrates the analytical computed step to l+1
. The blue line f (
illustrates , t ) + f
to step the solution forward. The pink line illustrates compute( , t )
rated is the local truncation the analytical
error, solution.
which is the Alsobetween
dierence illustrated is the local truncation error, which is the
blue line illustrates the analytical solution. Also illustrated is
numerical solutions at time dierence
step l + 1.between the analytical and numerical solutions at time step l + 1.
error, which is the dierence between the analytical and numeri
step l + 1.
(t) Illustration of explicit Eulers method
Predicted value of l+1
d l l
Error
= f( , t )
dt
l+1 (t)
(t )
True value of l+1
l+1 l l l
l = + tf ( , t )
(t )
Eulers formula
t
l l+1
t t
(t) Illustration of the implicit Euler method
l+1 l l+1 l+1
= + tf ( ,t )
Error
l
Predicted value of l+1
t
l
t t l+1
Summary
(t) Illustration of explicit Eulers method (t) Illustration of the implicit Euler method
Predicted value of l+1 l+1
= l
+ tf ( l+1
, tl+1 )
d Error
= f ( l , tl )
dt True value of l+1
(tl+1 ) (t)
True value of l+1
Error
l+1 l
= + tf ( l , tl ) l
(tl ) Predicted value of l+1
Eulers formula
t t
t l
tl+1 tl tl+1
(t) Illustration of Crank Nicolson method
l+1 l 1
= + t f ( l , tl ) + f ( l+1 l+1
,t )
2
Error
Predicted value of l+1
l
t
tl tl+1
Example O09.3:
!
Using Crank-Nicolson method, solve
!
! d
! =1
! dt
!
For 0 < t < 8 with (t=0)=0 and
a) t=2
b) t=1
c) t=0.5
d) t=0.1
!
Compare your solution with Eulers method
d
=1 = f ( , t)
dt
Crank Nicolson formula
l+1 l 1
= + t f ( l , tl ) + f ( l+1
,t l+1
)
2
1
l+1
= l
+ t f1( l , tll) + f1( l+1l+1
l+1
,t )
2
t l+1 t l
1+ = 1 + t
2 2
t l
l+1 1 2 + t
= t
1+ 2
function MPO09p3()!
!
close all!
clear all!
!
t l
tmin=0.0;!
l+1 1 2 + t
tmax=8.0;!
! = t
[t1,phi1]=MyCrankNicolson([tmin tmax],0.0,2.0);!
!
1+ 2
!
plot(t1,phi1,'ko-');!
hold on!
ezplot(@(t)1-exp(-t),[0,8,0,2])!
xlabel('t');!
ylabel('\phi');!
legend('Crank-Nicolson','True');!
!
function [t,phi]=MyCrankNicolson(tspan,phi0,Delta_t)!
t=tspan(1):Delta_t:tspan(2);!
1exp(t)
phi=zeros(length(t),1);! 2
phi(1)=phi0;! Euler
for n=1:length(t)-1! 1.8 True
phi(n+1)=((1-Delta_t/2)*phi(n)+Delta_t)/(1+Delta_t/2);!
end!
Crank-Nicolson
1.6
1.4
1.2
0.8
0.6
Output 0.4
0.2
0
0 1 2 3 4 5 6 7 8
t
Compare implicit Euler, explicit Euler and Crank Nicolson method for t=2.0
1exp(t) 2
2
Euler
t=2.0
Euler True
t=2.0
True 1.8
1.8
1.6
1.6
1.4
1.4
1.2 1.2
0.8 0.8
0.6 0.6
0.4 0.4
0.2 0.2
0 0
0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8
t 1exp(t) t
2
Euler
t=2.0
1.8 True
1.6
1.4
1.2
0.8
0.6
0.4
0.2
0
0 1 2 3 4 5 6 7 8
t
End of Example O09.3
Example O09.4: Write a MATLAB program that uses the Crank-
Nicolson method to solve
!
! d
! = i2
!
dt
!
for 0 < t < 10 with (t=0)=1.
l+1 1
= l + t i2 l
+ i2 l+1
2
l+1 l
(1 i t) = (1 + i t)
l+1 1+i t l
=
1 i t
function MPO9p4()!
% Solve dphi/dt = i*2*phi, phi(0) = 1, 0 < t <
10.!
clear all;! l+1 1+i t l
close all;! =
! 1 i t
Delta_t=0.1; % Time step size!
t=0:Delta_t:10.0; % Time vector!
phi=zeros(size(t)); % Solution vector!
phi(1)=1.0; % Initial condition!
for n=1:length(t)-1!
phi(n+1)=phi(n)*(1+Delta_t*i)/(1-
Delta_t*i);!
end!
!
plot(t,real(phi),'ko-') % Plot numerical
real(exp(i 2 t))
solution! 2
hold on! CrankNicolson
True
ezplot(@(t)real(exp(i*2*t)),[0,10,-2,2]) % Plot 1.5
analytical solution!
xlabel('t');! 1
ylabel('\phi');!
legend('Crank-Nicolson','True'); 0.5
0.5
1.5
Output 2
0 1 2 3 4 5 6 7 8 9 10
t
real(exp(i 2 t))
2
CrankNicolson
Explicit Euler
1.5 Implicit Euler
True
0.5
0.5
1.5
2
0 1 2 3 4 5 6 7 8 9 10
t