Octave

1. Basic Operations (Arithmatic)

  • Addition
  • Subtraction
  • Division
  • Multiplication
  • Exponent
  • element-by-element .+, .-
octave:1>50+2
ans=52
octave:2>20-8
ans=12
octave:3>60/6
ans=10
octave:4>40*20
ans=800
octave:5>50^2
ans=2500
octave:6>14.+10
ans=24

Matrix Operations

octave:1>A=[1 2 3 ;4 5 6;0 0 3]
A =
1 2 3
4 5 6 
0 0 3
//determinant of matrix
octave:2>det(A)
ans = -9
//mode of matrix. Return most occurring value in matrix.
octave:3>mode(A)
ans=
0 0 3
// Rank of matrix
octave:4>rank(A)
ans= 3
octave:5>A*A
ans=
9 12  24
24 33 60
0  0  9 
octave:6> B=[5;1;9;]
ans=
5
1
9
octave:7>A*B 
ans =
34
79
27
octave:8>A+B
ans=
6 7 8
5 6 7
9 9 12
octave:9>ones(3,3)
1 0 0
0 1 0
0 0 1
octave:10>zeros(2,3)
0 0 0
0 0 0
octave:11>eye(3,4)
1 0 0 0
0 1 0 0
0 0 1 0
//combining matrix
octave:12>x=[2 3 4;5 6 7;]
ans=
2 3 4
5 6 7
octave:13>y=[1 0 4;4 9 0;]
ans=
1 0 4
4 9 0
octave:14>z=[x,y]
z =
2 3 4 1 0 4
5 6 7 4 9 0

Creating Vectors in octave

octave:1>V=[9 0 4 2 3 7 ]
ans = 
9 0 4 2 3 7
octave:3>max(V)
ans = 9
octave:4>min(V)
ans = 0
octave:5>VV=1:8     //another method to declare vector
ans =
1 2 3 4 5 6 7 8 

Basic Functions

octave:1>a=2.3;
octave:2>ceil(a)
ans = 3
octave:3>floor(a)
ans = 2

2. Plotting data

  • point plot
  • scatter
  • histogram
  • pie
  • 3D mesh
  • contours
// plot single point
octave:1>plot(50,100)
octave:2>plot(50,100, 's')    // 's' plot small square instead of plotting point. 

// plot series of points
octave:3> x = 0:20;
octave:4> x = x.^2;
octave:5>plot(x);

//scatter shows small circles on plotted line.
octave:6>y=0:20;
octave:7>scatter(x,y);

//histogram 
octave:8>a=randn(2000,1);
octave:9>hist(a);

//pie chart
octave:10>a=1:6;
octave:11>pie(a);
octave:12>e=[1 0 1 0 1 1]  //here 1 cuts slice from pie
octave:13>pie(a,e);

//3D mesh
octave:14>x=-10:10;
octave:15>y=x;
octave:16>[x,y]=meshgrid(x,y);
octave:17>x.^2+y.^2;
octave:18>z
octave:19>mesh(x,y,z)   //create mesh using values of 3 variables
octave:20>meshc(x,y,z)  // show shadow of mesh
octave:21>surf(x,y,z)   // fill color in mesh

3. Programs in octave terminal

octave:1>marks=60;
 >if(marks>70)
 > disp("student passed with first division");
 > elif(marks>50)
 > disp("passed with 1st division");
 > elif(marks>40)
 > disp("passed in 3rd division");
 > else
 > disp("student fail");
 > end; //after end; it will show output on terminal.

Similarly you can create another prograns according to your need. Input/Output in files
are shown in previous octave post.

4. Set theory in octave

octave:1> A=[5 6 3 1];
octave:2> B=[9 5 2 7]; 
octave:3>intersect(A,B)
ans=5 
octave:4>union(A,B)
ans= 1 2 3 5 6 7 9 
octave:5>setdiff(A,B) //show only setA elements,But B's(commom)elements are cut from A.
ans= 1 3 6 1

// Run octave program through launching octave on prompt( .m files)

1. Type your octave code in text file .

$ vi transpose.m

2. Type following code in it.

A=[1 2 3 4 ; 4 7 7 4; 3 6 7 0]
A

3. Run transpose.m using below command.

$ octave transpose.m

4. output:

A= 
 1   2   3   4
 4   7   7   4
 3   6   7   0 
A = 
1   2   3   4
4   7   7   4
3   6   7   0

// Run octave program without launching octave (mkoctfile)

You can run octave program on command prompt. But without launching octave on prompt. For this, you have compile your octave, c++ code using mkoctfile.The mkoctfile function compiles source code written in C, C++, or Fortran. Depending on the options used with mkoctfile, the compiled code can be called within Octave or can be used as a stand-alone application.

1. Type code in  octdemo.cc file.

$ vi  octdemo.cc

2. Type below code in file

octdemo.cc

#include<iostream>
#include<octave/oct.h>
using namespace std;
int main()
{
  cout<<"hello octave";
  cout<<"\n"
  int  n=2;
  Matrix mat=Matrix (n,n);
   for(octave_idx_type i=0;i<n;i++)
     for(octave_idx_type j=0;j<n; j++)
        mat(i,j)=(i+j)* 10+(j+1);
   cout<<mat;
   return 0;
}

3. Run on command prompt, But don’t launch octave

$ mkoctfile --link-stand-alone octdemo.cc -o octdemo //-o create outputfile.
./octdemo

4. Done

// Run octave through CGI

you can  run octave code on CGI means your local server Apache2. For this, you must have install Apache2 and configure it with CGI . Follow july 17,2015 post to configure apache2 with CGI. After this follow following steps:

1. Firstly , goto  /public_html/cgi-bin folder of Apache2 through command prompt. Create file in it.

$ vi octavecgi.cc

2. Type octave and c++ code in octavecgi.cc

octavecgi.cc
#include<iostream>
#include<octave/oct.h>
using namespace std;
int main()
{
  cout<<"hello octave";
  cout<<"\n"
  int  n=2;
  Matrix mat=Matrix (n,n);
   for(octave_idx_type i=0;i<n;i++)
     for(octave_idx_type j=0;j<n; j++)
        mat(i,j)=(i+j)* 10+(j+1);
   cout<<mat;
   return 0;
}

3. compile octavecgi.cc and run it.

$ mkoctfile --link-stand-alone octavecgi.cc -o octavecgi //-o create outputfile.
./octdemo

4. Create another script file in /public_html/cgi-bin

octscript.sh

#!/bin/bash  //location of bash script.To find, type $ which bash
echo Content-type: text/html
echo "<pre>"
./octavecgi
echo "</pre>"

5. change permissions of file.

$ chmod  755 /public_html/cgi_bin/octscript.sh

6. Run script file on browser

localhost/cgi-bin/octscript.sh
output:
hello octave cgi 1 12
11 22

6. Done

7. If NOT FOUND error is found while running script file on browser. Then check permissions of each folder where your file is placed. Permissions  must be  drwxr-xr-x for /public_html folder and /cgi-bin folder and  -rwxr-xr-x for octscript .sh file.

manpreet@hp:~/OCTAVE$ octave –persist demograph.m (to show graph through m file)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s