Introduction To SCILAB
Introduction To SCILAB
Introduction To SCILAB
This chapter is intended to get the user started using SCILAB through simple exercises in
numerical calculations. The chapter starts by describing how to download and install SCILAB in
a Windows environment. Installation of the software in other operating systems is very similar
and is explained in detail in the SCILAB website.
What is SCILAB?
SCILAB is a numerical, programming and graphics environment available for free from the
French Government's "Institut Nationale de Recherche en Informatique et en Automatique -
INRIA (National Institute for Informatics and Automation Research)." It is similar in operation
to MATLAB and other existing numerical/graphic environments, and it can be run using a
variety of operating systems including UNIX, Windows, Linux, etc.
• Introduction to Scilab
• Communication Toolbox Documentation
• Signal Processing
• Lmitool: Linear Matrix Inequalities Optimization Toolbox Documentation
• Metanet User's Guide and Tutorial
• Scicos
• Scilab's Internals Documentation
The documentation is available in html format, which you can access by clicking in any of the
options shown above. If you want to have your own copy of the documentation in PostScript or
PDF format, press the appropriate option in the web page.
To load a value into a variable use an assignment statement (one that includes the
equal sign), e.g., a = 3.2. Try the following exercises for simple arithmetic operations:
a = 3.2 <return>
b = 6.4 <return>
a+b <return>
a-b <return>
a*b <return>
a/b <return>
a^b <return>
who <return>
The last command will return a list of the active variables in your worksheet. Many of
them are system variables that cannot be modified by the user.
SCILAB has a number of special constants, such as %i, %pi, and %e, corresponding to the
unit imaginary number, π = ratio of circumference to diameter, and e = the base of the
natural logarithms, respectively. The value %eps is another special constant corresponding
to the maximum value for which 1 + %eps = 1. Other important constants are %inf =
infinity, and %nan = not-a-number. Boolean (i.e., logical ) constants are %t = true and %f =
false.
Try the following exercises to see what values are returned by SCILAB:
%e <return>
%i <return>
%pi <return>
%eps <return>
%inf <return>
%nan <return>
%t <return>
%f <return>
Scalars: real, logical, string, polynomial, rational, and basically any object that is not
between brackets in SCILAB is referred to as a scalar. Examples are:
a = 1 // real constant <return>
2>1 // Boolean constant (i.e., logical) <return>
'my name' // character string or constant <return>
r = poly(1.,'x') // polynomial with variable 'x' and root at 1.0 <return>
q = y/r // rational expression <return>
Using the "Introduction to SCILAB" demo: SCILAB is provided with a number of demos to
show the software abilities. To access the demos click on the Demos menu. This will
provide a dialogue window (SCILAB Choose) with a list of subjects, such as Introduction to
SCILAB, Graphics, etc. Select the Introduction to SCILAB, and press OK. A file with SCILAB
commands is loaded in memory. To see each line press <return>. The lines are shown and
executed (if executable). Comments have been added to each line to explain the
operation shown. (The entire demo session is listed in pages 118 to 137 of the
documentation file Introduction to SCILAB.) Keep pressing the <return> key to observe a
good summary of SCILAB abilities. In particular, pay special attention to item 2 (MATRICES)
since SCILAB is a matrix-based system. Also of interest are the items labeled OPERATIONS
and SOME NUMERICAL PRIMITIVES. (The term primitives is used to refer to simple
numerical operations pre-programmed in SCILAB or any other numerical environment).
Using other demos: To see any of the other demos provided with SCILAB select the
option Demos from the File menu, then select the Demo subject you want (e.g. , Graphics:
Introduction), and select a particular demo (e.g., plot2d3 in Graphics: Introduction).
Notice that, when using graphics in SCILAB, the program generates a window called
SCILABGraphic0.
Getting help: In the Help menu there are three options that you can use to get help
from SCILAB:
• Help Dialogue: provides a list of help items classified by chapters. There will be two
windows available using this option, the lower window shows the chapter titles while
the upper window shows the subjects in the chapter currently selected. Select the
item you want to display and press the Show button located to the left side of the help
window.
• Topic: enter a topic you want help with in the window shown when using this option
and press the OK button. If no help is available you will get a message indicating so in
the main SCILAB window. If help is available, SCILAB will show you the corresponding
instructions. For example, use this option and request information on the keyword inv.
It will give you information on the inverse operation for matrices.
• Apropos: enter a keyword related to the topic that you are looking for (e.g., inverse)
and press the OK button. You will see a window with topics including the keyword you
selected. Scroll through the list and find that topic closer to your interest (e.g., inv -
matrix inverse), and press OK to get information on that particular topic.
Pause mode: when entering Pause mode, SCILAB creates a new (numbered) prompt. In this
mode you can enter SCILAB commands without affecting the main line of calculation
(represented by the main SCILAB prompt !). To get into the pause mode you can type
pause at the main prompt or click on the Pause option in the upper menu bar of the main
SCILAB window. (The option Interupt [sic] in the upper menu bar also allows you to enter
into pause mode, however, it will interrupt any operation currently being executed by
SCILAB).. You can have several levels of pause, one for every time you enter a pause
command. To leave the current pause level and move the upper one type return. To
move up two levels use the command quit. (Note, when used in the main SCILAB prompt,
quit will terminate your SCILAB session). For more information on the pause mode use the
help feature as described above, or simple type help pause.
Vectors:
• To enter vectors use the square brackets and separate the elements with commas or
blanks for a row vector, e.g.: v = [-1. , 2., %pi].
• The transpose of a vector (or matrix) is expressed using the apostrophe, for example,
type: v'
• To enter a column vector, use any of the following procedures: w = [3; -2; 5] <return>
or
r = [6 <return>
-2 <return>
10 ] <return>
• You can create a row vector by indicating a starting value, an increment (or
decrement), and an ending value, all separated by the colon (:) operator as follows:
Note: Expressions such as ‘a’, ‘b’, etc., are referred to as strings. Therefore, only
those string operations such as concatenation, part, etc. are allowed for vectors with
character elements. SCILAB strings and string operations are presented in a subsequent
chapter.
(Press Restart)
diary ('session1.txt') <return>
A = [1. 2. 3.; 2. 3. 1.; 3. 2. 1.];b=[5; 4; -1.];<return>
A <return>
b <return>
x = linsolve(A,b) <return>
Next, use NOTEPAD, or other text editor, to open the file session1.txt to see the contents
of the file. The default directory for storing a diary file is the bin subdirectory within the
SCILAB directory.
Note: The SCILAB worksheet does not allow cutting and pasting. Therefore, the use of the
diary function with a filename is the only way to copy SCILAB output into a text file.
Command Input: you can read SCILAB commands from a script file, which is basically a
text file listing all the commands you want to use. As an example, create the following
text file in the bin subdirectory of the SCILAB directory using NOTEPAD, and call it
session2.txt:
//--------------------------------------------------------------------------
clear
A = [1. 2. -3. // entering
3. 4. 5. // elements of
7. 8. 9.] // matrix A
b = [1.; 2.; 3.] // enter vector b
xa = inv(A)*b // calculate x using matrix inverse
xb = linsolve(A,b) // calculate x using SCILAB's own linsolve function
//--------------------------------------------------------------------------
1 - Press the Restart option in the menu bar to clear the SCILAB environment.
2 - Enter the following commands (you don’t need to enter the comments):
-->x = [0:%pi/20:2*%pi];
3 - Use cntl-P and edit the previous command (y = sin(x) + sin(2*x);) to read:
5 - So far you have created vectors x, y, z, and p. Next, we use the functions xset with the
option ‘window’, and the function plot to produce a plot of y-vs.-x:
-->xset('window',0); plot(x,y)
-->xset('window',1); plot(x,z)
7 - Continue using cntl-P to edit the last commands and produce the following plots:
-->xset('window',2); plot(x,p)
-->xset('window',3); plot(y,z)
-->xset('window',4); plot(y,p)
-->xset('window',5); plot(z,p)
! 1. 2. 5. - 4. !
! 0. 2. 3. - 5. !
-->x*y
!--error 10
inconsistent multiplication
-->x.*y
ans =
! 0. 4. 15. 20. !
39.
-->sum(x.*y)
ans =
39.
-->x*y'
ans =
39.
-->x'*y
ans =
! 0. 2. 3. - 5. !
! 0. 4. 6. - 10. !
! 0. 10. 15. - 25. !
! 0. - 8. - 12. 20. !
-->y'*x
ans =
! 0. 0. 0. 0. !
! 2. 4. 10. - 8. !
! 3. 6. 15. - 12. !
! - 5. - 10. - 25. 20. !
-->x*y' + y'*x
ans =
Note: These commands and their corresponding results represent an exploratory session for
vector operations.
3 - Suppose that you are only interested in the commands defining vectors x and y, in the
operations that produce the dot product of the vectors (i.e., sum(x.*y) and x*y'), and in
the very last command (b = x*y' + y'*x). Using the diary command create the file
c:\myVectors.txt and collect only the commands of interest out of the command history
buffer by using cntl-P and cntl-N as needed. The resulting SCILAB session should look like
this:
! 1. 2. 5. - 4. !
! 0. 2. 3. - 5. !
-->sum(x.*y)
ans =
39.
-->x*y'
ans =
39.
-->diary(0)
The session, except for the very first command () is stored in file c:\myVectors.txt. This
file can be edited or printed from a text editor such as NOTEPAD, or my favorite, PFE
(Programmer’s File Editor) available for free from:
http://www.lancs.ac.uk/people/cpaap/pfe
PFE can be used in combination with SCILAB to type scripts and function files or to edit
diary files produced from within SCILAB.
-->pwd
Under a Windows operating system, the default current directory is typically c:\
I recommend that you create a sub-directory, or folder, called work and locate it under the
SCILAB main directory. For example, under a Windows operating system, the SCILAB main
directory will typically be
c:\Program Files\SCILAB2.5
c:\Program Files\SCILAB2.5\work
At the beginning of a SCILAB session, you can change the current directory to the work
directory by using the function chdir:
You can use your work directory to store scripts and functions that you create. For
example, if you create a script called script1 in the work directory, once you change the
current directory to the work directory, you can simply use:
-->exec(‘script1’)
If you type a function called function1 in the work directory, with the work directory
selected as the current directory, you can load the function by simply using:
-->getf(‘function1’)
Scripts and function files can be created using NOTEPAD, PDE, or any other text editor.
To find out more about these functions use the help command. For example, try:
-->help roots
-->help eye
-->help trace
Exercises
Determine the result of the following calculations using SCILAB if a = 2.3, b = -2.3, c= π/2, x =
2/π, and y = √3:
[1]. (a2 + bc + x)
[2]. sin(c) + y/c
[3]. (a+c)/(x+y)
[4]. 1/(cos(c) + ln(x))
[5]. (a+c)3/b
Check if the following Boolean statements are true or false based on the values of a, b, c, x,
and y given above.
[6]. a > c
[7]. a = b
[8]. (2a+b)/x2 < 1
[9]. x + 2ab + b2 ≤ 23
[10]. 2ac = 2cb
[11]. Use SCILAB’s help facility to find out information about function deff and use it to define
a function y = f(x) = x2 + 1.
Using the vectors u = [3, 2, -1], and v = [4, -6, 2], calculate the following operations:
[12]. w = u+v
Using the matrices A, B, and C, shown below, perform the following operations:
5 5 − 2 − 5 4 − π 2 5
A = 1 − 2 0 , B = − 1 − 1 10 , C = 1 − 3
4 − 1 − 2 0 4 − 2 4 2
[16]. A+B
[17]. A⋅B
[18]. B⋅A
[19]. B⋅C
[20]. A⋅ B⋅C