ENGR90024
Computational Fluid Dynamics
Lecture O06
(see page 18 of printed lecture notes) Our analysis thus far shows that the explicit Euler’s method is not very accurate or stable. To come up with a method that is more stable, consider the Taylor series expanded about t=tl
If we now expand the Taylor series about t=tl+1
Ignoring higher order terms give
+ . . .
+
t
22!
d
2dt
2(t
l)
(t
l+1) = (t
l) +
t
1!
d
dt
(t
l)
+
t
33!
d
3dt
3(t
l)
+ . . .
(t
l) = (t
l+1)
t
1!
d
dt
(t
l+1)
+
t
22!
d
2dt
2(t
l+1)
t
33!
d
3dt
3(t
l+1)
(see page 18 of printed lecture notes) Our analysis thus far shows that the explicit Euler’s method is not very accurate or stable. To come up with a method that is more stable, consider the Taylor series expanded about t=tl
If we now expand the Taylor series about t=tl+1
Ignoring higher order terms give
l+1
=
l+
tf (
l+1, t
l+1)
The above equation is called the implicit Euler method. Hence,
+
t
22!
d
2dt
2(t
l)
(t
l+1) = (t
l) +
t
1!
d
dt
(t
l)
+
t
33!
d
3dt
3(t
l)
+ . . .
(t
l) = (t
l+1)
t
1!
d
dt
(t
l+1)
f (
l+1, t
l+1)
l+1
=
l+
tf (
l+1, t
l+1)
(O06.1)Eq. (O06.1) is the formula for the implicit Euler method.
l+1
=
l+
tf (
l, t
l)
The only difference is the time level the right hand side, dɸ/dt=f(ɸ,t) is evaluated at. Compare now with the formula for the explicit Euler method
For the explicit Euler method, the function f(ɸ,t) is evaluated at time level tl.
For the implicit Euler method, the the function f(ɸ,t) is evaluated at time level tl+1.
(t)
t
l
t
l+1
l
t
Predicted value of ɸ
l+1
True value of ɸ
l+1
Smaller Δt will lead to smaller error
(t)
t
t
l
t
l+1
l
Predicted value of ɸ
l+1
True value of ɸ
l+1
Smaller Δt will lead to smaller error
(t)
t
t
l
t
l+1
l
Predicted value of ɸ
l+1
True value of ɸ
l+1
Smaller Δt will lead to smaller error
Example O06.1:
!
Using implicit Euler’s method, solve
! ! ! ! !
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 the explicit Euler’s method
d
l+1
=
l+
t
Start with the implicit Euler’s formula
Replace f(ɸ,t) with the function given in the question
(1
l+1)
l+1
=
l+
tf (
l+1, t
l+1)
Rearranging gives
l+1
=
l+
t
function MPO06p1() close all clear all tmin=0.0; tmax=8.0; [t1,phi1]=MyImplicitEuler([tmin tmax],0.0,1.0); plot(t1,phi1,'ko-'); hold on ezplot(@(t)1-exp(-t),[0,8,0,2]) xlabel('t'); ylabel('\phi');
legend('Euler','True');
function [t,phi]=MyImplicitEuler(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)=(phi(n)+Delta_t)/(1+Delta_t); end 0 1 2 3 4 5 6 7 8 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 t 1−exp(−t) φ Euler True
Output
0 1 2 3 4 5 6 7 8 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 t 1−exp(−t) φ Euler True 0 1 2 3 4 5 6 7 8 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 t 1−exp(−t) φ Euler True 0 1 2 3 4 5 6 7 8 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 t 1−exp(−t) φ Euler True 0 1 2 3 4 5 6 7 8 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 t 1−exp(−t) φ Euler True
Δt=2.0
Δt=1.0
Δt=0.5
Δt=0.1
0 1 2 3 4 5 6 7 8 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 t 1−exp(−t) φ Euler True
Δt=2.0
0 1 2 3 4 5 6 7 8 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 t φ Euler TrueΔt=2.0
Compare implicit and explicit Euler’s method for Δt=2.0
Stability analysis of the implicit Euler Method
We will now analyse why the Implicit Euler method is so stable. Consider the model problem
d
dt
=
Applying the implicit Euler method gives
l+1
=
l+
t
l+1(1
t)
l+1=
ll+1
=
1
1
t
lObserve that
| | =
1
1
t
=
p
1
(1
Ret)
2+ (
Imt)
2This is any area outside a circle of radius 1 centred at (1,0) Hence, the stability region is given by
Im t
Re
t
1 2
Im t
Re
t
Note that the implicit Euler method is will be stable as long as λRe is negative.
1 2
(1
Ret)
2+ (
Imt)
21
This does not mean the the Euler method is accurate, only that the method is very stable. The implicit Euler method is much more stable than the explicit Euler method.
So far, we have learnt to solve single ODE using implicit
Euler method. What if you are required a system of M
ODEs?
d 1 dt = f1( 1, 2, 3, . . . , M, t) d 2 dt = f2( 1, 2, 3, . . . , M, t) d 3 dt = f3( 1, 2, 3, . . . , M, t) .. . = ... d M dt = fM( 1, 2, 3, . . . , M, t)Implicit Eulers method can easily be extended to solve a
system of M equations
Implicit Euler’s method
for a system of equations
d 1 dt = f1( 1, 2, t) d 2 dt = f2( 1, 2, t) IMPLICIT EULER d 1 dt = f1( 1, t) l+1 1 = l1 + f1( l1, tl) t IMPLICIT EULER
IMPLICIT EULER
d
1
dt
= f
1
(
1
,
2
,
3
, t)
d
2
dt
= f
2
(
1
,
2
,
3
, t)
d
3
dt
= f
3
(
1
,
2
,
3
, t)
l+1 1 = l1 + f1( l+11 , l+12 , tl+1) t l+1 2 = l2 + f2( l+11 , l+12 , tl+1) t l+1 1=
l1+ f
1(
l+11,
l+12,
l+13, t
l+1) t
l+1 2=
l2+ f
2(
l+11,
l+12,
l+13, t
l+1) t
l+1 3=
l3+ f
3(
l+11,
l+12,
l+13, t
l+1) t
d
1
dt
= f
1
(
1
,
2
,
3
, t)
d
2
dt
= f
2
(
1
,
2
,
3
, t)
d
3
dt
= f
3
(
1
,
2
,
3
, t)
If {f} is a linear function, then the system of equations can
be put in matrix form as
d dt 8 < : 1 2 3 9 = ; = 2 4 KK1121 KK1222 KK1323 K31 K32 K33 3 5 8 < : 1 2 3 9 = ;
d dt 8 < : 1 2 3 9 = ; = 2 4 KK1121 KK1222 KK1323 K31 K32 K33 3 5 8 < : 1 2 3 9 = ;
Applying implicit Euler’s formula gives
l+1
=
l+ [K]
l+1t
[I
[K] t]
l+1=
ll+1
= [I
[K] t]
1 ld
Example O06.2
!
Rewrite the program MPO05p1.m and use functions to solve the following two ODEs
! ! ! ! !
for 0 < t < 10 with ɸ1(t=0)=1 and ɸ2(t=0)=0.
d
dt
⇢
1 2=
0
1
4
1
⇢
1 2[K] =
0
1
4
1
[I
[K] t] =
1 0
0 1
0
1
4
1
t
For this question
So
l+1
= [I
[K] t]
1 lfunction MPO06p2()! ! close all! clear all! ! tmin=0.0;! tmax=10.0;! ! phi0=[1 0];! Delta_t=0.01;! ! [t,phi]=MyEulerLinearSysODEs(@f,[tmin tmax],phi0,Delta_t);! ! plot(t,phi(:,1),'k-',t,phi(:,2),'r-');!
legend('\phi_1','\phi_2');! ! ! ! ! function [t,phi]=MyEulerLinearSysODEs(ode,tspan,phi0,Delta_t)! ! a=tspan(1);! b=tspan(2);! ! t=a:Delta_t:b;! ! phi=zeros(length(t),numel(phi0));! !
phi(1,:)=phi0; %setting initial conditions! ! K=[0 1; -4 -1];! temp=inv(eye(2)-K*Delta_t)! ! for n=1:length(t)-1! temp2=temp*phi(n,:)'! phi(n+1,:)= temp2';! end
[K] =
0
1
4
1
[I
[K] t]
1 l+1= [I
[K] t]
1 l0 1 2 3 4 5 6 7 8 9 10 −3 −2 −1 0 1 2 3 φ1 φ2 0 1 2 3 4 5 6 7 8 9 10 −3 −2 −1 0 1 2 3 φ1 φ2 0 1 2 3 4 5 6 7 8 9 10 −3 −2 −1 0 1 2 3 φ1 φ2 0 1 2 3 4 5 6 7 8 9 10 −3 −2 −1 0 1 2 3 φ1 φ2
Δt=1.0
Δt=0.5
Δt=0.2
Δt=0.5
Δt=0.1
Note that the solution computed using implicit Euler’s method is stable for all Δt. This is in!
contrast with the solution computed using explicit Euler’s method which was unstable for larger! values of Δt (see Example O05.1)
Example O06.3a:
Use explicit Euler’s method to solve the ODE!
! ! !
in the domain 0<t<2x105. You are given that ɸ(0)=10-5
d
dt
=
2
l+1
=
l+
t(
l(
l(
l)
2)
Applying the explicit Euler equation gives
function MPO06p3a() clear all; close all; epsilon=0.00001; Delta_t=1; t=0:Delta_t:2.0/epsilon; %Preallocating memory phi=zeros(size(t)); phi(1)=epsilon; for l=1:length(t)-1 phi(l+1)=phi(l)+phi(l)*(phi(l)-phi(l)^2)*Delta_t; end %
%Computing Matlab solution %
[tmat,phimat]=ode23(@f,[0 2/epsilon],epsilon);
plot(t,phi,'ko-',tmat,phimat,'bs-')
hold on
xlabel('t');
ylabel('\phi');
legend('Explicit Euler','MATLAB');
function dphidt=f(t,phi) dphidt=phi*(phi-phi^2);
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 x 105 0 0.2 0.4 0.6 0.8 1 1.2 1.4 t φ Explicit Euler MATLAB
Example O06.3b:
Use implicit Euler method to solve the ODE!! ! !
in the domain 0<t<2x105. You are given that ɸ(0)=10-5
d
dt
=
2
l+1
=
l+
t(
l(
l(
l)
2)
The last example shows that applying the explicit Euler equation gives
At any time level, we know the right hand side of this equation. So computing the explicit Euler solution is therefore straight forward using the above equation (see MPO06p3a.m)
Applying the implicit Euler’s formula
l+1
=
l+
tf (
l+1, t
l+1)
gives
Rearranging gives
For every time step, we typically know ɸl. We need to find ɸl+1. Details of the steps one needs to take
are as follows
l+1
=
l+
t
l+1 l+1(
l+1)
2Solve to get
1
= Given
2t((
2)
2(
2)
3)
1= 0
2 3t((
3)
2(
3)
3)
2= 0
Solve 3 to get 4t((
4)
2(
4)
3)
3= 0
Solve to get 4t
t
min 1 2t
min+
t
tmin + 2 t tmin + 3 t3
We know ɸ3. We want to find ɸ4
How do we solve for p? Use Newton-Raphson method
g(p) = 0
p
t(p
2p
3)
K = 0
4t((
4)
2(
4)
3)
3= 0
known known Cubic polynomial! in terms of pp
The value! of p that you! want to findp
newp
old tang ent to the point poldg(p)
g(p
old)
p
new= p
old dgg(p
old)
p
The value! of p that you! want to findp
old tang ent to the point poldp
newg(p)
g(p
old)
p
new= p
old dgg(p
old)
p
The value! of p that you! want to findp
old tangent to the point poldp
newg(p)
g(p
old)
p
new= p
old dgg(p
old)
function MPO06p4()
clear all;
close all;
%initial guess value of p
p=3; gp=p-0.1*(p^2-p^3)-10; dgdp=1-0.1*(2*p-3*p^2); while abs(gp)>1.0e-13 p=p-gp/dgdp; gp=p-0.1*(p^2-p^3)-10; dgdp=1-0.1*(2*p-3*p^2); end p
p
new= p
old dgg(p
old)
dp