Lecture O03: ENGR90024 Computational Fluid Dynamics
Lecture O03: ENGR90024 Computational Fluid Dynamics
Lecture O03
Analysis of the Eulers Method!
l+1
td
) = (t ) +
1! dt
l
tl
Eulers method
(t
l+1
t f (tl , l )
) = (t ) +
1!
l
Taylor series
td
l+1
l
(t ) = (t ) +
1! dt
t2 d 2
+
2
2!
dt
l
t
tl
tl
t l
l+1
l
(t ) = (t ) +
f (t ,
1!
2 2
t
d
l
)+
2! dt2
t l
l+1
l
(t ) = (t ) +
f (t ,
1!
2
t
d d
l
)+
2! dt dt
t l
l+1
l
(t ) = (t ) +
f (t ,
1!
2
t
d
l
)+
f (t, (t))
2! dt
(t
(t
l+1
l+1
t l
) = (t ) +
f (t ,
1!
l
t l
) = (t ) +
f (t ,
1!
l
t
)+
2!
2
t
)+
2!
@f l
(t ,
@t
@f l
(t ,
@t
tl
tl
Chain rule
tl
@f l
)+
(t ,
@
@f l
)+
(t ,
@
)f (tl ,
d
)
dt
l
(tl+1 ) = (tl ) +
t l
f (t ,
1!
)+
t
2!
@f l
(t ,
@t
)+
@f l
(t ,
@
)f (tl ,
O03.1
Example O03.1:
!
!
!
!
!
!
d
=1
dt
t l
f (t ,
1!
)+
t
2!
@f l
(t ,
@t
)+
@f l
(t ,
@
f (t, ) = 1
@f
=0
@t
@f
=
@
)f (tl ,
(tl+1 ) = (tl ) +
t l
f (t ,
1!
)+
t
l+1
l
(t ) = (t ) +
(1
1!
l+1
@f
=
@
@f
=0
@t
f (t, ) = 1
t
l
= +
(1
1!
t
2!
@f l
(t ,
@t
)+
@f l
(t ,
@
)f (tl ,
2
t
l
)+
0 + 1(1
2!
2
t
l
)+
1
2!
Eulers method!
Example O02.1
l+1
t(1
function MPO02p1()
clear all;
close all;
Delta_t=2.0;
t=0:Delta_t:8.0
phi(1)=0.0;
Only need!
to change one!
line of code!
l+1
t
+
(1
1!
t2
)+
1
2!
function MPO03p1()
clear all;
close all;
Delta_t=0.5;
phi(1)=0.0;
t=0:Delta_t:8.0
for l=1:length(t)-1
phi(l+1)=phi(l)
+Delta_t*(1-phi(l))
end
for l=1:length(t)-1
phi(n+1)=phi(n)+Delta_t*(1phi(l))-(Delta_t^2/2)*(1-phi(l))
end
plot(t,phi,'ko-')
hold on
ezplot(@(t)1-exp(-t),
[0,8,0,2])
xlabel('t');
ylabel('\phi');
legend('Euler','True');
plot(t,phi,'ko-')
hold on
ezplot(@(t)1-exp(-t),[0,8,0,2])
xlabel('t');
ylabel('\phi');
legend('Euler','True');
function MPO03p1()
clear all;
close all;
Delta_t=0.5;
phi(1)=0.0;
t=0:Delta_t:8.0
for l=1:length(t)-1
phi(l+1)=phi(l)+Delta_t*(1-phi(l))-(Delta_t^2/2)*(1-phi(l))
end
plot(t,phi,'ko-')
hold on
ezplot(@(t)1-exp(-t),[0,8,0,2])
xlabel('t');
ylabel('\phi');
legend('Euler','True');
Output
1.6
1.4
1.2
Note: the
Taylor
method for
t=2 predicts
=0 for all
values of t!
1.8
1
0.8
0.6
0.4
0.2
0
1.8
t=2.0
1.6
1.6
1.2
1.2
1.4
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
t=1.0
1.8
1.4
4
t
1.6
1.2
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
4
t
4
t
t=0.1
1.6
1.2
1.8
1.4
1.4
t=0.5
1.8
4
t
Euler
1exp(t)
2
Euler
True
1.8
1.4
1.4
t=1.0
1.2
1.2
1
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
0
0
4
t
4
t
1.4
1.6
1.4
1.2
1.2
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
4
t
1.8
t=0.5
1.6
2
Euler
True
1.8
t=1.0
1.6
1.6
1.8
t=0.5
4
t
For a given t, the Taylor 2nd order method is more accurate than
Euler
When we make t big enough, the solution will blow up. Can we
predict the value of t where the solution will blow up? For a
numerical method to be useful, we need it to be stable.
Stability and consistency are two quite different things (see later). It is
possible for a method can be consistent but not stable.
d
=
dt
(O03.2)
Re
+i
Im
Strategy
d
=
dt
Compare
Find approximated Eulers!
solution at time level lt
(t)
1 (
Re t
ei
Re t
(cos(
e
e
t
Re +i Im )t
Im t
Im t)
+ i sin(
Im t))
(t)
Re t
(cos(
Im t)
+ i sin(
(t)
Im
(t)
(t)
Im
(t)
(t)
Re
Re
Re
Im t))
(t)
Re t
(cos(
Im t)
+ i sin(
Im t))
d
=
dt
Compare
Find approximated Eulers!
solution at time level lt
Applying Eulers formula for the model problem (Eq. (O03.2)) gives
l+1
= (1 +
=
t)
(O03.3)
l+1
..
.
l+1
=
=
=
=
=
=
Given
1
2
3
..
.
=
l
2 1
=
=
=
3 1
..
.
l 1
(O03.4)
For solution to be
stable!| | 1
| |2
1+
(1 +
Re
(1 +
Re ) + ( t
+i t
2
Im )
2
Im ) 1
Im
-2
-1
Re
Example O03.2:
!
!
!
!
!
!
d
=
dt
for 0 < t < 2 with (t=0)=1. Using the analysis from the previous slides, what
are the values of t for the numerical solution to be stable?
We have
d
= 8
dt
So for this problem =-8. So Re=-8 and Im=0.0. In order for the
numerical solution to be stable, t need to be within the stable
region.!
Im t
-8t, t large
-2
-1
Re
t0
8 t0
t 1/4
So for solution to !
be stable t must!
be between 0 and
0.25
Im
-2
-1
Re
function MPO03p2()
clear all;
close all;
Delta_t=0.01;
t=0:Delta_t:2.0;
%Preallocating memory
phi=zeros(size(t));
phi(1)=1.0;
for l=1:length(t)-1
phi(l+1)=phi(l)+Delta_t*(-8*phi(l));
end
plot(t,phi,'ko-')
hold on
ezplot(@(t)exp(-8*t),[0,2,-2,2])
xlabel('t');
ylabel('\phi');
legend('Euler','True');
100
Euler
True
80
t=0.5
60
0.5
40
Euler
True
t=0.25
1.5
20
0.5
20
40
1.5
0.2
0.4
0.6
0.8
1
t
1.2
1.4
1.6
1.8
0.2
1.5
0.8
1
t
1.2
1.4
1.6
1.8
0.5
0.5
0.5
0.5
1.5
1.5
0.2
0.4
0.6
0.8
1
t
1.2
1.4
1.6
1.8
Euler
True
t=0.01
1.5
t=0.1
0.6
Euler
True
0.4
0.2
0.4
0.6
0.8
1
t
1.2
1.4
1.6
1.8
100
Euler
True
80
t=0.5
60
0.5
40
Euler
True
t=0.25
1.5
20
0.5
20
40
1.5
0.2
0.4
0.6
0.8
1
t
1.2
1.4
1.6
1.8
0.2
1.5
0.8
1
t
1.2
1.4
1.6
1.8
0.5
0.5
0.5
0.5
1.5
1.5
0.2
0.4
0.6
0.8
1
t
1.2
1.4
1.6
1.8
Euler
True
t=0.01
1.5
t=0.1
0.6
Euler
True
0.4
0.2
0.4
0.6
0.8
1
t
1.2
1.4
1.6
1.8
Im
-2
-4
-1
Re
100
Euler
True
80
t=0.5
60
40
20
t=
20
40
0.2
0.4
0.6
0.8
1
t
1.2
1.4
1.6
1.8
8 0.5 =
Im
-2
-1
t=0.25
1.5
Re
Euler
True
0.5
0.5
t=
1.5
0.2
0.4
0.6
0.8
1
t
1.2
1.4
1.6
1.8
8 0.25 =
Im
-2
-1
Re
2
Euler
True
t=0.1
1.5
0.5
0.5
t=
1.5
0.2
0.4
0.6
0.8
1
t
1.2
1.4
1.6
1.8
8 0.1 =
0.8
Im
-2
-1
Re
2
Euler
True
t=0.01
1.5
0.5
0.5
t=
1.5
0.2
0.4
0.6
0.8
1
t
1.2
1.4
1.6
1.8
8 0.01 =
0.08
Re
+i
Im
d
=
dt
d
=i
dt
Im
Example O03.3:
!
!
!
!
!
!
d
= 2i
dt
for 0 < t < 10 with (t=0)=1. Using the analysis from the previous slides, what
are the values of t for the numerical solution to be stable?
d
= 2i
dt
0 t 10
(t = 0) = 1
= Ae2it
1 + 0i = ARe + iAIm
Hence
ARe = 1
AIm = 0
d
= 2i
dt
0 t 10
(t = 0) = 1
ARe = 1
AIm = 0
(t)
e2it
cos(2t) + i sin(2t)
function MPO03p3()!
clear all;!
close all;!
Delta_t=0.02;!
!
!
t=0:Delta_t:10.0;!
%Preallocate Memory!
phi=zeros(size(t));!
phi(1)=1.0;!
!
!
for n=1:length(t)-1!
phi(n+1)=phi(n)+Delta_t*(i*2*phi(n));!
end!
!
plot(t,imag(phi),'ko-')!
xlabel('t');!
ylabel('\phi');!
hold on!
ezplot(@(t)sin(2*t),[0,10,-5,5])!
legend('Euler','True');
Im()
sin(2 t)
5
4
t=0.5
Euler
True
t=0.2
4
3
sin(2 t)
5
Euler
True
1
2
2
3
3
4
4
5
5
t
10
sin(2 t)
Euler
True
0
1
5
t
5
t
10
10
Euler
True
t=0.02
2
1
sin(2 t)
t=0.1
5
t
10
Im
t = 2i 0.5 = i
-1
Re
sin(2 t)
Euler
True
t=0.5
3
2
1
-2
0
1
2
3
4
5
10
Im
0.4i
t = 2i 0.2 = 0.4i
-1
Re
sin(2 t)
Euler
True
t=0.2
4
3
2
1
-2
0
1
2
3
4
5
10
Im
t = 2i 0.1 = 0.2i
0.2i
-1
Re
sin(2 t)
Euler
True
t=0.1
4
3
2
1
-2
0
1
2
3
4
5
5
t
10
Im
t = 2i 0.02 = 0.04i
0.04i
-1
Re
t
sin(2 t)
Euler
True
t=0.02
4
3
2
1
-2
0
1
2
3
4
5
10