Professional Documents
Culture Documents
Lecture 07
The Implicit Euler Method (continued)!
Example O06.3b (continued from previous lecture): Use implicit Euler
method to solve the ODE!
!
d 2
=
! dt
!
in the domain 0<t<2x105. You are given that (0)=10-5
Example O06.3a 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 ) dg
The value! = (pold )
of p that you! pold pnew dp
want to find
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 )
Example O06.4: Write a MATLAB program that uses the Newton Raphson
method to solve!
!
2 3
! g(p) = p 0.1(p p ) 10 = 0
!
(p0.1 (p2p3)10)
6
g(p) 2
The
4
answer!
6
should be
8
around
10
p=4.1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
p
For this question
g(p) = p 0.1(p2 p3 ) 10 = 0
so
dg 2
=1 0.1(2p 3p )
dp
Iterate using
g(pold )
pnew = pold
dg/dp(pold )
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
Now back to Example O06.3b
l l+1
Newton!
Raphson !
loop
Hence, for every time step, we need to solve an equation that looks like
g(pold )
pnew = pold
dg/dp(pold )
to find a value of p such that g(p)=0
g(pold )
Newton-Raphson formula pnew = pold dg
dp (pold )
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
Comparing implicit and explicit Euler solution
1.06
Explicit Euler
MATLAB (ode23())
1.04
1.02
t=2
0.98
0.96
t=2
0.995
0.99
0.995 1 1.005 1.01 1.015
t 5
x 10
Comparing implicit and explicit Euler solution
1.04
1
1.02
0.995
0.98
0.96
0.99
0.98 1 1.02 1.04 1.06 1.08 1.1 1.12 0.995 1 1.005 1.01 1.015
t 5
t x 10
5 x 10
Explicit Euler and MATLAB ode23() Implicit Euler and MATLAB ode23s()
For a given t, explicit methods are usually less stable than implicit method!
Near a sharp gradient, explicit methods can give oscillatory (wrong) solution!
MATLAB function ode23() and ode23s() are explicit and implicit methods
respectively
Summary
The solution using implicit Euler method is much more difficult to compute than the explicit
Euler method, especially for nonlinear problems (need to use root finding methods).
Implicit Euler method is much more stable than the explicit Euler method
System of Nonlinear ODEs
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
At every time step you need to solve a system of 3 equations for the 3 unknowns, 1l+1, 2l+1 and
3l+1. Note that the values of 1l, 2l and 3l are usually known.
8 l+1 l l+1 l+1
9 8 9
< 1 1 t 2 3 = < 0 =
l+1 l l+1 l+1
f= 2 2 + t 1 3 = 0 (O07.2)
: l+1 l l+1 l+1 ; : ;
3 3 +0.5 t 1 2
0
This equation is nonlinear and we need some iterative method to solve it. In Example O06.3, we saw
how the Newton-Raphson method can be used to solve 1 equation. We will now see how we can
use Newton-Raphson method to solve more than 1 equation.
End of first part of Example O07.1
Newton-Raphson method to solve multiple equations
Recall in the previous lecture that we used the Newton-Raphson method to solve a single
equation
g(p) = 0
We derived the iterative formula
g(pold )
pnew = pold dg
dp (pold )
We will now see how this formula can be generalized to solve a set of equations
g1 (t, p1 , p2 , p3 , p4 , . . . , pM ) = 0
g2 (t, p1 , p2 , p3 , p4 , . . . , pM ) = 0
g3 (t, p1 , p2 , p3 , p4 , . . . , pM ) = 0
g4 (t, p1 , p2 , p3 , p4 , . . . , pM ) = 0
.. .. ..
. . .
gM (t, p1 , p2 , p3 , p4 , . . . , pM ) = 0
g(pold )
pnew = pold
dg
dp (pold )
g(pold )
pnew pold = dg
dp (pold )
dg
(pold )( pnew pold )= g(pold )
dp
The above equation was derived for 1 equation. For a system of equations, we need to solve
g
{{pnew } {pold }} = { g(pold )}
p
g
{{pnew } {pold }} = { g(pold )}
p
2 3 8 9
g1 / p1 g1 / p2 ... g1 / pM > p1,new p1,old
>
g1 (p1,old , p2,old , . . . , pM,old )
> >
6 7 >
> p2,new
>
>
p2,old
g2 (p1,old , p2,old , . . . , pM,old )
6 g2 / p1 g2 / p2 ... g2 / pM 7 >
> >
>
6 g3 / p1 g3 / p2 ... g3 / pM 7 >
< p3,new
>
=
p3,old g3 (p1,old , p2,old , . . . , pM,old )
6 7
6
6 g4 / p1 g4 / p2 ... g4 / pM 7
7 > p4,new p4,old > = g4 (p1,old , p2,old , . . . , pM,old ) (O07.3)
6 .. .. .. .. 7 >
> .. >
>
..
4 . . . . 5 >
>
> .
>
>
>
.
>
: >
;
gM / p1 gM / p2 ... gM / pM pM,new pM,old gM (p1,old , p2,old , . . . , pM,old )
So to get the solution to Eq. (O07.2), you need to iterate using Eq. (O07.3). Note that Eq. (O07.3)
is a system of linear algebraic equation and you need to solve it to get the new value of pi (pi,new).
Single Equation Multiple Equations
g1 (t, p1 , p2 , p3 , p4 , . . . , pM ) = 0
g(p) = 0 g2 (t, p1 , p2 , p3 , p4 , . . . , pM ) = 0
g3 (t, p1 , p2 , p3 , p4 , . . . , pM ) = 0
g4 (t, p1 , p2 , p3 , p4 , . . . , pM ) = 0
.. .. ..
. . .
gM (t, p1 , p2 , p3 , p4 , . . . , pM ) = 0
Iterate using
Iterate using
2 3 8 9
g1 (p1,old , p2,old , . . . , pM,old )
g(pold ) 6
6
g1 /
g2 /
p1
p1
g1 /
g2 /
p2
p2
...
...
g1 /
g2 /
pM
pM
>
>
7 >
>
7 >
>
7 >
p1,new
p2,new
p1,old
p2,old
>
>
>
>
>
>
>
g2 (p1,old , p2,old , . . . , pM,old )
pnew = pold dg
6
6
6
6
g3 /
g4 /
p1
p1
g3 /
g4 /
p2
p2
...
...
g3 /
g4 /
pM
pM
7
7
<
7 >
7 >
p3,new
p4,new
p3,old
p4,old
=
>
>
=
g3 (p1,old , p2,old , . . . , pM,old )
g4 (p1,old , p2,old , . . . , pM,old )
dp (pold )
6 .. .. .. .. > .. >
..
4 . . . . 5 >
>
> .
>
>
>
.
>
: >
;
gM / p1 gM / p2 ... gM / pM pM,new pM,old gM (p1,old , p2,old , . . . , pM,old )
g1 / p1 g1 / p2
[J] =
g2 / p1 g2 / p2
The generic formula to perform iteration is
2 3 8 9
g1 / p1 g1 / p2 ... g1 / pM > p1,new p1,old >
g1 (p1,old , p2,old , . . . , pM,old )
> >
6 7 >
> p2,new p2,old
>
>
g2 (p1,old , p2,old , . . . , pM,old )
6 g2 / p1 g2 / p2 ... g2 / pM 7 >
> >
>
6 g3 / p1 g3 / p2 ... g3 / pM 7 >
< p3,new p3,old
>
= g3 (p1,old , p2,old , . . . , pM,old )
6 7
6
6 g4 / p1 g4 / p2 ... g4 / pM 7
7 > p4,new p4,old > = g4 (p1,old , p2,old , . . . , pM,old )
6 .. .. .. .. 7 >
> .. >
>
..
4 . . . . 5 >
>
> .
>
>
>
.
>
: >
;
gM / p1 gM / p2 ... gM / pM pM,new pM,old gM (p1,old , p2,old , . . . , pM,old )
@g1 p1 @g1
=2+e = 2
@p1 @p2
@g2 @g2
= 1 =2+e p2
@p1 @p2
@g1 /@p1 @g1 /@p2 p1,new p1,old g1 (p1,old , p2,old )
=
@g2 /@p1 @g2 /@p2 p2,new p2,old g2 (p1,old , p2,old )
2+e p1,old
2 p1,new p1,old 2p1,old p2,old e p1,old
=
1 2+e p2,old
p2,new p2,old p1,old + 2p2,old e p2,old
So we need to solve the matrix equation above for every iteration until we get a pair of p1 and p2 that
satisfies both Eqs. (O07.4) and (O07.5)
function MPO07p2()
clear all
close all
eps=1.0;
p=ones(2,1); %Guess value for p
while eps>1.0e-13
[gfunc,jacobian]=ExampleFunctionWithJacobian(p);
eps=max(abs(gfunc));
temp=jacobian\(-gfunc); %this is more efficient than
inv(jacobian)*(-gfunc)
p=temp+p
end
function [gfunc,jac]=ExampleFunctionWithJacobian(p)
gfunc=[2*p(1)-2*p(2)-exp(-p(1)); -p(1)+2*p(2)-exp(-p(2))];
2+e p1,old
2 p1,new p1,old 2p1,old p2,old e p1,old
=
1 2+e p2,old
p2,new p2,old p1,old + 2p2,old e p2,old
p1,old 1 p1,old
p1,new p1,old 2+e 2 2p1,old p2,old e temp1
= p2,old p2,old =
p2,new p2,old 1 2+e p1,old + 2p2,old e temp2
p1,new temp1 p1,old
= +
p2,new temp2 p2,old
End of Example O07.2
Going back now to Eq. (O07.2), we need to solve
8 l+1 l l+1 l+1
9 8 9
< 1 1 t 2 3 = < 0 =
l+1 l l+1 l+1
f= 2 2 + t 1 3 = 0
: l+1 l l+1 l+1 ; : ;
3 3 +0.5 t 1 2
0
Using the notation in Eq. (O07.2), we let pn= nl+1 to get
8 l
9 8 9
< p1 1 tp2 p3 = < 0 =
l
f= p2 2 + tp1 p3 = 0
: l ; : ;
p3 3 +0.5 tp1 p2 0
The Jacobian for this system of equation is given by
2 3
@f1 /@p1 @f1 /@p2 @f1 /@p3
J = 4 @f2 /@p1 @f2 /@p2 @f2 /@p3 5
@f3 /@p1 @f3 /@p2 @f3 /@p3
For the system of equation we are required to solve, this becomes
2 3
1 p3 t p2 t
@fm
Jmn = =4 p3 t 1 p1 t 5
@pn
0.5p2 t 0.5p1 t 1
The generic formula for iteration for system of 3 equations
2 38 9 8 9
@f1 /@p1 @f1 /@p2 @f1 /@p3 < p1,new p1,old = < f1 (p1,old , p2,old , p3,old ) =
4 @f2 /@p1 @f2 /@p2 @f2 /@p3 5 p2,new p2,old = f2 (p1,old , p2,old , p3,old )
: ; : ;
@f3 /@p1 @f3 /@p2 @f3 /@p3 p3,new p3,old f3 (p1,old , p2,old , p3,old )
The iterative formula for the Newton Raphson method to solve Eq. (O07.2) can be written as
2 38 9 8 l
9
1 p3 t p2 t < p1,new p1,old = < p1 1 p 2 p3 t =
4 p3 t 1 p1 t 5 p2,new p2,old = p2 l
2 + p 1 p3 t
: ; : l ;
0.5p2 t 0.5p1 t 1 p3,new p3,old p3 3 +0.5 p 1 p2 t
(O07.6)
We need to iterate using Eq. (O09.7) at every time step so that pnew converges.
function MPO07p1()
clear all;
close all;
Delta_t=0.02;
t=0:Delta_t:10;
%
%Preallocating Memory
%
phi=zeros(length(t),3);
phi(1,1)=0.0;phi(1,2)=1.0;phi(1,3)=1.0;
for l=1:length(t)-1
phi(l+1,:)=FindPhilplus1(Delta_t,phi(l,:));
end
%Check with Matlab solution
[tmat,phimat] = ode23(@ExampleO07p1,[0 10],[0 1 1]);
hold off!
plot(t,phi(:,1),'ko',tmat,phimat(:,1),'b-');hold on!
legend('Implicit Euler','MATLAB ode23()');!
plot(t,phi(:,2),'ko',tmat,phimat(:,2),'b-');!
function p=FindPhilplus1(Delta_t,Phil)
eps=1.0; %Just making sure you go through the loop at least once
p=Phil; %Guess value for p
while eps>1.0e-13
[gfunc,jacobian]=ExampleFunctionWithJacobian(p,Delta_t,Phil);
eps=max(abs(gfunc));
temp=jacobian\(-gfunc); %this is more efficient than inv(jacobian)*(-gfunc)
p=temp'+p;
end
Delta_t=0.02;
t=0:Delta_t:10;
%
%Preallocating Memory
%
phi=zeros(length(t),3);
phi(1,1)=0.0;phi(1,2)=1.0;phi(1,3)=1.0;
for l=1:length(t)-1
phi(l+1,:)=FindPhilplus1(Delta_t,phi(l,:));
end
%Check with Matlab solution
[tmat,phimat] = ode23(@ExampleO07p1,[0 10],[0 1 1]);
plot(t,phi,'ko',tmat,phimat,'b-');
function p=FindPhilplus1(Delta_t,Phil)
Use Newton-Raphson method to solve the equation below for l+1
8 l+1 l l+1 l+1
9 8 9
< 1 t = < 0 =
eps=1.0; 1%Just
l+1 making
l
2
sure you go l+1 3
through
l+1 the loop
f =least 2once
at 2 + t 1 3 = 0
: %Guess
p=Phil; l+1 valuel for p l+1 l+1 ; : ;
3 3 +0.5 t 1 2
0
[tmat,phimat] = ode23(@ExampleO07p1,[0 10],[0 1 1]);
plot(t,phi,'ko',tmat,phimat,'b-');
function p=FindPhilplus1(Delta_t,Phil)
while eps>1.0e-13
[gfunc,jacobian]=ExampleFunctionWithJacobian(p,Delta
_t,Phil);
eps=max(abs(gfunc));
temp=jacobian\(-gfunc); %this is more efficient
than inv(jacobian)*(-gfunc)
p=temp'+p;
end
function [gfunc,jac]=ExampleFunctionWithJacobian(p,
Use Newton-Raphson method to solve the equation below for pi,new
Delta_t,Phil)
gfunc=[p(1)-Phil(1)-p(2)*p(3)*Delta_t;
2 38 9p(2)-8 l
9
1 p3 t p2 t < p(3)-
Phil(2)+p(1)*p(3)*Delta_t; p1,new p1,old = < p1 1 p 2 p3 t =
4 p3 t 1 p1 t 5 p2,new p2,old = p2 l
+ p 1 p3 t
Phil(3)+0.5*p(1)*p(2)*Delta_t]; : ; : 2
;
0.5p2 t 0.5p1 t 1 p3,new p3,old l
p3 3 +0.5 p 1 p2 t
jac=[1 -p(3)*Delta_t -p(2)*Delta_t; p(3)*Delta_t 1
p(1)*Delta_t;0.5*p(2)*Delta_t 0.5*p(1)*Delta_t 1];
while eps>1.0e-13
[gfunc,jacobian]=ExampleFunctionWithJacobian(p,Delta
_t,Phil);
eps=max(abs(gfunc));
temp=jacobian\(-gfunc); %this is more efficient
than inv(jacobian)*(-gfunc)
p=temp'+p;
end
function [gfunc,jac]=ExampleFunctionWithJacobian(p,
Delta_t,Phil)
gfunc=[p(1)-Phil(1)-p(2)*p(3)*Delta_t; p(2)-
Phil(2)+p(1)*p(3)*Delta_t; p(3)-
Phil(3)+0.5*p(1)*p(2)*Delta_t];
[gfunc,jacobian]=ExampleFunctionWithJacobian(p,Delta
_t,Phil);
eps=max(abs(gfunc));
temp=jacobian\(-gfunc); %this is more efficient
than inv(jacobian)*(-gfunc)
p=temp'+p;
end
function [gfunc,jac]=ExampleFunctionWithJacobian(p,
Delta_t,Phil)
gfunc=[p(1)-Phil(1)-p(2)*p(3)*Delta_t; p(2)-
Phil(2)+p(1)*p(3)*Delta_t; p(3)-
Phil(3)+0.5*p(1)*p(2)*Delta_t];
plot(t,phi,'ko',tmat,phimat,'b-');
function p=FindPhilplus1(Delta_t,Phil)
while eps>1.0e-13
[gfunc,jacobian]=ExampleFunctionWithJacobian(p,Delta
_t,Phil);
eps=max(abs(gfunc));
temp=jacobian\(-gfunc); %this is more efficient
than inv(jacobian)*(-gfunc)
p=temp'+p;
end
function [gfunc,jac]=ExampleFunctionWithJacobian(p,
Calculate
Delta_t,Phil)
8 gfunc=[p(1)-Phil(1)-p(2)*p(3)*Delta_t;
9 2 3 1 8 p(2)- l 9 8 9
< p1,new p1,old = 1 p3 t p2 t < p1 1 p2 p3 t = < temp1 =
Phil(2)+p(1)*p(3)*Delta_t;
p2,new p2,old = 4 p3 t 1 p(3)-
p1 t 5 p2 l
+ p1 p3 t = temp2
2
: Phil(3)+0.5*p(1)*p(2)*Delta_t];
; : l ; : ;
p3,new p3,old 0.5p2 t 0.5p1 t 1 p3 3 +0.5 p p
1 2 t temp 3
plot(t,phi,'ko',tmat,phimat,'b-');
function p=FindPhilplus1(Delta_t,Phil)
while eps>1.0e-13
[gfunc,jacobian]=ExampleFunctionWithJacobian(p,Delta
_t,Phil);
eps=max(abs(gfunc));
temp=jacobian\(-gfunc); %this is more efficient
than inv(jacobian)*(-gfunc)
p=temp'+p;
end
function [gfunc,jac]=ExampleFunctionWithJacobian(p,
Calculate
Delta_t,Phil)
8 9 8 9 8p(2)- 9
gfunc=[p(1)-Phil(1)-p(2)*p(3)*Delta_t;
< p1,new = <
Phil(2)+p(1)*p(3)*Delta_t; temp1 = < p1,old =
p(3)-
p2,new = temp2
Phil(3)+0.5*p(1)*p(2)*Delta_t]; + p2,old
: ; : ; : ;
p3,new temp3 p3,old
jac=[1 -p(3)*Delta_t -p(2)*Delta_t; p(3)*Delta_t 1
p(1)*Delta_t;0.5*p(2)*Delta_t 0.5*p(1)*Delta_t 1];
Output from MPO07p1.m
1
Implicit Euler
0.8 MATLAB ode23()
0.6
0.4
0.2
0
0.2
0.4
0.6
t=0.1
0.8
1
0 1 2 3 4 5 6 7 8 9 10
t
End of Example O07.1