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
an easy way to test programs before writing them in more advanced and
complicated computer languages.
a fast way to develop small models without fighting with bugs that
arise from such issues as memory allocation.
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.
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.
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 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 :
Efficient utility functions : diff, ones,
zeros, eye, any, eig, linspace, logspace, real, imag, find and many, many
more ...
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.
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.
Logical operations :
&,|,&&,||,==,~=,true,false
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.