an introduction to octave

11
Octave Octave and scilab programs will be covered here in 3 lessons, each. These lessons are introduction (basic commands & functions), automation (the use of .m and .sce files) and plotting. The examples presented here were run on PCLOS for Octave and MEPIS for Scilab. I] OCTAVE: Lesson 1: 1) Create the following folder Octave_files. 2) Open Octave, and type: octave:1> pwd ans = /home/kheirha octave:2> cd /home/kheirha/Octave_files octave:3> pwd ans = /home/kheirha/Octave_files 3) Enter the following as shown: octave:4> x=10 x = 10 octave:5> y=4 y = 4 octave:6> z=x-y z = 6 octave:7> save octave_test0 octave:8> clear octave:9> x error: `x' undefined near line 9 column 1 octave:9> load octave_test0 octave:10> x x = 10 octave:11> y y = 4 octave:12> 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 (octave_test0), getting the values printed to the screen (x, y, z). 4) Try with the following commands: octave:13> pi ans = 3.1416 octave:14> sin (pi/2) ans = 1 octave:15> cos (pi/2) ans = 6.1230e-17 octave:16> cos (pi/3) ans = 0.50000 octave:17> cos (pi/3) ans = 0.50000 octave:18> sin (pi/3) ans = 0.86603 5) Now enter the following commands:

Upload: hany-kheir

Post on 02-Apr-2015

363 views

Category:

Documents


0 download

DESCRIPTION

Doing basic operations & calculations, preparing scripts &f unctions and plotting.

TRANSCRIPT

Page 1: An introduction to Octave

Octave

Octave and scilab programs will be covered here in 3 lessons, each. These lessons are introduction (basic commands & functions), automation (the use of .m and .sce files) and plotting. The examples presented here were run on PCLOS for Octave and MEPIS for Scilab.I] OCTAVE:Lesson 1:

1) Create the following folder Octave_files.2) Open Octave, and type:

octave:1> pwdans = /home/kheirhaoctave:2> cd /home/kheirha/Octave_filesoctave:3> pwdans = /home/kheirha/Octave_files

3) Enter the following as shown:octave:4> x=10x = 10octave:5> y=4y = 4octave:6> z=x-yz = 6octave:7> save octave_test0octave:8> clearoctave:9> xerror: `x' undefined near line 9 column 1octave:9> load octave_test0octave:10> xx = 10octave:11> yy = 4octave:12> zz = 6Explanation: 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 (octave_test0), getting the values printed to the screen (x, y, z).

4) Try with the following commands:octave:13> pians = 3.1416octave:14> sin (pi/2)ans = 1octave:15> cos (pi/2)ans = 6.1230e-17octave:16> cos (pi/3)ans = 0.50000octave:17> cos (pi/3)ans = 0.50000octave:18> sin (pi/3)ans = 0.86603 5) Now enter the following commands:

Page 2: An introduction to Octave

octave:1> deg=pi/180deg = 0.017453octave:2> sin (30*deg)ans = 0.50000octave:3> cos (30*deg)ans = 0.86603octave:4> whoVariables in the current scope:

ans deg

octave:5> format longoctave:6> degdeg = 0.0174532925199433octave:7> format shortoctave:8> degdeg = 0.017453Explanation: Define a variable deg and makes it equap 3.1416 divided by 180, then try to find sin 30 degrees as shown, who will show a list of all the named functions, variables that have been created or used. You can format the output as long or short to display the number of digits after the decimal (accuracy or precision).

6) Now enter the following commands, before moving to vectors & matrices (line 1 will be covered in the next lesson Automation & Functions):

octave:1> oct0ans = /home/kheirhaans = /home/kheirha/Octave_filesdeg = 0.017453e = 2.7183octave:2> x=sqrt(9)x = 3octave:3> sqrt(x)ans = 1.7321octave:4> load octave_test0octave:5> xx = 10

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 Octave:octave:1> a=[1 2 3]a =

1 2 3

octave:2> b=[3,2,1,]b =

3 2 1

Page 3: An introduction to Octave

octave:3> c=[5> 6> 7> ]c =

5 6 7

octave:4> d=[a,4]d =

1 2 3 4

octave:5> x=[2:6]x =

2 3 4 5 6

octave:6> y=[2:2:6]y =

2 4 6octave:7> a(3)ans = 3octave:8> y(2)ans = 4octave:9> x(4)ans = 5octave:10> x(2:4)ans =

3 4 5octave:11> x*2ans =

4 6 8 10 12

octave:12> b*cans = 34octave:13> b.*cerror: product: nonconformant arguments (op1 is 1x3, op2 is 3x1)octave:13> a.*bans =

3 4 3

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

Page 4: An introduction to Octave

angles =

Columns 1 through 11:

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

octave:15> y=sin(angles)y =

Columns 1 through 11:

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 radians up to 2 pi in pi/6 increments, line 15 will calculate and display the values of sine of the angles vector.

If you ask Octave to display a matrix or vector that will not fit onto a single screen, it will present the values one page at a time. Press the space bar to see the next page of values and use the ‘q’ key to quit the display and return to the Octave command prompt. more off: turns off pagination option,more on: turns it on. The vector creation functions are:linspace(x1, x2,N) Create a vector of N elements, evenly spaced between x1 and x2logspace(x1, x2,N) Create a vector of N elements, logarithmically spaced between 10x1 and 10x2 and they are mostly used when creating plots from data, will be covered in lesson 3.

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:octave:16> A =[1,2> 5 6]A =

Page 5: An introduction to Octave

1 2 5 6

octave:17> A'ans =

1 5 2 6

