You are on page 1of 17

ORDINARY DIFFERENTIAL

EQUATION
HOW TO SOLVE ODE
• ODE MATLAB SOLVER
• SHOOTING METHOD
• FINITE DIFFERENCE
• BVP MATLAB SOLVER
ODE MATLAB SOLVER

• Ode113: variable order solution to nonstiff system


• Ode15s: varaible order, multistep method for solution of
stiff system
• Ode23: Lower order adaptive stepsize routine for nonstiff
systems
• Ode23s: Lower order adaptive step size routine for stiff
systems
• Ode45: higher order adaptive stepsize routine for
nonstiff system
Stability of system of ODEs
dy1
 f1 ( x, y1 , y2 ,......, yn )
• Stability of system of ODES is dx
dy2
determined based on the sign of  f 2 ( x, y1 , y2 ,......, yn )
dx
eigenvalues of Jacobian matrix.

dyn
 f1 f1 f1   f n ( x, y1 , y2 ,......, yn )
 x dx
x2 xn 
 1 
 f 2 f 2 f 2 
 x x2 xn 
J  
1

 
 
 
 f n f n f n 
 
 x1 x2 xn  y
ss

If all eigenvalues of J are negative the system is stable


Example: given the chemical reaction system described by
dy1 2 2 2
 4 y1 y2  2 y1 y2  5 y2  9
dx
dy2 3 2
 3 y1 y2  12 y1 y2  3 y2  9
dx
a. Show that y1=0.07709 and y2=1.89351 is a steady-state solution of the system
b. Study the stability of the dynamic system around its steady state.
c. Use an ODE MATLAB solver to examine the dynamic behavior of the system
around its steady-state.

function f=ode_stability(y)
f(1)=4*y(1)^2*y(2)^2-2*y(1)*y(2)^2+5*y(2)-9
(a) f(2)=-3*y(1)^3*y(2)-12*y(1)*y(2)^2-3*y(2)+9

ans =
y0=[0.07709; 1.89351]
y=fsolve('ode_stability',y0)
0.0771
1.8935
 f1 f1 
 y y2   8 y1 y22  2 y22 8 y 21 y2  4 y1 y2  5
J  
1

 f 2 f 2   9 y12 y2  12 y22 3 y12  24 y1 y2  3 
 y y2 
 1
• (b)
Eigenvalues of J at steady state:
y0=[0.07709; 1.89351]
ys=fsolve('ode_stability',y0)

J=[8*ys(1)*ys(2)^2-2*ys(2)^2 8*ys(1)^2*ys(2)-4*ys(1)*ys(2)+5;
-9*ys(1)^2*ys(2)-12*ys(2)^2 -2*ys(1)^2-24*ys(1)*ys(2)-3]

eig(J)

J=

-4.9596 4.5061
-43.1261 -6.5153
ans = System is stable since all eigenvalues have
negative real parts.
-5.7374 +13.9186i
-5.7374 -13.9186i
function dydx=ode_dynamic(x,y)
dydx(1)=4*y(1)^2*y(2)^2-2*y(1)*y(2)^2+5*y(2)-9
dydx(2)=-3*y(1)^3*y(2)-12*y(1)*y(2)^2-3*y(2)+9
dydx=dydx'

xspan=[0 2];
y0=[0.1; 1.0];
[x,y]=ode45('ode_dynamic',xspan,y0)
plot(x,y)
Stiff ODEs
• When the eigenvalues of the Jacobian are all of the
same order: non-stiff system and problem with
intergration.
• If lmax >>>lmin  stiff system
• lmax gives very steep profile and needs very small h.
• lmin very slow dynamics and need large final time so
using explicit ODE solvers is time intensive.
• For stiff systems, implicit methods are
preferred.

max  Real( j ) 
SR (Stifeness Ratio)=
min  Real( j ) 
ODE MATLAB SOLVER

• function lab3
function lab3
• v0=[2 3]; y0=[4 -2];
• tspan=[0 0.1]; tspan=[0 10];
• [t,v]=ode45(@exp1,tspan,v0) [t,y]=ode45(@exp1,tspan,y0)
• plot(t,v(:,1),t,v(:,2)); plot(t,y(:,1),t,y(:,2));
legend(‘y1',’y2')
• legend('v1','v2') function dydt=exp1(t,y)
• function dvdt=exp1(t,v) dydt=[2*y(1)-3*y(2)
• dvdt=[4*v(1) y(1)^2-10*y(2)]
• v(2)^2-2*v(1)]
• function lab3
• y0=[1 0 0.2 1];
• tspan=[0 100];
• [t,y]=ode23s(@exp1,tspan,y0)
• figure(1)
• plot(t,y(:,1),t,y(:,2),t,y(:,3),t,y(:,4));
• legend('y1','y2','y3','y4')
• figure(2)
• plot(y(:,1),y(:,2))
• figure(3)
• plot(y(:,3),y(:,4))
• figure(4)
• plot(y(:,1),y(:,3))
• figure(5)
• plot(y(:,1),y(:,4))
• figure(6)
• plot(y(:,2),y(:,4))
• figure(7)
• plot(y(:,2),y(:,3))


