You are on page 1of 36

Basic of Scilab

Course Outline
Introduction to Scilab Environment
Introduction to Environment
Basic Operations
Matrices
Predefined Functions
Programming
Boolean/Condition Statements
Loops
Creation of User defined Functions

Different
Visualization
(with
commands)

Introduction
Scilab is a scientific software package for
technical computing.
The following are the main features of Scilab:
A high-level programming language (interpreter)
A data environment
A large collection of predefined functions
Integrated graphics

Introduction
International Presence
France
Strong presence in mechatronics and engineering
Used in high schools, at the agregation de
mathmatiques, engineering schools and universities

Europe
European projects
Eurocodes (civil engineering)

North America
Collaboration with National Instruments:
LabView/Scilab interface

Introduction
International Presence
China
Since 2002 : yearly Scilab Contest
2005 : Scilab used in secondary schools
Chinese Scilab Consortium

Japan
Collaboration with National Institute of informatics (NII)
Since 2006 : yearly Scilab Contest

India
Collaboration with Indian Institute of Technology (IIT)
Bombay
Scilab user group and Web site

Introduction
Application Area of SCILAB
Mathematics
linear algebra
univariate polynomials
differential equations

Data Analysis
Statistics
Optimization

Signal and Systems


Control Systems
Signal Processing
Image Processing
GUI Creation & Application Deployment

Getting Started
Scilab command line is denoted
with the symbol -->
It accepts variable declarations,
expressions, script and function
calls
Scilab scripts and functions are
ASCII files
To re-use a previous command,
type the up arrow

Getting Started
Scilab Shortcut Commands
Keyboa
CTRL
rd
Command

Description

ctrl-p

Recall previous line

ctrl-n

Recall next line

ctrl-b

Move back one character

ctrl-f

Move forward one character

home

ctrl-a

Move to beginning of line

end

ctrl-e

Move to end of line

esc

ctrl-u

Clear line

del

ctrl-d

Delete character at cursor

backspa
ce

ctrl-h

Delete character before cursor

ctrl-k

Delete to end of line

ctrl-c

Interrupt Scilab and pause after carriage

Getting Started
File Menu
Execute
Open a file
Load Environment
Save Environment
Change Current
Directory
Page setup
Print
Quit

Getting Started
Type pwd or getcwd to know the current
directory

Create your Working Directory


In you local drive (C:), create a folder named
Scilab_CS10-2L
C:\Scilab_CS10-2L

Getting Started
Edit Menu

Cut (Ctrl-X)
Copy (Ctrl-C)
Paste (Ctrl-V)
Select All

Getting Started
Preferences Menu
Colors
Console background
Console font

Font
Show / Hide Toolbar
Clear History
--> resethistory
Clear Console
--> clc

Getting Started
Control Menu
Resume - resume
execution
of SCILAB after
a
pause.
Abort end execution of
current
program.
Interrupt - interrupt
execution of
current
program,
triggering a

Getting Started
Applications Menu
Editor (SciNotes
5.3.3) - Opens up a
separate text editor
window where the
user can edit Scilab
code.
(you may also type:
edit)
Xcos - Opens the
xcos application for
simulations.
(you may also type:
xcos)
Matlab to Scilab

Getting Started
Help (?) Menu

Scilab Basic Command


who - displays current variable names.
whos - listing of variables in long form.
who_user - listing of user's variables.
clc - Clear Command Window.
resethistory - Deletes all entries in the Scilab
history.
clear - kills variables.
apropos - searches keywords in Scilab help.
disp - displays variables .
pwd - print Scilab current directory

Practice Exercises
1) Scilab is also capable of processing
trigonometric function like sin, cos, and tan.
Using help find out what are the equivalent
commands for sin-1, cos-1, and tan-1.
2) Use apropos to find out the available function
to convert numbers from base 10 (decimal) to
base 16 (hexadecimal). Test the function with
the following data:
- convert F00D to base 10
- convert 62804 to base 16

Scilab Basic
Variables
A variable can be defined simply with the
syntax
-->name = instruction;
The ; symbol prevents to print on screen the
results
Standard rules common to languages as C or
Pascal apply for Scilab variables
Names are case sensitive: a is not the same as
A
To see the variable value just type its name on
the prompt, or go to Applications menu and
choose variable browser , or type