octave:18> inv(A)ans =

-1.50000 0.50000 1.25000 -0.25000

octave:19> det(A)ans = -4.0000octave:20> A*inv(A)ans =

1 0 0 1

octave:21> B=[1,2,5> 7,8,4> 9,4,7]B =

1 2 5 7 8 4 9 4 7

octave:22> B'ans =

1 7 9 2 8 4 5 4 7

octave:23> inv(B)ans =

-0.194175 -0.029126 0.155340 0.063107 0.184466 -0.150485 0.213592 -0.067961 0.029126

octave:24> B*inv(B)ans =

Page 6: An introduction to Octave

1.0000e+00 5.5511e-17 2.7756e-17 -2.2204e-16 1.0000e+00 2.3592e-16 2.2204e-16 5.5511e-17 1.0000e+00

octave:25> det(B)ans = -206Explanation: line 16 entering a 2 by 2 matrix, line 17 obtaining the transpose of A matrix, line 18 the inversion of the matrix is obtained, line 19 calculates the determinant of the matrix and line 20 is a check 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.

Lesson 2: Scripts & functions1) Automating the work with octave is possi, ble through the use of m-files (files with .m extension). These files are classified into script and function files. The former has commands stored in it and when the file is called (typing its name without the extension), the stored commands will be executed one after the other. The latter will have one user defined function, where the name of h\the function is the name as the name of file itself (without the .m extension).2) Example for a script file that can be run immediately after starting octave, to display the current directory, change it to the octave files directory, calculate the value of deg that can be used as a constant until changed by the user and display the value of e (natural logarithm base).oct0.m: saved in the directory where octave opens in, in general:/home/user_name, can be checked from within octave with the following command: pwd.

pwdcd /home/kheirha/Octave_filespwddeg=pi/180e = exp(1)3)Examples for function m-files.testa: saved in /home/user_name/Octave_files.

function [root1, root2] = testa (a,b,c)%TESTA(a,b,c) Calculates the roots of ax^2+bx+c=0root1 = (-b+sqrt((b^2)-(4*a*c)))/(2*a)root2 = (-b-sqrt((b^2)-(4*a*c)))/(2*a)

testb: saved in /home/user_name/Octave_files.

function [sino, coso, tano] = testb(x)%TESTB(x) Calculates the sin, cos & tan of the angle enterd in degreessino=sind(x)coso=cosd(x)tano=tand(x)

testc: saved in /home/user_name/Octave_files.

function [square, cube, sqroot, cubicroot] = testc(x)%TESTC(x) Calculates the square, cube, square-root and cubic root of the number enterd

Page 7: An introduction to Octave

square= x^2cube=x^3sqroot=sqrt(x)cubicroot=x^.3333333333

Lesson 3: plottingWith the plotting facility of Octave (which is GNU plot) you can produce single pr even 2 graphs per plot (as shown in the figure below).

1) Open octave and type oct0, then as shown on line 2 below enter the angles function (that will build the vector shown, per line 3 you shall produce another vector for equal to sine the angles, then you will plot the angle vs. it sine value (line 4) and finally save the plot in .png graphics format (line 5).oct0ans = /home/kheirhaans = /home/kheirha/Octave_filesdeg = 0.017453e = 2.7183octave:2> angles=[0:pi/6:2*pi]angles =

Columns 1 through 11:

0.00000 0.52360 1.04720 1.57080 2.09440 2.61799 3.14159 3.66519 4.18879 4.71239

Page 8: An introduction to Octave

5.23599

Columns 12 and 13:

5.75959 6.28319

octave:3> y=sin(angles)y =

Columns 1 through 11:

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.00000

octave:4> plot (angles,y)octave:5> print ('fig1.png','-dpng')Could not find/open font when opening font "arial", using internal non-scalable fontoctave:6> plot (angles*180/pi,y)octave:7> print ('fig2.png','-dpng')Could not find/open font when opening font "arial", using internal non-scalable font

2) The above commands can be placed in a script and saved with a,m file extension. Every time you want to plot a sine curve you just have to type the file name (eg. plotsinefile: plotsine.m that automates the process of plotting a sine wave (example for a script):angles=[0:pi/6:2*pi]y = sin(angles)plot (angles,y)plot (angles*180/pi,y)Another script is given hereafter, to plot the cos function.file: plotcosine.m that automates the process of plotting a cosine wave (example for a script):angles1=[0:pi/6:2*pi]z = cos(angles)plot (angles1,z)plot (angles1*180/pi,z)This example will plot the 2 graphs shown in the above figure.file: plotsinecos.m 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 9: An introduction to Octave

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

file: teste.m that automates the process of plotting a straight line y=linspace(1,10,30); x = linspace(1,4,30); plot(x,y) ;

file: testf.m that automates the process of plotting a curve on log-log scale y=logspace(1,10,30); x = logspace(1,4,30); plot(x,y) ;

file: testg.m that automates the process of plotting 2 curves on the same log-log graph y=logspace(1,3,30);z=logspace(1,3.2,30); x = logspace(1,2,30); plot(x,y,':',x,z,'-') ; %is used to produce 2 curves on the same graph (plot)print ('fig10.png','-dpng')

Page 10: An introduction to Octave

Note: the produced curves are given hereafter

Page 11: An introduction to Octave

A final plotting example:y=logspace(1,3,30);z=logspace(1.3,3.2,30);w=logspace(1.5,3.5,30)x = logspace(1,2,30); plot(x,y,':',x,z,'-',x,w,'-') ; %is used to produce 3 curves on the same graph (plot)print ('fig11.png','-dpng')