Intro To Scilab
Intro To Scilab
Developed at INRIA, SCILAB has been developed for system control and signal
processing applications. It is freely distributed in source code format.
High level scientific computing environments such as Matlab, RLab, Octave and Scilab
are an enjoyable way to solve problems numerically.
It is particularly easy to generate some results, draw graphs to look at the interesting
features, explore the problem further and manipulate matrices.
• An interpreter
• Libraries of functions
• Libraries of Fortran and C routines
- scilab prompt
Up arrow key – to display
// - comment
%pi = 3.1415 //constant pi
clc = clear the screen
clear = clear memory
clear varname = clear specific variable
xbasc() = erase the previous plot
exp(sin(%pi/2)) //The usual functions are provided log, log10, cos, tan, asin,…
= 2.7182818
Calling sequence:
exp(X)
Parameters
• X : scalar, vector or matrix with real or complex entries.
Description
exp(X) is the (element-wise) exponential of the entries of X.
Note: Scilab does all calculations correct only to about 16 significant decimal digits.
This is enough accuracy for most purposes. Usually only 5 significant digits are
displayed on the screen.
Ex.
%pi
= 3.1415927
11*(15/11)-15 //This shows there is round off error when scilab uses fractions.
= 1.776D-15
Variable – Scilab also uses variables to store intermediate answers. A variable can have
almost any name, but is must begin with a letter.
- Scilab is a case-sensitive language.
Ex.
X = 2+3
Y = 4+5
result = x/y
Ex.
p = 2+3;
q= 3+5;
ratio = p/q
= 0.5555556
>> A number of commands can be placed on the one line. Use comma (,) or a semicolon (;)
Ex.
p1 = 2+3 ; q1 = x+4, ratio1 = p1/q1
q1 =
9
ratio1 =
0.5555556
abs(x)
= 3.6055513
real(x)
=2
imag(x)
=3
sin(x)
= 9.1544991 – 4.168907i
exp(%pi * %i ) + 1
= 1.225D-16i
- This section shows how to produce simple plots of lines and data.
Ex.
Xk .5 .7 .9 1.3 1.7 1.8
Yk .1 .2 .75 1.5 2.1 2.4
Although SciLab is a useful calculator, its main power is that it gives a simple way of
working with matrices and vectors.
a = eye(3,3)
b = ones(4,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(-10, 10, 20)
linspace
syntax: linspace(x1, x2,n]
where:
x1, x2 : real or complex scalars
n : integer (no of values) default = 100
a(1,1)
=1
a([2 3], : )
= 4 5 6
7 8 9
MATRIX MANIPULATION
+ addition
- subtraction
* multiplication
/ division
\ left division
^ power
‘ transpose
.* array multiply
./ array division
.^ array power
A= D=A –B
1 2 3 D=
4 5 6 0 -1 2
7 8 9 3 -2 2
4 3 2
B=
1 3 1 E = B-A
1 7 4 E=
3 5 7 0 1 -2
-3 2 -2
C=A+B -4 -3 -2
C=
2 5 4
5 12 10
10 13 16
F = 3*A G = 2*B
F = G =
3 6 9 2 6 2
12 15 18 2 14 8
21 24 27 6 10 14
H = A’ I = B’
H = I=
1 4 7 1 1 3
2 5 8 3 7 5
3 6 9 1 4 7
A= [1 2 3; 4 5 6]; A = [2 4 6 8; 2 3 1 4]
B = [2 4 6 8; 1 2 3 4; 1 3 5 7] B = [1 2 3 4; 2 3 1 4]
C = A*B C = A.*B
C= C=
7 17 27 37 2 8 18 32
19 44 69 94 4 9 1 16
C = B*A C = A*B
ERROR! Inconsistent multiplication ERROR! Inconsistent multiplication
C = A.*B
ERROR! Inconsistent multiplication
A = [2 4 6 8; 2 4 6 8; 1:4; 5:8]
B = [1 5 1 4; 1 8 3 9; 1 7 9 2; 1 0 5 9]
C = A/B
C=
1.6354 -0.7500 0.2604 0.8542
1.6354 -0.7500 0.2604 0.8542
0.8177 -0.3750 0.1302 0.4271
Inverse of Matrix
C = A*inv(B)
C=
1.6354 -0.7500 0.2604 0.8542
1.6354 -0.7500 0.2604 0.8542
0.8177 -0.3750 0.1302 0.4271
6.8385 -3.8750 0.4010 1.6354
Left Division
Given:
-x1+ x2 + 2x3 = 2
3x1- x2+ x3 = 6
-x1 + 3x2 + 4x3 = 4
A = [-1 1 2; 3 -1 1; -1 3 4]
B = [2; 6;4]’
X = inv(A)*B
X=
1
-1
2
A\B
X=
1
-1
2