9
II] SCILAB: Lesson 1: Introduction 1) Create the following directory (folder): Scilab_files. 2) Open Scilab, and type: --> pwd ans = /home/kheirha/Documents --> cd /home/kheirha/Scilab_files --> pwd ans = /home/kheirha/Scilab_files 3) Enter the following as shown: --> x=10 x = 10 --> y=4 y = 4 --> z=x-y z = 6 --> save ('test0.dat') --> clear --> x !--error 4 undefined variable : x --> load ('test0.dat') --> x x = 10 --> y y = 4 --> z z = 6

An introduction to Sci Lab

Embed Size (px)

DESCRIPTION

Doing basic operations and calculations, functions & scripts as well as plotting.

Citation preview

Page 1: An introduction to Sci Lab

II] SCILAB:

Lesson 1: Introduction1) Create the following directory (folder): Scilab_files.2) Open Scilab, and type:

--> pwdans = /home/kheirha/Documents--> cd /home/kheirha/Scilab_files--> pwdans = /home/kheirha/Scilab_files

3) Enter the following as shown:--> x=10x = 10--> y=4y = 4--> z=x-yz = 6--> save ('test0.dat')--> clear--> x!--error 4 undefined variable : x--> load ('test0.dat')--> xx = 10--> yy = 4--> zz = 6

Page 2: An introduction to Sci Lab

Explanation: you are assigning values to variables (x & y), obtaining results (z to the screen), saving the variables and result to a file (save), clearing memory (clear), loading (load) the previously saved file (test0.dat), getting the values printed to the screen (x, y, z).

4) Try with the following commands:-->% pi%pi = 3.1415927--> sin (%pi/2)ans = 1octave:15> cos (%pi/2)ans = 6.1230D-17octave:16> cos (%pi/3)ans = 0.5--> sin (%pi/3)ans = 0.8660254 5) Now enter the following commands:--> deg=%pi/180deg = 0.0174533--> sin (30*deg)ans = 0.5--> cos (30*deg)ans = 0.8660254Explanation: Define a variable deg and makes it equal 3.1416 divided by 180, then try to find sin 30 degrees as shown, the who command will list the names of variables in Scilab workspace, variables that have been created or used, whos lists entire contents of workspace (functions, libraries & constants).

6) Before moving to vectors & matrices, we shall introduce the Scipad (tool that allows automation, through the creation of scripts and user defined function files). Open scilab, then click the button in the top Editor or type scipad in scilab workspace. Enter the following lines:pwdcd /home/user_name/Scilab_filespwdpi = %pie = %e //natural logarithm baseThen save this file (in the directory where scilab opens (in general it is /home/user_name/Documents), it is called in our examples here:sci0.sce. Click Execute then Load into Scilab, you will see the result of this script in the scilab workspace window, the current directory is indicated then changed and confirmed the change, indicated the value of pi and e. Note to get out of any situation (program) to get the prompt click Ctrl-c (very famous use). Assuming the file you have just saved I scipad is sci0.sce, you exited scilab and re-started, you can load this file without scipad by executing the following command: exec('sci0.sce'). Now type the following in scilab workspace:

-1-> sin(pi/3)ans = .8660254-1-> sin(60*pi/180)ans = .8660254

Now get scipad up and running again, open file sci0.sce and add the following line deg=pi/18, save the file and reload it ito scilab (using one of the previous mentioned

methods). Now enter the following:-->sin(30*deg)

Page 3: An introduction to Sci Lab

ans = 0.5--> sin(60*deg)ans = .8660254–->sqrt(9)ans = 3

7) In fact, vectors (arrays) are a simple case of a matrix (which is just a two-dimensional grid of numbers). A vector is a matrix with only one row, or only one column. Type the following while you are in Scilab:

--> a=[1 2 3]a =

1. 2. 3.

--> b=[3,2,1,]b =

3 . 2. 1.

--> c=[5> 6> 7> ]c =

5. 6. 7.

--> d=[a,4]d =

1. 2. 3. 4.

--> x=[2:6]x =

2. 3. 4. 5. 6.

--> y=[2:2:6]y = 2. 4. 6.--> a(3)ans = 3.--> y(2)ans = 4.--> x(4)ans = 5.--> x(2:4)ans =

Page 4: An introduction to Sci Lab

3. 4. 5.--> x*2ans =

4. 6. 8. 10. 12.

--> b*cans = 34.--> b.*c!--error 9999 inconsistent element-wise operation--> a.*bans =

3. 4. 3.

--> angles=[0:pi/6:2*pi]angles =

