Introduction To Scilab: Kannan M. Moudgalya IIT Bombay Kannan@iitb - Ac.in
Introduction To Scilab: Kannan M. Moudgalya IIT Bombay Kannan@iitb - Ac.in
Kannan M. Moudgalya
IIT Bombay
www.moudgalya.org
[email protected]
Scilab Workshop
Bhaskaracharya Pratishtana
4 July 2009
I Software engineering
I Implication to scientific computations
I Scilab as a possible solution
I Scilab - a tutorial introduction
I Simple arithmetic
I Matrix operations
I Vector arithmetic
I Conditionals
I Plots
I Installation
I Special functions
I Bessel
I Gamma
I Error function
I Elliptic integral
I Polynomials
I Characteristic polynomial
I Roots
I Multiplication
I Division
I Curve fitting
I Matrix condition
I Condition number
I 1,2,F and ∞ norms
I rank
I C like langugae
I Control flow
I if
I while
I select
I break
I Procedures
I Scripts
I Functions
I Other features
I Diary
I Can call C and Fortran programs
4+6+12
ans =
22.
a = 4, b = 6; c = 12
a =
4.
c =
12.
a+b+c
ans =
22.
Kannan Moudgalya Introduction to Scilab 17/52
Useful Commands
I demos
I Gives demos on several different things
I apropos
I Helps locate commands associated with a word
I help
I functional invocation with no arguments
I Helps draw plots
I diary
I Stores all commands and resulting outputs
a = 4; b = 6; c = 12;
d = a+b+c
d =
22.
d = a+b+c;
d =
22.
format(’v’,10)
e = 1/30
e =
0.0333333
format(’v’,20)
e
e =
0.03333333333333333
format(’e’,20)
e
e =
3.3333333333333E-02
Kannan Moudgalya Introduction to Scilab 20/52
Simple Arithmetic
format(’v’,10)
x = sqrt(2)/2, y = asin(x)
x =
0.7071068
y =
0.7853982
y_deg =
45.
Kannan Moudgalya Introduction to Scilab 21/52
Rounding, Truncation, etc.
x =
2.6
y1 =
2.
y2 =
2.
y3 =
3.
y4 =
Kannan Moudgalya Introduction to Scilab 22/52
Different Ways to Specify a List
x = (0:0.1:1)*%pi;
x = linspace(0,%pi,11);
-->x = (0:0.1:1)*%pi;
-->y = sin(x)
y =
column 1 to 6
column 7 to 11
ans =
0.9510565
Kannan Moudgalya Introduction to Scilab 24/52
Vector Operation - 2
a =
! 1. 2. 3. 4. 5. !
b =
! 1. 3. 5. 7. 9. !
-->c = [b a]
c =
! 1. 3. 5. 7. 9. 1. 2. 3. 4. 5. !
-->d = [b(1:2:5) 1 0 1]
d =
! 1. 5. 9. 1. 0. 1. !
Kannan Moudgalya Introduction to Scilab 25/52
Vector Operation - 3
-->a, b
a =
! 1. 2. 3. 4. 5. !
b =
! 1. 3. 5. 7. 9. !
-->a - 2
ans =
! - 1. 0. 1. 2. 3. !
-->2*a-b
ans =
! 1. 1. 1. 1. 1. !
Kannan Moudgalya Introduction to Scilab 26/52
Vector Operation - 5
-->a
a =
! 1. 2. 3. 4. 5. !
-->a.^2
ans =
! 1. 4. 9. 16. 25. !
-->a.^a
ans =
! 1. 4. 27. 256. 3125. !
Kannan Moudgalya Introduction to Scilab 27/52
Vector Operation - 6
-->a, b
a =
! 1. 2. 3. 4. 5. !
b =
! 1. 3. 5. 7. 9. !
-->a./b
ans =
! 1. 0.6666667 0.6 0.5714286 0.5555556 !
-->b.\a
ans =
! 1. 0.6666667 0.6 0.5714286 0.5555556 !
Kannan Moudgalya Introduction to Scilab 28/52
Vector Operation - 7
-->a, b
a =
! 1. 2. 3. 4. 5. !
b =
! 1. 3. 5. 7. 9. !
-->a/b
ans =
0.5757576
-->a, b
a =
! 1. 2. 3. 4. 5. !
b =
! 1. 3. 5. 7. 9. !
-->a\b
ans =
! 0. 0. 0. 0. 0. !
! 0. 0. 0. 0. 0. !
! 0. 0. 0. 0. 0. !
! 0. 0. 0. 0. 0. !
! 0.2 0.6 1. 1.4 1.8 !
Kannan Moudgalya Introduction to Scilab 30/52
Machine Epsilon
-->num=0; EPS=1;
-->while (1+EPS)>1
--> EPS = EPS/2;
--> num = num+1;
-->end
-->num
num =
53.
-->EPS=2*EPS
EPS =
2.220E-16
Kannan Moudgalya Introduction to Scilab 31/52
Logical Operators
== equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
<> or ∼= not equal to
-->x = (-2:2)/3
x =
! - 0.6666667 - 0.3333333 0. 0.3333333 0.6666667 !
-->sin(x)./x
!--error 27
division by zero...
-->x = x+(x==0)*%eps
x =
! -0.6666667 -0.3333333 2.22E-16 0.3333333 0.6666667 !
-->sin(x)./x
ans =
! 0.9275547 0.9815841 1. 0.9815841 0.9275547 !
A =
! 1. 2. 3. 4. 5. 6. 7. 8. 9. !
B =
! 8. 7. 6. 5. 4. 3. 2. 1. 0. !
-->tf = A==B
tf =
! F F F F F F F F F !
-->tf = A>B
tf =
! F F F F T T T T T !
Kannan Moudgalya Introduction to Scilab 35/52
Transpose
-->c = [1;2;3]
c =
! 1. !
! 2. !
! 3. !
-->a=1:3
a =
! 1. 2. 3. !
-->b = a’
b =
! 1. !
! 2. !
! 3. !
Kannan Moudgalya Introduction to Scilab 36/52
Submatrix
A =
! 1. 2. 3. !
! 4. 5. 6. !
! 7. 8. 9. !
-->A(3,3)=0
A =
! 1. 2. 3. !
! 4. 5. 6. !
! 7. 8. 0. !
Kannan Moudgalya Introduction to Scilab 37/52
Submatrix
A =
! 1. 2. 3. !
! 4. 5. 6. !
! 7. 8. 0. !
-->B=A(3:-1:1,1:3)
B =
! 7. 8. 0. !
! 4. 5. 6. !
! 1. 2. 3. !
-->A
A =
! 1. 2. 3. !
! 1. 4. 7. !
! 7. 8. 0. !
-->B=A(:,2)
B =
! 2. !
! 4. !
! 8. !
b =
! 5. - 3. !
! 2. - 4. !
-->x=abs(b)>2
x =
! T T !
! F T !
-->y=b(abs(b)>2)
y
=
! 5. !
! - 3. !
! - 4. !
Kannan Moudgalya Introduction to Scilab 40/52
Special Matrices
-->zeros(3,3)
ans =
! 0. 0. 0. !
! 0. 0. 0. !
! 0. 0. 0. !
-->ones(2,4)
ans =
! 1. 1. 1. 1. !
! 1. 1. 1. 1. !
-->rand(2,1)
ans =
! 0.2113249 !
! 0.7560439 !
Kannan Moudgalya Introduction to Scilab 41/52
Go for Vector Computation
-->a = ones(10000,1);
-->timer()
ans =
0.02
-->for i = 1:10000, b(i)=a(i)+a(i); end
-->timer()
ans =
0.31
-->c = a+a;
-->timer()
ans =
0.03
Kannan Moudgalya Introduction to Scilab 43/52
Plots
I Source version
I Scilab requires approximately 130 MB of disk storage to
unpack and install (all sources included).
I Also, X Window (X11R4, X11R5 or X11R6), C and Fortran
compilers are needed.
I Binary version
I The minimum requirement for running Scilab (without
sources) is about 40 MB when decompressed.
I Being partially and statically linked, these versions do not
require a Fortran compiler.
I Windows
I Download scilab-4.1.exe
I Click this file and follow the instructions
I Launch from its icon on the Desktop.
I Linux
I Download scilab-4.1.bin.linux-i686.tar.gz
I Issue the following commands
I tar zxvf scilab-4.1.bin.linux-i686.tar.gz
I cd scilab-4.1
I make
I The binary is at bin/scilab