• function dydt=exp1(t,y)
• dydt=[y(3); y(4); -4*y(2)-3*y(1); 0.5*y(1)-6*y(2)];

Finite Difference Method
Forward
• Replace the derivative y y
y  i 1 i
with finite difference h
y  yi 1 Backward
approximation y  i
h
• ODEs become algebraic y y
y  i 1 i 1
equations 2h Central
yi 1  2 yi  yi 1
y 
h2
Example:

d 2c c ( x  h)  2c ( x )  c ( x  h)
2
 2c  0  2
 2c ( x )  0
dx h
Example of FD method
d 2c dc
 2c, 0 c(1)  1
dx 2 dx x 0

Let h=0.25
0 0.25 0.5 0.75 1
h
c(.25)  c(0)
x0  0  c(0.25)  c (0)
h
c(0.5)  2c(0.25)  c(0)
x  0.25  2c(0.25)  0
0.252
c(0.75)  2c(0.5)  c(0.25)
x  0.5  2c (0.5)  0
0.252
c(1)  2c(0.75)  c(0.5)
x  0.75 2
 2c (0.75)  0
0.25
x  1.0 c(1)  1

c(0)=c(0.25)=0.546, c(0.5)=0.615, c(0.75)=0.760, c(1)=1


Example: solve the BVP using FD
technique

d 2T 1.4
 0.01(T  20) 0  x  10
dx 2
T (0)  200, T (10)  40
Example: solve the BVP using FD technique

d 2T 1.4
 0.01(T  20) 0  x  10
dx 2
T (0)  200, T (10)  40

Ti 1  2Ti  Ti 1 1.4
 0.01(T i  20)
h2
Ti 1  2Ti  Ti 1  h 2 *0.01(Ti  20)1.4  0
Example: solve
d T
the BVP using FD technique
2

2
 0.01(T  20)1.4 0  x  10
dx
T (0)  200, T (10)  40
Ti 1  2Ti  Ti 1 1.4
 0.01(T i  20)
h2
Ti 1  2Ti  Ti 1  h 2 *0.01(Ti  20)1.4  0
function f=bvp_fd(t)
h=1
f(1)=t(2)-2*t(1)+200-0.01*(t(1)-20)^1.4;
for n=2:8
f(n)=t(n+1)-2*t(n)+t(n-1)-0.01*(t(n)-20)^1.4;
end
f(9)=40-2*t(9)+t(8)-0.01*(t(9)-20)^1.4; % Finite differnce method for solving
%nonlinear BVP
% Initial conditions
% nine nodes
d2V/dt2-3dV/dt=0.5t
to=[190 180 160 140 120 100 80 60 50]
xx=[1 2 3 4 5 6 7 8 9]'
V(0)=4; dV/dt=2
t=fsolve('bvp_fd',to)
0<=t<=10  100
MATLAB command
BVP4C Solve two-point boundary value problems for ODEs by collocation.
SOL = BVP4C(ODEFUN,BCFUN,SOLINIT) integrates a system of ordinary
differential equations of the form y' = f(x,y) on the interval [a,b],

• Example
• solinit = bvpinit([0 1 2 3 4],[1 0]);
• sol = bvp4c(@twoode,@twobc,solinit);
• solve a BVP on the interval [0,4] with differential equations and
• boundary conditions computed by functions twoode and twobc,
respectively.
• This example uses [0 1 2 3 4] as an initial mesh, and [1 0] as an initial
• approximation of the solution components at the mesh points.
• xint = linspace(0,4);
• yint = deval(sol,xint);
• evaluate the solution at 100 equally spaced points in [0 4]. The first
• component of the solution is then plotted with
• plot(xint,yint(1,:));
d 2T
2
 0.01(T  20)1.4 0  x  10
dx
T (0)  200, T (10)  40

function dydx = ge501pvb(x,y)

dydx = [ y(2); 0.01*(y(1)-20)^1.4 ];

ya (1) yb (1)
ya(2) yb(2) function res = ge501_bc(ya,yb)
res = [ ya(1)-200; yb(1)-40 ];

solin=bvpinit([0 2 4 6 8 10],[50 50]);


a= mendefinisikan di x=0
sol=bvp4c(@ge501pvb,@ge501_bc,solin);
b= mendefinisikan di x=L
xint=linspace(0,10);
(1)= mendefinisikan sebagai y1
yint=deval(sol,xint);
(2)= mendefinisikan sebagai y2
plot(xint,yint(1,:))

You might also like