Académique Documents
Professionnel Documents
Culture Documents
Matlab Code:
Scilab Code:
While syntax is slightly different between MATLAB and Scilab, function plots
that are essentially the same can be produced
Significant difference:
o MATLAB will clear the plot if another plot command is given
o multiple functions on the same axis must be done in the same statement or
the hold plot function must be used
o allows easy, smooth animation of plots: simply use another plot statement
to create the next frame
o Scilab plots do not clear unless a clear command is given
o multiple functions on the same axis are easy
o smooth animation is harder and has so far remained elusive
Matlab Plot
Scilab Plot
Graphing: Histograms
Note: differences in the profile of the two histograms results from the fact that they
are both generated from a random process and any two histograms from
the same program would look a bit different
o code producing these plots is the code shown in an earlier slide
Differences in appearance that dont affect functionality
o color and tick marks
Functions in each program are significantly different
o MATLAB hist function puts the data first in the statement, followed by the
number of bins
o Scilab histplot function puts the number of bins first, followed by the data
o Scilab also defaults to a normalized histogram
o If a non-normalized histogram is desired, an argument must be provided in
the histplot function to specify this.
Matlab Histogram
Scilab Histogram
Major Differences
Functions
Functions in Scilab are NOT Matlab m-files but variables. One or several functions can
be defined in a single file (say myfile.sci). The name of of the file is not necessarily related
to the the name of the functions. The name of the function(s) is given by
function [y]=fct1(x)
...
function [y]=fct2(x)
...
The function(s) are not automatically loaded into Scilab. Usually you have to execute
the command getf("myfile.sci") before using it.
Functions can also be defined on-line (or inside functions) by the command deff.
To execute a script file you must use exec("filename") in Scilab and in Matlab you just
need to type the name of the file.
Comment lines
Scilab comments begins with: //
Matlab comments begins with: %
Variables
Predefined variables usually have the % prefix in Scilab (%i, %inf, ...). They are write
protected.
Strings
Strings are considered as 1 by 1 matrices of strings in Scilab. Each entry of a string matrix
has its own length.
Boolean variables
Boolean variables are %T, %F in Scilab and 0, 1 in Matlab. Indexing with boolean variables
may not produce same result.
Example:
x=[1,2];x([1,1])
[1,1]
in Scilab and
Matlab.
Also if x is a matrix x(1:n,1)=[] or x(:)=[] is not valid in Matlab.
Department of Computer Science and Engineering, SRM University, Kattankulathur
[1,2]
in
Polynomials
Polynomials and polynomial matrices are defined by the function poly in Scilab (builtin variables). They are considered as vectors of coefficients in Matlab.
Empty matrices
[ ]+1 returns 1 in Scilab and [ ] in Matlab.
Plotting
Except for the simple plot and mesh (plot3d) function plots, Scilab and Matlab are not
compatible.
Scicos
Scicos (Scilab) and Simulink (Matlab) are not compatible.
Built-in-functions
Most built in functions are identical in Matlab and Scilab. Some of them have a slightly
different syntax. Here is a brief, partial list of commands with significant different syntax.
Matlab
all
any
balance
cputime
echo
eig
eval
exist
fopen
fwrite
fclose
feof
ferror
feval
filter
finite
fread
Scilab
and
or
balanc
timer
mode
Spec or bdiag
evstr
exists + type
mopen
mwrite
mclose
fseek
ftell
global
home
isglobal
isinf(a)
isnan(a)
isstr(a)
lookfor
pause
randn
setstr
srcmp(a,b)
which
nargin
nargout
uimenu
file
a == %inf
a ~= a
type(a) == 10
apropos
halt
rand
code2str
a == b
whereis
[nargout, nargin]=argn(0)
getvalue
Scilab Exercises
Basic Commands
help
Purpose = to get the list of help topics
clock
Purpose = to show date in vector form
ans =
2014. 2. 23. 10. 22. 20.000001
date
Purpose = to show date in string form
Ans=
29-Oct-2014
ver
Purpose = to show current version of Scilab
Ans =
Scilab Version:
5.5.1.1412169962
pwd
Purpose = to show the present working directory
Ans =
C:\Users\dell\Documents
cd
Purpose = to change the directory
dir
Purpose = to display the name of all files and folder in the present directory
Ans =
AppData\
Application Data\
Contacts\
Cookies\
Desktop\
Documents\
Downloads\
ls
Purpose = to display the name of all files and folder in the current directory
Ans =
!My Videos
!
!
!
!My Pictures
!
!
!
Department of Computer Science and Engineering, SRM University, Kattankulathur
!
!
mkdir
Purpose = to create a new directory
who
Purpose = to list all the variables in workspace
Ans =
Your variables are:
Ans
whos
Purpose = to display the information of all the variables
Ans =
Name
Size
Bytes
Class
Ans
1X58
116
char
what
Purpose = Lists low level primitives and commands.
Ans =
Internal Functions:
!!_invoke_
%H5Object_e %H5Object_fieldnames
Commands:
abort
apropos
break
case
Attributes
%H5Object_p
clear
Purpose = to clear all variables
clc
Purpose = to clear all the commands in the workspace
clf
Purpose = to clear the figure window
quit
Purpose = to exit the scilab
exit
Purpose = to exit the scilab
Solution:
x3+5x2-2.67x-52
-->x=13.5
-->x^3+5*(x^2)-2.67*x-52
Output = 3283.58
((14x3)1/2)/e3x
-->x=13.5
-->(sqrt(14*(x^3)))/exp(3*x)
Output = 4.782D-16
Note: The above output contains D. To Remove that we change the output format.
--> format(25)
-->(sqrt(14*(x^3)))/exp(3*x)
Output = 0.0000000000000004782315
log|x2-y3|
-->x=13.5
-->log(abs((x^2)-(x^3)))
Output = 20.3444
Prove LHS=RHS
when x=/5
Cos2(x/2) = tanx+sinx/2tanx
LHS
-->cos2(x/2)
(cos(x/2))^2
Output = 0.9045
RHS
-->tanx+sinx/2tanx
(tan(x)+sin(x))/(2*tan(x))
Output = 0.9045
Thus, LHS=RHS
Polynomial evaluation
Q1) f(x) = x5-12.1 x4+40.59 x3-17.015 x2-71.95 x+35.88, Calculate f(9) and roots of this equation.
Q2) Perform the polynomial multiplication and addition for the given two polynomial
f1(x) = 3 x6+15 x4-10 x3-3 x2+15 x-40
f2(x) = 3 x3-2 x-6
Q3) Divide the polynomial 4 x4+6 x3-2 x2-5 x-3 by x2+4x+2
Basic Operations:
--> p = poly([2, 3], x)
Or
Note: r indicates that the first argument array are the roots of the second argument. If the third argument
is not present, it defaults to 'r', as in the first statement.
=> p = 6 - 5x + x2
--> p1 = poly([6, -5, 1], x, c)
Note: c indicates that the first argument array are the coefficients of the second argument.
=> p1 = 6 -5x + x2
1.
2.
3.
4.
disp ("Odd");
end;
Output:
Check whether the input number is odd or even
Enter a number: 56
Even
Enter a number: 3
Odd
Ans 3)
Code:
disp("Calculating BMI")
w = input ("Enter Weight: ")
h = input ("Enter Height: ")
r=w/(h^2);
mprintf("The BMI is: %d", r)
if(r<18.5)
disp ("Underweight")
elseif(r > 18.5 & r < 24.9)
disp ("Normal")
elseif(r > 25 & r < 29.5)
disp("Overweight")
else
disp("Obese")
end
Output:
Calculating BMI
Enter weight: 50
Enter height: 1.5
The BMI is:
22.222
Normal
Calculating BMI
Enter weight: 75
Enter height: 1.1
The BMI is:
61.9835
Obese
sum =0
n=input("Enter the number of numbers: ")
for i = 1:1:n
sum = sum+i
end
mprintf("The sum of first %d natural numbers is %d",n,sum)
Output:
Enter the number of numbers: 10
The sum of first 10 natural numbers is 55
Ans 2)
Code:
sum =0
n=input("Enter the number of numbers: ")
i=1
while i<n
sum=sum+i
i=i+2
end
mprintf('The sum of odd natural numbers upto %d is: %d',n,sum)
Output:
Enter the number of numbers: 10
The sum of odd natural numbers upto 10 is: 25
Ans 3)
Code:
a = 'HELLO';
b = 'hai';
mprintf('1 for length \n 2 to compare \n 3 to apply lower case \n 4 to apply upper case');
ch = input ('Enter any number : ');
select ch
case 1
disp (length(a));
disp (length(b));
Department of Computer Science and Engineering, SRM University, Kattankulathur
case 2
disp (strcmp(a,b));
case 3
disp (convstr(a,'l'));
disp (convstr(b,'l'));
case 4
disp (convstr(a,'u'));
disp (convstr(b,'u'));
else
disp ('Wrong Choice');
end;
Output:
1 for length
2 to compare
3 to apply lower case
4 to apply upper case
Enter any number: 1
5.
3.
1 for length
2 to compare
3 to apply lower case
4 to apply upper case
Enter any number: 3
hello
hai
1 for length
2 to compare
3 to apply lower case
4 to apply upper case
Enter any number: 4
HELLO
HAI
1 for length
2 to compare
3 to apply lower case
4 to apply upper case
Enter any number: 2
- 1.
1 for length
2 to compare
3 to apply lower case
4 to apply upper case
Enter any number: 3
hello
hai
1 for length
2 to compare
3 to apply lower case
4 to apply upper case
Enter any number: 7
Wrong Choice
File Operations
Q1) Open a text file in Scilab.
Q2) Writing information WELCOME into a file and read that file.
Q3) Copy and close the file.
Solutions:
Ans 1)
Code:
--> fid = mopen('test.txt', 'w')
fid =
1.
Ans 2)
Code:
fid = mopen(test.txt, w);
mfprintf(fid,hello %s %d.\n,world,1);
mclose(fid);
f1 = mopen(test.txt,r);
mfscanf (f1, %s %s)
Output:
ans = !hello world !
Ans 3)
Code:
copyfile test.txt test2.txt
fid = fopen (test2.txt, r);
mfscanf (fid, %s %s)
Output:
ans = !hello world !
11
10
13 9
B=
4
12
2) A =
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
i.
Form a matrix B from the first, third and fourth rows and the first, third, fourth and seventh column
of the matrix A.
ii.
Create a matrix C with 17 elements long row vector from matrix A from the elements of the third
row, the fifth & seventh column.
Solutions:
Ans 1)
Code:
-->A = [ 3 11 6 ; 4 7 10 ; 13 9 0 ]
-->B = [ 4 6 8 ; 3 5 7 ; 6 9 12 ]
-->C = A+B
-->D = A-B
-->E = A*B
Department of Computer Science and Engineering, SRM University, Kattankulathur
1 2 3 4 5 6 7
Department of Computer Science and Engineering, SRM University, Kattankulathur
21
5
18 15 12 9 6 3
10 15 20 25 30 35
c=
21 18 15 12 9 6 3
5 10 15 20 25 30 35
d=
1
21
5
e=
3 4
15 12
15 20
f=7
3
35
B=
1 3 4 7
21 15 12 3
5 15 20 25
g = 21 18 15 12 9 6 3
h= 5
10
9
25
I=7
14
3
35
h = 5 10 9 25
I = 7 14 3 35
U = 21 18 15 12 9 6 3 5 10 9 25 7 143 35
Rule of matrix
From the matrix A, B and C
A=
B=
5 2 4
1 7 -3
6 -10 0
11 5 -3
0 -12 4
2 6 1
C=
7 14 1
10 3 -2
8 -5 9
Q1) Calculate A+B and B+A, to show that addition of matrix is commutative.
Q2) Calculate A(A+B) and (A+B)+C, to show that addition of matrix is associative.
Q3) Calculate A*(B+C) and A*B+A*C, to show that multiplication of matrix is multiplication.
Solutions:
A = [ 5 2 4 ; 1 7 -3 ; 6 -10 0]
B = [ 11 5 -3 ; 0 -12 4 ; 2 6 1]
C = [ 7 14 1 ; 10 3 -2 ; 8 -5 9]
Ans 1)
Code:
Ans 2)
Code:
Ans 3)
Code:
X=A+B
Y=B+A
X=A+(B+C)
X=A+(B+C)
X=A*(B+C)
Y=A*B+A*C
Output:
X=
Y=
Hence, A+B = B+A
Output:
X=
Y=
Hence, A(A+B) = (A+B)+C
Output:
X=
Y=
Hence, A*(B+C) = A*B+A*C
2D plots
Year
2008
Placement 65
2009
2010
2011
2012
72
74
76
84
Q1) Plot the graph for the table with dashed dotted red lines, marker type diamond, marker edge color
green, marker size = 12 and label the graph along with grid and box. Also plot the bar chart and pie diagram
for the data.
Solutions:
Ans 1)
Code:
-->year = [ 2008,2009,2010,2011,2012 ]
-->placement = [ 65,72,74,76,84 ]
-->plot (year,placement,'rd-','markeredgecolor','g','markersize',12);
-->title('2D PLOTS and PRINTING LABELS');