Vous êtes sur la page 1sur 45

ENGR90024

Computational Fluid Dynamics

Lecture 08
Euler Method: Error Analysis &!

Runge Kutta Methods!


Error Analysis
In many engineering applications, the solution would behave in a !
sinusoidal fashion. In this section, we are going to analyse the error in
using implicit Eulers method to solve sinusoidal problem. The model
problem we will be dealing with (which will give sinusoidal solution) is
d
= i! (O08.1)
dt
Find analytical solution!
at time level lt

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

The analytical solution to Eq. (O08.1) is given as


1 i!t
(t) = e
where 1 is the value of (t=0).

Hence the true solution at lt is


1 i!l t
(t = l t) = e (O08.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
=
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

is just a complex number which can be written in polar form as


i
= Ze (8.4)
where

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
=

Dividing the numerical solution (Eq. (O08.2)) with the


analytical solution (Eq. (O08.3)) gives
l+1 l
=
(l t) ei!l t

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

Approximated solution True solution

From Eq. (O08.5), it is clear that the approximate solution,


l+1, is the true solution, (lt), amplified by Z1 with its !
phase shifted byl( ! t)
Z is called the amplitude error
( ! t) is called the phase error
l+1 l il( ! t)
= (l t)Z e

Recall from Lecture O04 that for explicit Euler method


p
Zexplicit Euler = 1 + (! t)2

We have just shown that for implicit Euler method


1
Zimplicit Euler = p
1 + (! t)2

Note that for finite values of t, Zimplicit Euler < 1 but


Zexplicit Euler > 1. Hence, for sinusoidal problems, implicit
Euler solutions will decay with time and explicit Euler
solution will grow with time.
Note that the phase error

( ! 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

(! t)3 (! t)5 (! t)7


arctan(! t) = ! t + + ...
3 5 7
Substituting into Eq. (O08.6)
( ! t) = arctan(! t) ! t

gives the phase error to be


3 5 7

(! t) (! t) (! t)
arctan(! t) ! t = ! t + + ... ! t
3 5 7
(! t)3 (! t)5 (! t)7
= + + ...
3 5 7 3
(! t)
The leading order term of the phase error is 3
Recall from Lecture O04, the leading order term for the phase error for
3
the explicit Euler method is also (! t)
3

So the phase error for the explicit Eulers method is the


same as the implicit Eulers method
Example O08.1: Compute the solution to !
!
! d
!
= i2
dt
!
using both the implicit and explicit Eulers method. Convince
yourself that!
!
The explicit Eulers solution grow with time and the implicit
Eulers solution decay with time.!
The phase error for both the explicit and implicit Eulers
solution are the same. !
!
Use 0<t<10 and you are given that (0)=1.0.
Explicit Euler Implicit Euler
d d
= i2 = i2
dt dt

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

Phase of the implicit and explicit Euler solution


(t)

1 are the same.


2

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

The eigenvalues for



0 1
eig = i!
!2 0
Thus, using the method described in Lecture O05 (see Example O05.1), the above equation can be
decoupled to be
d 1
= i! 1
dt
d 2
= i! 2
dt
The eigenvalues lies outside the stability region of the explicit Euler method. That is why solution
obtained using explicit Euler method is always unstable for this problem.

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

In general, Runge-Kutta solution to

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)

2nd order Runge-Kutta formula

Strategy to find Compare


a1, a2, q11 and p1
Taylor series

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 )

Put Eqs. (O08.11) and (O08.12) into (O08.13) gives

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

Strategy to find Compare


a1, a2, q11 and p1
Taylor series
2nd order Runge-Kutta formula

Strategy to find Compare


a1, a2, q11 and p1
Taylor series

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

Initial value problem can be expressed as


d
= f (t, )
dt
Put into Eq. (O08.16) and neglecting higher order terms give
2
t l t df l
(t ) = (t ) +
l+1 l
f (t , l
)+ (t , l
)
1! 2! dt
2
t l t df l
(t ) = (t ) +
l+1 l
f (t , l
)+ (t , l
)
1! 2! dt
2
l+1 l t l l @f l lt @f l l d
(t ) = (t ) + f (t , )+
(t , ) + (t , )
1! @t 2! @ dt
2

l+1 l t l l t @f l l @f l l
(t ) = (t ) + f (t , ) + (t , ) + (t , )f (tl , l
) (O08.17)
1! 2! @t @
The equation above is called the Taylor Method order 2. It was shown in Lecture O03 that is it more
accurate than Euler method. The main disadvantage is that we have to calculate the derivatives df/dt
and df/d in order to use it to compute solution to ODE. The 2nd order Runge-Kutta method
allows us to get the accuracy of Eq. (O08.13) without having to calculate the derivatives.

2nd order Runge-Kutta formula

Strategy to find Compare


a1, a2, q11 and p1
Taylor series
2nd order Runge-Kutta formula

Strategy to find Compare


a1, a2, q11 and p1
Taylor series

Compare Eq. (O08.14)


l+1 l @f @f
= + (a1 + a2 ) tf ( , t ) + a2 q11 f ( , t ) ( t) + a2 p1 ( t)2 + . . .
l l 2 l l
@ @t
with Eq. (O08.17)

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.

One possible solution is to set a2=1/2 so that we have


1
a1 =
2
p1 = 1
q11 = 1
Thus the 2nd order Runge-Kutta method can be written as


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

1exp(t) 1exp(t) 1exp(t)


2 2 30

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

1exp(t) 1exp(t) 1exp(t)


2 2 30

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

Vous aimerez peut-être aussi