Octave: A Free, High-Level Language for Mathematics

A quick look at a language designed to manipulate matrices and provide other numerical functions.

For numerical computing, high level
languages offer advantages over more traditional languages, such as
FORTRAN or C. Built-in graphics capabilities, automatic variable
typing and flexible data structures combine to provide an
environment in which it is easy to develop your ideas without
having to fight with the language. That's not to say that FORTRAN
and C are of no use, just that sometimes you want to make life a
bit easier.

Matlab is a one such language. It is available on many
platforms (including Linux) and provides powerful facilities for
manipulating matrices, as well as other numerical functions.
Unfortunately, Matlab is commercial software and wasn't available
for Linux until recently (in the last twelve months or so).
However, there are other, freely-available alternatives, and Octave
is one such alternative.

Superficially, Octave looks very much like Matlab, and the
description in its LSM entry reads “GNU Matlab—A numerical matrix
mathematics program.” To begin, type octave at
the shell prompt, and Octave greets you with its own prompt. Now we
can start doing math.

Matrix Operations

As you might expect, entering and manipulating matrices is
one of Octave's strengths. (In order to differentiate Octave
commands from the output, the prompt
octave:number of the
command precedes the commands in the
examples below.) We can enter a matrix with the command:

octave:1 a=[1 2 ; 3 4]

Octave then reports the result of the command, namely:

a =
1 2
3 4

To suppress output, simply place a semicolon after the command.
Note that we didn't have to worry about declaring the size or type
of the matrix a, we just type it in and start
working with it. For example, we can get the transpose of
a by typing:

octave:2 a'
ans =
1 3
2 4

Arithmetic operators such as +,
- and * act as matrix
operators unless they are preceded by a period, in which case they
act in an element by element sense. Octave also provides the
forward and backward slash operators that perform matrix right and
left division. Again, these can be used in an element-wise fashion.
So, given another matrix:

octave:3 b=[1 0; 3 2]
b =
1 0
3 2

we can find the matrix product of a and
b with the command:

octave:4 a*b
ans =
7 4
15 8

Or define the (i,j)th entry of the product is
the product of the (i,j) entries in
a and b with the command:

octave:5 a.*b
ans =
1 0
9 8

Matrix elements can easily be selected by index, so to get the
(1,1) entry of a, type:

octave:6 a(1,1)
ans=1

We can select a row or column using the colon operator, exactly as
in Matlab. Thus, to select the first column of a
type the command:

octave:7 a(:,1)
ans =
1
3

And to select the first row of a type:

octave:8 a(1,:)
ans =
1 2

As well as these elementary operations, Octave provides functions
that perform higher-level operations, such as finding the
eigenvalues of a matrix, by using a command like the following:

octave:9 eig(a)
ans =
5.37228
-0.37228

Alternatively, you can find the eigenvalues and eigenvectors by
giving the following command:

This is a demonstration of one of the main advantages of using a
high level language like Octave. Writing a FORTRAN or C program to
find the eigenvalues of a matrix would take a lot more time and
effort than it does in Octave. Moreover, Octave routines are
usually based on well-known, high quality algorithms, so you can
have faith in the results.

Octave provides many other matrix routines, which are
detailed in the manual and in the on-line help system.

User-Defined Functions

Octave also lets the user define his own functions via the
function keyword. A function definition looks
like this:

The output came from the statement x+y--if we
had ended the line with a semicolon, there wouldn't have been any
output from the add command. To assign the
output to a variable, define the function in this way:

octave:15 function sum=add(x,y)
sum=x+y;
endfunction

Now, if we type add(1,2), we get exactly the
same result as before. However, by defining an output variable, we
can assign the result of the add function to a
variable in this way:

octave:16 fred=add(1,2)
fred=3

A very powerful feature of Octave is the ability to return multiple
values from a function. This feature exists in Matlab, but not in
FORTRAN. For example, the following function:

Functions can be defined at the keyboard, as we did in these
examples, or stored in a file and used again. This lets you build a
suite of routines for whatever tasks you want. All you have to do
is put the files (identified with a .m suffix)
somewhere where Octave can find them. The built-in variable
LOADPATH specifies where Octave should look for
the .m files. Many of Octave's standard
functions are defined in .m files. You can also
access user-supplied C++ routines within Octave, although this
feature is not yet fully developed.

Octave also provides a full programming language, with flow
control and looping constructs, as well as extensive input-output
facilities. It is possible to write quite sophisticated programs in
Octave, and development time is considerably shorter than you would
expect using FORTRAN or C.