Scilab Basic
Variable Types
A specific set of operators is available for the
corresponding type
Common types are
(constant, polynomial, function, handle,
string, boolean, list, rational, state -space,
sparse, boolean sparse)
The type of a variable can be obtained using
the command : typeof ( variable_name)

Scilab Basic
Write protected variables
Certain variables are predefined and write-protected
%i
= 1 imaginary unit
%pi = 3.1415927 . . . pi greek
%e = 2.718281 . . . number of Nepero
%eps
= 2.2 1016 precision (machine
dependent)
%inf
infinity
%nan NotANumber
%s
s polynomial variable
%z
z polynomial variable
%t
true boolean variable
%f
false boolean variable

Scilab Basic
Operators & Order of Precedence
Operators
( ) parentheses
comp.
transpose
^ power
/ division
* multiplication
- subtraction
+ addition

Scilab Basic
Functions
Is the main abstraction feature in SCILAB
A collection of commands which are executed in
a new environment thus isolating function
variables from the original environment variables.
Consist of built-in functions or user defined
functions
To display List of Built-in Functions type what

Scilab Basic
Functions (Predefined)
Basic function calling syntax :
functionname(input)
Example:
X = sqrt(25)

Output =

Take note :
The result of a function is assigned to the output
variable.
The input value is always entered in parenthesis
after the function name.
The input can be a value or a variable that
contains a value.
Functions can operate on a vector or matrix.

Scilab Basic
Functions (Predefined)
Elementary functions :sqrt, diag, cos
Rounding : round, fix, ceil, floor
Sorting : sort, sortup, gsort, find
Specific matrices : zeros, eye, ones
Linear algebra : det, inv
Polynomials : poly, roots, coeff
Random numbers : rand
Character strings : string, part, evstr, execstr

Scilab Basic
Matrices
To insert a matrix the syntaxes are:

Scilab Basic
Scalars and vectors
Scalars and vectors are matrices

row
column

Scilab Basic
Incremental vectors

Also available linspace(x0,xf,npti)

Scilab Basic
Accessing matrix elements

A(i,j) access the element in row i and column j


A(2,1:3) select the columns from 1 to 3 of the
second row
B=A(3:5,1:3) assign to B the selected sub
matrix
A(:,2) select column 2
A([1 3],3) select a 1x2 vector of two elements:
A(1,3) and A(3,3)

Scilab Basic
Matrix operations
A wide number of operations are available, in
addition to the basic operations such as sum,
product, transpose, matrix exponential, inverse,
rank, kernel, etc.
A library of operations are available under the
Linear Algebra section of the help
Some operations are also defined to be
performed on the single elements of the matrix

Scilab Basic
Matrix Functions
-->
-->
-->
-->
-->
-->
-->
-->
-->

a = eye(3,3)
b = ones(4,3)
b = zeros(3,4)
c = diag([ 1 2 3 4])
d = rand(3,3)
e = [1:5]
f = [1:3; 4:6; 7:9]
g = [1:0.1:5]
h = linspace(1, 5, 5)

Scilab Basic
Matrix Operation
+ addition
- subtraction
* multiplication
/ division
\ left division
^ power
transpose
.* array multiply
./ array division
.^ array power

Scilab Basic
Polynomial operations
Defining a variable as polynomial allows to access
several specific operations

Scilab Basic
Polynomial operations
Define a polynomial from the coefficient: poly
Define a polynomial from its expression (previous
example)
Define a polynomial by operations on elementary
polynomials
Extract the coefficients of a polynomial: coef

Scilab Basic
Complex Numbers
Scilab handles complex numbers as easily as
real numbers
%i stands for 1

Practice Exercises
1.) Given
C
A
1 2 2
7 3 0
2 7 7
1 2 0

the following values of matrix A, B, and


C
B
1 2 3 0
5
3 6 3 7
0 3 0 20
6
1 2 4 8
6 11 7 1
2
16 32 54 1
1 10 0 30
3
28 5 2 6
R

1 1 1 1 2 0 1
6 6 6 6
2
Generate
the 0matrix
0 3 0
1 1below
1 using
1 4 some
6 2of
matrix operations6 and
6 functions.
6 6
2
0 0 7 0 1 1 1 1 6 0 1

2
the
2
0
0

Practice Exercises
2.) Generate the matrix below using some of the
matrix operations and functions.
M
0
0
1
1
0
0

0
0
1
1
0
0

1
1
1
1
1
1

1
1
1
1
1
1

0
0
1
1
0
0

0
0
1
1
0
0

You might also like