Aditya Sengupta
Indian Institute of Technology Bombay
apsengupta@iitb.ac.in
Difference Equations
ODEs in Scilab
Difference Equations
ODEs in Scilab
ODEs in Scilab
ODEs in Scilab
ODEs in Scilab
ODEs in Scilab
Example
f u n c t i o n dx = f ( t , x )
dx = s i n ( 2 t ) ;
endfunction
ODEs in Scilab
Example
f u n c t i o n dx = f ( t , x )
dx = s i n ( 2 t ) ;
endfunction
ODEs in Scilab
Solution
ODEs in Scilab
Depending on the initial value, the plot will look like this:
ODEs in Scilab
The simulation tool we will use for solving ODEs in Scilab is the
ode function
The simplest calling sequence for ode is:
y=ode(y0, t0, t, f)
where y0 is the initial value at t0 and t contains the points in
time at which the solution is to be determined. f is the function
corresponding to
y = f (t, y )
ODEs in Scilab
t0 = 0
x0 = 0.5
t = 0:0.1:5;
x = ode ( x0 , t0 , t , f ) ;
plot2d (t , x)
ODEs in Scilab
t0 = 0
x0 = 0.5
t = 0:0.1:5;
x = ode ( x0 , t0 , t , f ) ;
plot2d (t , x)
ODEs in Scilab
ODEs in Scilab
ODEs in Scilab
ODEs in Scilab
ODEs in Scilab
ODEs in Scilab
ODEs in Scilab
f u n c t i o n dx = f ( t , x )
dx ( 1 ) = x ( 2 )
dx ( 2 ) = s i n ( 2 t )
endfunction
5
6
7
t = 0 : 0 . 0 1 : 4 %pi ;
y=ode ( [ 0 ; 1/2] , 0 , t , f ) ;
// Note t h e i m p o r t a n c e o f g i v i n g c o r r e c t s t a r t i n g v a l u e s .
Try t o p u t a l t e r n a t e s t a r t i n g v a l u e s and s e e t h e
difference .
plot2d (t , [ y ( 1 , : ) y (2 , : ) ] )
// The c u r v e i n b l a c k i s t h e f i n a l s o l u t i o n . The o t h e r c u r v e
i s f o r i l l u s t r a t i o n t o show t h e i n t e r m e d i a t e s t e p .
ODEs in Scilab
f u n c t i o n dx = f ( t , x )
dx ( 1 ) = x ( 2 )
dx ( 2 ) = s i n ( 2 t )
endfunction
5
6
7
t = 0 : 0 . 0 1 : 4 %pi ;
y=ode ( [ 0 ; 1/2] , 0 , t , f ) ;
// Note t h e i m p o r t a n c e o f g i v i n g c o r r e c t s t a r t i n g v a l u e s .
Try t o p u t a l t e r n a t e s t a r t i n g v a l u e s and s e e t h e
difference .
plot2d (t , [ y ( 1 , : ) y (2 , : ) ] )
// The c u r v e i n b l a c k i s t h e f i n a l s o l u t i o n . The o t h e r c u r v e
i s f o r i l l u s t r a t i o n t o show t h e i n t e r m e d i a t e s t e p .
ODEs in Scilab
Root Finding
ODEs in Scilab
Root Finding
ODEs in Scilab
Root Finding
ODEs in Scilab
ODEs in Scilab
ODEs in Scilab
Example
Let us consider the example of the engine that is revved. We wish
to constrain the revving of the engine till it reaches a certain point.
We build a first order approximation of an engine using the
following code (call it engine.sci):
1
2
3
e x e c engine . sci
revs = ode ( 0 , 0 , 0 : 0 . 1 : 1 0 , engine ) ;
p l o t 2 d ( 0 : 0 . 1 : 1 0 , revs )
ODEs in Scilab
Example
Let us consider the example of the engine that is revved. We wish
to constrain the revving of the engine till it reaches a certain point.
We build a first order approximation of an engine using the
following code (call it engine.sci):
1
2
3
e x e c engine . sci
revs = ode ( 0 , 0 , 0 : 0 . 1 : 1 0 , engine ) ;
p l o t 2 d ( 0 : 0 . 1 : 1 0 , revs )
ODEs in Scilab
Example
Let us consider the example of the engine that is revved. We wish
to constrain the revving of the engine till it reaches a certain point.
We build a first order approximation of an engine using the
following code (call it engine.sci):
1
2
3
e x e c engine . sci
revs = ode ( 0 , 0 , 0 : 0 . 1 : 1 0 , engine ) ;
p l o t 2 d ( 0 : 0 . 1 : 1 0 , revs )
ODEs in Scilab
e x e c engine . sci
e x e c gearbox . sci
[ revs , stop_time ]= ode ( r o o t , 0 , 0 , 0 : 0 . 1 : 1 0 , engine , 1 ,
gearbox ) ;
p l o t 2 d ( [ 0 : 0 . 1 : stop_time ( 1 ) , stop_time ( 1 ) ] , revs )
ODEs in Scilab
e x e c engine . sci
e x e c gearbox . sci
[ revs , stop_time ]= ode ( r o o t , 0 , 0 , 0 : 0 . 1 : 1 0 , engine , 1 ,
gearbox ) ;
p l o t 2 d ( [ 0 : 0 . 1 : stop_time ( 1 ) , stop_time ( 1 ) ] , revs )
ODEs in Scilab
Compare the two graphs- can you see where the simulation was
halted in the second case?
ODEs in Scilab
Linear Systems
Since they appear so often, there are special functions for modeling
and simulating linear systems.
For instance, you can create a linear system thus:
1
2
s = poly (0 , s )
sys = s y s l i n ( c , 1 / ( s+1) )
t = 0:0.1:10;
y = c s i m ( s t e p , t , sys ) ;
plot2d (t , y) ;
z = c s i m ( s i n ( 5 t ) , t , sys ) ;
plot2d (t , z) ;
bode ( sys , 0 . 0 1 , 1 0 0 ) ;
ODEs in Scilab
Linear Systems
Since they appear so often, there are special functions for modeling
and simulating linear systems.
For instance, you can create a linear system thus:
1
2
s = poly (0 , s )
sys = s y s l i n ( c , 1 / ( s+1) )
t = 0:0.1:10;
y = c s i m ( s t e p , t , sys ) ;
plot2d (t , y) ;
z = c s i m ( s i n ( 5 t ) , t , sys ) ;
plot2d (t , z) ;
bode ( sys , 0 . 0 1 , 1 0 0 ) ;
ODEs in Scilab
ODEs in Scilab
Thanks
ODEs in Scilab