Columns 1 through 11 (values shown are rounded up to 5 digits, the ones you get from scilab are rounded up to 7 digits:

0.00000 0.52360 1.04720 1.57080 2.09440 2.61799 3.14159 3.66519 4.18879 4.71239 5.23599

Columns 12 and 13:

5.75959 6.28319

--> y=sin(angles)y =

Columns 1 through 11 (numbers shown are rounded to 5 digits, the ones you will gwt will be to 7):

0.00000 0.50000 0.86603 1.00000 0.86603 0.50000 0.00000 -0.50000 -0.86603 -1.00000 -0.86603

Columns 12 and 13:

-0.50000 -0.00000Explanation: line 1 is a vector with 3 elements, line 4 is vector named d and is equal a plus an additional element equal 4, line 5 is a vector having the first element 2 and incremented by 1 (defult) until reaching 6, line 6 is vector starting with value 2 incremented by 2 until reaching 6, extracting an element or elements from the vectors are shown on lines 7, 8, 9 & 10, line 11 shows a vector multiplied by a scalar (2), line 12 shows 2 vectors multiplied by each other (the matrix approach is used), to multiply the corresponding elements within the vector and display such a result you use .* (shown on the second line 13) when 2 vectors that are not compatible multiplied together as shown on line 13, the error message is displayed. Line 14 will calculate and display the vector angles starting at angle 0

Page 5: An introduction to Sci Lab

radians up to 2 pi in pi/6 increments, line 15 will calculate and display the values of sine of the angles vector.A matrix is a rectangular array of numbers, the size of which is usually described as m × n, meaning that it has m rows and n columns.Type in the folllowing:--> A =[1,2> 5 6]A =

1. 2. 5. 6.

--> A'ans =

1. 5. 2. 6.

--> inv(A)ans =

-1.5 0.5 1.25 -0.25

--> det(A)ans = -4.--> A*inv(A)ans =

1. 1.110D-16 8.882D-16 1.

--> B=[1,2,5> 7,8,4> 9,4,7]B =

1. 2. 5. 7. 8. 4. 9. 4. 7.

--> B'ans =

1. 7. 9. 2. 8. 4. 5. 4. 7.

--> inv(B)

Page 6: An introduction to Sci Lab

ans =

-0.194175 -0.029126 0.155340 0.063107 0.184466 -0.150485 0.213592 -0.067961 0.029126Note: again the values you will be getting will be rounded up to 7 digits

--> det(B)ans = -206Explanation: entering a 2 by 2 matrix, obtaining the transpose of A matrix, the inversion of the matrix is obtained, calculating the determinant of the matrix and checking - multiplying the matrix by its inversion to get the identity matrix (which has 1s in the diagonal elements and 0s otherwise). The remaining lines do the same calculations for 3 by 3 matrix with exception of the checking one.

Lesson 2: Scripts & functionsScripts were introduced on page 2, in this lesson, functions will be introduced, you can use scipad to produce such files or a text editor. The function file can have more than one function and when the file is loaded using scipad or the exec command (from within the scilab workspace), all functions will be available for use from scilab workspace. Now you can open scilab and if you have the scipad facility, open it and type the following into a new file and save it as functions.sci:function [sind] = sind(x) sind = sin(x*pi/180)endfunction

function [cosd] = cosd(x) cosd = cos(x*pi/180)endfunction

function [tand]=tand(x) if x == 45 then, tand=1; else tand = tan(%pi*x/180); endendfunction

function [y1,y2]=root(a,b,c) y1 = (-b+((b^2-4*a*c)^.5))/(2*a) y2 = (-b-((b^2-4*a*c)^.5))/(2*a) endfunction

function [square, cube, sqroot, cubicroot] = innum(x) square=x^2 cube=x^3 sqroot=sqrt(x) cubicroot=x^.33333334Note: if you do not have the Editor (scipad), then you can use gedit, kwrite or any other text editor, enter the above functions and save the file with .sci extension.Afterl loading the file, you can execute the any of the functions saved in such file by calling it, eg for calling sind function to get the sine of 30 degrees is sind(30), to get the roots of an equation [y1,y2]=root(10,20,30), in order to obtain the square-root, cubic-root, squared and cubed for the number inputted you may call the function as follows: [a,b,c,d,]=inum(8).

Page 7: An introduction to Sci Lab

Lesson 3: Plotting & graphs1) Open scilab and type exec('sci0.sce'), then aenter the angles function (that will build the vector shown, then produce another vector equal to sine the angles, then you will plot the angle vs. it sine value and finally save the plot in .gif graphics format.-->exec('sci0.sce')ans = /home/kheirhaans = /home/kheirha/Scilab_filespi = 3.1415927e = 2.7182818deg = 0.0174533--> angles=[0:pi/6:2*pi]angles = the values angles vector will be displayed

Columns 1 through 13:--> y=sin(angles)y =the values angles vector will be displayed

Columns 1 through 13:

--> plot2d (angles,y)To save the graph: Click the File button above the graph and select Export, from the dialog box that will appear choose GIF in the Format Selection field, enter the File-name (without extension) and click OK. Then enter the following line:--> plot2d (angles*180/pi,y)To save the plot, follow the process mentioned above.

2) The above commands can be placed in a script and saved with a sce file extension. Every time you want to plot a sine curve you just have to type exec('file_name.sce') where file_name is the name of the script file with the .sce extension.file: plotsine.sce that automates the process of plotting a sine wave (example for a script):angles=[0:pi/6:2*pi]y = sin(angles)plot (angles*180/pi,y)Another script is given hereafter, to plot the cos function.file: plotcosine.sce that automates the process of plotting a cosine wave (example for a script):angles1=[0:pi/6:2*pi]z = cos(angles)plot (angles1*180/pi,z)This example will plot the 2 graphs shown in the above figure.file: plotsinecos.sce that automates the process of plotting a the sine wave above and the cosine wave below in one graph (example for a script):angles=[0:pi/6:2*pi];y = sin(angles);subplot(2,1,1);plot (angles*180/pi,y);z = cos(angles);subplot(2,1,2);plot (angles*180/pi,z);

Page 8: An introduction to Sci Lab

3) More plotting examples, using scripts:file: linetestd.sce that automates the process of plotting , the fig. below will appeary=[2,4,9,10] x = [1,2,3,4] plot(x,y)

Page 9: An introduction to Sci Lab

Still much more to come