+ All Categories
Home > Documents > Scilab Documentation (2)

Scilab Documentation (2)

Date post: 28-Sep-2015
Category:
Upload: psingh9
View: 22 times
Download: 3 times
Share this document with a friend
Description:
scilab
Popular Tags:
23
Department of Computer Science and Engineering, SRM University, Kattankulathur Department of Computer Science and Engineering, SRM University, Kattankulathur Introduction To Scilab Programming What are the main differences between Scilab and MATLAB? Program Structure, Functions, and Syntax Very closely paralleled Most structure and functions identical o xlabel and ylabel functions in this example are identical some functions with same name but different results o the axis function shown in the MATLAB code will produce in Scilab, if proper arguments are provided, draw an axis in the output window independent of the rest of the figure some functions with identical results but different name or syntax o note the difference in comment lines o hist and histplot are notable in the examples shown Matlab Code: Scilab Code: Layout and Workspaces Very functionally similar MATLAB has a layout more like the most recent versions of MS Office (icons with explanatory text beneath) while Scilab has the icons alone with explanatory text available by hovering the mouse. Same sub-windows o file browser o command window o variable workspace o command history a Both can dock and undock all sub-windows in the main window
Transcript
  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    Introduction To Scilab Programming

    What are the main differences between Scilab and MATLAB?

    Program Structure, Functions, and Syntax

    Very closely paralleled Most structure and functions identical

    o xlabel and ylabel functions in this example are identical some functions with same name but different results

    o the axis function shown in the MATLAB code will produce in Scilab, if proper arguments are provided, draw an axis in the output window independent of the rest of the figure

    some functions with identical results but different name or syntax o note the difference in comment lines o hist and histplot are notable in the examples shown

    Matlab Code:

    Scilab Code:

    Layout and Workspaces

    Very functionally similar MATLAB has a layout more like the most recent versions of MS Office (icons

    with explanatory text beneath) while Scilab has the icons alone with explanatory text available by hovering the mouse.

    Same sub-windows o file browser o command window

    o variable workspace o command history

    a Both can dock and undock all sub-windows in the main window

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    Editor Window Toolbars

    Differences and similarities parallel from the main workspaces o icons with text as opposed to icons with hover text

    Toolbar design different in style, but provides similar functionality o MATLAB has functionality available through different tabs in the toolbar o Scilab has more of the functionality available through drop-down menus

    Graphing: function plots

    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

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    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

    Cost and accessibility: o MATLAB is paid software with proprietary code o Scilab is free and open source

    more accessible may be less support, especially for add-on modules

    Plot clearing: o MATLAB clears one plot automatically when another one is plotted o Scilab leaves all plots on the same axis unless cleared

    GUIs o MATLAB comes standard with a GUI builder, GUIDE, built in o Scilab comes with a GUI, Guide

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    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]) [which is NOT x([%T,%T])] returns [1,1] in Scilab and [1,2] in 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

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    Polynomials

    Polynomials and polynomial matrices are defined by the function poly in Scilab (built-in 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 Scilab all and any or balance balanc cputime timer echo mode eig Spec or bdiag eval evstr exist exists + type fopen mopen fwrite mwrite fclose mclose feof ferror feval evstr and strcat filter rtitr finite x < %inf fread read

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    fseek file ftell global home isglobal isinf(a) a == %inf isnan(a) a ~= a isstr(a) type(a) == 10 lookfor apropos pause halt randn rand setstr code2str srcmp(a,b) a == b which whereis nargin [nargout, nargin]=argn(0) nargout uimenu getvalue

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    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

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    !My Music ! ! !

    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 Attributes Ans 1X58 116 char

    what Purpose = Lists low level primitives and commands. Ans = Internal Functions: !!_invoke_ %H5Object_e %H5Object_fieldnames %H5Object_p Commands: abort apropos break case

    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

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    Evaluating the Expressions

    1. x3+5x2-2.67x-52 , when x=13.5

    2. ((14x3)1/2)/e3x , when x=13.5

    3. log|x2-x3| , when x=13.5

    4. [37 log(76)/(73+546)] + (910)1/3

    5. Prove LHS=RHS when x=/5

    a. Cos2(x/2) = tanx+sinx/2tanx

    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)))

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    Output = 7.7311080151970230645020

    -->[(37 log(76))/(73+546)] + (910)1/3

    (((3^7)*log(76))/((7^3)+546))+(nthroot(910,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

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    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 --> p = poly([2, 3], x, r)

    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. coeff(p) returns a row vector containing the coefficients of the polynomial.

    2. roots(p) returns a column vector containing the roots of the polynomial.

    3. varn(p) returns the variable of a polynomial.

    4. companion(p) returns the companion matrix (a matrix whose characteristic equation is the given

    polynomial) of the polynomial

    5. derivat(p) returns a polynomial which is the derivative of p

    6. typeof(p) returns the type of the variable. In this case, it will return the type as polynomial

    Function horner() evaluates the polynomial for the given value/values of the independent variable. If the

    input is a single number, polynomial is evaluated for that value of the independent variable. If the input is

    a vector, polynomial is evaluated at each value in the vector, and returns a vector as the result.

    -->horner(p, 2)

    ans =

    0.

    -->horner(p, [2, 3])

    ans = `

    0. 0.

    -->horner(p, 4)

    ans =

    2.

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    Solutions:

    Ans Q1)

    Code:

    -->P = poly([35.88 -71.95 -17.015 40.59 -12.1 1], 'x', 'c')

    P = 35.88 - 71.95x - 17.015x2 + 40.59x3 - 12.1x4 + x5

    -->horner(P,9)

    ans = 7261.125

    -->roots(P)

    ans =

    6.5

    4.

    2.3

    - 1.2

    0.5

    Ans Q2)

    Code:

    -->P = [-40 15 -3 -10 15 3]

    -->Q = [-6 -2 0 3]

    -->P+Q

    -->P*Q

    Output:

    -->P = poly([-40 15 -3 -10 15 3],'x','c')

    P = - 40 + 15x - 3x2 - 10x3 + 15x4 + 3x5

    -->Q = poly([-6 -2 0 3],'x','c')

    Q = - 6 - 2x + 3x3

    -->P + Q

    ans = - 46 + 13x - 3x2 - 7x3 + 15x4 + 3x5

    -->P * Q

    ans = 240 - 10x - 12x2 - 54x3 - 25x4 - 57x5- 36x6 + 45x7 + 9x8

    Ans Q3)

    Code:

    P = poly([-3 -5 -2 6 4],x,c)

    Q = poly([2 4 1], x, c]

    Output:

    P = 4 6 -2 -5 3

    Q = 1 4 2

    D = 4 -10 30

    R= 0 0 0 -105 -57

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    Programming using control structures

    Q1) Display the grade of a student using if-else statement

    a >80 - Distinction

    a < 80 and a > 60 - First Class

    a < 60 and a > 45 - Second Class

    a < 45 - Fail

    Q2) Find if the given number is odd or even using if-else structure.

    Q3) To find the body mass index (BMI) by using height and weight.

    BMI = weight/(height)2

    Weight (KG)

    Height (Meters)

    BMI 18.5 and 25 and 30 = obese

    Solution:

    Ans 1) Code:

    disp("Display the grade of the student "); a = input("Enter Marks: "); if a >=80 disp("Distinction"); elseif(a>=60 & a=45 & a

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    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 & 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

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    For, While and Switch statements

    Q1) Find the sum of N natural numbers using FOR loop. Q2) Find the sum of odd numbers using WHILE loop. Q3) implement string functions such as length and compare to upper and lower case using switch case. Solution:

    Ans 1)

    Code:

    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

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    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: 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: 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: 7

    Wrong Choice

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    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 !

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    Matrix Arithmetic Operations 1) Perform the arithmetic operations for the given 3X3 matrix

    A+B, A-B, A*B, det(A), det(B) A = 3 11 6 4 7 10 13 9 0 B = 4 6 8 3 5 7 6 9 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

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    -->F = det (A)

    -->G = det (B)

    Output: A = 3 11 6 4 7 10 13 9 0 B = 4 6 8 3 5 7 6 9 12 C = 7 17 14 7 12 17 19 18 12 D = -1 5 -2 1 2 -3 7 0 -12 E = 81 127 173 97 149 201 79 123 167 F = 830 G = 0.0000000000000006661338 Ans 2) Code: A = [ 1:7 ; 2:2:14 ; 21:-3:3 ; 5:5:35 ] a = A (1, :) b = A (3:4, :) c = [a;b] d = c (:,1) e = c (:,3:4) f = c (:,7) B = [ d e f ] g = A (3,:) h = A (:,5) I = A (:,7) h=h i=I U = [ g h i] Output: A = a = 1 2 3 4 5 6 7

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    b = 21 18 15 12 9 6 3 5 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

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    Rule of matrix

    From the matrix A, B and C

    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

    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:

    X=A+B

    Y=B+A

    Output:

    X =

    Y =

    Hence, A+B = B+A

    Ans 2)

    Code:

    X=A+(B+C)

    X=A+(B+C)

    Output:

    X =

    Y =

    Hence, A(A+B) = (A+B)+C

    Ans 3)

    Code:

    X=A*(B+C)

    Y=A*B+A*C

    Output:

    X =

    Y =

    Hence, A*(B+C) = A*B+A*C

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    2D plots

    Year 2008 2009 2010 2011 2012

    Placement 65 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');

  • Department of Computer Science and Engineering, SRM University, Kattankulathur

    Department of Computer Science and Engineering, SRM University, Kattankulathur

    Bar chart and pie chart

    Q1) Write a Scilab program to get 5 subjects for 5 members of student and calculate the average and draw

    the bar chart and pie chart for individual students performance.

    Solution:

    Ans 1)

    Code:

    S1 = [30 35 48 36 40];

    S2 = [32 33 25 12 27];

    S3 = [4 0 16 5 1];

    S4 = [47 49 48 45 50];

    S5 = [27 18 39 34 30];

    a = sum(S1)/5;

    b = sum(S2)/5;

    c = sum(S3)/5;

    d = sum(S4)/5;

    e = sum(S5)/5;

    S = [a b c d e];

    bar (S,'y');

    pie (S);


Recommended