Documente Academic
Documente Profesional
Documente Cultură
Lecture 09!
Analysis of the Runge-Kutta method & !
the Crank-Nicolson method
k1
k2
+
l
t
l
f( , t )
f(
1
1
k1 + k2
2
2
l
tk1 , t +
to model problem
d
=
dt
= f( )
t)
k1
k2
f ( l , tl )
l+1
f(
l
+
t
tk1 , t +
1
1
k1 + k2
2
2
d
=
dt
l
k1
f( , t ) =
k2
f(
t)
= f( )
l
ll
f( , t )
tk1 , tl +
ll
= k1
t)
f(
tk 1, tll + tt) = (
tk1 )
f(
tk1 , tl +
t(
t) = (
l )) =
k2
k1
k2
= (
l+1
l+1
=
=
l+1
l+1
= (1 +
l+1
+
+
t( l ))
1
1
t
k1 + k2
2
2
1 l 1
l
t
+
( +
2
2
t
t +
=
l 1
1 2
+
t
2
t2
2 l
/2)
where
=1+
t +
t2
/2
Prove that
l+1
1
2
3
4
..
.
l+1
=
=
=
=
l 1
Given
1
2
3
..
.
=
=
=
l
2 1
3 1
..
.
l 1
t +
2 2
/2| 1
| | = |1 +
t +
2 2
/2| 1
t +
2 2
/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+
1+
t +
t +
t2
t2
/2 = ei
2
/2 ei = 0
1+
t +
Note that
2 2
/2
=0
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
t+
t
2
t
6
where
0 8 = 0
24
+1
=0
t2
t +
ei = 0
/2
Hence
1 +p
+p /2
2
1 + p + p /2
e
e
=0
=0
This is a 2nd order polynomial in terms of p. We can use NewtonRaphson polynomial to solve this equation. Note that
0 2n
g(p) = 1 + p + p /2
=0
dg
=1+p
dp
Newton Raphson
g(pold )
pnew = pold
dg
(p
)
old
dp
3
Im t
function MPO09p1a()!
clear all;!
close all;!
!
theta=0:0.1:4*pi;!
= 2
=0
= 4
3
4
= 3
3
1
Re t
t +
t2
/2|
function MPO09p1b()!
clear all;!
close all;!
!
relamdt=-4:0.1:4;!
imlamdt=-4:0.1:4;!
[x,y]=meshgrid(relamdt,imlamdt);!
axis square;!
lamdt=x+i*y;!
sig=1+lamdt+lamdt.^2/2; !
contour_levels=[1 2 3 4];!
contour(x,y,abs(sig),contour_levels,'linewidth
',4);!
xlabel('Re \lambda \Delta t','Fontsize',14);!
ylabel('Im \lambda \Delta t','Fontsize',14);!
hold on;!
plot([-4 4],[0 0],'k');!
plot([0 0],[-4 4],'k');!
title('contour |\sigma|','Fontsize',14);!
(-4,4)
(4,4)
(-4,-4)
(4,-4)
t = Re(
=1+
t) + iIm(
t +
t2
t)
/2
contour ||
| |
Im t
| |=4
| |=3
| |=2
| |=1
4
4
Re t
2.83
1+ h+
-2.79
2 2
h
2
eI = 0
-2.0
h+
2 2
h
+
2
3 3
4 4
h
h
+
+1
6
24
eI = 0
-2.83
2 2
h
2
eI = 0
d
=
dt
-2.79
10
tmax,Euler
2
1
=
= = 0.2
10
5
2 2
h
+
2
3 3
4 4
h
h
+
+1
6
24
eI = 0
tmax,RK4
2.79
=
= 0.279
10
1exp(t)
1.8
1.8
1.6
1.6
1.4
1.4
1.2
1.2
1exp(t)
30
20
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
Explicit Euler
10
10
20
4
t
1exp(t)
1.8
1.6
1.6
1.4
1.4
1.2
1.2
30
1exp(t)
1.8
4
t
4
t
1exp(t)
30
20
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
2nd-order!
Runge-Kutta
10
10
20
4
t
1exp(t)
1.8
1.6
1.6
1.4
1.4
1.2
1.2
30
1exp(t)
1.8
4
t
4
t
1exp(t)
30
20
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
4th-order!
Runge-Kutta
10
10
20
4
t
4
t
30
4
t
M , t)
M , t)
M , t)
M , t)
d 1
= f1 (
dt
1 , t)
RK-2
l+1
1
k1
k2
=
=
l
1
f(
f(
l l
1, t )
l
1+
1
1
k1 + k2
2
2
t)
tk1 , t +
d 1
= f1 (
dt
d 1
= f1 (
dt
d 2
= f2 (
dt
1 , t)
RK-2
k1
f(
k2
f(
l l
1, t )
l
1+
l+1
1
l
1
tk1 , tl +
1
1
k1 + k2
2
2
t)
1,
2 , t)
1,
2 , t)
k1 associated with !
f1
RK-2
l+1
1
l+1
2
l
1
l
2
1
t
k11 +
2
1
t
k12 +
2
k11
f1 (
k12
f2 (
k21
f1
k22
f2
l
1
l
1
l
1,
l
1,
tk11 ,
tk11 ,
1
k21
2
1
k22
2
l l
2, t )
l l
2, t )
l
2
l
2
k2 associated with !
f1
k2 associated with !
f2
k1 associated with !
f2
tk12 , tl +
tk12 , tl +
d 1
= f1 (
dt
d 1
= f1 (
dt
d 2
= f2 (
dt
d 3
= f3 (
dt
1 , t)
RK-2
k1
k2
=
l+1
1
l l
1, t )
l
1+
f(
f(
=
l
1
d 1
= f1 (
dt
d 2
= f2 (
dt
l+1
1
=
=
l
2
k11
f1 (
k12
f2 (
l+1
2
k21
f1
k22
f2
l
1
l
1
tk1 , t +
1
1
k1 + k2
2
2
1,
1,
tk11 ,
tk11 ,
l
1,
l
1,
l
2
l
2
t)
2,
3 , t)
1,
2,
3 , t)
1,
2,
k1 associated with !
f1
3 , t)
k2 associated with !
f1
RK-2
2 , t)
2 , t)
RK-2
1
t
k11 +
2
1
t
k12 +
2
l
1
1,
1
k21
2
1
k22
2
l l
2, t )
l l
2, t )
tk12 , tl +
tk12 , tl +
k21
f1
k22
f2
k23
f3
l+1
1
l
1
l+1
2
l
2
l+1
3
l
3
1
t
k11 +
2
1
t
k12 +
2
1
t
k13 +
2
k11
f1 (
k12
k13
=
=
f2 (
f3 (
l
1
l
1
l
1
tk11 ,
tk11 ,
tk11 ,
l
2
l
2
l
2
l
1,
l
1,
l
1,
l
2,
l
2,
l
2,
tk12 ,
tk12 ,
tk12 ,
1
k21
2
1
k22
2
1
k23
2
l l
3, t )
l l
3, t )
l l
3, t )
l
3
l
3
l
3
k2 associated with !
f2
k1 associated with !
f2
k2 associated with !
f3
k1 associated with !
f3
tk13 , tl +
tk13 , tl +
tk13 , tl +
d3
1 d2
+
dt3
2 dt2
(9.1)
=0
d2
(0) = 0.332
2
dt
d
(0) = 0.0
(0) = 0.0
dt
Let
1
so
= ,
d 1
d
=
=
dt
dt
= d /dt,
=d
d 2
d2
= 2 =
dt
dt
/dt
0 t 25
and
1 (0)
= 0.0
2 (0)
= 0.0
1
2
3 (0)
3 1
= 0.332
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!
!
plot(t,phi(:,2),'ko-')
!
hold on!
xlabel('t');!
ylabel('d\phi/dt');!
!
function dphidt=f(phi)!
dphidt=[phi(2) phi(3) -0.5*phi(3)*phi(1)];!
d 1
=
dt
d 2
=
dt
d 3
=
dt
1 (0)
= 0.0
0 t 25
1
2
2 (0)
3 1
= 0.0
d 1
= f1 (
dt
d 2
= f2 (
dt
d 3
= f3 (
dt
3 (0)
1,
2,
3 , t)
1,
2,
3 , t)
1,
2,
3 , t)
= 0.332
RK-2
k21
f1
k22
f2
k23
f3
k11
f1 (
k12
f2 (
k13
f3 (
l
1
l
1
l
1
tk11 ,
tk11 ,
tk11 ,
l+1
1
l
2
l
2
l
2
l
1
l+1
2
l
2
l+1
3
l
3
l
1,
l
1,
l
1,
l
2,
l
2,
l
2,
tk12 ,
tk12 ,
tk12 ,
1
t
k11 +
2
1
t
k12 +
2
1
t
k13 +
2
l l
3, t )
l l
3, t )
l l
3, t )
l
3
l
3
l
3
tk13 , tl +
tk13 , tl +
tk13 , tl +
1
k21
2
1
k22
2
1
k23
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!
!
plot(t,phi(:,2),'ko-')
!
hold on!
xlabel('t');!
ylabel('d\phi/dt');!
!
function dphidt=f(phi)!
dphidt=[phi(2) phi(3) -0.5*phi(3)*phi(1)];!
0.9
0.8
0.7
0.6
d/dt
0.5
0.4
0.3
0.2
0.1
0
10
15
t
20
25
CRANK-NICOLSON FORMULA
ODE to solve:
d
(t) = f ( , t)
dt
Apply Crank-Nicolson:
l+1
1
l l
t f( , t ) + f(
2
l+1
l+1
,t
19
GRAPHICAL COMPARISON
This method is an implicit method because it has l+1 on both sides of the equation,
so we are using information at time step l + 1 that we dont yet know in order to
take our next step (Figure 1.7). If we are only 1.3.
solving
a single ODE then, depending
CRANK-NICOLSON
METHODS
upon the form of f , we may still be able to rearrange Equation 1.11 to obtain an
explicit expression. If we are solving a system
of ODEs
then we will need to solveMethods
1.3
Crank-Nicolson
l of equations (either linear or nonlinearl+1
a system
depending upon the form of f ) at
l
l+1
eachDIFFERENTIAL
time step.
CHAPTER 1. ORDINARY
EQUATIONS
Explicit Euler:
take slope at t
Crank-Nicolson:
take average of slopes
at t and t
Implicit Euler:
take slope at t
l+1
El+1local
tl+1)
tl+1)
t )
l+1
} l+1
E local
l+1
El+1local
l+1
tl
tl+1
l+1
l+1
1
l+1
l
l l
l+1
l
l+1 l+1
l+1
l
=
+
tf
(
,
t
)
=
+
tf
(
,
t
)
= illustrating
+ Thet one step in the Crank-Ni
1.7: Euler
A schematic
in the1.11:
implicit
Euler method.
matic illustrating one step in Figure
the explicit
method. illustrating
The green one stepFigure
A
schematic
2 l l
green
illustrates
the gradient
)
which
is
used
to
step
the
solution
l+1 l+1
e gradient f ( l , tl ) which is used
to arrow
step the
solution forward.
The f ( l+1 , tl+1
green
arrows
illustrate
the
gradients
f
(
,
t
)
and
f
(
,t )
l
l
l+1
l+1
l+1
l+1
forward.
The
pink
line
illustrates
computed
step
to
.
The
blue
line
illustrates
f
(
,
t
)
+
f
(
,
t
)
computed step to
. The blue line illustrates the analytical
to step the solution forward. The pink line illustrates compute
analytical
solution.
Alsobetween
illustrated is the local truncation error, which is the
rated is the local truncation the
error,
which is the
dierence
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.
(t)
d
l l
= f( , t )
dt
(t
l+1
(t)
(t )
l+1
tf ( , t )
Eulers formula
(t)
tf (
l+1
l+1
,t
t
t
l+1
Summary
(t)
(t)
(tl+1 )
l+1
tf (
l+1
, tl+1 )
Error
(tl )
Error
tf ( l , tl )
Eulers formula
t
t
tl
tl+1
(t)
tl+1
1
t f ( l , tl ) + f (
2
l+1
l+1
,t
Error
Predicted value of l+1
l
t
tl
tl+1
Example O09.3:
!
!
!
!
!
!
d
=1
dt
d
=1
dt
= f ( , t)
l+1
t
1+
2
l+1
1
t f ( l , tl ) + f (
2
1
t f1( l , tll) + f1(
2
l+1
t
2
t
2
1+
t
2
l+1
l+1
,t
l+1l+1
l+1
,t
function MPO09p3()!
!
close all!
clear all!
!
tmin=0.0;!
tmax=8.0;!
l+1
[t1,phi1]=MyCrankNicolson([tmin tmax],0.0,2.0);!
!
!
t
2
1+
t
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);!
phi=zeros(length(t),1);!
phi(1)=phi0;!
for n=1:length(t)-1!
phi(n+1)=((1-Delta_t/2)*phi(n)+Delta_t)/(1+Delta_t/2);!
end!
1exp(t)
Euler
True
1.8
Crank-Nicolson
1.6
1.4
1.2
1
0.8
0.6
Output
0.4
0.2
0
4
t
Compare implicit Euler, explicit Euler and Crank Nicolson method for t=2.0
1exp(t)
Euler
True
t=2.0
1.8
1.6
1.4
1.2
1.2
1.4
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
4
t
4
t
1exp(t)
Euler
True
t=2.0
1.8
1.6
1.4
1.2
1.6
Euler
True
t=2.0
1.8
1
0.8
0.6
0.4
0.2
0
4
t
Example O09.4: Write a MATLAB program that uses the CrankNicolson method to solve
!
d
!
= i2
!
dt
!
!
for 0 < t < 10 with (t=0)=1.
(t) = ei2t
Analytical solution:
(1
i t)
l+1
l+1
1
= l + t i2
2
l
= (1 + i t)
1+i t l
=
1 i t
+ i2
l+1
function MPO9p4()!
% Solve dphi/dt = i*2*phi, phi(0) = 1, 0 < t <
10.!
clear all;!
close all;!
!
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)/(1Delta_t*i);!
end!
!
plot(t,real(phi),'ko-') % Plot numerical
solution!
hold on!
ezplot(@(t)real(exp(i*2*t)),[0,10,-2,2]) % Plot
analytical solution!
xlabel('t');!
ylabel('\phi');!
legend('Crank-Nicolson','True');
l+1
1+i t
=
1 i t
real(exp(i 2 t))
CrankNicolson
True
1.5
0.5
0.5
Output
1.5
5
t
10
real(exp(i 2 t))
CrankNicolson
Explicit Euler
Implicit Euler
True
1.5
0.5
0.5
1.5
5
t
10