Short Introduction to Octave

Octave is a high-level computer language that makes computing easy if your priority is not speed. It is, to a large extent, the open-source version of Matlab and one which I find very useful especially from a pedagogical point of view. For a programmer writing scientific code, Octave provides Octave comes with most distributions of Linux. To start it, all you have to do is go to your command line and type "octave". Commands in Octave are usually intuitive and straightforward.

Example :

  octave:1> x=1/sqrt(3);
  octave:2> y=atan(x)*180/pi

The above example is a simple trigonometric operation. In the first line, a numeric value is assigned to a variable x. In the second line, the arctan of x is calculated and assigned to y, at the same time being converted to degrees(angles are in radians in Octave). The semicolon (;) at the end of the first line suppresses the output while leaving it out causes the output to be displayed on screen, as done in the second line.

Octave can work with arrays as easily as it does with single numbers.

Example :

  octave:1> x=0:0.01:pi;
  octave:2> y=sin(x);
  octave:3> plot(x,y)

Here the first line creates an equally-spaced array that goes from 0 to pi in increments of 0.01. The second line evaluates the sine of all the numbers in the array x and assigns to a new array variable y. Finally, the third line draws a plot of y with respect to x. For plotting, Octave calls another open source program, gnuplot.

In fact, you can do algebra even with matrices :

Example :

  octave:1> M=rand(3,3);
  octave:2> M=M+M';
  octave:3> M*=2;
  octave:4> M=M.^3;
  octave:5> [a,b]=eig(M);

In this example, the rand function creates a 3-by-3 matrix of uniformly distributed random numbers. In the second line, the prime (') operator takes the transpose of the matrix. This is then added to the matrix itself to symmetrize it. The result is then re-assigned to the same variable. In the third line, the entire matrix is simply multiplied by 2 whereas in the fourth line, each element of the matrix is raised to the third power. The dot after the matrix denotes a so-called point-by-point operation where the operator is applied to every single element of the matrix rather than the matrix as a whole. This can also be done with division of one matrix by another. In the final line, the eigenvalues and eigenvectors of the matrix are calculated.

Apart from the obvious algebra, arithmetic and trigonometry operations for numbers, vectors and matrices, Octave also offers the following useful features :
  1. Efficient utility functions : diff, ones, zeros, eye, any, eig, linspace, logspace, real, imag, find and many, many more ...
  2. Polynomial manipulation : polyval, polyder, polyfit
  3. Plotting : plot, mesh, contour, bar, hist
  4. Loops and control expressions : while, for, if, if-elseif-else
    NOTE that loops are extremely slow in Octave. Avoid using loops as much as possible. Instead make use of functions that are specially optimized. You can test the inefficiency of loops by using the tic/toc functions for timing.

    Example :

      octave:1> N=1000000;
      octave:2> a=randn(N,1);
      octave:3> b=zeros(N,1);
      octave:4> tic;for n=2:N
      > b(n)=a(n)-a(n-1);
      > endfor;toc
      ans = 33.922
      octave:5> tic;c=diff(a);toc
      ans = 0.38571

    In the above example, carrying out the same operation (calculating the difference between successive elements of an array) takes about 34 seconds using a loop while with the special diff function, it only takes a fraction of a second. Of course, the numbers are system-dependent and the difference is negligible for small arrays.
  5. Logical operations : &,|,&&,||,==,~=,true,false
  6. User-defined functions :

    Example :

      octave:1> function out=iseven(in)
      > if ( rem(in,2)==0 )
      > out=true;
      > else
      > out=false;
      > endif
      > endfunction
      octave:2> a=iseven(3)
      a = 0
      octave:3> a=iseven(4)
      a = 1

    If you find yourself needing the same function over and over again, you can also place it in a file with a ".m" extension and call it with the same syntax as in the above example, for which the file would be called "iseven.m". Functions may return any number of values, which can be of mixed type.
Useful Octave pages

You can read more about Octave on the following pages.