Professional 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
t d 2 2 3 3
l+1 l l t d l t d l +...
(t ) = (t ) + (t ) + 2
(t ) + 3
(t )
1! dt 2! dt 3! dt
t d l+1 t2 d 2 t3 d 3
(tl ) = (tl+1 ) (t ) + (t l+1
) (t l+1
) + . . .
1! dt 2! dt 2 3! dt3
Ignoring higher order terms give
(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
t d 2 2 3 3
l+1 l l t d l t d l +...
(t ) = (t ) + (t ) + 2
(t ) + 3
(t )
1! dt 2! dt 3! dt
l l+1 t d l+1
(t ) = (t ) ( l+1) , tl+1 )
f(t
1! dt
Hence,
Compare now with the formula for the explicit Euler method
l+1 l l l
= + 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).
(t) Smaller t will lead to smaller error
Error
l
Predicted value of l+1
t
l
t t l+1
(t) Smaller t will lead to smaller error
Error
l
Predicted value of l+1
t
l
t t l+1
(t) Smaller t will lead to smaller error
t
l
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 l+1
= + t (1 )
Rearranging gives
l
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);
plot(t1,phi1,'ko-');
hold on
Output
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); 1exp(t)
2
phi=zeros(length(t),1);
phi(1)=phi0; Euler
for n=1:length(t)-1 1.8 True
phi(n+1)=(phi(n)+Delta_t)/(1+Delta_t);
end 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
1exp(t) 1exp(t)
2 2
Euler Euler
1.8
t=2.0 True
1.8 True
1.6
1.6
t=1.0
1.4
1.4
1.2
1.2
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
1exp(t)
Euler 2
1.8 True Euler
1.6
t=0.5 1.8
t=0.1
True
1.6
1.4
1.4
1.2
1.2
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
Compare implicit and explicit Eulers method for t=2.0
1exp(t)
2
2
Euler Euler
1.8
t=2.0 True
1.8 t=2.0 True
1.6
1.6
1.4
1.4
1.2
1.2
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
0 1 2 3 4 5 6 7 8
t
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
l+1 l l+1
= + t
l+1 l
(1 t) =
l+1 1
= l
1 t
l+1 l
=
Observe that
1
| |=
1 t
1
=p
(1 Re t)2 + ( Im t)2
(1 Re t)2 + ( Im t)2 1
1 2 t
Re
2 2
(1 Re t) + ( Im t) 1 Im t
1 2 t
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 , . . . . . . , M , t)
dt
d 2
= f2 ( 1 , 2 , 3 , . . . . . . , M , t)
dt
d 3
= f3 ( 1 , 2 , 3 , . . . . . . , M , t)
dt
.. ..
.=.
d M
= fM ( 1 , 2 , 3 , . . . . . . , M , t)
dt
IMPLICIT EULER
8 9 2 38 9
K11 K12 K13
d < 1 = < 1 =
2 = 4 K21 K22 K23 5 2
dt : ; : ;
3 K31 K32 K33 3
8 9 2 38 9
K11 K12 K13
d < 1 = < 1 =
2 = 4 K21 K22 K23 5 2
dt : ; : ;
3 K31 K32 K33 3
d
{ } = [K] { }
dt
l+1 l l+1
= + [K] t
l+1 l
[I [K] t] =
l+1 1 l
= [I [K] t]
Example O06.2
!
Rewrite the program MPO05p1.m and use functions to solve the
following two ODEs
!
!
!
d 1 0 1 1
=
! dt 2 4 1 2
!
for 0 < t < 10 with 1(t=0)=1 and 2(t=0)=0.
l+1 1 l
= [I [K] t]
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);! 0 1
! [K] =
plot(t,phi(:,1),'k-',t,phi(:,2),'r-');!
legend('\phi_1','\phi_2');!
4 1
!
!
!
!
function [t,phi]=MyEulerLinearSysODEs(ode,tspan,phi0,Delta_t)!
!
a=tspan(1);!
1
b=tspan(2);!
!
[I [K] t]
t=a:Delta_t:b;!
!
phi=zeros(length(t),numel(phi0));!
!
phi(1,:)=phi0; %setting initial conditions!
! l+1 1 l
K=[0 1; -4 -1];! = [I [K] t]
temp=inv(eye(2)-K*Delta_t)!
!
for n=1:length(t)-1!
temp2=temp*phi(n,:)'!
phi(n+1,:)= temp2';!
end
3 3
1 1
2 2
t=0.5
2 2
t=1.0
1 1
0 0
1 1
2 2
3 3
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10
3 3
1 1
2 2
2 2
1
t=0.2 1
t=0.1
0 0
1 1
2 2
3 3
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 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!
5 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 2
! =
!
dt
in the domain 0<t<2x105. You are given that (0)=10-5
l+1 l l l l 2
= + t( ( ( ) )
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
l+1 l l l l 2
phi=zeros(size(t));
= + 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);
1.4
Explicit Euler
MATLAB
1.2
0.8
0.6
0.4
0.2
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
t 5
x 10
Example O06.3b: Use implicit Euler method to solve the ODE!
!
d 2
! =
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 l l l l 2
= + t( ( ( ) )
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)
Rearranging gives
2 2 2 2 3 1 Solve 2
t(( ) ( ) ) =0 to get
3 3 2 3 3 2 Solve
t(( ) ( ) ) =0 3
to get
4 4 2 4 3 3 Solve 4
t(( ) ( ) ) =0 to get
known
Cubic polynomial!
in terms of p p t(p2 p3 ) K=0
g(p) = 0
How do we solve for p? Use Newton-Raphson method
g(p)
g(pold )
old
int p
e po
to th
ent
tang
p
pnew pold
g(pold )
The value! pnew = pold dg
of p that you!
dp (p old )
want to find
g(p)
g(pold )
ld
po
int
po
e
th
o
tt
en
ng
ta
p
pnew pold
The value!
g(pold )
of p that you! pnew = pold dg
want to find
dp (p old )
g(p)
g(pold ) d
p ol
int
e po
th
o
ntt
tan
ge
p
pnew pold
The value!
g(pold )
of p that you!
pnew = pold dg
want to find
dp (p old )
function MPO06p4() g(p) = p 0.1(p2 p3 ) 10
clear all;
close all;
dg 2
%initial guess value of p
= 1 0.1(2p 3p )
p=3;
dp
gp=p-0.1*(p^2-p^3)-10;
g(pold )
pnew = pold dg
dgdp=1-0.1*(2*p-3*p^2);
dp (pold )
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); 6
end
p 4
g(p) 0
6
3 3.2 3.4 3.6 3.8 4 4.2 4.4 4.6 4.8 5
p