Académique Documents
Professionnel Documents
Culture Documents
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
(tl ) = (tl+1 )
t d l+1
t2 d 2
l+1
(t ) +
(t
)
2
2! dt
1! dt
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
(t ) = (t
l+1
t d l+1
f(t
( l+1) , tl+1 )
1! dt
Hence,
l+1
tf (
l+1
l+1
,t
l+1
tf (
l+1
, tl+1 )
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)
t
t
l+1
(t)
t
t
l+1
l+1
(t)
t
t
l+1
Example O06.1:
!
!
!
!
!
!
d
=1
dt
l+1
l+1
tf (
l+1
,t
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
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
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
(1
Re
t)2 + (
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.
M , t)
M , t)
M , t)
M , t)
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
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
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
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)
Nonlinear ODEs
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
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
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 )
g(p)
3.2
3.4
3.6
3.8
4.2
4.4
4.6
4.8