You are on page 1of 32

Introduction To Scilab

Ramesh Vulavala
Professor in Chemical Engineering
D. J. Sanghvi College of Engineering
Mumbai
What is Scilab?
Computational Tool
Programming Language
Special-purpose Tool Boxes
Graphical Tools
GUI Tool Box
Scilab Objects
Matrices
Strings
Polynomials
Structures
Matrices
// Defining three vectors:
A = [1,2,3];
B = [4,5,6];
C = [7,8,9];
// Defining a matrix by putting the row vectors together:
D = [A;B;C];
D =
1. 2. 3.
4. 5. 6.
7. 8. 9.
// Defining another vector:
E = [10,11,12];
Matrices
// Combining a vector and a matrix to define a new matrix:
F = [D;E];
F = 1. 2. 3.
4. 5. 6.
7. 8. 9.
10. 11. 12.
// Defining a new column vector:
G = ones(4,1);
G = 1.
1.
1.
1.
// Combining a matrix and a column vector:
H = [F G];
Matrices
H = 1. 2. 3. 1.
4. 5. 6. 1.
7. 8. 9. 1.
10. 11. 12. 1.
// Reshaping the matrix in new dimensions:
HA = matrix(H,2,8);
HA = 1. 7. 2. 8. 3. 9. 1. 1.
4. 10. 5. 11. 6. 12. 1. 1.

// Reassigning specific elements of a matri :

HA([1,2],[3,5]) = zeros(2,2);

HA = 1. 7. 0. 8. 0. 9. 1. 1.
4. 10. 0. 11. 0. 12. 1. 1.
Operations with Matrices
A = [1 2 3;4 5 6;7 8 9];
A = 1. 2. 3.
4. 5. 6.
7. 8. 9.
B = A + A;
B = 2. 4. 6.
8. 10. 12.
14. 16. 18.
Operations with Matrices
// Multiplication of a matrix by a scalar:
C = 2*A;
C = 2. 4. 6.
8. 10. 12.
14. 16. 18.

D = B - C;
// Multiplication of a matrix by another matrix:
M = A*A;

M = 30. 36. 42.

66. 81. 96.
102. 126. 150.
Operations with Matrices
// Multiplication of a matrix element-wise:

N = A.*A;
N = 1. 4. 9.
16. 25. 36.
49. 64. 81.
// Multiplication of a matrix by each element of another matrix:

P = A.*.A;
Check this matrix by running your program!!!
Application of For Loop
// The following code shows the use of the for loop:
for i = 1:10,
printf('This is iteration No. %d \n',i );
end
This is iteration No. 1
This is iteration No. 2
This is iteration No. 3
This is iteration No. 4
This is iteration No. 5
This is iteration No. 6
This is iteration No. 7
This is iteration No. 8
This is iteration No. 9
This is iteration No. 10
Application of While Loop
// The following code shows the use of the while loop:
n = 1;

while (n<=5),
printf('This is iteration No. %d \n',n );
n = n + 1;
end
This is iteration No. 1
This is iteration No. 2
This is iteration No. 3
This is iteration No. 4
This is iteration No. 5
Functions
// This program shows the use of functions:

function x = my_fn(a,b);
x(1) = a + b;
x(2) = b- a;
endfunction

y = my_fn(2,5);
y = 7.
3.
Functions
function x = my_fn(a,b);
global c;
printf('value of c within the function = %d \n',c);
x(1) = a + b;
x(2) = b- a;
c = 2*c;
endfunction
global c;
a = 5;
b = 10;
c = 15;
printf('value of c before the function call = %d \n',c);
y = my_fn(a,b);
printf('value of c after the function call = %d \n',c);
Functions
value of c before the function call = 15
value of c within the function = 15
value of c after the function call = 30
Plots
// This program shows how to plot two dimensional plots:
x = [0:0.1:2*%pi];
y = sin(x);
plot(x,y);
xgrid;
xlabel('x');
ylabel('Sin x');
xtitle('Plot of Sin x');
Plots
Polynomials
// Defining a polynomial with its roots:
p = poly([0,1],'x');

2
p = -x+x

// Determining its roots:

rt = roots(p);

q = p-1;
r = poly([0.5,1.5],'x');

2
q= -1-x+x

Polynomials
// Defining a polynomial in terms of its co-efficients:

v = poly([1,2],'s','coeff');
v = 1 + 2s
u = poly([1,2],'s','coeff');
w = u*v;

w= 2
1 + 4s + 4s
Polynomials
// This program shows how to define and use polynomials:

tou = input('Type the value of the time-constant: ');

xi = input('Type the value of the damping co-efficient: ');
// Defining the polynomial:
s=poly(0,"s");
p = tou^2 * s^2 + 2*tou*xi*s + 1;
// Defining the transfer-function:
gofs = 1/p;
t = [0:0.05:20];
[rt,ct] = size(t);

// Defining the step-input:

us = ones(1,ct);
Polynomials
// Defining the impulse input:
ui = zeros(1,ct);
ui(1,1) = 1;
// Calculating the Step-response:
ys = csim(us,t,gofs);
// Plotting the Response
clf(0);
figure(0);
plot2d(t,ys);
xgrid;
xlabel('time');
ylabel('output');
xtitle('Step Response');
Polynomials
Polynomials
Solution of Linear Equations
// This program shows how to solve a system of linear equations:
// x + 2y = 5
// 2x + 3y = 8

// Defining the A matrix:

A = [1,2;2,3];
b = [5;8];
// Solving the system:
x = A\b;
Solution of Non-Linear Equations
// The program solves a set of NLE using fsolve:
function z=fct(x)
z(1) = x(1)^2 + 2*x(2) - 10;
z(2) = 3*x(1) - x(2)^3 + 21;

endfunction

x0=[0.2 0.2]; // initial condition

[x,v]=fsolve(x0,fct);
x = 2. 3.
v = 10^(-12) *

0.1101341 0.9556800

Solution of ODE
// This program solves a set of odes:
function dx = fun1(t,x);
dx(1) = x(1) + 0.05*x(2) - 0.01*x(1)*x(2);
dx(2) = 0.5*x(1) + x(2) - 0.01*x(1)^2;
Endfunction
t0 = 0;
tt = 0:0.01:5;
x0 = [0.5;0.5];
x = ode(x0,t0,tt,rtol=1.0e-12,atol=1.0e-12,fun1);
figure(1);
plot(tt,x(1,:));
xgrid;
ylabel('x(1)');
Solution of ODE
Solution of ODE
Chemical Reactions
Chemical Reactions
function dx = chemical(t,x)
f1 = k1*x(1)*x(2)
f2 = k2*x(1)*x(1)*x(3)
dx(1) = -f1 - 2*f2
dx(2) = -f1 + f2
dx(3) = f1 - f2
dx(4) = f1
endfunction
Chemical Reactions
k1 = 1e2
k2 = 1
t = 0:0.001:0.1;
x0 = [1 ;1; 0; 0];
x = ode(x0,0,t,chemical);
// Plotting the outputs:

plot2d(t,x(1,:),style=1)
plot2d(t,x(2,:),style=2)
plot2d(t,x(3,:),style=3)
plot2d(t,x(4,:),style=4)
legends(['A','B','C','D'],[1 2 3 4],"ur")
xtitle('Chemical ODE')
Chemical Reactions
Thank You !!!
Any Questions?