353 views

Uploaded by هانى خير

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

save

You are on page 1of 9

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

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 = 1

octave:15> cos (%pi/2)

ans = 6.1230D-17

octave:16> cos (%pi/3)

ans = 0.5

--> sin (%pi/3)

ans = 0.8660254

5) Now enter the following commands:

--> deg=%pi/180

deg = 0.0174533

--> sin (30*deg)

ans = 0.5

--> cos (30*deg)

ans = 0.8660254

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

pwd

cd /home/user_name/Scilab_files

pwd

pi = %pi

e = %e //natural logarithm base

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

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 =

3. 4. 5.

--> x*2

ans =

4. 6. 8. 10. 12.

--> b*c

ans = 34.

--> b.*c

!--error 9999

inconsistent element-wise operation

--> a.*b

ans =

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

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

-0.50000 -0.00000

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

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)

ans =

0.063107 0.184466 -0.150485

0.213592 -0.067961 0.029126

Note: again the values you will be getting will be rounded up to 7 digits

--> det(B)

ans = -206

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

Scripts 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

cosd = cos(x*pi/180)

endfunction

function [tand]=tand(x)

if x == 45 then, tand=1; else tand = tan(%pi*x/180); end

endfunction

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

square=x^2

cube=x^3

sqroot=sqrt(x)

cubicroot=x^.33333334

Note: 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).

Lesson 3: Plotting & graphs

1) 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/kheirha

ans = /home/kheirha/Scilab_files

pi = 3.1415927

e = 2.7182818

deg = 0.0174533

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

angles = the values angles vector will be displayed

--> y=sin(angles)

y =the values angles vector will be displayed

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

3) More plotting examples, using scripts:

file: linetestd.sce that automates the process of plotting , the fig. below will appear

y=[2,4,9,10]

x = [1,2,3,4]

plot(x,y)

Still much more to come

- First Semester Curriculum All BranchesUploaded bykamal
- ME101 Tutorial4 AnswersUploaded byAnand Azar
- Matlab Manual1Uploaded byvenkatcharms
- Le Bellac M. a Short Introduction to Quantum Information and Quantum Computation.. Solutions of Exercises (Draft, 2006)(28s)_PQmUploaded bythilin
- 10.09.16_solUploaded byrahul
- Assignment 1-AME - (12ED2)Uploaded byDuy Trần
- Riley, Hobson ToCUploaded bydomenico123
- An Introduction to Matlab for EconometricsUploaded byAlzarani
- A15 201-NYC Linear AlgebraUploaded byNicole Wong
- XI Maths - First Terminal Exam ModelUploaded byPurisai Rajamani Kumar
- vivekUploaded byvitthalpatnecha
- sMath Studio PrimerUploaded bymeceman
- Inter Class Math Quiz 2015Uploaded byjesudassaj
- C4 Practice Paper A1 mark schemeUploaded byaneesaahmed1991
- 1 - 1 - Lecture 0 IntroductionUploaded byRahulsinghoooo
- All Purpose Conversion WorksheetUploaded byAminah Usman
- 2010-Paper1Uploaded byAbhishek Seth
- Assignment1_2014Uploaded byJoshua Nolan Luff
- Higher Mathematics for Engineering and Technology Problems and SolutionsUploaded byAnonymous 5AamXv
- Etnographic MapmakingUploaded byYakir Sagal
- classroom o assignment-1 z yamanUploaded byapi-322827649
- All Purpose 2003 FormulaUploaded bysivakumar

- تعليم الإنجليزية بدون معلمUploaded byهانى خير
- Indoor & outdoor substations, an introductionUploaded byهانى خير
- Script languages by exampleUploaded byهانى خير
- Basic GreekUploaded byهانى خير
- Programming languages by exampleUploaded byهانى خير
- Learn ArabicUploaded byهانى خير
- تعليم الإنجليزية بدون معلمUploaded byهانى خير
- An introduction to OctaveUploaded byهانى خير
- تعليم الفرنسية بدون معلم المستوي: المبادىءUploaded byهانى خير
- Underground cablesUploaded byهانى خير
- Introduction to motor controllers and motor control centers, part 2Uploaded byهانى خير
- Introduction to motor controllers and motor control centers, part 1Uploaded byهانى خير
- تعليم الإنجليزية بدون معلم المستوي المتوسطUploaded byهانى خير
- Padmounted switchgearUploaded byهانى خير
- 15418296 Electrical Distribution Systems Questions Answers Part IUploaded byJagadish Neelamraju
- Electrical Distribution Equipment, Questions & Answers, part IIUploaded byهانى خير
- Introduction to medium voltage circuit breakersUploaded byهانى خير
- Transformers in electrical power distribution systemsUploaded byهانى خير

- Apostila_Calculo_1aUploaded bymfsss666
- cours2et3.pdfUploaded byMedab Abd El Malek
- TABLA DE INTEGRALES INMEDIATAS_fma (2.1).pdfUploaded byFran Viejo
- examiner report a levelUploaded bysakhararshed
- January 2011 MS - C2 EdexcelUploaded byRishita Singh
- Habilidades Numericas II (2)Uploaded byNéstor André
- ME SYLLABUS.pdfUploaded byVinod Kumar
- Notes-2nd Order ODE Pt2Uploaded byAnonymous Q0zrjJiJa
- Faster Sin and Cosine Through Lookup TablesUploaded bydipatel
- Common Derivatives & IntegralsUploaded bystr8spades
- WJEC GCSE Maths Higher - P2 A2008Uploaded byphilgiddyboy
- reflectionUploaded byapi-275663406
- Maths questionsUploaded bySafwan Zulfazli
- Michele Nardelli, Francesco Di Noto, Roberto Servi - "On some equations concerning the cusp anomalous dimension from a TBA equation and generalized quark-antiquark potential at weak and strong coupling; some equations concerning the complete four-loop four-point amplitude of N = 4 super-Yang-Mills theory. Mathematical connections with some sectors of Number Theory"Uploaded byMichele Nardelli
- Las funciones trigonométricas y su aplicación en el análisis de patrones de rastros sanguíneosUploaded byjlcisnerosmxl
- Core 2 Revision BookletUploaded byjake1731
- Calculus in a WeekUploaded byBob Smith
- DIVERSIFICADO_MATEMÁTICA_2011Uploaded byWilliam Sànchez
- Ultimo 2 appb.pdfUploaded bymri_leon
- Analisis de Esfuerzos y Deformaciones Libro DefinitivoUploaded byJose Enrique Rodriguez Arreguin
- PLAN DE MEJORAMIENTO 10° 1 2016Uploaded byAndreaNaranjoChavarro
- Calculus WorkbookUploaded byrandonam
- Eje Geometrico-Metrico TodosUploaded byAnonymous QdfpIcdrt
- MEPDF_P2_E11-12Uploaded bymkan_3
- FunctionsUploaded byMichael Ndavi
- CK-12-Trigonometry-Concepts b v1 Xdd s1Uploaded byGilberto Rodrigues Pimentel Junior
- 218 39 Solutions Instructor Manual Chapter 5 Cam DesignUploaded bykentetsu123456789
- Statyka i Stereomechanika w Zadaniach - Zdzisław Iwulski, Robert KlisowskiUploaded byAssesor
- Generation of Crowned Parabolic Novikov gearsUploaded byfgsldfjglkjsdf
- 188369649-Theory-and-Applications-of-Numerical-Analysis.pdfUploaded byزكرياء بنحيرت