Вы находитесь на странице: 1из 8

Analysis and Discussion of session-1

AE 332
, August 9 , 2019, Modelling and Analysis lab,

session-1

SOLVING ODE USING SCILAB

N Sri Vaishnav,A Samindra Simha


5th Semester Aerospace Engineering
IIST, Thiruvananthapuram

ABSTRACT
In this session we motivate ourself to do modelling,solving ordinary differential equations in scilab gives the simple way
to understand mechanical system with less of work load. Here, we modelled by starting from first order ode to application of
second order ode to mechanical systems . This session has been divided into three parts . Graphs are plotted in each part and
tolerances are given to it to study and compare accuracy of the solution .

INTRODUCTION :
Mathematical modeling is used to transform a particular event into a mathematical description. In general, the event is too
complex to be described . Mathematical modeling gives us a recipe how to simplify observed phenomenon and to arrive at a
computationally tractable description
Numerical methods are techniques by which mathematical problems are formulated so that they can be solved with arith-
metic operations. Although there are many kinds of numerical methods, they have one common characteristic: they invariably
involve large numbers of heavy arithmetic calculations. . Though analytical problems are still essential for understanding ,
numerical methods give a practical sense of it . Numerical methods sometimes are difficult to solve . So we use softwares that
can solve heavy operations . These are extremely useful in various departments of science , economics etc.. . One of those
software that is highly useful is SCILAB . In this assignment we model ordinary differential equations through this software .

OBJECTIVE :
Our motto is to solve ordinary differential equation and to simulate simple mechanical systems.

Theory
To solve ODE ,we use euler methods because they are simple and useful for quick programming although their accuracy is
0 00
not high . Eulers can be derived from taylor’s series expansion f (xi+1 ) = f (xi ) + f (xi ) ∗ h + f (xi ) ∗ h2 + ...... . Then re-write
or arrange only the first order forward difference will give them
f (xi+1 )− f (xi ) 0
h ≈ f (xi ) and consider as equation 1 and use h as step size x(i+1 ) − x(i ) .

f (xi+1 ) − f (xi ) 0
≈ f (xi ) (1)
h

1 Copyright
c August 9 , 2019 by SC17B029 and SC17B007
0
f (xi+1 ) = f (xi ) + f (xi ) ∗ h
0
let us consider f (xi ) = f (xi , yi ) Therefore equation 1 can be written as ;

f (xi+1 ) = f (xi ) + f (xi , yi ) ∗ h (2)

Equation (2) is called Eulers method . Then consider i is a integer number where i = 0,1,2,3...n . Now the equation 2 can be
expanded into ;
0
f (x1 ) = f (x0 ) + f (x0 ) ∗ h
0
f (x2 ) = f (x1 ) + f (x1 ) ∗ h
0
f (x3 ) = f (x2 ) + f (x2 ) ∗ h
f (xi+1 ) = f (xi ) + f (xi , yi ) ∗ h
.
.
.
f (xn ) = f (xn−1 ) + f (xn−1 , yn−1 ) ∗ h

The final answer of euler can be more accurate if we modify it by using slope of function of estimated midpoints of (xi , yi )
and (xi+1 , yi+1 ) to approximately yi+1 .

Again we use the euler equation f (xi+1 ) = f (xi ) + f (xi , yi ) ∗ h .

xi +xi+1 yi +yi+1
yi+1 = yi + f ( 2 , 2 )∗h

2xi +(xi+1 −xi ) 2yi +(yi+1 −yi )


= yi + f ( 2 , 2 )∗h

yi+1 = yi + f (xi + h/2, yi + 4y/2) ∗ h (3)

where 4y is estimated incremental value of y from yi and can be obtained by using the formula as
4y = f (xi , yi ) ∗ h
Then equation 3 can be written as

yi+1 = yi + f (xi + h/2, yi + f (xi , yi ) ∗ h/2) ∗ h (4)

