Scilab and Scicos Revised
Scilab and Scicos Revised
Scilab and Scicos Revised
Compiled by
Dr. PISUPATI SADASIVA SUBRAMANYAM
SENIOR MEMBER,I.E.E.E.
VIGNANA BHARATHI INSTITUE OF
TECHNOLOGY,
HYDERABAD-501301.
What is Scilab?
Scilab is a mathematical software
Similar software: Matlab, Mathematica,
Octave, Euler Math Toolbox, Maxima,
What is special about Scilab: free, highly
supported, powerful, many users,
Home page of Scilab: www.scilab.org
A short introduction of Scilab:
http://hkumath.hku.hk~nkt/Scilab/IntroToScil
ab.html
What is Scilab? (Contd.)
Common Operators
+ addition
- subtraction
* multiplication
/ division
^ power
' conjugate transpose
Here is a list of common operators in Scilab:
FUNCTIONS IN SCILAB
The term function in Scilab to (at least):
sin, cos, tan, asin, acos, atan, abs, min, max, sqrt, sum
Eg., when we enter:
sin(0.5)
then it displays:
ans =
0.4794255
Another example:
max(2, 3, abs(-5), sin(1))
ans =
5
SCILAB-ADVANTAGES
Numeric computing is better suited for complex tasks than
symbolic computing
Scilab requires less disk space than Matlab and GNU Octave
It includes a Matlab-to-Scilab translator (.m files to .sci files)
Data plotting is said to be simpler than with GNU Octave (but the
trend is toward more complex handle structures)
The Xcos toolbox installs automatically with Scilab, be it, that Xcos is
not compatible with Simulink
load('save.dat','b');
// It loads only variable b, but not
// variable a in the name of b
load('save.dat','b');
// It loads only variable b, but not
// variable a in the name of b
load('save.dat','d');
// It will not show any error messages.
// Variable d is undefined, not empty.
listvarinfile('save.dat');
// list variables in a file saved by
// the function save
Name Type Size Bytes
----------------------------------------------------
a constant 1 by 1 24
b boolean 1 by 1 20
s string 1 by 1 44
5. Dealing with Matrices
Entering Matrices
There are many ways to enter a matrix. Here is
the simplest method:
Separate each element in a row by a blank space
or a comma,
Separate each row of elements with a semi-
colon, and
Put the whole list of elements in a pair of square
brackets.
For example, to enter a 3 x 3 magic square and
assign to the variable M :
M = [8 1 6; 3 5 7; 4 9 2]
M=
8. 1. 6.
3. 5. 7.
4. 9. 2.
Calculating Sums
For a magic square, we may wish to check for its column sums and row sums and the sum of
diagonals. This is done by entering:
sum(M,'c') // column sums
ans =
15.
15.
15.
sum(M,'r') // row sums
ans =
15. 15. 15.
The sum of the main diagonal is easily done with the help of the function diag.
diag(M)
ans =
8.
5.
2.
Subscripts
M([2,1], 3:-1:1)
ans =
7. 5. 3.
6. 1. 8.
The operator $, which gives the largest value of an index, is
handy for getting the last entry of a vector or a matrix. For
example, to access all elements except the last of the last
column, we type:
M(1:$-1, $)
We sometimes want a whole row or a column. For example, to
obtain all the elements of the second row of M, enter:
M(2,:)
ans =
3. 5. 7.
Now we have a new way to perform operations like
mtlb_fliplr(M). It is done by entering M(:, $:-1:1). However the
function mtlb_fliplr(M) would obtain result faster (in
computation time) than using the subscript expression.
Simple Matrix Generation
8 * ones(2,2)
ans =
8. 8.
8. 8.
eye(2,3)
ans =
1. 0. 0.
0. 1. 0.
e = [a; b; c]
e=
1. 2. 3.
4. 5. 6.
7. 8. 9.
Concatenation must be row/column consistent:
x = [1 2]; y = [1 2 3];
z = [x; y]
!-error 6
inconsistent row/column dimensions
We can also concatenate block matrices, e.g.:
[eye(2,2) 5*ones(2,3); zeros(1,3) rand(1,2)]
ans =
1. 0. 5. 5. 5.
0. 1. 5. 5. 5.
0. 0. 0. 0.6525135 0.3076091
Remember that it is an error to access out-of-bound element of a
matrix. However, it is okay to assign values to out-of-bound
elements. The result is a larger size matrix with all unspecified
entries 0:
M = matrix(1:6, 2, 3); M(3,1) = 10
M=
1. 3. 5.
2. 4. 6.
10. 0. 0.
It is remarked that this method is slow. If the size of the matrix is
known beforehand, we should use pre-allocation:
M = zeros(3,3); // pre-allocation
M([1 2], :) = matrix(1:6, 2, 3);
M(3,1) = 10;
Deleting Rows and Columns
x=A\b
x=
- 0.3561912
1.7908789
- 0.5271342
Another method is to type inv(A) * b. Although it gives the same result, it is slower
than A\b because the first method mainly uses Gaussian Elimination which saves
some computation effort. Please read the Scilab help file for more details about
the slash operator when A is non-square.
A / b solves for x in the equation xb = A.
Entry-wise operations, Matrix Size
Creating Functions
Scilab has an open programming environment
that enables users to build their own functions
and libraries. It is done by using the built-in
editor SciPad. To call the editor, type scipad() or
editor(), or click Editor at the menu bar.
The file extensions used by scilab are .sce and
.sci. To save a file, click for the menu File and
choose Save. To load a file, choose Load under
the same menu. To execute a file, type
exec('function_file_name');
in the command line, or click for load into Scilab under the
menu Execute.
To begin writing a function, type:
function [out1, out2, ...] = name(in1, in2, ...)
where function is a keyword that indicates the start of a
function, out1, out2 and in1, in2, ..., are variables that are
outputs and inputs, respectively, of the function; the variables
can be Boolean, numbers, matrices, etc., and name is the
name of the function. Then we can enter the body of the
function. At the end, type:
endfunction
to indicate the end of the function.
Comment lines begin with two slashes //. A sample function is
given as below:
function [d] = distance(x, y)
// this function computes the distance
// between the origin and the point (x, y)
d = sqrt(x^2 + y^2);
endfunction
A Scilab function can call upon itself recursively. Here is an
example.
Unlike Matlab, Scilab allows multiple function declaration (with
different function names) within a single file. Also Scilab allows
overloading (it is not recommended for beginners). Please refer
to the chapter overloading in its help file for details.
Flow Control
while r ~= 0
r = pmodulo(n1, n2);
n1 = n2; n2 = r;
end
endfunction
The break and continue commands
They are for ending a loop and to immediately start the next iteration,
respectively. For example:
// user has to input 10 numbers and for those which
// are integers are summed up, the program are
// prematurely once a negative number is entered.
M(find(M>0))'
ans =
4. 2. 9.
We remark that M(M>0) gives results quicker than M(find(M>0)
because the find function is not necessary here.
It is important to distinguish & and and, | and or. The first one of
each pair is entry-wise operation and the other one reports truth
value based on all entries of a Boolean matrix.
M = [0 -2; 1 0]; M==0 | M==1
ans =
TF
TT
t = (0:1/100:2) * %pi;
y = sin(t);
plot(t,y);
2D Graphs
The plot function has different forms, depending on the input
arguments. If y is a vector, plot(y) produces a piecewise linear
graph of the elements of y versus the index of the elements of
y. When x and y are vectors of the same length, plot(x,y)
produces a graph of y versus x. E.g., to plot the value of the sine
function from zero to 2 pi:
3D Surfaces
The command plot3d(x,y,z) plots 3D surfaces. Here x and y (x-axis and y-axis
coordinates) are row vectors of sizes n1 and n2, and the coordinates must be
monotone, and z is a matrix of size n1xn2 with z(i,j) being the value (height) of the
surface at the point (x(i),y(j)).
8. Scilab versus Matlab
This section is based on some user comments found in the
internet, thus not necessarily all true. It is intended to give
readers a general image about their differences besides those in
syntax.
Matlab has a thorough documentation; the one in Scilab is brief.
Matlab has a lot of optimization on computation, thus it is faster
than Scilab.
Matlab has a very powerful simulation component called
Simulink.
Scilab has Scicos that serves the same purpose but it is weaker.
Matlab has a much better integration with other programming
languages and programs such as C, C++ and Excel.
The graphics component of Scilab is weak (has fewer functions).
Most importantly, Scilab is FREE. It certainly outweighs its
deficiencies. It is remarked that Scilab is more than enough for
casual and educational uses.
Vectors and matrices in Scilab (2)
Matrices in Scilab:
> A = [0 1 0 1; 2 3 4 0]
>B=A
> A * y, x * B, A * B, B * A, (B*A)^2
Special matrices (and vectors):
> ones(2,3), zeros(1,2), eye(3,3)
> rand, rand(3,2)
Empty vector or matrix: > a = [ ]
Building matrix by blocks:
> C = [A 2*A], x = [9 x 7], a = [a 1]
The colon : operator
> 1:10, 1:100, xx = 1:100;
Using ; to suppress answer output
> sum(xx)
> 1:2:10, 3:3:11, 4:1:1, 2:1:0,
> t = 0: 0.1: 2*%pi
> y = sin(t)
> plot(t,y), plot(t,sin(t),t,cos(t))
Task 1: plot the straight lines
y = x +1 and y = exp(x) on the same graph, from
x = 2 to x = 2
Elements of vectors and matrices
Example
> v = rand(4,1)
> v(1), v(3), v([2 4]), v(4:-1:1), v($)
$ means the last entry
Example
> A = [1 2 3 4 5; 6 7 8 9 10]
> A(2,3), A(1,:), A(:, 2), A(:, [4 2])
Programming in Scilab
An improved programme:
function K = fibonacci(n)
//function K = fibonacci(n)
//Gives the n-th term of the Fibonacci
sequence ,1,1,2,3,5,8,13,... if n==1, K = 0;
elseif n==2, K = 1;
elseif n>2 & int(n)==n
// check if n is an integer greater than 2
K = fibonacci(n-1) + fibonacci(n-2);
else disp('error! -- input is not a positive integer');
end
endfunction
Programming in Scilab (4)
Source code: The Scicos diagrams for all the demos are in the
directory $SCI/demos/scicos/
Scicos is a graphical dynamical system
modeler and simulator. User can create block
diagrams to model and simulate the dynamics of
hybrid dynamical systems (continuous and
discrete time) and compile such models into
executable code. It is used for signal processing,
systems control, queuing systems, and to study
physical and biological systems.
the code generated is, for the internal section, fully device and board agnostic. This means
that the same Scicos diagrams can be used for simulation and different target implementation
with not modifications at all or, for the code generation, only changing the device/board specific
input/output blocks.
http://wiki.scilab.org/Porting%20Scicos-Flex%20to%20Scilab%205.0
4. Real-Time Systems Laboratory. RETIS Lab.
Mauro Marinoni [[email protected]]. Scilab/Scicos Code.
Generator for Flex
http://www.artist-embedded.org/docs/Events/2008/RT-
Kernels/SLIDES/s6-Scicos.pdf
5. Scilab/Scicos Code Generator for FLEX
http://www.evidence.eu.com/scilabscicos-code-generator-flex-and- !
xenomai-products.html
Concept
To develop a single-click digital control automatic code generation
tool for the FLEX boards!
6. Free & Open Source Simulation Software
(Modeling, Simulation, Control and Visualization)
http://www.linkedin.com/groups/ScicosFLEX-embedded-code-
generator-100- 1389147.S.57376484
http://erika.tuxfamily.org/scilabscicos.html
MATH TOOLS - Complex Number
Conversion
Summary :
Convert complex matrices to/from polar (phasor) form
often used in electrical engineering.
Description: It adds only two routines: to_r() and to_p().
These routines
make it easy to work with complex numbers in the polar
(or
phasor) form often used by electrical engineers. In polar
form a complex number is written as a (magnitude,
angle) pair
where the angle is in degrees measured
counterclockwise (CCW) from the positive real axis.
In engineering texts the notation
mag /_ angle is often used, so 10 /_ 45 would represent the
complex number with magnitude 10 at a 45 degree CCW from the
real axis. to_r(10,45) converts this to standard form and
gives 7.07+7.07i.
The routines define and work with two matrix "polar forms".
In the first form the magnitudes and angles are in adjacent
columns of a single matrix. In the second form the magnitudes
and angles are in separate matrices. The to_r() example at
the end of the previous paragraph used the second form. The
same result can be obtained using the first form via
to_r([10 45]).
to_r() is used to convert polar form numbers or matrices
to standard form:
! - 0.2947621 - 0.2717314i !
! 0.2292141 - 0.0289284i !
-->to_p(I)
ans =
! 0.4009023 - 137.32806 !
! 0.2310324 - 7.1930998 !
-->n=-1000:1000;
-->tn=n*Ts;
-->xn=2*cos(40*3.14159*tn).*cos(800*3.14159*tn);
-->plot(tn, xn);
This program shows the Beat waves for the equations given:
X(t)=cos(220t)+cos(2400t)
X(t)=2cos(220t)cos(2400t)
The individual plots and the resultant beat wave is shown below
The Help Browser that can be accessed from various windows. Its utility
improved with Scilab 5.3.1 when demonstrations were included, but the
Help Browser is still a hard nut for newbies. It confuses by sometimes
referring to obsolete functions
Demonstrations that can be accessed from the Console. Not really tutorials
and some of them act funny, some may cause Scilab to crash, and others
still ask for a C compiler
What is really missing is an embedded tutorial (or even a users manual of the
Matlab style) that is updated with each Scilab
SCILAB INFORMATION ON THE WEB:
The main portal is Wiki Scilab, <http://wiki.scilab.org/Tutorials>, were most of the
accessible tutorials are listed
Scilabs forge <http://forge.scilab.org/> is a repository of work in progress, many of
which exist only in name. Its set of draft documents is valuable
Wiki Scilabs HowTo page <http://wiki.scilab.org/howto> has some articles of
interest
Free sites:
Scilab File Exchange website <http://fileexchange.scilab.org/>. A new
discussion forum managed by the Scilab team and dedicated to easily exchange
files, script, data, experiences, etc.
Google discussion group at <http://groups.google.com/group/ comp.soft-
sys.math.scilab/topics>
MathKB <http://www.mathkb.com/>. Contains, among other things, a Scilab
discussion forum. Mostly advanced questions
spoken-tutorial <http://spoken-tutorial.org/Study_Plans_Scilab/>. Screencasts
under construction by IIT Bombay. Scilab basics
SCILAB INFO ON WEB ( CONTD.)
YouTube has some video clips on Scilab, but nothing really valuable
Jianfeng Li
Institute of Industrial Process Control, Control Science and
Engineering Department,zhejiang university
2006.9
Introduction
The queue systems performance index
The optimal number of servers
Simulation
Conclusion
Introduction
Queuing theory
It is the study of the waiting line phenomena.
It is a branch of applied mathematics utilizing concepts from the
field of stochastic processes.
It is the formal analysis of this phenomenon in search of finding
the optimum solution to this problem so that everybody gets
service without waiting for a long time in line.
Introduction
busy
the customers arrive queuing system waiting line
free
finished
1 n
P( n c ) ( ) P0
n c c !c n c
The optimal number of servers
Step 1, according to [ out
N min, find ] the initialization number of
servers ;
N Nmin
Step 2, compute the performance indexes, such as the average
quantity of cars in the waiting queue, the average time of each car
stay in the system, and the average time of each car stay in the
waiting queue. Comparing them with desired indexes, if it is
dissatisfied, go to step 3; otherwise, the optimal number of servers is
N and the process is end.
Step 3, let , go to step 2.
N N 1
Simulation
In this system, the arrival time and service time of car are similar to
the exponential distribution.
Assume that the left time of the ith car is the begin service time of
the (i+1)th car.
All the events can be classified to two sorts: one is the arrival event of
cars; the other is the leave event of car.
When the arrival event and the leave event occur at a same time,
disposed the former first.
Simulation
In Scilab simulation
environment, the average
arrival rate of cars, the
average service rate of cars
and the simulation
terminate time are given by
the following:
Simulation
Dr.P.S.SUBRAMANYAM :