Scilab Documentation (2)

March 21, 2018 | Author: psingh9 | Category: Matlab, Polynomial, Body Mass Index, Chart, Matrix (Mathematics)


Comments



Description

Department of Computer Science and Engineering, SRM University, KattankulathurIntroduction 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 variable workspace o command window 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 built in o Scilab comes with a GUI. 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. 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. SRM University. Kattankulathur . SRM University. Guide Department of Computer Science and Engineering. followed by the number of bins o Scilab histplot function puts the number of bins first. 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 don’t 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.Department of Computer Science and Engineering. 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. GUIDE. The name of the function(s) is given by function [y]=fct1(x) . Functions can also be defined on-line (or inside functions) by the command deff... Department of Computer Science and Engineering. Kattankulathur [1.. Each entry of a string matrix has its own length.1] in Scilab and Matlab.1]) [which is NOT x([%T. Comment lines Scilab comments begins with: // Matlab comments begins with: % Variables Predefined variables usually have the % prefix in Scilab (%i.sci") before using it.2].sci). Also if x is a matrix x(1:n. %F in Scilab and 0.%T])] returns [1. Usually you have to execute the command getf("myfile.Department of Computer Science and Engineering. Example: x=[1. Kattankulathur Functions Functions in Scilab are NOT Matlab m-files but variables. 1 in Matlab. They are write protected.). SRM University. %inf..1)=[] or x(:)=[] is not valid in Matlab.. SRM University. function [y]=fct2(x) . Boolean variables Boolean variables are %T. One or several functions can be defined in a single file (say myfile.. . The function(s) are not automatically loaded into Scilab. Strings Strings are considered as 1 by 1 matrices of strings in Scilab.x([1. Indexing with boolean variables may not produce same result. The name of of the file is not necessarily related to the the name of the functions. 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.2] in . partial list of commands with significant different syntax. Plotting Except for the simple plot and mesh (plot3d) function plots. SRM University. Empty matrices [ ]+1 returns 1 in Scilab and [ ] in Matlab. Scilab and Matlab are not compatible.Department of Computer Science and Engineering. Here is a brief. Built-in-functions Most built in functions are identical in Matlab and Scilab. Some of them have a slightly different syntax. Kattankulathur . Kattankulathur Polynomials Polynomials and polynomial matrices are defined by the function poly in Scilab (builtin variables). 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 evstr and strcat rtitr x < %inf read Department of Computer Science and Engineering. SRM University. Scicos Scicos (Scilab) and Simulink (Matlab) are not compatible. They are considered as vectors of coefficients in Matlab. Kattankulathur .Department of Computer Science and Engineering. SRM University. Kattankulathur fseek ftell global home isglobal isinf(a) isnan(a) isstr(a) lookfor pause randn setstr srcmp(a. nargin]=argn(0) getvalue Department of Computer Science and Engineering. SRM University.b) which nargin nargout uimenu file a == %inf a ~= a type(a) == 10 apropos halt rand code2str a == b whereis [nargout. Kattankulathur . 22. 20. 23. 2.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. SRM University.Department of Computer Science and Engineering. 10. Kattankulathur Scilab Exercises Basic Commands • help Purpose = to get the list of help topics • clock Purpose = to show date in vector form ans = 2014.5.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.1. SRM University. 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 Department of Computer Science and Engineering.Department of Computer Science and Engineering. 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 Ans 1X58 116 char • what Purpose = Lists low level primitives and commands. Kattankulathur . SRM University. 67*x-52 Output = 3283.5 3.58 • ((14x3)1/2)/e3x -->x=13. when x=13. when x=13. SRM University.67x-52 -->x=13.Department of Computer Science and Engineering.5 2.67x-52 . SRM University.5 -->(sqrt(14*(x^3)))/exp(3*x) Output = 4.5 4.0000000000000004782315 • log|x2-y3| -->x=13. when x=13. Prove LHS=RHS when x=∏/5 a.782D-16 Note: The above output contains D. To Remove that we change the output format. log|x2-x3| .5 -->log(abs((x^2)-(x^3))) Department of Computer Science and Engineering. Kattankulathur Evaluating the Expressions 1. --> format(25) -->(sqrt(14*(x^3)))/exp(3*x) Output = 0. x3+5x2-2. Cos2(x/2) = tanx+sinx/2tanx Solution: • x3+5x2-2. Kattankulathur .5 -->x^3+5*(x^2)-2. ((14x3)1/2)/e3x . [37 log(76)/(73+546)] + (910)1/3 5. SRM University. LHS=RHS Department of Computer Science and Engineering.3444 • Prove LHS=RHS when x=∏/5 Cos2(x/2) = tanx+sinx/2tanx LHS -->cos2(x/2) (cos(x/2))^2 Output = 0.3)) Output = 20.7311080151970230645020 • -->[(37 log(76))/(73+546)] + (910)1/3 (((3^7)*log(76))/((7^3)+546))+(nthroot(910. Kattankulathur Output = 7.9045 RHS -->tanx+sinx/2tanx (tan(x)+sin(x))/(2*tan(x)) Output = 0. Kattankulathur .9045 Thus.Department of Computer Science and Engineering. SRM University. coeff(p) returns a row vector containing the coefficients of the polynomial. -->horner(p. ‘x’. Kattankulathur . it defaults to 'r'.Department of Computer Science and Engineering. 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. 4) ans = 2. ‘r’) Note: ‘r’ indicates that the first argument array are the roots of the second argument. derivat(p) returns a polynomial which is the derivative of p 6. ‘x’) Or --> p = poly([2.95 x+35. 3]. ‘x’. polynomial is evaluated for that value of the independent variable. ‘c’) Note: ‘c’ indicates that the first argument array are the coefficients of the second argument.015 x2-71. [2. 3]) ans = ` 0. -->horner(p. => p = 6 . and returns a vector as the result. 0. If the input is a single number. In this case. 4. -->horner(p. SRM University. polynomial is evaluated at each value in the vector. 2. If the input is a vector.59 x3-17.1 x4+40. varn(p) returns the variable of a polynomial. typeof(p) returns the type of the variable. => p1 = 6 -5x + x2 1. it will return the type as ‘polynomial’ Function horner() evaluates the polynomial for the given value/values of the independent variable. Calculate f(9) and roots of this equation. -5. 3]. Kattankulathur Polynomial evaluation Q1) f(x) = x5-12. If the third argument is not present. Department of Computer Science and Engineering. as in the first statement.5x + x2 --> p1 = poly([6. SRM University. companion(p) returns the companion matrix (a matrix whose characteristic equation is the given polynomial) of the polynomial 5. 3. 2) ans = 0. 1].88. roots(p) returns a column vector containing the roots of the polynomial. ’x’.95x .5 4.2x + 3x3 -->P + Q ans = .7x3 + 15x4 + 3x5 -->P * Q ans = 240 .59x3 .015x2 + 40.71. Kattankulathur Solutions: Ans Q1) Code: -->P = poly([35.57x5. 'c') P = 35.’c’) Q = poly([2 4 1]. 2.'c') Q = .1 1]. SRM University.40 + 15x .'x'.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].9) ans = 7261.95 -17. SRM University.10x .6 . ‘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.'c') P = . 'x'.1.1x4 + x5 -->horner(P.17.3x2 .10x3 + 15x4 + 3x5 -->Q = poly([-6 -2 0 3].54x3 .Department of Computer Science and Engineering.36x6 + 45x7 + 9x8 Ans Q3) Code: P = poly([-3 -5 -2 6 4].125 -->roots(P) ans = 6.88 -71.3x2 . Kattankulathur .12.2 0.25x4 . ‘x’.12x2 .3 .88 .015 40.46 + 13x .'x'.59 -12. a = input("Enter a number: ").5 = under weight BMI > 18.9 = normal BMI > 25 and <29. if a >=80 disp("Distinction").9 = overweight BMI >30 = obese Solution: Ans 1) Code: disp("Display the grade of the student "). SRM University. Q3) To find the body mass index (BMI) by using height and weight. end. elseif(a>=45 & a<60) disp("Second Class"). if (modulo(a. Kattankulathur . elseif(a>=60 & a<80) disp("First Class").Distinction a < 80 and a > 60 .Department of Computer Science and Engineering. SRM University. BMI = weight/(height)2 Weight (KG) Height (Meters) BMI <18.Fail Q2) Find if the given number is odd or even using if-else structure. a = input("Enter Marks: "). Kattankulathur Programming using control structures Q1) Display the grade of a student using if-else statement a >80 .5 and <24.2)==0) disp ("Even").First Class a < 60 and a > 45 . else disp("Fail").Second Class a < 45 . else Department of Computer Science and Engineering. Output: Display the grade of the student Enter Marks: 70 First Class Display the grade of the student Enter Marks: 39 Fail Ans 2) Code: disp("Check whether the input number is odd or even"). 222 Normal Calculating BMI Enter weight: 75 Enter height: 1.5) disp ("Underweight") elseif(r > 18. Kattankulathur disp ("Odd").1 The BMI is: 61.9) disp ("Normal") elseif(r > 25 & r < 29. SRM University.5 The BMI is: 22. r) if(r<18. mprintf("The BMI is: %d". 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). SRM University.9835 Obese Department of Computer Science and Engineering. Kattankulathur . end.Department of Computer Science and Engineering.5) disp("Overweight") else disp("Obese") end Output: Calculating BMI Enter weight: 50 Enter height: 1.5 & r < 24. sum) Output: Enter the number of numbers: 10 The sum of odd natural numbers upto 10 is: 25 Ans 3) Code: a = 'HELLO'. SRM University. Kattankulathur For.Department of Computer Science and Engineering. 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 : '). Department of Computer Science and Engineering. SRM University. Q3) implement string functions such as length and compare to upper and lower case using switch case. Kattankulathur . disp (length(b)).n. b = 'hai'. While and Switch statements Q1) Find the sum of ‘N’ natural numbers using FOR loop.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'. 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". Q2) Find the sum of odd numbers using WHILE loop.n. select ch case 1 disp (length(a)). 1.'u')). Kattankulathur case 2 disp (strcmp(a.'l')). disp (convstr(b. 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 . SRM University. case 4 disp (convstr(a.Department of Computer Science and Engineering. case 3 disp (convstr(a. SRM University. disp (convstr(b. Kattankulathur .b)). else disp ('Wrong Choice'). 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 Department of Computer Science and Engineering. 3.'u')). end.'l')). Output: 1 for length 2 to compare 3 to apply lower case 4 to apply upper case Enter any number: 1 5. ’r’). SRM University.txt’ ‘test2. Kattankulathur . ‘r’). SRM University. mfscanf (f1. mclose(fid).’world’. Solutions: Ans 1) Code: --> fid = mopen('test. f1 = mopen(‘test. Kattankulathur File Operations Q1) Open a text file in Scilab.Department of Computer Science and Engineering. mfprintf(fid.txt’. ‘w’). Ans 2) Code: fid = mopen(‘test.\n’.’hello %s %d.txt’ fid = fopen (‘test2. ‘%s %s’) Output: ans = !hello world ! Ans 3) Code: copyfile ‘test. ‘%s %s’) Output: ans = !hello world ! Department of Computer Science and Engineering. Q2) Writing information ‘WELCOME’ into a file and read that file.1). Q3) Copy and close the file.txt'. mfscanf (fid.txt’. 'w') fid = 1.txt’. Kattankulathur . 4 7 10 . third and fourth rows and the first. the fifth & seventh column. 3 5 7 . 13 9 0 ] -->B = [ 4 6 8 . 6 9 12 ] -->C = A+B -->D = A-B -->E = A*B Department of Computer Science and Engineering. 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. ii. fourth and seventh column of the matrix A. third. det(A).Department of Computer Science and Engineering. Form a matrix B from the first. Kattankulathur Matrix Arithmetic Operations 1) Perform the arithmetic operations for the given 3X3 matrix A+B. Solutions: Ans 1) Code: -->A = [ 3 11 6 . Create a matrix C with 17 elements long row vector from matrix A from the elements of the third row. A-B. A*B. SRM University. SRM University. :) h = A (:. 5:5:35 ] a = A (1.0000000000000006661338 Ans 2) Code: A = [ 1:7 . SRM University.5) I = A (:. 2:2:14 .3:4) f = c (:.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. :) c = [a.1) e = c (:. 21:-3:3 .Department of Computer Science and Engineering.7) B=[def] g = A (3. 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. :) b = A (3:4.b] d = c (:. Kattankulathur . Department of Computer Science and Engineering. Kattankulathur . SRM University. SRM University. Kattankulathur b= 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 Department of Computer Science and Engineering. Kattankulathur 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 multiplication of matrix is multiplication. Q2) Calculate A(A+B) and (A+B)+C. A+B = B+A Output: X= Y= Hence. A*(B+C) = A*B+A*C Department of Computer Science and Engineering. to show that addition of matrix is associative. 10 3 -2 . 0 -12 4 . 2 6 1] C = [ 7 14 1 . A(A+B) = (A+B)+C Output: X= Y= Hence. Kattankulathur . 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. 1 7 -3 . Q3) Calculate A*(B+C) and A*B+A*C. 6 -10 0] B = [ 11 5 -3 . to show that addition of matrix is commutative.Department of Computer Science and Engineering. SRM University. Solutions: A = [ 5 2 4 . SRM University. Kattankulathur 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 size = 12 and label the graph along with grid and box.2012 ] -->placement = [ 65.76.'g'. marker type diamond.2011. SRM University.Department of Computer Science and Engineering. -->title('2D PLOTS and PRINTING LABELS'). SRM University. Solutions: Ans 1) Code: -->year = [ 2008.placement.84 ] -->plot (year.12).2009.'rd-'. marker edge color green.72.2010.74. Kattankulathur . Also plot the bar chart and pie diagram for the data. Department of Computer Science and Engineering.'markeredgecolor'.'markersize'. S4 = [47 49 48 45 50]. S3 = [4 0 16 5 1]. a = sum(S1)/5. bar (S. SRM University. S = [a b c d e].'y').Department of Computer Science and Engineering. Department of Computer Science and Engineering. S2 = [32 33 25 12 27]. b = sum(S2)/5. 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 student’s performance. c = sum(S3)/5. S5 = [27 18 39 34 30]. Solution: Ans 1) Code: S1 = [30 35 48 36 40]. e = sum(S5)/5. SRM University. pie (S). Kattankulathur . d = sum(S4)/5.
Copyright © 2024 DOKUMEN.SITE Inc.