Академический Документы
Профессиональный Документы
Культура Документы
by
Rahul Kumar Soni
(rahul@ismu.ac.in)
Signature
2
Acknowledgement
My sincere thanks to Prof. Kannan Moudgalya for having given
me this opportunity to work on such a lucrative project. I wish to
thank the KVPY cell for having provided a great work environment.
This project would not have been complete without the guidance and
timely inputs from Prof. G K Srinivasan ( Professor, Department
of Mathematics, IIT Bombay), Prof. V K Gupta (Tata Steel Chair
Professor at Department of Fuel and Mineral Engineering, ISMU),
Dr. Nikkam Suresh(Asso. Prof. and HOD, Department of Fuel and
Mineral Engineering, ISMU), Dr. Biswajit Paul (Department of En-
vironment Science and Engineering, ISMU), Dr. S Bhattacharya
(Asso. Prof., Department of Fuel and Mineral Engineering, ISMU),
Dr. N K Singh(Asso. Prof. and HOW, Department of Mechani-
cal Egineering, ISMU), Ms. Inderpreet Arora (M.Tech Student, IIT
Bombay), Ms. Sandhya Sourirajan (B.E. student, Coimbtore Insti-
tute of Technology), all teachers and my friends.
I am thankful to the entire work force at ERTS lab, CDEEP and
Chemical Department of IIT Bombay for the part they played in
making the period of work a joyful experience.
Last but not the least, I thank to Indian Institute of Science
Banglore (the Organizing Institute) and Department of Science and
Technology, Government of India (the sponsoring agency) for their
support and encouragement.
3
Abbreviations and meaning of symbols
ODE: ordinary dierential equation
IVP: initial value problem
BVP: boundary value problem
rk: Runge-Kutta 4th order method
rkf: Fehlberg's Runge-Kutta order 4 and 5 method (RKF45)
LT: Laplace Transform
ILT: Inverse Laplace Transforms
//**S: Shows the start of the part of program executed online in Scilab
window.
//**E: Shows the end of the part of program executed online in Scilab win-
dow.
//##S: Shows start of the writer's contribution to the program or in the
matter taken from references or
complete work of writer.
//##E: Shows end of the writer's contribution to the program or in the
matter taken from references or
complete work of writer.
Machine congurations
Simulation has done in two machines of following congurations
1. Notepad
2. Lab PC
Disclaimer
1. All the programs written in the report are tested in Scilab and
produced gure are attached in the report, so in any case not proper
working of program is might be due to printing mistake.
4
Contents
1 Scilab 8
1.1 SCILAB HISTORY . . . . . . . . . . . . . . . . . . . 8
5
6 Miscellaneous Problems 36
6.1 Predator-Prey model . . . . . . . . . . . . . . . . . . 36
6
Abstract
Techniques and methods for obtaining solutions to dierent kind
of Ordinary Dierential Equations is investigated in Scilab. The
approach is based on solving dierent kind of Ordinary Dieren-
tial Equations with dierent method some which are user dened
for example Euler's method and other which are ready made in
Scilab for example Runge-Kutta, Fehlberg's runge-Kutta, Adams-
Bashforth and Sti (belonging to sti category problems). Empha-
sis is placed on mathematical justication of the approach. Time
required to complete a task and step size for desired accuracy of so-
lution are the main concern and basis of comparison between meth-
ods. On the basis of this approach problem and diculties in Scilab
are observed and suggestions are made for their remedies.
Preamble
In mathematics, an ordinary dierential equation (or ODE) is
a relation that contains functions of only one independent variable,
and one or more of its derivatives with respect to that variable. Sir
Isaac Newton has rst developed the concept of ordinary Dierential
Equations for a series solution in the eld of Astronomy and from
that time ODE has wide applications in Industry, scientic work,
Economics, Ecology, Geology and many other elds including bio-
logical sciences. ODE always gives the relation among independent
variables, dependent variable and their derivatives which are di-
cult to analyze. A better analysis of these equations requires their
solution in terms of independent and dependent variables. Various
techniques are available for the solution of these ODEs in dierent
software packages like Mathematica, Maple, Matlab, Scilab etc. This
report mainly deals with the solution of ODEs in Scilab by dierent
methods like Adams-Bashforth, Runge-kutta , Fehlberg's Runge-
Kutta , Discrete time simulation and nally Sti type of equations
along with examples, comparison of their solutions (error involved,
smoothness of curve).
7
Aim
Aim of this paper is to examine the apparent trend in
Simulation of Ordinary Dierential Equation using Scilab
by various methods, their applicability and nally ana-
lyzing the method with optimize solution for a various
category of problems.
1 Scilab
1.1 SCILAB HISTORY
It is an open source software package developed at INRIA (France), for system
control and signal processing applications. It also features a wide variety of
tools for various Engineering and Mathematical applications. It was introduced
as an Open source alternative to MATLAB. It is also a vector and matrix based
program. Scilab introduced Scicos equivalent to Simulink. It has constantly
undergone vital changes ever since its inception in 1994.
8
2 Method involve in solution to ODE
2.1 Analytical Solutions of Ordinary Dierential Equa-
tions (ODEs)
(Preparation done by attending Continuing Education Program-
CEP02 on Ordinary Dierential Equations at Indian Institute of
Technology Bombay held from May 11,09 to May 22,09.)
dy
= ax + bxy
dt
dx
= cy + dxy
dt
2. Clairut's ODE
dy dy
y=x + f( )
dx dx
3. Singular and General solution, General solution as Envelope of Singular
solution.
M dx + N dy = 0
8. Linear rst order ODE and its solution by operation with Integrating
factor.
dy
+ P (x)y = Q(x)
dx
9
9. Bernoulli's Equation and its solution.
dy
+ P (x)y = Q(x)y n
dx
n−1
X
Dn y + aj (x)Dj y = 0
j=0
Calculation of Wronskian.
n−1
X
Dn y + aj Dj y = 0
j=0
n−1
X
Dn y + aj Dj y = R(x)
j=0
T able1
R(x) Annihilator
xk eax (D − a)k+1
xk eax sinbx {(D − a)2 + b2 }k+1
xk eax cosbx {(D − a)2 + b2 }k+1
10
18. Gamma function and its properties.
Z ∞
Γ(a) = exp(−t)ta−1 dt
0
3 Legendre Equation
3 Tchebychev Equation
3 Airy Equation
3 Hermits Equation
3 Laguerre Equation
3 Bessel's Equation
3 Hypergeometric Equation
3 Jacobi's Equation.
11
2.2 Numerical methods for solution of ODEs available in
Scilab
2.2.1 Euler's method (Non-Sti): Solution of ODEs of non-sti type
by the basic denition of dierentiation and by fourier transform
expansion.
2.2.2 Scilab ODE Solvers
Although there are other ode solvers like ode, dassl, dassrt, odedc are available
in Scilab but we will consider only 'ode' solver here.
type is a character string given in a single quote and direct machine for any
specic type of numerical solution method. This methods and corresponding
string are as follows;
3 '(empty input)': Default Lsoda method uses non-sti procedure initially and
then uses Sti backward dierentiation method , if required.
3 'adams': Solves Non-sti problems by Adams-Bashforth method.
3 'rk': Uses Runge-Kutta 4th order method to solve non-sti problem.
3 For non-sti and mildly sti problems, uses Fehlberg's Runge-Kutta pair
'rkf ':
of order 4 and 5 (RKF45) method, should not use when high accuracy is
desired.
3 'x':Similar to Fehlberg's Runge Kutta method, with easier user interface (for
non-sti problems).
3 'root':Use default lsoda method and gives roots of the solution (for non-sti
problems).
3 'discrete':For Discrete time simulation, can solve ODE at discrete points (for
non-sti problems).
3 'sti ': Only method which can deal with to deal with sti problems.
Note: However, sti ode solver is particularly for sti problems but other can
also solve them under depression of intervals for the whole range of t or for the
segment where solution is unstable, same is explained in an example later also.
12
3 Some Useful Commands regarding to ODE so-
lutions:
3 ODE solvers: ode, dassl, dassrt, odedc
3 Programming: function, de, argn, for, if, end, while, select,warning, er-
ror, break, return
13
4.2 Sti and Non-sti problems
What's a sti equation? No, precise denition exist. Operationally
dY
= f (x, y)
dx
Y (x0 ) = Y0
Take an example of
dy
= −15y
dt
solution of this by Euler's method shows followoing plots for dierent values of
steps of t (i.e. h). As h goes smaller solution tends to stable condition.
Problems other than sti therefore those not show instability while solve by
general methods of Solution of ODE are Non-sti problems, also these don't
requires more precision on step increment.
14
and the ODE is
dY
= AY
dt
solution of this ODE for two values of h; h1 = 2.702703 × 10−2 and
−2
h2 = 2.73972 × 10 is
(References: http://en.wikipedia.org/wiki/stiff_equation,http://
www.physics.arizona.edu/~restrepo/475B/Notes/source/node16.html)
15
5 Scilab Programs for solving ODEs and their
examples
5.1 Solution to Simple ODE explicit in terms of indepen-
dent variable
dy
= f (x)
dx
Above can be easily solve by basic integration method like Summation of
elemental rectangular area, Trapezoidal rule, Simpon's 1/3 rd etc.
rectangular area. Here area between xi and xi+1 can be calculated in three ways
a. h*f(xi )
f (xi )+f (xi+1 )
b. h*(
2 )
c. h*f(xi+1 )
Above three are called Lower sum, Middle sum and Upper sum respectively.
Program 1:
//##S
function [I]=integration(stype,a,b,n,f )
//stype is the way of integration may be 'U','M' & 'L' for upper, middle &
lower sum
//a,b is the range of x for integration, n=no. of intervals, f be the function
dened online
h=(b-a)/n;
xset('window',1)
if (stype<>'L')&(stype<>'M')&(stype<>'U') then
error('stype must be L,M,U as string ');
abort;
end
if stype=='L' then x=a & j=1
elseif stype=='M' then x=a+(h/2) & j=2
else x=a+h & j=3
end
A=zeros(1,n)
X=zeros(1,n)
Y=zeros(1,n)
for i=1:n
X(1,i)=x
y=feval(x,f )
Y(1,i)=y
16
A(1,i)=h*y
x=x+h
end
subplot(2,2,1)
plot(X,Y)
title('x,y function plot')
I=sum(A)
B=zeros(1,n);
for i=1:n
B(1,i)=sum(A(1,1:i))
end
subplot(2,2,2)
plot(X,B) title('Integrated curve for '+stype)
endfunction
//##E
Example 1:
Let
dy
= x2 + x + 3
dx
//**S
//##S
de('[z]=f(x)','z=x^2+x+3');
getf('integration.sci');
integration('L',0,5,10,f )
ans =
68.41875
integration('M',0,5,10,f )
ans =
69.165625
integration('M',0,5,10,f )
ans =
69.91875
//**E
//##E
Three answer shown above are for Lower, Middle and Upper sum respetively,
let them call L-ans, M-ans and U-ans.
Note:
17
L − ans < M − ans < U − ans
and from the exact solution we have answer as 69.166667, which is closest to
the M-ans. Why? This can be explained by gure.
Fig 1.4 shows the plot of integrated curve of given problem, there are three
curves for each sum and so make the curve line thick.
Fig 1
Note: However, above program is correct to get answer and individual plots
but given Fig 1 is not produce by above program. Some more programming is
required for the same.
18
or
yn+1 = yn + hg(tn , yn )
Following above program for solution by Euler's method can be written as:
Program 2:
function [t,y]=Euler(t0,y0,tn,h,g)
//Euler 1st order method for solving ODE
//dy/dx=g(t,y)
//t0 and tn are the range of t and h is the interval bet
ymaxallowed=1e+100; //Actually it is abs(ymaxallowed)
t=t0:h:tn; y=zeros(t); n=length(y); y(1)=y0;
for j=1:n-1
y(j+1)=y(j)+h*g(t(j),y(j));
if abs(y(j+1))>ymaxallowed then
disp('Euler-warning: underow or overow')
n=j; t=t(1,1:n); y=y(1,1:n);
break;
end;
end;
endfunction;
19
Fig 2
//**S
//##S
>t0=1; y0=0; //initial condition
>de('[z]=f(t,y)','z=1./(2*(1+y))')//dening function
>t=1:1:10;
>y=ode(y0,t0,t,f );
>plot(t,y)
>xtitle('Fig 1-Solution plot to Example 1','t','y')
//**E
//##S
It can be observe that the output plots between dependent and independent
variable are similar for both the method therefore by Euler's method and Scilab
ODE solver.
It is dicult to distinguish them with such simple problems. Their perfor-
mance may be distinguish in case of complicated problems or in system of ODEs
with large no. of iterations.
20
Fig 3
Example 4:
Note: In particular solution rst part goes zero rapidly while second part
produces a straight line
Program 4:
//##S
de('[y]=f(x)','y=-exp(-1000.*x)+x+2')
//PHASE 1: Plotting exact solution for small values of x
x1=linspace(0,0.01,100);
y1=feval(x1,f );
xset('window',0)
clf
subplot(4,1,1)
plot(x1,y1)
21
xtitle('Fig 4.1:Plot for exact solution for small values of x','x','y')
//PHASE 2:Plotting exact solution for small values of x
x2=linspace(0,1,100);
y2=feval(x2,f );
subplot(4,1,2)
plot(x2,y2)
xtitle('Fig 4.2:Plot for exact solution for comparatively large values of x','x','y')
//PHASE 3:Now we will see what happen when we plot the curves for
dierent numerical solutions
de('[Dy]=g(x,y)','Dy=-1000.*(y-x)+2001')
//Call 'Euler' program for Numerical solution by Euler method
getf('Euler.sci')
//Plot the curve for dierent increment as 0.0005,0.0010,0.0020,0.0025
//For initial condition y(0)=1
y0=1; x0=0;
h1=0.0005; h2=0.0010; h3=0.0020; h4=0.0025;
[x1,y1]=Euler(x0,y0,0.01,h1,g);
[x2,y2]=Euler(x0,y0,0.01,h2,g);
[x3,y3]=Euler(x0,y0,0.01,h3,g);
[x4,y4]=Euler(x0,y0,0.01,h4,g);
ymin=min([y1 y2 y3 y4]);
ymax=max([y1 y2 y3 y4]);
disp(ymin)
disp(ymax)
//Let us dene the axis properties
rect=[0 -4 0.01 6];
subplot(4,1,3)
//plots to give discrete curve with dierent signs
plot2d(x1,y1,-1,'011',,rect)
plot2d(x2,y2,-2,'011',,rect)
plot2d(x3,y3,-3,'011',,rect)
plot2d(x4,y4,-4,'011',,rect)
//plots to give continuous curves
plot2d(x1,y1,1,'011','h=0.0005',rect)
plot2d(x2,y2,5,'011','h=0.0010',rect)
plot2d(x3,y3,3,'011','h=0.0020',rect)
plot2d(x4,y4,2,'011','h=0.0025',rect)
title('Fig 4.3:Plots by Euler method for dierent value of h (increment)')
//Here corresponding curves for dierent colors are as follows
//h=0.0005, color=Black
//h=0.0010, color=Red
//h=0.0020, color=Pale Green
//h=0.0025, color=Blue
//PHASE 4:Now Let us see what happen when we plot the above curves
with sti ODE solver of Scilab
subplot(4,1,4)
22
rect=[0 1 0.01 2.2]
x1=x0:h1:0.01;
x2=x0:h2:0.01;
x3=x0:h3:0.01;
x4=x0:h4:0.01;
y1=ode(y0,x0,x1,g);
y2=ode(y0,x0,x2,g);
y3=ode(y0,x0,x3,g);
y4=ode(y0,x0,x4,g);
plot2d(x1,y1,1,'011','h=0.0005',rect)
plot2d(x2,y2,5,'011','h=0.0010',rect)
plot2d(x3,y3,3,'011','h=0.0020',rect)
plot2d(x4,y4,2,'011','h=0.0025',rect)
title('Fig 4.4:Plot for dierent h (x-increment) by sti solver of Scilab')
//From the plot it is obvious that sti ODE solver of Scilab stabilizes solution
for x-increment=0.0005,0.0010,0.0020,0.0025
//##E
Fig 4.2 gives the same solution plot for comparatively larger values of x. It is
obvious from the plots that solution faces sudden changes between 0.0 and
0.01.
Fig 4.3 gives the solution of ODE by Euler's Method at dierent values of
x-increment (h), for those curves are shown with dierent colors. Answer given
after execution of program is the minimum and maximum value of y
respectively, comes in the solution of ODE by Euler's method (just to check
degree of instability).
Table 2
h=0.0005 Black
h=0.0010 Red
h=0.0020 Pale Green
h=0.0025 Blue
23
Fig 4.4 comes after solution of given ODE by SCILAB Sti ODE solver at all
previously dened values of h (colors of curve are corresponds to values of h as
similar to Fig 4.3).
It is apparent from the plots that sti solver stabilize the solution but in
better way for higher value of h.
Fig 4
24
5.5 Solution to Linear rst order linear system of ODEs
This is example gives the idea behind how to solve system of ODE with a single
ode solver
Example 5:
dy1
= y2 + x
dx
and
dy2
= −y1 + y2
dx
with initial condition as
Program 5:
//**S
de('[w]=f(x,y)',['f1=y(2)+x';'f2=-y(1)+y(2)';'w=[f1;f2]'])
x0=0; Dx=0.1; xn=2;
y0=[1;2];
x=[x0:Dx:xn];
y = ode(y0,x0,x,f );
plot2d([x',x'],[y(1,:)',y(2,:)'],[1,-1],'111','y1@y2',[0 -3 2 4])
xtitle('ode solution to a system of ODEs in example 3','x','f(x)')
pause
//**E
In the Fig 5 continuous plot gives the solution y1 and the plot which is shown
in discrete pattern shows solution y2 .
25
Fig 5
dY
L{ } = A ∗ L{Y } + L{g(t)}
dt
26
Taking Inverse Laplace Transform at both side
//##S
function [Y]=LT(A,x0)
[m,n]=size(A);
if m>n
error('matrix must of square type')
elseif m<n
error('matrix must of square type')
end
syms s;
S=s*eye(2,2)-A;
Si=inv(S);
IL=ilaplace(Si);
syms t;
syms C1 C2;
ParticularSolutions=IL*x0'
GeneralSolutions=IL*[C1; C2]
//##E
dy1
= 4y1 + 2y2
dt
dy2
= −2y2
dt
for initial conditions as y1 (0) =0 and y2 (0) =1 and 0<t<20
This problem can be re-written as
dY
=A∗Y
dt
where
4 2
A=
0 −2
//**S
//##S
A=[4 2;0 -2];
x0=[0 1];
LT(A,x0)
//##E
//**E
27
Answers:
ParticularSolutions =
y1=2/3*exp(t)*sinh(3*t)
y2=exp(-2*t)
GeneralSolutions =
y1=exp(4*t)*C1+2/3*exp(t)*sinh(3*t)*C2
y2=exp(-2*t)*C2
Let m be the mass, k be the spring constant and b be the damping constant
which appear due to frictional losses (may be heat losses in case of Electrical
LCR circuits).
F = −kx
F = −bv
28
where x and v are instant position and velocity of mass respectively.
d2 x
−kx − bv = m
dt2
dx d2 x
−kx − b =m 2
dt dt
d2 x k b dx
2
+ x+ =0
dt m m dt
b k
D2 + D+ =0
m m
which is a quadratic equation whose roots may be real distinct, real repeated
and complex roots. A particular type of roots gives the relation among m, b
and k or inequalities among them which is important because a particular type
of root decides the characteristic of system and its functioning, this can be
seen as follows:
Program 7:
//##S
function []=dampedoscillation(m,B,k,t0,x0,v0,t1,t2)
//m=mass, b=damping constant, k=spring constant, t0=initial time...
//at which xo,vo are given,
//t=time range for plot,
//F1=-kx, F2=-bv
//Di. Equation: (D^2+(b/m)D+(k/m))x=0
t=linspace(t1,t2,1000);
D=poly([(k/m) (B/m) 1],'D','coe ')
R=roots(D)
disp('roots')
disp(R)
a=R(1,1)
b=R(2,1)
//PHASE 1: For distinct real roots
if imag(a)==0 & a<>b then
M=[exp(a*t0) exp(b*t0);a*exp(a*t0) b*exp(b*t0)]\[x0 v0]'
//A & B are coecients in the solution x=Aexp(R1*t)+Bexp(R2*t)
A=M(1,1)
29
B=M(2,1)
x=(A*exp(a*t))+(B*exp(b*t))
v=(a*A*exp(a*t))+(b*B*exp(b*t))
a=(a*a*A*exp(a*t))+(b*b*B*exp(b*t))
clf
subplot(2,2,1)
plot(t,x)
title('x-t curve')
subplot(2,2,2)
plot(t,v)
title('v-t curve')
subplot(2,2,3)
plot(t,a)
title('a-t curve')
subplot(2,2,4)
plot(t,x,t,v,t,a)
title('x-t, v-t & a-t curve at one place')
//PHASE 2: For repeated real roots
elseif a==b then
M=(exp(a*t0))*[1 t0;a (a*t0)+1]\[x0 v0]' //A & B are coecients ...
//in the solution x=Aexp(R1*t)+Bexp(R2*t)
A=M(1,1)
B=M(2,1)
x=(A*exp(a*t))+(B*t.*exp(a*t))
v=(a*A*exp(a*t))+(B*(a*t+1).*exp(b*t)) a=(a*a*A*exp(a*t))+
...(a*B*(a*t+2).*exp(b*t))
clf
subplot(2,2,1)
plot(t,x)
title('x-t curve')
subplot(2,2,2)
plot(t,v)
title('v-t curve')
subplot(2,2,3)
plot(t,a)
title('a-t curve')
subplot(2,2,4)
plot(t,x,t,v,t,a)
title('x-t, v-t & a-t curve at one place')
//PHASE 3: For complex roots
else
b=imag(a)
b=abs(b)
a=real(a)
c=(a*cos(b*t0))-(b*sin(b*t0))
d=(a*sin(b*t0))+(b*cos(b*t0))
30
M=(((exp(a*t0)))*[cos(b*t0) sin(b*t0);c d])\[x0; v0]
//A & B are coecients in the solution
x=Aexp(R1*t)+Bexp(R2*t)
A=M(1,1)
B=M(2,1)
x=((A*cos(b*t))+(B*sin(b*t))).*exp(a*t)
v=(exp(a*t)).*(((a*A+b*B)*cos(b*t))+((a*B-b*A)*sin(b*t)))
a=(exp(a*t)).*((((a*a*A+b*a*B)*cos(b*t))+((a*a*B-b*a*A)*sin(b*a*t)))+...
...(((-b*a*A-b*b*B)*sin(b*t))+((a*b*B-b*b*A)*cos(b*a*t))))
// Note that in all above three equation 'a' i.e. the real part of the roots
has the role for damping similarly 'b' i.e. the imaginary part of roots...
//has the role for frequency or say angular frequency=b
clf
subplot(2,2,1)
plot(t,x)
title('x-t curve')
subplot(2,2,2)
plot(t,v)
title('v-t curve')
subplot(2,2,3)
plot(t,a)
title('a-t curve')
subplot(2,2,4)
plot(t,x,t,v,t,a)
title('x-t, v-t & a-t curve at one place')
end
xset('window',5)
clf subplot(3,1,1)
plot(x,v)
title('v(vertical axis)-x(horizontal axis)')
subplot(3,1,2)
plot(v,a)
title('a(vertical axis)-v(horizontal axis)')
subplot(3,1,3) plot(x,a)
title('a(vertical axis)-x(horizontal axis)')
endfunction;
//##E
3 Complex roots
31
Fig 6
Table 3
32
Assume Fig 6 a matrix of 4×3 then the rst row plots the position vs time
(x-t), velocity vs time (v-t) and acceleration vs time (a-t). Second, third and
forth rows gives velocity vs position (v-x), acceleration vs velocity (a-v) and
acceleration vs position (a-x) plots respectively.
3 In case of both real root cases motion is only decaying not oscillatory.
b2 < 4mk
3 In the complex root case when damping constant b6= 0, oscillatory motion
goes on reducing which is obvious from all last plots of rst, second and
third rows.
or
n−1
X
Dn y = − aj Dj y + R(x)
j=0
d4 y d3 y d2 y dy x2
+ 3 − 2 + 5 + y =
dx4 dx3 dx2 dx 2
or
d4 y d3 y d2 y dy x2
4
= −3 3 + 2 2 − 5 −y+
dx dx dx dx 2
for initial conditions at x=0
D3 y = −1, D2 y = 0, Dy = −1,y = 1
33
This problem can be re-written as
0.5x2
u3 (x) −3 2 −5 −1 u3 (x)
d u
2 (x) = 1
0 0 0 u2 (x)
∗
0
+
dx u1 (x) 0 1 0 0 u1 (x) 0
y(x) 0 0 1 0 y(x) 0
Now above problem can be easily solve by Scilab ode solver for 0<x<10.
Let us also check here is sti ode solver is required for a non-sti problem or
already stabilize problem.
Program 8:
// D^4(y)+3D^3(y)-2D^2(y)+5D(y)+y=(x^2)/2, 0<t<10
clf
t0=0;
v0=[-1 0 -1 1]';
A=[-3 2 -5 -1;1 0 0 0;0 1 0 0;0 0 1 0];
t=t0:0.5:10;
de('[Dv]=f(x,v)','Dv=A*[v(1);v(2);v(3);v(4)]+[(x^2)/2;0;0;0]')
v=ode(v0,t0,t,f );
subplot(5,1,1)
plot(t,v(4,:))
xgrid(5)
xtitle('solution to the 4th order non-homogeneous ODE ...
(constant coecient), Abscissa-time',,'y')
subplot(5,1,2)
plot(t,v(3,:))
xgrid(5)
xtitle('solution to the 4th order non-homogeneous ODE...
(constant coecient), Abscissa-time',,'Dy')
subplot(5,1,3)
plot(t,v(2,:))
xgrid(5)
xtitle('solution to the 4th order non-homogeneous ODE...
(constant coecient), Abscissa-time',,'(D^2)y')
subplot(5,1,4)
plot(t,v(1,:))
34
xgrid(5)
xtitle('solution to the 4th order non-homogeneous ODE...
(constant coecient), Abscissa-time',,'(D^3)y')
yprim4=-3*v(1,:)+2*v(2,:)-5*v(3,:)-v(4,:)+(t.^2)/2;
subplot(5,1,5)
plot(t,yprim4)
xgrid(5)
xtitle('solution to the 4th order non-homogeneous ODE...
(constant coecient), Abscissa-time',,'(D^4)y')
pause
40
20
0
−20
0 1 2 3 4 5 6 7 8 9 10
solution to the 4th order non−homogeneous ODE (constant coefficient), Abscissa−time
100
80
60
Dy
40
20
0
−20
0 1 2 3 4 5 6 7 8 9 10
solution to the 4th order non−homogeneous ODE (constant coefficient), Abscissa−time
60
40
20
(D^2)y
0
−20
−40
−60
−80
0 1 2 3 4 5 6 7 8 9 10
solution to the 4th order non−homogeneous ODE (constant coefficient), Abscissa−time
40
20
0
(D^3)y
−20
−40
−60
−80
−100
−120
−140
−160
0 1 2 3 4 5 6 7 8 9 10
solution to the 4th order non−homogeneous ODE (constant coefficient), Abscissa−time
40
20
0
(D^4)y
−20
−40
−60
−80
−100
−120
0 1 2 3 4 5 6 7 8 9 10
Fig 7
35
6 Miscellaneous Problems
6.1 Predator-Prey model
Model describes the interaction of two species in an eco-system when they are
isolated from others. Here we will discuss growth rate of Fish and Shark when
they don't interact with other species (assumed). In this situation rate
changes will follow relations given below
Growth rate of sh=Rate at which sh born-Rate at which sh are eaten by
sharks
Now
where F and S are instant population of Fish and Shark respectively. So, we
have
dF
= αF − βF S (1)
dt
dS
= βF S − γS (2)
dt
with the initial conditions
F (0) = F0
S(0) = S0
where
γ :death rate of sharks in the absence of their prey i.e. sh (1/years)
36
:eciency of turning predated sh into shark (shark/sh)
1. In absence of shark
dF
= αF
dt
F = F0 eαt (3)
2. In absence of sh
dS
= −γS
dt
S = S0 e−γt (4)
Non-dimensionalization:
Non-dimensionalization of equations is a process to make all terms
dimensionless. Benets of this are described later
If we nondimensionalize according to
F
F∗ =
F0
S
S∗ =
S0
t∗ = αt
dF ∗ βS0 ∗ ∗
∗
= F∗ − F S
dt α
dS ∗ βF0 ∗ ∗ γ ∗
= F S − S
dt∗ α α
if we write
βS0
a=
α
βF0
b=
α
37
γ
c=
α
Then equations can be re-writtem as
dF ∗
= F ∗ − aF ∗ S ∗ (5)
dt∗
dS ∗
= bF ∗ S ∗ − cS ∗ (6)
dt∗
with initial conditions
F ∗ (0) = 1
S ∗ (0) = 1
38
f2=zeros(1,e);
y1=zeros(1,e+1);
y2=zeros(1,e+1);
y1(1,1)=1;
y2(1,1)=1;
f1(1,1)=y1(1,1)-a*y1(1,1)*y2(1,1);
f2(1,1)=b*y1(1,1)*y2(1,1)-c*y2(1,1);
for i=1:e
f1(1,i+1)=y1(1,i)-a*y1(1,i)*y2(1,i);
f2(1,i+1)=b*y1(1,i)*y2(1,i)-c*y2(1,i);
y1(1,i+1)=y1(1,i)+Dt*f1(1,i);
y2(1,i+1)=y2(1,i)+Dt*f2(1,i);
end
y1max=max(y1);
y2max=max(y2);
y1min=min(y1);
y2min=min(y2);
Fmax=y1max*F0
Smax=y2max*S0
Fmin=y1min*F0
Smin=y2min*S0
disp(Fmin, Fmax, Smin, Smax)
disp(Fmin)
disp(Fmax)
disp(Smin)
disp(Smax)
if y1min < y2min then
p=-y1min
else
p=-y2min
end
if y1max > y2max then
q=y1max+0.1*y1max
else
q=y2max+0.1*y2max
end
r=(max(t))/Alpha;
rect=[0 p r q];
t=t/Alpha;
subplot(2,1,1)
plot2d(t,y2(1,1:e),5,'111',,rect)
plot2d(t,y1(1,1:e),11,'111',,rect)
xtitle('Fish and Shark population Vs time','time',...
'Shark(S/S0-Red),Fish(F/F0-Blue)')
subplot(2,1,2)
plot(y1(1,1:e),y2(1,1:e))
39
xtitle('Shark Vs Fish relative population plot',...
'Fish(F/F0)','Shark(S/S0)')
endfunction
//##E
Example 8: Following are the results come from the solution by Euler's
method for given values of
Table 4
Conclusion:
2. It is aapprent from the plots that as time steps goes smaller solution tends
to cyclic solution with invariable cycle amplitudes.
Here, Fish and Shark relative population plot with time and their phase
portrait for three values of time steps 1.00e+00, 6.25e-02 and 7.81e-03 are
given.
40
Fig 8
41
6.1.2 Predator-Prey problem solution by Scilab ODE Solver
Program 10:
//##S
function []=PPS(stype,Alpha,Beta,Gamma,Epsilon,t,Dt,F0,S0) //Here stype
is the method by which...
//solution is going to be taken and it may be 'adams', 'rk', 'rkf ', 'sti ' etc.
a=Beta*S0/Alpha;
b=Epsilon*Beta*F0/Alpha;
c=Gamma/Alpha;
t=t*Alpha;
Dt=(Dt/52)*Alpha;
t=0:Dt:t; y0=[1;1];
t0=0;
de('[w]=f(x,y)',['f1=y(1)-a*y(1).*y(2)';'f2=b*y(1).*y(2)-c*y(2)';'w=[f1;f2]'])
y=ode('stype',y0,t0,t,f );
t=t/Alpha;
subplot(2,1,1)
plot2d([t',t'],[y(1,:)',y(2,:)'],[11,5],'111','Fish@Shark',[0 -0.5 50 5])
title('Fish and Shark population Vs time')
subplot(2,1,2)
plot(y(1,:),y(2,:))
xtitle('Shark Vs Fish relative population plot','Fish(F/F0)','Shark(S/S0)')
endfunction
//##E
Above Predator-Prey problem has been also solved by adams, rk, rkf , sti
method and conclusions after them are as follows:
1. All these methods are able to give ne results even for a time step of 1 week.
2. Problem is not of sti type because for a same time step solution by
method other than sti method and solution by sti method are similar.
Solution is already stabilize in nature so there is no need for sti solution.
3. It has been observed that for larger time steps (for eg: 1 year) plots are non
smooth and similar for all method, one of this i.e. by rk method is given in Fig
10.
4. Plots in Fig 9 are given for adams, rk, rkf and sti method with time step
of 1 week.
42
Fig 9.1
Fig 9.2
43
Fig 10
dx
= σ(y − x)
dt
dy
= x(τ − z) − y
dt
dz
= xy − βz
dt
where
44
where the three parameter σ, τ , β are positive and are called the Prandtl
number, the Rayleigh number, and a physical proportion, respectively. It is
important to note that the x, y , z are not spacial coordinate. The x is
proportional to the intensity of the convective motion, while y is proportional
to the temperature dierence between the ascending and descending currents,
similar signs of x and y denoting that warm uid is rising and cold uid is
descending. The variable z is proportional to the distortion of vertical
temperature prole from linearity, a positive value indicating that the
strongest gradients occur near the boundaries.
Program 11:
//##S
function []=Lorenz(stype,Sigma,Tou,Beta,x0,y0,z0)
//Let for convenience xset('window',4)
clf
a=Sigma;
b=Tou;
c=Beta;
de('[w]=f(t,u)','w=[a*(u(2)-u(1)); u(1).*(b-u(3))-u(2); u(1).*u(2)-c*u(3)]');
t0=0;
t=0:0.01:50;
u0=[x0 y0 z0]';
u=ode(stype,u0,t0,t,f );
subplot(3,1,1)
plot(t,u(1,:),,'t','x')
subplot(3,1,2)
plot(t,u(2,:),,'t','y')
subplot(3,1,3)
plot(t,u(3,:),,'t','z')
xset('window',2)
clf
plot3d3(u(1,:),u(2,:),u(3,:))
title('Phase portrait of x,y,z')
endfunction
//##E
We will take here three examples by changing parameters to see their eect.
45
3. σ = 10, τ = 100 and β = 8/3 (which is the classical example).The initial
condition of the system is (x0 , y0 , z0 )= (3,0,1).
Conclusions:
Fig 11
(Reference: http://planetmath.org/encyclopedia/lorenzEquation.html)
46
Here a program has been made without using Scilab readymade curve tting
functions. Program can t up to ve curve at a time and shoes their plot at
one place for analysis purpose. Function curvetting given below requires
three input these are vector of independent variable, vector of experimental
data and a vector which may have size up to ve and contains degree of
polynomials on which curve has to be tted.
y = a0 + a1 x + ax22 + a3 x3 + a4 x4 ........
Program 12:
//##S
//let k be the degree of the polynomial
//Fundamental equation A*a=B
function []=curvetting(x,y,D)
xset('window',1);
clf
//let us rst dene how many elements are there in D
p=size(D);
q=p(1,2);
//##PHASE1: Getting A
subplot(6,2,1);
plot(x,y)
xgrid(#)
title('DATA CURVE')
m=size(x);
//n actually gives the no. of elements in the vector x or y n=m(1,2);
for l=1:q // starting of this for loop
k=D(1,l)
A=zeros(k+1,k+1);
for j=1:k+1
for i=1:k+1
A(i,j)=sum(x.^(i+j-2))
end
end
//##PHSAE2:Getting B
B=zeros((k+1),1);
for i=1:k+1;
B(i,1)=sum(y.*(x.^(i-1)))
end
//##PHASE3:Getting 'a' the coecient matrix
47
//'a' is the desired coecient matrix shows a0,a1,a2...
// in the tting polynomial
a=(A\B)';
disp(a)
//##PHASE4:Ploting curve by dierent solutions
//c & d be the rst & last value of vector x
c=x(1,1);
d=x(1,n);
x1=linspace(c,d,50);
//getting the smooth value of x
x2=x1';
//getting transpose of x1
//we have made x1 because to get a smooth curve of resultant polynomial
x3=zeros(50,k+1);
// an assumed matrix for further operation
//we going to make x3 a matrix having rst row as...
//1,x2.^2,x2.^3.... for rst value of x2, similarly second row...
//will be for second value of x2, so in the above manner we...
//will get a matrix having its row as 1,x2.^2,x2.^3....
//now if any of the row of this matrix multiply with 'a'...
//& suppose then give a vector then sum of that vector...
//element will be the corresponding y2(or say y3 here) for x2
x3(:,1)=1;
x3(:,2)=x1';
for i=1:k+1;
x3(:,i)=x2.^(i-1)
end
y3=zeros(50,1);
for i=1:50
y3(i,1)=sum(a.*x3(i,:))
end
//dening y4 as calculated from result polynomial according to 'a' matrix
subplot(6,2,(2*(l-1)+3))
plot(x2,y3)
xgrid(3)
title('CURVE FOR DEGREE='+string(k));
//PHASE5:To get the value of y corresponding to given x for resultant poly-
nomial
x4=zeros(n,k+1);
for i=1:k+1;
x4(:,i)=x'.^(i-1)
end
y4=zeros(n,1);
for i=1:n
y4(i,1)=sum(a.*x4(i,:))
end
48
//PHASE6:To get the error curve for corresponding plot,...
//i.e. plot between (ye-yr) & x. where ye is the experimental... //
value and yr is the real value
z=y'-y4;
z1=z'.^2;
z2=sum(z1);
z3=sqrt(z1);
b=[k,z2];
subplot(6,2,2*(l+1))
plot(x,z3)
xgrid(3)
title('SUM OF SQUARE OF DEVIATIONS FOR THIS ERROR CURVE='+string(z2));
ylabel('DEGREE OF CURVE='+string(k));
end // end of rst for loop
endfunction;
//##E
Example 10.1: Let us try to t the known data for a circle of radius 5 centered
at origin.
Producing data
//**S
>de('[y]=f(x)','y=sqrt(25-x.^2)')
>x=-5:0.1:5;
>y=feval(x,f );
//**E
Now tting the generated data for polynomial of degrees 1,2,3,4, and 5 by
using the program 12.
It is apparent from the plots second and third degree curves are more or less
representing the circle.
The answer came from program are as follows:
1 3.9,-1.6D-17 142.87233
2 5.2,1.0D-16,-0.2 7.0324985
3 5.2,-2.5D-17,-0.2,8.5D-18 7.0324985
4 5.0,2.0D-16,-0.1,-3.0D-18,-0.01 1.8451398
5 5.0,-6.0,-0.1,1.1D-15,-0.01,-4.0D-17 1.8451398
49
Fig 12.1
From the above plot conclusion can be made that while approaching to
higher degree plots although sum of square of deviation (sum of least squares)
reduces but along with this smoothness of the curve is also reduces, which are
opposite to each other for a desired result in industry. So one should go with
the optimize result.
Example 10.2:
Similar thing has been done a Gaussian curve for σ = 0.9 ,xmean = 5and
thus the Fig 12.2 came out concludes that no tted curve able to represent the
Gaussian curve.
50
DATA CURVE
0.45
0.40
0.35
0.30
0.25
0.20
0.15
0.10
DEGREE OF CURVE=1
0.05
0.00
−10 −5 0 5 10 15 20
CURVE FOR DEGREE=1 SUM OF SQUARE OF DEVIATIONS FOR THIS ERROR CURVE=2.8021607
1.0 0.45
0.8 0.40
0.6 0.35
0.4 0.30
0.2 0.25
0.0 0.20
−0.2 0.15
−0.4 0.10
−0.6
DEGREE OF CURVE=2
−0.8 0.05
−1.0 0.00
−10 −5 0 5 10 15 20 −10 −5 0 5 10 15 20
CURVE FOR DEGREE=2 SUM OF SQUARE OF DEVIATIONS FOR THIS ERROR CURVE=2.3957277
0.08 0.40
0.06 0.35
0.04 0.30
0.02 0.25
0.00 0.20
−0.02 0.15
0.10
DEGREE OF CURVE=3
−0.04 0.05
−0.06 0.00
−10 −5 0 5 10 15 20 −10 −5 0 5 10 15 20
CURVE FOR DEGREE=3 SUM OF SQUARE OF DEVIATIONS FOR THIS ERROR CURVE=2.3957277
0.08 0.40
0.06 0.35
0.04 0.30
0.02 0.25
0.00 0.20
−0.02 0.15
0.10
DEGREE OF CURVE=4
−0.04 0.05
−0.06 0.00
−10 −5 0 5 10 15 20 −10 −5 0 5 10 15 20
CURVE FOR DEGREE=4 SUM OF SQUARE OF DEVIATIONS FOR THIS ERROR CURVE=3.0490262
0.03 0.45
0.02 0.40
0.01 0.35
0.00 0.30
−0.01 0.25
−0.02 0.20
−0.03 0.15
0.10
DEGREE OF CURVE=5
−0.04 0.05
−0.05 0.00
−10 −5 0 5 10 15 20 −10 −5 0 5 10 15 20
CURVE FOR DEGREE=5 SUM OF SQUARE OF DEVIATIONS FOR THIS ERROR CURVE=3.1182013
0.015 0.45
0.010 0.40
0.005 0.35
0.30
0.000 0.25
−0.005 0.20
−0.010 0.15
−0.015 0.10
0.05
−0.020 0.00
−10 −5 0 5 10 15 20 −10 −5 0 5 10 15 20
Fig 12.2
//##S
de('[z]=g(t,y)','z=(2+18*t+68*t^2+180*t^3+250*t^4+250*t^4)/(y^2)')
plot(t,y)
51
//##E
//**E
1400
1200
1000
800
y
600
400
200
0
0 5 10 15 20 25
t
Fig 13
since there are spring attached (not shown) in the system for restoring pur-
pose and it has found that cylinders also behave like spring, so if we have have
combined spring constant as k, then natural frequency of the system
p
w0 = k/m
52
2πrnBi0
Fl = (L − x)sin(wt)
m
where
r =radius of coil
n =no. of turns per unit length of coil
B =Magnetic eld produce by Magnetic core
i0 =Amplitude of supplied AC current to coil
L =Length of the coil inside magnetic core at steady or mean state
x =Position of the moving part (or piston) at any time
w =Angular frequency of the AC current
d2 x Bi0
+ w02 x = (L − x)sin(wt)
dt2 m
or
d2 x 2πrnBi0 2πrnBi0
+ {w02 + sin(wt)}x = Lsin(wt)
dt2 m m
Let
a = w02
2πrnBi0
b=
m
So we have equations as
d2 x
+ {a + bsin(wt)}x = bLsin(wt)
dt2
Above things are for ideal conditions, in practical we have one more term in
the equation as
d2 x b
+ {a + bsin(wt)}x = (2L + hg )sin(wt)
dt2 2
where hg is the height of air gap between coil and magnetic core.
To solve this dierential equations in Scilab we can re-write them as
dx1
= x2
dt
and
dx2 b
= −{a + bsin(wt)}x1 + (2L + hg )sin(wt)
dt 2
Following program have been made to solve above dierential equations
53
Program 13:
//##S
function []=mech1(n,r,B,w,i0,m,v0,t0,tn,Dt,hg,k,w0,L)
if length(hu)<>length(w0) then
error('length of w0 and lv must be same')
abort;
end
t=t0:Dt:tn;
p=length(w0);
a=(w0)^2; b=B*(i0)*n*2*%pi*r/m;
de('[z]=f(t,x)','z=[x(2);-(a+b*sin(w*t))*x(1)+(b/2)*(hg+2*L)*sin(w*t)]')
t0=[0; 0];
x=ode([0 v0]',0,t,f );
xset('window',k)
clf
subplot(2,1,1)
plot(t,x(1,:))
xtitle('Position Vs time plot','time','Position(x)')
subplot(2,1,2)
plot2d(x(1,:)',x(2,:)')
xtitle('Phase plot between velocity and position','Position','Velocity')
endfunction
##E
In the program v0 is the initial condition for the velocity, t0 and tn are start
and end time for solution, Dt is the time increment for time range, k is the
gure window no. on which we want the current solution plot.
For ideal conditions of manufacturing the solution plot should have sinusoidal
pattern, which depends upon all input parameteres.
Simulation for the above has been done for following parameters (all values
are given in their corresponding SI units) to approach ideal conditions and the
corresponding solutions are plotted in Fig 13:
−1
(a)n=12000m ,r=0.02m,B=0.5T,w=314rad/sec,
54
Fig 14
Fig 13 clearly explains how changes in parameters can change the solution.
3. Scilab has a very poor help browser as compared to its competitive soft-
ware packages. While searching by some string, mostly Scilab only looks
for matching strings in the topic title only and not inside the topic also.
The help contains neither a categorical listing of functions nor an exten-
sive search. It does not feature tutorials or a Getting Started tool. The
online help available is also limited and not many tutorials are available.
55
4. Scilab has week GUI in command, Editor and other windows while com-
paring with Matlab. While error for something wrong in Editor window,
Scilab only shows at which line it is and not shows the column position,
for complicated functions sometimes it becomes dicult to x it. Simi-
lar case is with brackets in the Editor window, at any moment it doesn't
shows desired animation for paired brackets.
6. Many times it crash or hang or shows 'stack size exceeded' for large no.
of iterations.
−− > derivat(y)
−− > ans = 43 + 8x
Now how to derivate this
9. Frequently while loading les it hangs and request for unnecessary time.
10. While looking for help regarding plot or scicos related commands, help
browser doesn't show visual graphic example like plots and scicos model.
56
Summary and Conclusion
The purpose of the present investigation was to study an optimize
approach for solution of a particular category of ordinary dierential
equation. However, almost all type of ordinary dierential equation
can solve by all given methods but our concern is on two points rst
is the simulation time for numerical solution and the second is the
time step required for a desired accuracy of solution. In general for
a particular method time taken for computation increases as time
step (or may be independent variable step in some cases) reduces.
Comparative study has done by keeping one of this constant and
other as variable. It is found that although Euler's method can
solve problems but simulation time required by it is usually more or
say time step should be less in this case, especially in case of sti
category problem it takes large time as compared to other methods.
On, other hands in this study methods other than Euler's method
all methods are found to more or less similar (Note: Higher Engi-
neering problems are not taken in this study they may distinguish
these methods) until unless problem is not of sti type. In the sti
category problem it is possible to solve them other methods but sim-
ulation time are very high for a desired accuracy which is found to
be highest in case of Euler's method, Sti solver stands best here
in terms of simulation time, time step and performance of machine.
Meanwhile time of above work some other task related to main work
done for example curve tting, solution of ode by Laplace Transform
Technique which concludes that Scilab generates interactive plots at
one place for a better analysis but it should be equipped with some
essential toolboxes. The main conclusion of the approach is that
Scilab is capable to lead everyone but needs a sincere and successive
development for this achievement. Also Scilab is capable to solve dif-
ferent kind of ordinary dierential equations and their system which
regularly comes in industry. The important consequence is being
the Scilab free of cost and open source so that can be use easily and
sometimes it is faster than other software packages.
57
References
1. http://www.physics.arizona.edu/~restrepo/475B/Notes/source/node16.html,
http://en.wikipedia.org/wiki/sti_equation
2. numerical and statistical methods with SCILAB for science and
engineering, vol. 1, gilberto e. urroz
3. Youngstown state University, http://www.eng.ysu.edu/~jalam/
engr6924s07/sessions/session27/session27.pdf
4. Stanford University, http://uid.stanford.edu/~nger/teaching/
numerical_methods_02/tutorials/tutorial2.pdf
5. http://planetmath.org/encyclopedia/lorenzEquation.html
58