use m1 and m2
m1 = f (xi , yi ) and m2 = f (xi + h/2, yi + m1 ∗ h/2
Then equation 4 will become

∴ yi+1 = y1 + m2 ∗ h (5)

Note that equation (5) is modified eulers or polygon method .

2 Copyright
c August 9 , 2019 by SC17B029 and SC17B007
Notation
solution -solution obtained by ode solver
solution(rtol) -solution obtained by ode solver when rtol = 0.2
solution(atol) -solution obtained by ode solver when atol = 0.2
h -integration intervel

Simulation and Analysis


1 . First Order ODE
Scilab code

Listing 1. Scilab Code of first order ODE


clear
clc
function xdot= deriv ( t , x )
xdot=cos ( t )
endfunction
/ / when t i m e i n t e r v a l i s l a r g e
t = 0 : 0 . 1 : 7 ∗ %pi ;
x = ode ( 0 , 0 , t , d e r i v ) ;
y = ode ( 0 , 0 , t , 0 . 2 , 0 . 0 0 1 , d e r i v ) ;
z = ode ( 0 , 0 , t , 0 . 0 0 1 , 0 . 2 , d e r i v ) ;

subplot (2 ,2 ,1)
plot ( t , x)
plot ( t , y , ’ red ’ ) / / with respect to r e l a t i v e tolerance
plot ( t , z , ’ green ’ ) / / with respect to absolute tolerance
t i t l e ( ’ when h i s 0 . 1 ’ , ’ f o n t s i z e ’ , 4 ) / / h i s i n t e g r a t i o n i n t e r v a l

subplot (2 ,2 ,3)
p l o t ( t , y−x , ’ r e d ’ ) / / e r r o r when r e l a t i v e t o l e r a n c e i s h i g h
p l o t ( t , z−x , ’ g r e e n ’ ) / / e r r o r when a b s o l u t e t o l e r a n c e i s h i g h
t i t l e ( ’ when h i s 0 . 1 ’ , ’ f o n t s i z e ’ , 4 ) / / h i s i n t e g r a t i o n i n t e r v a l

/ / when t i m e i n t e r v a l i s small
t = 0 : 0 . 0 1 : 7 ∗ %pi ;
x = ode ( 0 , 0 , t , d e r i v ) ;
y = ode ( 0 , 0 , t , 0 . 2 , 0 . 0 0 1 , d e r i v ) ;
z = ode ( 0 , 0 , t , 0 . 0 0 1 , 0 . 2 , d e r i v ) ;
subplot (2 ,2 ,2)
plot ( t , x)
plot ( t , y , ’ red ’ ) / / with respect to r e l a t i v e tolerance
plot ( t , z , ’ green ’ ) / / with respect to absolute tolerance
t i t l e ( ’ e r r o r when h i s 0 . 0 1 ’ , ’ f o n t s i z e ’ , 4 ) / / h i s i n t e g r a t i o n i n t e r v a l

subplot (2 ,2 ,4)
p l o t ( t , y−x , ’ r e d ’ ) / / e r r o r when r e l a t i v e t o l e r a n c e i s h i g h
p l o t ( t , z−x , ’ g r e e n ’ ) / / e r r o r when a b s o l u t e t o l e r a n c e i s h i g h
t i t l e ( ’ e r r o r when h i s 0 . 0 1 ’ , ’ f o n t s i z e ’ , 4 ) / / h i s i n t e g r a t i o n i n t e r v a l

/ / d i s p l y i n g t h e max e r r o r o c c u r e d i n t h e s o l u t i o n

3 Copyright
c August 9 , 2019 by SC17B029 and SC17B007
a=x−s i n ( t )
k=max ( abs ( a ) )
disp ( k )

Results and Discussions


The solution(blue)and solution occurred when the relative tolerance (rtol)(red) and absolute tolerance (atol)(green) when
changed are plotted.And error are also plotted down below the plots when the integration intervel(h) is appeared as 0.1 and
0.01.

.
It is observed that the accuracy of the solution also depends on integral interval h since the integral intervelh is smaller than
the relative tollerence the accuracy of the solution is incresed .As the solution typically varies b/w -1 and 1 .At the solution near
zero the rtol limitaion is does not hold good than absolute tolerence limitaion . since for the solution(rtol) is given much tighter
value of atol the soltion(rtol) holds well at the solution near zero . because the relative error is becoming very large when the
solution is near zero.hence the solution(rtol) much deviates at the values of 1 and -1 since the relative tollerence is taken in the
consideraion.Accuracy of the solution(atol) is much less the accuracy of soution(rtol),because the solution values are having
less slope at the solution near 1. hence the consiteration of relative tollerence holds bad and the value of atol is much loser,so
the accuracy becomes most difficult at this place.hence,the solution(atol) is much deviated at the max values of the solution.

2 . System of Coupled ODE’s


Scilab code

4 Copyright
c August 9 , 2019 by SC17B029 and SC17B007
Listing 2. Scilab Code of coupled ODE
clear
function xdot= deriv ( t , x )
xdot (1)=1∗ x (1)+2∗ x (2)
xdot (2)=3∗ x (1)+4∗ x (2)
endfunction
t =0:0.01:1
x= ode ( [ 1 ; 1 ] , 0 , t , d e r i v )
y= ode ( [ 1 ; 1 ] , 0 , t , 0 . 2 , 0 . 0 1 , d e r i v )
z= ode ( [ 1 ; 1 ] , 0 , t , 0 . 0 1 , 0 . 2 , d e r i v )

subplot (2 ,2 ,1)
plot ( t , x ( 1 , : ) )
plot ( t , y ( 1 , : ) , ’ red ’ ) / / with respect to r e l a t i v e tolerance
plot ( t , z ( 1 , : ) , ’ green ’ ) / / with respect to absolute tolerance
t i t l e ( ’ x1 ’ , ’ f o n t s i z e ’ , 4 )

subplot (2 ,2 ,3)
p l o t ( t , y ( 1 , : ) − x ( 1 , : ) , ’ r e d ’ ) / / e r r o r when r e l a t i v e t o l e r a n c e i s h i g h
p l o t ( t , z ( 1 , : ) − x ( 1 , : ) , ’ g r e e n ’ ) / / e r r o r when a b s o l u t e t o l e r a n c e i s h i g h
t i t l e ( ’ e r r o r i n x1 w r t t o l l e r e n c e ’ , ’ f o n t s i z e ’ , 4 )

subplot (2 ,2 ,2)
plot ( t , x ( 2 , : ) )
plot ( t , y ( 2 , : ) , ’ red ’ ) / / with respect to r e l a t i v e tolerance
plot ( t , z ( 2 , : ) , ’ green ’ ) / / with respect to absolute tolerance
t i t l e ( ’ x2 ’ , ’ f o n t s i z e ’ , 4 )

subplot (2 ,2 ,4)
p l o t ( t , y ( 2 , : ) − x ( 2 , : ) , ’ r e d ’ ) / / e r r o r when r e l a t i v e t o l e r a n c e i s h i g h
p l o t ( t , z ( 2 , : ) − x ( 2 , : ) , ’ g r e e n ’ ) / / e r r o r when a b s o l u t e t o l e r a n c e i s h i g h
t i t l e ( ’ e r r o r i n x2 w r t t o l l e r e n c e ’ , ’ f o n t s i z e ’ , 4 )

clc
e1 = [ ( y ( 1 , 5 0 ) − x ( 1 , 5 0 ) ) / x ( 1 , 5 0 ) ( z ( 1 , 5 0 ) − x ( 1 , 5 0 ) ) / x ( 1 , 5 0 ) ]
e2 = [ ( y ( 2 , 5 0 ) − x ( 2 , 5 0 ) ) / x ( 2 , 5 0 ) ( z ( 2 , 5 0 ) − x ( 2 , 5 0 ) ) / x ( 2 , 5 0 ) ]
d i s p ( ’ e r r o r i n x1 w i t h i t e r a t i o n 50 f o r r e l a t i v e and a b s o l u t e t o l l e r e n c e r e s p e c t i v l y a r e ’ )
d i s p ( e1 )
d i s p ( ’ e r r o r i n x2 w i t h i t e r a t i o n 50 f o r r e l a t i v e and a b s o l u t e t o l l e r e n c e r e s p e c t i v l y a r e ’ )
d i s p ( e2 )

e1 = [ ( y ( 1 , 9 0 ) − x ( 1 , 9 0 ) ) / x ( 1 , 9 0 ) ( z ( 1 , 9 0 ) − x ( 1 , 9 0 ) ) / x ( 1 , 9 0 ) ]
e2 = [ ( y ( 2 , 9 0 ) − x ( 2 , 9 0 ) ) / x ( 2 , 9 0 ) ( z ( 2 , 9 0 ) − x ( 2 , 9 0 ) ) / x ( 2 , 9 0 ) ]
d i s p ( ’ e r r o r i n x1 w i t h i t e r a t i o n 90 f o r r e l a t i v e and a b s o l u t e t o l l e r e n c e r e s p e c t i v l y a r e ’ )
d i s p ( e1 )
d i s p ( ’ e r r o r i n x2 w i t h i t e r a t i o n 90 f o r r e l a t i v e and a b s o l u t e t o l l e r e n c e r e s p e c t i v l y a r e ’ )
d i s p ( e2 )

5 Copyright
c August 9 , 2019 by SC17B029 and SC17B007
Result and Discussion
The solution(blue)and solution occurred when the relative tolerance (rtol)(red) and absolute tolerance (atol)(green) when
changed are plotted.And error are also plotted down below the plots.

As the solution values are all greater than 1 the absolute tollerence does not make accuracy much deviated.it is observed that
the accuracy is maitained well in the solution(atol) than solution(rtol) .since the only parameter comes into the picture is relative
tollerence ,the tighter the relative tollerence give more accurate results.The values are deviate in both the solution at much higher
values because the relative error becomes relatively smaller than the integration intervel h.(see the observation below).hence the
solution(atol) is much accurate than solution (rtol) because, the value of relative tollerence given is much tighter in solution(atol)
than in solution(rtol).

observation
error in x1 with iteration 50 for relative and absolute tollerence respectivly are
0.0471343 0.0471343
error in x2 with iteration 50 for relative and absolute tollerence respectivly are
0.0497807 0.0497807
error in x1 with iteration 90 for relative and absolute tollerence respectivly are
0.0821069 0.0767292
error in x2 with iteration 90 for relative and absolute tollerence respectivly are
0.0825646 0.0771569

6 Copyright
c August 9 , 2019 by SC17B029 and SC17B007
3 . Simulating a Pendulum
Scilab code

Listing 3. Scilab Code of pendulum


clear
function xdot= deriv ( t , x )
xdot (1)= x (2)
xdot (2)= −9.8∗ s i n ( x ( 1 ) )
endfunction
t =0:0.01:4
x= ode ( [ 0 ; 1 ] , 0 , t , d e r i v )
y= ode ( [ 0 ; 1 ] , 0 , t , 0 . 2 , 0 . 0 1 , d e r i v )
z= ode ( [ 0 ; 1 ] , 0 , t , 0 . 0 1 , 0 . 2 , d e r i v )
subplot (2 ,2 ,1)
plot ( t , x ( 1 , : ) )
plot ( t , y ( 1 , : ) , ’ red ’ ) / / with respect to r e l a t i v e tolerance
plot ( t , z ( 1 , : ) , ’ green ’ ) / / with respect to absolute tolerance
t i t l e ( ’ position ’ , ’ fontsize ’ ,4)

subplot (2 ,2 ,3)
p l o t ( t , y ( 1 , : ) − x ( 1 , : ) , ’ r e d ’ ) / / e r r o r when r e l a t i v e t o l e r a n c e i s h i g h
p l o t ( t , z ( 1 , : ) − x ( 1 , : ) , ’ g r e e n ’ ) / / e r r o r when a b s o l u t e t o l e r a n c e i s h i g h
t i t l e ( ’ e r r o r in p o s i t i o n wrt t o l l e r e n c e ’ , ’ f o n t s i z e ’ ,4)

subplot (2 ,2 ,2)
plot ( t , x ( 2 , : ) )
plot ( t , y ( 2 , : ) , ’ red ’ ) / / with respect to r e l a t i v e tolerance
plot ( t , z ( 2 , : ) , ’ green ’ ) / / with respect to absolute tolerance
t i t l e ( ’ velocity ’ , ’ fontsize ’ ,4)

subplot (2 ,2 ,4)
p l o t ( t , y ( 2 , : ) − x ( 2 , : ) , ’ r e d ’ ) / / e r r o r when r e l a t i v e t o l e r a n c e i s h i g h
p l o t ( t , z ( 2 , : ) − x ( 2 , : ) , ’ g r e e n ’ ) / / e r r o r when a b s o l u t e t o l e r a n c e i s h i g h
t i t l e ( ’ e r r o r in v e l o c i t y wrt t o l l e r e n c e ’ , ’ f o n t s i z e ’ ,4)

e1 = [ ( y ( 1 , 5 0 ) − x ( 1 , 5 0 ) ) / x ( 1 , 5 0 ) ( z ( 1 , 5 0 ) − x ( 1 , 5 0 ) ) / x ( 1 , 5 0 ) ]
e2 = [ ( y ( 2 , 5 0 ) − x ( 2 , 5 0 ) ) / x ( 2 , 5 0 ) ( z ( 2 , 5 0 ) − x ( 2 , 5 0 ) ) / x ( 2 , 5 0 ) ]

/ / c h e a k i n g t h e v a l u e s by e n e r g y e q u a t i o n
/ / i n i t i a l v e l o c i t y i s 1 m/ s
/ / i n i t i a l s p e c i f i c k e i s 0 . 5 m2 / s 2
/ / c h e k i n g a t 50 i t i r a t i o n
E = 0 . 5 ∗ x ( 2 , 5 0 ) ∗ x ( 2 , 5 0 ) + (1− c o s ( x ( 1 , 5 0 ) ) ) ∗ 9 . 8 0 0

clc
/ / c h e k e d a t t h e i t e r a t i o n 50
d i s p ( ’ t o t a l e n e r g y i s 0 . 5 and c a l c u l a t e d e n e r g y E i s ’ )
disp (E)

d i s p ( ’ e r r o r i n p o s i t i o n w i t h i t e r a t i o n 50 f o r r e l a t i v e and a b s o l u t e t o l l e r e n c e r e s p e c t i v l y a r e
d i s p ( e1 )
d i s p ( ’ e r r o r i n v e l o c i t y w i t h i t e r a t i o n 50 f o r r e l a t i v e and a b s o l u t e t o l l e r e n c e r e s p e c t i v l y a r e

7 Copyright
c August 9 , 2019 by SC17B029 and SC17B007
d i s p ( e2 )
Result and Discussion
The solution(blue)and solution occurred when the relative tolerance (rtol)(red) and absolute tolerance (atol)(green) when
changed are plotted.And error are also plotted down below the plots

Disscution is comparable to the first discussion .As the solution typically varies b/w -1 and 1 .At the solution near zero the
rtol limitaion is does not hold good than absolute tolerence limitaion . since for the solution(rtol) is given much tighter value of
atol the soltion(rtol) holds well at the solution near zero.hence the solution(rtol) much deviates at the values of 1 and -1 since the
relative tollerence is taken in the consideraion.Accuracy of the solution(atol) is much less the accuracy of soution(rtol),because
the solution values are having less slope at maximum and minimum of solution. hence the consideration of relative tollerence
holds bad and the value of atol is much loser,so the accuracy becomes most difficult at this place.hence,the solution(atol) is
much deviated than solution(atol) from the max values of the solution.
The solution obtained is cheked using the conservation of energy .Given the initial velocity 1m/s this implise total energy
i.e specific k.e is 0.5 m2 /s2 .The total energy is calculated and magnitude in S.I units is displayed as 0.5 .to find the solution
solver has to perform many steps to arrive at the solution ,thus the error in each step is accumulated thus the boolean variable
E==0.5 gives the value F.cheked the below code to get the total error accumulate in the total energy
disp(E-0.5) this is in the order of 10−8

8 Copyright
c August 9 , 2019 by SC17B029 and SC17B007

Вам также может понравиться