Académique Documents
Professionnel Documents
Culture Documents
Lecture 08
Euler Method: Error Analysis &!
d Compare
Strategy = i!
dt
Find approximated!
approximated Eulers!
solution at time level lt
Find analytical solution!
at time level lt
d
1 i!l t
e Compare
= i!
dt
Find approximated Eulers!
solution at time level lt
d Compare
= i!
dt
Find approximated Eulers!
solution at time level lt
l+1 l 1
=
Applying Eulers method to Eq. (O08.1) will give you the solution at
time level l+1 (i.e. the solution at lt) as
l+1 l l+1
= + i! t
l+1 l+1 l
i! t =
l+1 1 l
=
1 i! t
l+1 l
=
l+1
= l 1
(O08.3)
where
1
=
1 i! t
l+1 l l 1
Prove that = =
1
= Given
2 1
=
3 2 2 1
= =
4 3 3 1
= =
.. .. ..
. = . = .
l+1 l l 1
= =
1 i!l t
Find analytical solution! e
at time level lt
d Compare
= i!
dt
Find approximated Eulers!
solution at time level lt
Applying Eulers method to Eq. (O08.1) will give you the solution at
time level l+1 (i.e. the solution at lt) as
l+1 l l+1
= + i! t
l+1 l+1 l
i! t =
l+1 1 l
=
1 i! t
l
=
l+1 ll 11
= (O08.3)
where
1
=
1 i! t
1 1 + i! t
= =
1 i! t 1 + (! t)2
1
Z=p = arctan (! t)
1 + (! t)2
1 i!l t
Find analytical solution! e
at time level lt
d Compare
= i!
dt
Find approximated Eulers!
solution at time level lt l+1 l 1
=
l+1 l i! t
= (l t) e
l il i!l t
= (l t)Z e e
l+1 l il( ! t)
! t)
= (l t)Z e t)Z e (O08.5)
l+1 l il( ! t)
= (l t)Z e
( ! t) = arctan(! t) ! t (O08.6)
Recall from LectureO01 that Taylor series can be written as
2 2 3 3
xd x d x d
(x) = (0) + + 2
+ 3
+ ...
1! dx 0 2! dx 0 3! dx 0
If we let (x)=arctan(x), then the equation above can be written as
x d x2 d2 x3 d3
arctan(x) = arctan(0) + arctan(x) + 2
arctan(x) + 3
arctan(x) + ...
1! dx 0 2! dx 0 3! dx 0
2
3
x 1 x 2x x 8x2 2
arctan(x) = 0 + + + + ...
1! 1 + x2 0 2! (1 + x2 )2 0 3! (1 + x2 )3 (1 + x2 )2 0
x x2 x3
arctan(x) = 0 + + 0 + ( 2) + . . .
1! 2! 3!
x x2 x3
arctan(x) = 0 + + 0 + ( 2) + . . .
1! 2! 3!
If we now let x = ! t
l+1 l l+1
= + ti2
l+1 l l
= + ti2 l+1 l
(1 ti2) =
l+1 l
= (1 + ti2) 1
l+1 l
=
(1 ti2)
function MPO08p1()
clear all
close all
Deltat=0.1;
t=0:Deltat:20.0;
phiImp=zeros(1,length(t)); %Implicit Euler solution
phiEx=zeros(1,length(t)); %Explicit Euler solution
phiImp(1)=1.0;
phiEx(1)=1.0;
l+1 l
= (1 + ti2)
for n=1:length(t)-1
phiEx(n+1)=(1+2*i*Deltat)*phiEx(n);
phiImp(n+1)=(1/(1-2*i*Deltat))*phiImp(n); l+1 1 l
end =
(1 ti2)
h=plot(t,real(phiEx),'bo-',t,real(phiImp),'ro-',t,cos(2*t),'k-');
legend('Explicit Euler','Implicit Euler','True solution');
set(h(3),'Linewidth',3);
xlabel('t');ylabel('\phi(t)');
axis([0 20 -5 5]);
5
Explicit Euler
4 Implicit Euler
True solution
3
The implicit Euler solution decay with time,
2
explicit Euler solution grow with time.
1
3 t=0.1
4
5
0 1 2 3 4 5 6 7 8 9 10
t 5
Explicit Euler
4 Implicit Euler
True solution
3
1
(t)
4
t=0.05
5
0 1 2 3 4 5 6 7 8 9 10
t
5
4
t=0.1 Peak location! Explicit Euler
Implicit Euler
of true solution
True solution
3
Peak location!
2 of true solution
1
(t)
2
Peak location!
3
of Eulers solution
Peak location!
4
of Eulers solution
5
0 1 2 3 4 5 6 7 8 9 10
t
Figure showing phase error of Eulers solutions
End of Example O08.1
Example O08.2: In ODE Workshop 2, you were asked to solve the model problem!
!
! 2
d
! = ! 2
! dt2
!
Analyse the numerical solution to this equation obtained using the implicit and explicit Euler
method. !
Letting
x1 =
x2 = d /dt
gives
d x1 0 1 x1
=
dt x2 !2 0 x2
Im t
-2 -1
Re t
- i
On the other hand, the eigenvalues lies inside the stability region of the implicit Euler method. That is
why solution obtained using implicit Euler method is always unstable for this problem.
Im t
1 2 t
Re
- i
End of Example O08.2
The Runge-Kutta Method
Runge-Kutta Methods (Page 33 printed lecture notes)
The Runge-Kutta method is arguably, the most popular method used in the industry. It is fast,
accurate and quite stable
d
= f ( , t) (O08.7)
dt
can be written as
l+1 l l l
= + tg( , t , t) (O08.8)
where
g = a1 k1 + a2 k2 + a3 k3 + a4 k4 + aN kN (O08.9)
k1 = f ( l , tl )
l
k2 = f( + q11 k1 t, tl + p1 t)
l
k3 = f( + q21 k1 t + q22 k2 t, tl + p2 t) (O08.10)
l
k4 = f( + q31 k1 t + q32 k2 t + q33 k3 t, tl + p3 t)
..
.
l
kN = f( + qN 1,1 k1 t + qN 1,2 k2 t + + qN 1,N 1 kN 1 t, tl + pN 1 t)
The more terms you have, the more accurate the solution.
The 2nd order Runge-Kutta method formula can be obtained by setting N=2 in Eq. (O08.9)
l+1 l
= + t (a1 k1 + a2 k2 )
l l
k1 = f( , t )
l
k2 = f( + q11 k1 t, tl + p1 t)
In order to use this formula, you need to find a1, a2, q11 and p1.
l+1 l
= + t (a1 k1 + a2 k2 )
k1 = f ( l , tl )
l l
k2 = f( + q11 k1 t, t + p1 t)
So what we should do is to analyse the Runge-Kutta solution first and then compare!
with the Taylor series expansion to come up with values for a1, a2, q11 and p1.
l l
k1 = f( , t ) (O08.11)
l l
k2 = f( + q11 k1 t, t + p1 t) (O08.12)
l+1 l (O08.13)
= + t (a1 k1 + a2 k2 )
l+1 l l l l l
= + t a1 f ( , t ) + a2 f ( + q11 k1 t, t + p1 t)
The third term on the RHS is a function of two variables. We can perform Taylor expansion about
the two variables, i.e.
@f @f
f ( + r, t + s) = f ( , t) + r +s + ...
@ @t
l+1 l
= + t a1 f ( l , tl ) + a2 f ( l
+ q11 k1 t, tl + p1 t)
f ( + r, t + s) = f ( , t) +
@f @f
r +s + ...
@ @t
l+1 l
= + t a1 f ( l , tl ) + a2 f ( l + q11 k1 t, tl + p1 t)
l l l l l @f @f
= + t a1 f ( , t ) + a2 f ( , t ) + q11 k1 t + p1 t + . . .
@ @t
k =f (see Eq. O08.11)
1
l l l l l l l @f @f
= + t a1 f ( , t ) + a2 f ( , t ) + f ( , t ) q11 t + p1 t + . . .
@ @t
l l l @f @f
= + (a1 + a2 ) tf ( , t ) + a2 q11 f ( , t ) ( t) + a2 p1 ( t)2 + . . .
l l 2
@ @t
(O08.14)
2nd order Runge-Kutta formula
Recall in Lecture O03, we derived Euler method and Taylor method order 2 by truncating Taylor
series at t2 & t3 respectively
td t2 d 2 t3 d 3
Euler l+1 l
(t ) = (t ) + + + + ... (O08.15)
1! dt tl 2! dt2 tl 3! dt3 tl
Taylor ! td t2 d 2 t3 d 3
l+1 l
Method order! (t ) = (t ) + + + + ... (O08.16)
1! dt 2! dt2 3! dt3
2 tl tl tl
l+1 l l l 1 @f l l l l 2 1 @f l l
(t ) = (t ) + 1 tf (t , )+ (t , )f (t , ) t + (t , ) t2
2@ 2 @t
gives
a1 + a2 = 1
1
a 2 p1 =
2
1
a2 q11 =
2
a1 + a2 = 1
1
a 2 p1 =
2
1
a2 q11 =
2
We have 4 unknowns, a1, a2, p1 & q11 but only 3 equations, hence there cannot be a unique solution.
l+1 l 1 1
= + t k1 + k2
2 2
k1 = f ( l , tl )
l l
k2 = f( + tk1 , t + t)
Example O08.3:
!
Using 2nd order Runge-Kutta 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
function MPO8p3()
clear all;
close all; l l
k1 = f( , t )
Delta_t=1;
l
t=0:Delta_t:8.0; k2 = f( + tk1 , tl + t)
%Preallocating memory
phi=zeros(size(t));
l+1 l 1 1
phi(1)=0.0;
= + t k1 + k2
for n=1:length(t)-1 2 2
k1=f(phi(n),t(n));
k2=f(phi(n)+Delta_t*k1,t(n)+Delta_t)
phi(n+1)=phi(n)+(Delta_t/2)*(k1+k2);
end
plot(t,phi,'rs-')
hold on
xlabel('t');
ylabel('\phi');
ezplot(@(t)1-exp(-t),[0,8,0,2])
function dphidt=f(phi,t)
dphidt=1-phi;
1exp(t) 2
2
1.8
t=1.0!
Euler
True 1.8 t=1.0! Taylor 2nd Order
True
1.6
Explcit Euler 1.6
Taylor 2nd order
1.4 1.4
1.2 1.2
1 1
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
0
0 1 2 3 4 5 6 7 8 0
t 0 1 2 3 4 5 6 7 8
t
1exp(t)
2
1.8
t=1.0!
Runge Kutta 2nd order
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 O08.3
To get better accuracy, one can use the 4th order Runge-Kutta
method
d
To solve (t) = f ( , t)
dt
4th-order Runge-Kutta (page 36 of printed lecture notes):
l+1 l 1 1 1 1
= + t k1 + k2 + k3 + k4
6 3 3 6
k1 = f ( l , tl )
l
k2 = f ( + tk1 /2, tl + t/2)
l l
k3 = f ( + tk2 /2, t + t/2)
l l
k4 = f ( + tk3 , t + t)
Example O08.4: Modify MATLAB program that uses the 2nd-
order Runge-Kutta method to instead use the 4th-order Runge-
Kutta method, solving
!
! d
! =1
! dt
!
for 0 < t < 8 with (0)=1.
l+1 l 1 1 1 1
= + t k1 + k2 + k3 + k4
6 3 3 6
l l
k1 = f ( , t )
l l
k2 = f ( + tk1 /2, t + t/2)
l l
k3 = f ( + tk2 /2, t + t/2)
l
k4 = f ( + tk3 , tl + t)
2nd order Runge Kutta 4th order Runge Kutta
k1 = f ( l , tl ) kfunction
1 = f ( l l
, tMPO8p4()
)
l l clear all;l l
k2 = f( + tk1 , t + t) kclose
2 = f ( +
all; tk 1 /2, t + t/2)
function MPO8p3() k3 = f ( l
+ tk2 /2, tl + t/2)
clear
l+1 all;
l 1 1 Delta_t=1;
= + t k1 + k2 l l
close all; 2 2 kt=0:Delta_t:8.0;
4 = f ( + tk 3 , t + t)
Delta_t=1; %Preallocating
l+1 l 1memory1 1 1
= + t k1 + k2 + k3 + k4
t=0:Delta_t:8.0; phi=zeros(size(t));
6 3 3 6
%Preallocating memory phi(1)=0.0;
phi=zeros(size(t));
for n=1:length(t)-1
phi(1)=0.0; k1=f(phi(n),t(n));
k2=f(phi(n)+Delta_t*k1/2,t(n)
for n=1:length(t)-1 +Delta_t/2);
k1=f(phi(n),t(n)); k3=f(phi(n)+Delta_t*k2/2,t(n)
k2=f(phi(n) +Delta_t/2);
+Delta_t*k1,t(n)+Delta_t) k4=f(phi(n)+Delta_t*k3,t(n)
phi(n+1)=phi(n)+(Delta_t/ +Delta_t);
2)*(k1+k2); phi(n+1)=phi(n)
end +Delta_t*(k1/6+k2/3+k3/3+k4/6);
end
plot(t,phi,'rs-')
hold on plot(t,phi,'rs-')
function MPO8p4()
clear all;
close all;
Delta_t=1;
t=0:Delta_t:8.0;
%Preallocating memory
phi=zeros(size(t));
phi(1)=0.0;
for n=1:length(t)-1
k1=f(phi(n),t(n));
k2=f(phi(n)+Delta_t*k1/2,t(n)+Delta_t/2);
k3=f(phi(n)+Delta_t*k2/2,t(n)+Delta_t/2);
k4=f(phi(n)+Delta_t*k3,t(n)+Delta_t);
phi(n+1)=phi(n)+Delta_t*(k1/6+k2/3+k3/3+k4/6);
end
plot(t,phi,'rs-')
hold on
xlabel('t');
ylabel('\phi');
ezplot(@(t)1-exp(-t),[0,8,0,2])
function dphidt=f(phi,t)
dphidt=1-phi;
d
Solve =1 for 0 < t < 8 with (0)=1.
dt
2
1exp(t)
Analytical (t) = 1 exp( t)
solution:
1.8
1.6
1.4
Explicit Euler
1.2
0.8
1exp(t)
2
0.6
4th-order!
1.8
0.4
1.6
0.2
0
0 1 2 3 4
t
5 6 7 8
1.4
1.2
Runge-Kutta
1
1exp(t)
2
0.8
1.8
2nd-order!
0.6
1.6
0.4
Runge-Kutta
1.4
0.2
1.2
0
0 1 2 3 4 5 6 7 8
t
1
0.8
0.6
0.4
0.2
0
0 1 2 3 4 5 6 7 8
Try bigger time steps: could we have predicted 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 O08.4