Sei sulla pagina 1di 5

DA-IICT

B.Tech. (Information & Communication Technology)


II Semester – Core Course
BASIC TELECOMMUNICATION (BTC) – CT111

EXPERIMENT NO. 1

INTRODUCTION TO SCILAB

Introduction:

Today there is a great need for the user-friendly programming environment that will facilitate the
task of numerical computations and data analysis. Some of the programming environments that
provide these requirements are Matlab, Scilab, COSSAP, Netlib, StatLib, Rlab, Octave. Scilab is
a powerful and user-friendly software package for engineering and scientific applications. It is
freely available from Internet since 1994 and can be downloaded from
http://www.rocq.inria.fr/scilab.

Its main features are:


§ A high-level programming language
§ An interpreter
§ A data environment
§ Transparent data types
§ A large collection of built-in primitive functions
§ A suite of Scilab coded function toolboxes
§ Integrated graphics

Scilab is fully endowed with a powerful programming language whose syntax is easy to use. It is
useful for performing numerical computation, especially those of simulation, optimization, signal
processing and control. The associated numerical facilities in conjunction with the open
computing environment, object oriented programming tools, the scilab interpreter, and the
integrated graphics make for a powerful scientific computing tool.

Getting Started with Scilab:

Open a terminal (shell), for windows its easy and we all are familiar with the steps. Now invoke
scilab by typing ‘scilab’ on the shell prompt. You will immediately get the Scilab window with
the banner and prompt as given below

==========
scilab-2.7
Copyright (C) 1989-2003 INRIA/ENPC
==========
Startup execution:
loading initial environment
Objective:

This objective of this write-up is to introduce SCILAB and to facilitate the user to write the
program in scilab’s programming language under scilab environment.

Facilities required:

1. IBM compatible PC with Linux6.2 or above (X-window must be running).


2. Scilab 2.4 or above.

Theory:

The various functions that have to be used for programming are listed below. For more
information on these functions, the user has to type help <function name> on the Scilab window
that will display the details about the function.

Declaring variables:

1. a = 10;
2. xyz = [1,2,3,4,5,6]; // row matrix
3. abc = [1;3;5;6;7;8;9;0] // column matrix
4. b = [1.025, 3.2, 6.0; 2.25, 4.78,7.992;8.452,5,9;0.02,1.0325,0.002]; // 3x4 matrix
5. a = [1, 2, 3, 4, 5]; b = [6, 7];
6. q = a(:); // convert any matrix to column matrix
7. c = [a , b]; d = [a ; a]; e = [a(:);a(:)]; // concatenation of matrices row wise &
column wise. Notice the dimensions for proper operations.
8. who // displays current variables.
9. clear <variable-name> // clears the variable name and releases the memory
10. %pi; %e; // value of ‘pi’ and ‘e’.
11. %i; // multiplying will result in imaginary number
12. a = 1.23 + 132*%i; b = 0.2569*%i; c = a + b; d = a.*b;

Matrix operations:

1. p = b’; //transpose operation


2. a = [1,2,3,4,5]; b = [5,4,3,2,1]; c = a + b; d = a – b; // addition & subtraction
3. c = a.*b; d = a./b; e = a.^b; // element wise operations (. ^ = power)
4. a = [0.844,0.603,0.95;0.064,0.519,0.76;0.257,0.908,0.769]; b =
[0.25,0.36,1.02;0.54,0.36,9.12;5.23,1.12,0.369];
5. c = inv(a); // finds matrix inverse Note: make sure ‘a’ is invertible
6. c = a*b; d = b/a; e = b*inv(a); //matrix multiplication & division
Some useful scilab functions:

1. a = ones(x , y); // creates matrix of dimension x*y, all elements = 1;


