# Octave: A Free, High-Level Language for Mathematics

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.

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 4To 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 4Arithmetic 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 2we can find the matrix product of

**a**and

**b**with the command:

octave:4 a*b ans = 7 4 15 8Or 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 8Matrix elements can easily be selected by index, so to get the (1,1) entry of

**a**, type:

octave:6 a(1,1) ans=1We 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 3And to select the first row of

**a**type:

octave:8 a(1,:) ans = 1 2As 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.37228Alternatively, you can find the eigenvalues and eigenvectors by giving the following command:

octave:10 [v,d]=eig(a) v = 0.41597 -0.82456 0.90938 0.56577 d = 5.37228 0.00000 0.00000 -0.37228This 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.

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

function [output values] = name (input values) sequence of commands endfunction

The input and output values are optional, so it is possible to write a function that takes no arguments and returns no values, such as

octave:11 function hello printf("hello\n") endfunctionThe

**printf**statement prints the quoted string to the screen, and the \n is interpreted as a newline. Invoke the function by typing its name:

octave:12 hello helloObviously, functions that don't take arguments or return values aren't all that useful. To accept arguments, list them after the function name in the following manner:

octave:13 function add(x,y) x+y endfunction octave:14 add(1,2) ans = 3The 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; endfunctionNow, 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=3A 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:

octave:17 function [sum,diff]=sumdiff(x,y) sum=x+y; diff=x-y; endfunctionreturns multiple values when invoked as:

octave:18 ns,d]=sumdiff(1,2) s = 3 d = -1Functions 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.

## Comments

## For those of you who had a

For those of you who had a problem with finding good software for your mathematical problems, here is your answer in a platter! The Octave is designed to solve your problems and give you the solutions to any questions you may have! I have used it for my facebook analytics and it just worked fine. Unlike the Matlab which is a commercial software and was unavailable till recently, Octave is readily available and has built-in graphics so it will allow you to give you flexibility in your data structures to help you with your ideas without having to be confused with the language!

## Re: Octave: A Free, High-Level Language for Mathematics

Hi: I

## Re: Octave: A Free, High-Level Language for Mathematics

Why yes you can. m files work just fine with octave.

## I think you've made some

I think you've made some truly interesting points. Not too many people would actually think about this the way you just did. I'm really impressed that there's so much about this subject that's been uncovered and you did it so well, with so much class. Good one you, man! Really great stuff here. Colorado Personal Injury

## Great Post.

Hi,

Would you mind if I reposted this article on my own programming blog? It's very useful and I think my readers could defo benefit from it.

Ebay Template

## Great

This is a good posting, I was wondering if I could use this write-up on my website, I will link it back to your website though. If this is a problem please let me know and I will take it down right away Roofing Denver

## Thank you for taking the

Thank you for taking the chance to write related to maths it is my best topics ever discuss. 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 !!!!!!!!

Home automation

## Nice blog having nice

Nice blog having nice information. some times we ignore this sort of things & also suffer a lot as well. However we can save a lot with the assistance of these tips for example time !!!!

Kidney stones treatment | Dui attorneys

## Re: Octave: A Free, High-Level Language for Mathematics

If Matlab compatiability is your aim you should definitely also install the octave-forge package available at http://octave.sourceforge.net. This contains

many additional functions for compatiability to Matlab (eg signal processing, sparse matrix, comms, etc).

Regards

David

## Re: Octave: A Free, High-Level Language for Mathematics

Hi... I tried to mail this straight to the address given, but Mr Murphy seems to have moved on... so I'll just post here instead...

Hello Mr Murphy,

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).

Thanks to your article, I'm up and running with Octave about five minutes after downloading and installing. Basically, I just wanted to thank you for a well-written, explanatory article with useful beginner's examples in it. This has saved me some time that could be far better used getting up and running with the subject content instead.

Just wanted to let you know that your work has NOT gone unappreciated. Again, thank you.

P.Petroff