Vous êtes sur la page 1sur 37

ENGR90024

Computational Fluid Dynamics

Lecture O06
The Implicit Euler Method!

(see page 18 of printed lecture notes) Our analysis thus far shows that the explicit Eulers 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
2 2
3 3
t
d
t
d
t
d
l+1
l
l
l
l +...
(t ) = (t ) +
(t ) +
+
(t
)
(t
)
3
2
3! dt
2! dt
1! dt

If we now expand the Taylor series about t=tl+1

(tl ) = (tl+1 )

t d l+1
t2 d 2
l+1
(t ) +
(t
)
2
2! dt
1! dt

Ignoring higher order terms give

t3 d 3
l+1
+
.
.
.
(t
)
3! dt3

(see page 18 of printed lecture notes) Our analysis thus far shows that the explicit Eulers 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
2 2
3 3
t
d
t
d
t
d
l+1
l
l
l
l +...
(t ) = (t ) +
(t ) +
+
(t
)
(t
)
3
2
3! dt
2! dt
1! dt

If we now expand the Taylor series about t=tl+1

Ignoring higher order terms give


l

(t ) = (t

l+1

t d l+1
f(t
( l+1) , tl+1 )
1! dt

Hence,

l+1

tf (

The above equation is called the implicit Euler method.

l+1

l+1

,t

l+1

tf (

l+1

, tl+1 )

Eq. (O06.1) is the formula for the implicit Euler method.

Compare now with the formula for the explicit Euler method

l+1

tf ( , t )

The only difference is the time level the right hand side, d/dt=f(,t) is evaluated at.
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.
This will give rise to complications, especially if f(,t) is a nonlinear function (see later).

(O06.1)

(t)

Smaller t will lead to smaller error

True value of l+1


Error
l

Predicted value of l+1

t
t

l+1

(t)

Smaller t will lead to smaller error

True value of l+1


Error
Predicted value of

t
t

l+1

l+1

(t)

Smaller t will lead to smaller error

True value of l+1


Error

Predicted value of l+1

t
t

l+1

Example O06.1:
!

Using implicit Eulers 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

Compare your solution with the explicit Eulers method

Start with the implicit Eulers formula

l+1

l+1

tf (

l+1

,t

Replace f(,t) with the function given in the question

l+1

l+1

t (1

Rearranging gives

l+1

+ t
=
1+ t

function MPO06p1()
close all
clear all
tmin=0.0;
tmax=8.0;
[t1,phi1]=MyImplicitEuler([tmin tmax],0.0,1.0);

Output

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

1exp(t)

Euler
True

1.8
1.6
1.4

1.2
1
0.8
0.6
0.4
0.2
0

4
t

1exp(t)

Euler
True

t=2.0

1.8

1.8

1.6

1.4

1.2

1.2

Euler
True

t=1.0

1.6

1.4

0.8

0.8

0.6

0.6

0.4

0.4

0.2
0

1exp(t)

0.2
0

4
t

4
t

1exp(t)

2
1.8

t=0.5

1.6

1exp(t)

Euler
True

Euler
True

t=0.1

1.8
1.6

1.4
1.4
1.2
1

1.2
1

0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
0

4
t

4
t

Compare implicit and explicit Eulers method for t=2.0

1exp(t)

Euler
True

t=2.0

1.8

1.6

1.4

1.4
1.2

1.2

0.8

1
0.8

0.6

0.6

0.4

0.4

0.2
0

t=2.0

1.8

1.6

Euler
True

0.2

4
t

4
t

Implicit Eulers method is much more stable than the explicit Eulers method

End of Example O06.1

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

(1

t)

l+1

l+1

l+1
l+1

=
=

l+1

1
1

t
l

Observe that

| |=

1
1

=p

t
1
(1

Re

t)2 + (

Im

t)2

Hence, the stability region is given by

(1

Re

t)2 + (

This is any area outside a circle of radius 1 centred at (1,0)

Im

t)2

(1

Re

t) + (

Im

t)

Im

Re

(1

Re

t) + (

Im

t)

Im

Re

Note that the implicit Euler method is will be stable as long as Re is negative.
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.

Implicit Eulers method


for a system of equations
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
= f1 ( 1 , 2 , 3 , . . . . . . ,
dt
d 2
= f2 ( 1 , 2 , 3 , . . . . . . ,
dt
d 3
= f3 ( 1 , 2 , 3 , . . . . . . ,
dt
.. ..
.=.
d M
= fM ( 1 , 2 , 3 , . . . . . . ,
dt

M , t)
M , t)
M , t)

M , t)

Implicit Eulers method can easily be extended to solve a


system of M equations

d 1
= f1 (
dt

d 1
= f1 (
dt
d 2
= f2 (
dt
d 3
= f3 (
dt

1 , t)

IMPLICIT EULER
l+1
1

l
1

d 1
= f1 (
dt
d 2
= f2 (
dt

+ f1 (

l l
1, t )

1,

2 , t)

1,

2 , t)

IMPLICIT EULER
l+1
1
l+1
2

=
=

l
1
l
2

+ f1 (
+ f2 (

l+1
1 ,
l+1
1 ,

l+1 l+1
)
2 ,t
l+1 l+1
)
2 ,t

1,

2,

3 , t)

1,

2,

3 , t)

1,

2,

3 , t)

IMPLICIT EULER
l+1
1
l+1
2
l+1
3

=
=
=

l
1
l
2
l
3

+ f1 (
+ f2 (
+ f3 (

l+1
1 ,
l+1
1 ,
l+1
1 ,

l+1
2 ,
l+1
2 ,
l+1
2 ,

l+1 l+1
)
3 ,t
l+1 l+1
)
3 ,t
l+1 l+1
)
3 ,t

t
t
t

If {f} is a linear function, then the system of equations can


be put in matrix form as
d 1
= f1 ( 1 , 2 , 3 , t)
dt
d 2
= f2 ( 1 , 2 , 3 , t)
dt
d 3
= f3 ( 1 , 2 , 3 , t)
dt
8
d <
dt :

1
2
3

9
=

K11
= 4 K21
;
K31

K12
K22
K32

K13
K23
K33

38
<
5
:

1
2
3

9
=
;

8
d <
dt :

1
2
3

9
=

K11
= 4 K21
;
K31

K12
K22
K32

K13
K23
K33

38
<
5
:

1
2
3

9
=
;

d
{ } = [K] { }
dt
Applying implicit Eulers formula gives

l+1

[I

+ [K]

[K] t]
l+1

l+1

= [I

l+1

=
[K] t]

t
l
1

Example O06.2

!
Rewrite the program MPO05p1.m and use functions to solve the
following two ODEs
!

!
d
0
1
1
1
!
=
!
4
1
dt
2
2
!
for 0 < t < 10 with 1(t=0)=1 and 2(t=0)=0.
For this question

[K] =

0
4

So

[I

[K] t] =

1
1

1 0
0 1

0
4

For a linear set of ODEs, the implicit Eulers formula gives

l+1

= [I

[K] t]

1
1

function 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');!

[K] =

!
!
!
!

0
4

1
1

[K] t]

function [t,phi]=MyEulerLinearSysODEs(ode,tspan,phi0,Delta_t)!
!

a=tspan(1);!
b=tspan(2);!

[I

t=a:Delta_t:b;!
!

phi=zeros(length(t),numel(phi0));!
!

phi(1,:)=phi0;
!

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

%setting initial conditions!

l+1

= [I

[K] t]

t=1.0

t=0.5

10

10

3
1

t=0.2

t=0.1

10

10

Note that the solution computed using implicit Eulers method is stable for all t. This is in!
contrast with the solution computed using explicit Eulers method which was unstable for larger!
values of t (see Example O05.1)

End of Example O06.2

Nonlinear ODEs

Example O06.3a: Use explicit Eulers method to solve the ODE!


!

d
=
dt

!
in the domain 0<t<2x105. You are given that (0)=10-5
Applying the explicit Euler equation gives

l+1

t( (

l 2

( ) )

Computing the explicit Euler solution is straight forward using the above equation

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));

l+1

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);

t( (

l 2

( ) )

1.4
Explicit Euler
MATLAB
1.2

0.8

0.6

0.4

0.2

0.2

0.4

0.6

0.8

1
t

1.2

1.4

1.6

1.8

2
5

x 10

Example O06.3b: Use implicit Euler method to solve the ODE!


!

d
=
dt

!
in the domain 0<t<2x105. You are given that (0)=10-5
The last example shows that applying the explicit Euler equation gives

l+1

t( (

l 2

( ) )

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 Eulers formula

l+1

tf (

l+1

, tl+1 )

gives

l+1

l+1

l+1

l+1 2

l+1

l+1

t((

l+1 2

l+1

l+1 2

l+1 3

Rearranging gives

l+1

) )

=0

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

t((

2 2

t((

3 2

t((

4 2

)
)
)

= Given

2 3

3 3

4 3

) )
) )
) )

=0
=0
=0

Solve

to get
Solve
to get
Solve

to get

4
3
1

tmin

tmin +

tmin + 2 t

tmin + 3 t

t((

4 2

4 3

) )

known
3

=0

We know 3. We want to find 4

Cubic polynomial!
in terms of p

known

t(p2

p3 )

K=0

g(p) = 0
How do we solve for p? Use Newton-Raphson method

g(p)

tang
ent

to th

e po
int p

old

g(pold )

pnew
The value!
of p that you!
want to find

pnew = pold

pold
g(pold )
dg
(p
)
old
dp

g(p)

ta
ng
en

tt

th

po
int

po

ld

g(pold )

pnew
The value!
of p that you!
want to find

pold
pnew = pold

g(pold )
dg
(p
)
old
dp

g(p)

g(pold )
o
tt

th

int
po

p ol

n
ge
tan

pnew pold
The value!
of p that you!
want to find

pnew = pold

g(pold )
dg
(p
)
old
dp

function MPO06p4()
clear all;
close all;

0.1(p2

g(p) = p

p3 )

10

dg
2
= 1 0.1(2p 3p )
dp
g(pold )
pnew = pold
dg
dp (pold )

%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

g(p)

3.2

3.4

3.6

3.8

4.2

4.4

4.6

4.8

Vous aimerez peut-être aussi