2. b = zeros(x , y); // creates matrix of dimension x*y, all elements = 0;
3. c = eye(x , x); // creates eigen matrix of size x*x;
4. d = [-10:0.1:10]; // creates matrix of one row with values starting from –10 to +10
with 0.1 step size
5. rand ("normal"); // sets Gaussian distribution;
6. rand ("uniform"); // sets Uniform distribution of random values
7. a = rand(x , y); //creates x*y matrix of random numbers between 0 & 1;
8. max(a); min(a); mean(a); sum(a); st_deviation(a);
9. floor(a); ceil(a); //floor - nearest rounded down integers; ceil -rounded up
elements
10. sin(d); cos(d); tan(d); sin(%pi/5.*[0:30]);
11. z = [1+3*%i, 0.23*%i;2.36,0];
12. real(z); imag(z); // computes the real and imaginary part of complex matrix z
13. squarewave(t,b) //where t is a set of numbers e.g. t=0:0.1:10, b is the duty cycle
in %
14. convol(h,x) //convolves h and x, where h and x are two matrices
15. [hz]=iir(n,ftype,fdesign,frq,delta) // iir digital filter
16. ldiv(n,d,k) // polynomial matrix long division
17. sqrt(x) // square root of x
18. log(x) // natural logarithm
19. log10(x) // logarithm of x with base 10
20. histplot(npoint,data,[style,strf,leg,rect,nax]) // plot a histogram
21. exp(x) // element-wise exponential
22. xtitle // adds title on a graphic window
23. xsetech // set the subwindow of a graphics window for plotting
24. modulo(n,m) // arithmetic remainder
25. length(m) // length of object m
26. rtitr // discrete time response

Some plotting functions in scilab:

1. xset("window",window_no) // creates a graphics window with the window number


specified
2. xbasc(window_no) //clears a graphics window and erase the associated recorded
graphics
3. plot(sin([0:0.1:20]));
4. x = -2*%pi:0.1:2*%pi; plot(x, cos(x));
5. plot2d([ x; x ; x],[sin(x); cos(x); (sin(x) + cos(x))]);
6. plot2d([ x; x ; x],[sin(x); cos(x); (sin(x) + cos(x))], [1,3,5], "151",
"sin@cos@sin+cos",[-4*%pi,-2,4*%pi,2]);
7. plot2d3("gnn",[x; x ; x ], [sin(x); cos(x); (sin(x) + cos(x))], [1,3,5], "151",
"sin@cos@sin+cos", [-%pi,-2,%pi,2]);
Writing and executing programs:

We can write programs for Scilab and execute under Scilab environment. Open an
editor like vi,emacs, pico etc. Write your program in a file and save the file with an
extension *.sci. Execute the program by the command exec <file name> OR select the
File from the menu then file operations, select the file and press "exec" button.

Some example programs in Scilab:

Example – 1:

This program is to plot multiple graphs in multiple windows.


// Start of the program
x = -10:0.1:10;
xset("window",1); // sets/create window for drawing
xbasc(1); // clears contents of window
plot([x]’,[sin(x)]’);
xset("window",2);
xbasc(2);
plot2d([ x; x ], [sin(x); cos(x)], [1,3], "151", "sin@cos", [-4*%pi,-2,4*%pi,2]);
xset("window",3);
xbasc(3);
plot2d3("gnn",[ x; x ], [sin(x); 3.*cos(x) ], [1,3], "151", "sin@cos", [-%pi,-2,%pi,2]);
clear x; // clear the variables declared
// End of the program

Example - 2:

This program plots multiple frames in same window


// Start of the program
x = -10:0.1:10;
xset("window",1); // sets/create window for drawing
xbasc(1); // clears contents of window
plotframe([0,0,1,1],[0,0,0,0],[%f, %f ],['example2','x','y'],[0,0,1,0.5]); // creates sub frame
plot2d(x, sin(x));
plotframe([0,0,1,1],[0,0,0,0],[%f, %f ],['example2','x','y'],[0,0.5,1,0.5]);
plot2d3("gnn", [x; x], [sin(x); cos(x)] , [1,4], ’151’,"sin@exp",[-10,-2,10,2]);
clear x;
// End of the program

Example – 3:

This program plots the distribution (histogram) of a variable


// Start of the program
rand("normal");
x = rand(1,5000);
average_x = mean(x)
std_deviation_x = st_deviation(x)
median_x = median(x)
histplot([-10:0.1:10],x);
clear x;
// End of the program

References:

§ Gomez Claude et al; Engineering and Scientific Computing with Scilab, Birkhauser Boston,
1999.
§ http://www-rocq.inria.com/
§ http://www.mathworks.com/
§ http://space.mit.edu/