Comment viewing options

Well, the post is actually the freshest topic on this registry related issue. jasmin live I fit in with your conclusions and will thirstily look forward to your coming updates. Just saying thanks will not just be sufficient, for the phenomenal lucidity in your writing.cameraboys.com I will right away grab your rss feed to stay abreast of any updates.

I wish our university used this as well as I have become used to it. The interface on Octave is a lot easier to use in my opinion. Matlab will just have to do sometimes though. college grants for students

the best part of octave is it can parse matlab code , so it's very useful for students like me .however, matlab can not parse octave code .
another thing,you can type single quotes and double quotes in octave which like we know that's impossible with matlab(accept only singles quotes).
these are two big difference for me so I choose Octave over Matlab .Orthopaedic Shoe Repairs

Thank you for taking the chance to deliver this affair. I’m pleased I discovered your site on this material. I’m doing research on this interest right now and this was great. Keep up the great work.I’m doing research on this interest right now and this was great. Keep up the great work !!!!!!!!koyun oyunları

There is difference between Matlab and Octave. Octave is open source whereas Matlab is proprietary. Matlab surly is more comfortable working whereas Octave is free, that's why I love to work with Octave.Sam

Yeah, I wish we had this too in our university. I imagine calculating mathematical equations in just pressing some keys in the keyboard and not solving those problems with very long solutions by hands.computer repairs in new york

Thanks for the information octave I heard about this first time and I don have much knowledge about it, now you article has given me all the details to understand the basics about it will learn more thanks.

Wow!I am pretty new to learn about this but would like to know which one in the end is the way to go?Is it Matlab or octave?I've read that Matlab can be more powerful, is that correct?Musikproduzent im Tonstudio

I have been learning computer languages but never even heard about Octave. May be because it is old now and isnt used much but it is always interesting to know more. I will be digging in deep to find more information on this. electric meat slicer

I see about the Visual Basic notation like if .. else .. end, but Octave, like Matlab uses C oriented notations also, for example a ! in Octave is a NOT, as it is in C, instead of <> in VB. Also you can use all the C based = symbols like: `+=', `-=', `*=', `/=', `^=', `.*=', `./=', and `.^='.

From what I'm getting, James B. Rawlings and John W. Eaton at the University of Texas were the first to start development of Octave, clear back in 1988. In '92 James Rawlings started development full time. How does open source software put food on the table. I'm still puzzled.

This is my first introduction to either Matlab or Octave. The format almost has a Visual Basic feel to it; not like C at all. Then again, maybe just the function definitions. For me, the advantage would be to do server side math, thereby not relying on flaky client side Javascript, which is anything but precision.

Octave is no doubt an equally impressive piece of software and just as functional as Matlab. I've used both and have no preference either way as they both work in very similar manners. There is no doubt that Octave is a great alternative to Matlab and gets the job done just as well. backpacking Europe

I am starting to learn about this but would like to know that this will be used for quite some time down the road. I see this is dated 1997, has there been any changes to this, or is there any news of switching to a different system? custom t-shirts

I see this is dated 1997, has there been any changes to this, or is there any news of switching to a different system? I am starting to learn about this but would like to know that this will be used for quite some time down the road. Outdoor Pool Furniture

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else! Colorado Personal Injury

You don't know me and I live way too far away... but my name is Peter. I'm doing an undergrad I.T. degree and this semester I'm doing a subject on Pattern Recognition (eventually using Neural Networks). The subject demands use of Matlab and for some reason, our I.T. faculty seems to be fairly poor at supporting anything Linux (except for network admin and programming based subjects). iphone headphones

the best part of octave is it can parse matlab code , so it's very useful for students like me .however, matlab can not parse octave code .
another thing,you can type single quotes and double quotes in octave which like we know that's impossible with matlab(accept only singles quotes).
these are two big difference for me so I choose Octave over Matlab .router wireless reviews

Yeah, I wish we had this too in our university. I imagine calculating mathematical equations in just pressing some keys in the keyboard and not solving those problems with very long solutions by hands.

Thank you for such a fantastic blog. Where else could anyone get that kind of info written in such a perfect way? I have a presentation that I am presently working on, and I have been on the look out for such information. Local Marketing

I like how it provides a convenient command line interface for solving linear and nonlinear problems numerically! Also great features are: Matrices as fundamental data type and Built-in support for complex numbers.