A Brief Introduction to Octave


Overview

Octave is a high-level language, primarily intended for numerical computations. It provides a convenient command line interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments. It may also be used as a batch-oriented language.

Language Features

The best way to introduce Octave's language is probably to show a few simple examples.

If you are new to Octave, I recommend that you try these examples to begin learning Octave by using it. Lines marked with octave:13> are lines you type, ending each with a carriage return. Octave will respond with an answer, or by displaying a graph.

Creating a Matrix

To create a new matrix and store it in a variable so that it you can refer to it later, type the command

    octave:1> a = [ 1, 1, 2; 3, 5, 8; 13, 21, 34 ]
Octave will respond by printing the matrix in neatly aligned columns. Ending a command with a semicolon tells Octave to not print the result of a command. For example
    octave:2> b = rand (3, 2);
will create a 3 row, 2 column matrix with each element set to a random value between zero and one.

To display the value of any variable, simply type the name of the variable. For example, to display the value stored in the matrix b, type the command

    octave:3> b

Matrix Arithmetic

Octave has a convenient operator notation for performing matrix arithmetic. For example, to multiply the matrix a by a scalar value, type the command

    octave:4> 2 * a

To multiply the two matrices a and b, type the command

    octave:5> a * b

To form the matrix product transpose (a) * a, type the command

    octave:6> a' * a

Solving Linear Equations

To solve the set of linear equations Ax = b use the left division operator, \:

    octave:7> a \ b
This is conceptually equivalent to inv (A) * b, but avoids computing the inverse of a matrix directly.

If the coefficient matrix is singular, Octave will print a warning message and compute a minimum norm solution.

Integrating Differential Equations

Octave has built-in functions for solving nonlinear differential equations of the form

    dx
    -- = f (x, t)
    dt
with the initial condition
    x(t = t0) = x0

For Octave to integrate equations of this form, you must first provide a definition of the function f (x, t). This is straightforward, and may be accomplished by entering the function body directly on the command line. For example, the following commands define the right hand side function for an interesting pair of nonlinear differential equations. Note that while you are entering a function, Octave responds with a different prompt, to indicate that it is waiting for you to complete your input.

    octave:8> function xdot = f (x, t) 
    >
    >  r = 0.25;
    >  k = 1.4;
    >  a = 1.5;
    >  b = 0.16;
    >  c = 0.9;
    >  d = 0.8;
    >
    >  xdot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1));
    >  xdot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2);
    >
    > endfunction

Given the initial condition

    x0 = [1; 2];
and the set of output times as a column vector (note that the first output time corresponds to the initial condition given above)
    t = linspace (0, 50, 200)';
it is easy to integrate the set of differential equations:
    x = lsode ("f", x0, t);

The function lsode uses the Livermore Solver for Ordinary Differential Equations, described in A. C. Hindmarsh, ODEPACK, a Systematized Collection of ODE Solvers, in: Scientific Computing, R. S. Stepleman et al. (Eds.), North-Holland, Amsterdam, 1983, pages 55-64.

Producing Graphical Output

To display the solution of the previous example graphically, use the command

    plot (t, x)

If you are using the X Window System, Octave will automatically create a separate window to display the plot. If you are using a terminal that supports some other graphics commands, you will need to tell Octave what kind of terminal you have. Type the command

    gset term
to see a list of the supported terminal types. Octave uses gnuplot to display graphics, and can display graphics on any terminal that is supported by gnuplot.

To capture the output of the plot command in a file rather than sending the output directly to your terminal, you can use a set of commands like this

    gset term postscript
    gset output "foo.ps"
    replot
This will work for other types of output devices as well. Octave's gset command is really just piped to the gnuplot subprocess, so that once you have a plot on the screen that you like, you should be able to do something like this to create an output file suitable for your graphics printer.

Or, you can eliminate the intermediate file by using commands like this

    gset term postscript
    gset output "|lpr -Pname_of_your_graphics_printer"
    replot

Editing What You Have Typed

At the Octave prompt, you can recall, edit, and reissue previous commands using Emacs- or vi-style editing commands. The default keybindings use Emacs-style commands.

Getting Help

Octave has an extensive help facility. The same documentation that is available in printed form is also available from the Octave prompt, because both forms of the documentation are created from the same input file.

In order to get good help you first need to know the name of the command that you want to use. This name of the function may not always be obvious, but a good place to start is to just type help. This will show you all the operators, reserved words, functions, built-in variables, and function files. You can then get more help on anything that is listed by simply including the name as an argument to help. For example,

    help plot
will display the help text for the plot function.

The complete text of the manual is availabe from Octave's command line using the command help -i. Because it is written in Texinfo, it is also possible to put the manual on the WWW.

Distribution Terms

Octave is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.

Octave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file COPYING for more details.

Availability

The latest released version of Octave is always available via anonymous ftp from ftp://ftp.che.wisc.edu/pub/octave. Complete source and binaries for several popular systems are available.

Installation and Bugs

Octave requires approximately 125MB of disk storage to unpack and compile from source (significantly less if you don't compile with debugging symbols or create shared libraries). Once installed, Octave requires approximately 65MB of disk space (again, considerably less if you don't build shared libraries or the binaries and libraries do not include debugging symbols).

In order to build Octave, you will need a current version of g++, libstdc++, and GNU make.

You must have GNU Make to compile Octave. Octave's Makefiles use features of GNU Make that are not present in other versions of make. GNU Make is very portable and easy to install.

See the notes in the files INSTALL and INSTALL.OCTAVE for more specific installation instructions, including directions for installing Octave from a binary distribution.

The file BUGS contains a recommended procedure for reporting bugs, as well as a list of known problems and possible fixes.

Documentation

Octave's manual has been revised for version 2.0, but it is lagging a bit behind the development of the software. In particular, there is currently no complete documentation of the C++ class libraries or the support for dynamic linking and user-defined data types. If you notice ommissions or inconsistencies, please report them as bugs to bug-octave@bevo.che.wisc.edu. Specific suggestions for ways to improve Octave and its documentation are always welcome.

Implementation

Octave is being developed with the Free Software Foundation's make, bison (a replacement for YACC), flex (a replacement for lex), gcc/g++, and libstdc++ on an Intel Pentium II system running Linux/GNU. It should be possible to install it on any machine that runs GCC/G++. It may also be possible to install it using other implementations of these tools, but it will most certainly require much more work. Do yourself a favor and get the GNU development tools, either via anonymous ftp from ftp.gnu.org or by writing the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

The underlying numerical solvers are currently standard Fortran ones like Lapack, Linpack, Odepack, the Blas, etc., packaged in a library of C++ classes (see the files in the libcruft and liboctave subdirectories). If possible, the Fortran subroutines are compiled with the system's Fortran compiler, and called directly from the C++ functions. If that's not possible, they are translated with f2c and compiled with a C compiler. Better performance is usually achieved if the intermediate translation to C is avoided.

The library of C++ classes may also be useful by itself.


Back to the Octave home page.


John W. Eaton
jwe@bevo.che.wisc.edu
University of Wisconsin
Department of Chemical Engineering
Madison WI 53719