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

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")
endfunction
The 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
   hello
Obviously, 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 = 3
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:
octave:17 function [sum,diff]=sumdiff(x,y)
   sum=x+y;
   diff=x-y;
endfunction
returns multiple values when invoked as:
octave:18 ns,d]=sumdiff(1,2)
   s = 3
   d = -1
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.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

nice thoughts

john stevenson's picture

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

James F's picture

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

Matlab or octave

Brandschutztüren's picture

Where else could anyone get that kind of info written in such a perfect way?
Matlab or octave

best regards

my answer

andriabolton2009's picture

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

Pramod's picture

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ı

@windows version

Ankit Sharma's picture

Octave can be used on windows using cygwin.

Very nice site. Spray

Tim's picture

Very nice site.
Spray Insulation

Didn't know that

Sean Trent's picture

Didn't know you could use it with windows. I'll check cygwin further. Thanks. S.T. repossessed cars insider tips
repossessed cars basics

Great article. It's hard to

Flash Developer's picture

Great article. It's hard to say which one is the best.

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

Anonymous's picture

Nice explanation, thanks. Business Marketing

Fortran is my first programming language

Warn Winch's picture

When I was young, Fortran is my first programming language. Then I learned Matlab and Octave.. but I think Matlab is easier than Octave. thanks, Pampers Diapers

disagree

Anonymous's picture

Have to disagree Octave is far more intuitive. curtains and blinds

Have used both, Octave and

online live's picture

Have used both, Octave and Matlab, but i prefer the latter, though i don't use matrixes that often. online sex cam live chat

Thanks to your article, you

Anonymous's picture

Thanks to your article, you don't know me and I live way too far away...
Why yes you can. m files work just fine with octave.

Übersetzung Spanisch Deutsch
Übersetzer Übersetzer Engisch Deutsch

Matlab's return is promising

Samt's picture

Matlab's return is promising it will help in Computer Aided Designing
Devs

There is difference between

Samt's picture

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

user friendly

ohio's picture

which one that give you more user friendly experience?
how to get pregnant

Doesn't this cause a conflict?

Sebski's picture

Great article but one quick question...?

"The input an' output values be optional, so it is possible t' write a function that takes no arguments an' returns no values, such as

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

How does this not result in conflict when compiling?

Mediation Jobs

What is my Love?

Anonymous's picture

My heart is my love
My love is my family
My family is my future
My future is my destiny
My destiny is my ambition
My ambition is my inspiration
My inspiration is my motivation
My motivation is my belief
My belief is my peace
My peace is my target
My target is heaven
My heaven is my home
My home is my eternity
My eternity is my judgment from my creator

If Tomorrow Never Comes
If I knew it would be the last time that I see you walking out of the door, I would have given you a hug and kiss and even call you back for one more.

If I knew it would be the last time I will hear your voice, I would have video taped each action and word of yours, so I could play them back day after day.

If I knew it would be the last time we would meet, I could have spared an extra minute or two to stop and say "I love you,"

If I knew it would be the last time I would be there to share your day, I could not have allowed this to slip away.

For surely there's always tomorrow to make up for an oversight, and we always get a second chance to make everything right.

The more we are still on earth living there will be always tomorrow

Send Free Texts

Send Free Texts to say good morning, good afternoon, good night, and toy I LOVE YOU

Send Free Texts Gtalk Unlimited India Calling

Octave: A Free, High-Level Language for Mathematics

nigelbiggs's picture

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

Octave: A Free, High-Level Language for Mathematics

Pehari's picture

Nice article.

First time I heard about this

Muzamil's picture

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.

???????? ?????: ???????? ???

peter murphy's picture

???????? ?????: ???????? ??? ???? ? ????????? ???????? ?? ? ???? ???????: ??? ????? ???? ????? ???? ??????? ?????????.
play blackjack safe online casino online bonus slots

thank you

Anonymous's picture

thnaks for shared ßß Our website is: http://www.cilekerotik.com

Wow...

Musikproduzent's picture

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

Nancy's picture

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

That's all we need :)

seo packages's picture

That's all we need, another high-level Maths language. I find C difficult enough to deal with. My Dad was right, should have done law rather than engineering :)

which one?

Travis Elliott's picture

So in the end which one is the way to go? Matlab or octave? Both have benefits
Hearing Loss Treatment

I agree with David

Anonymous's picture

I totally agree with David that it's almost mandatory that you also install the octave-forge package...

Backlink Software

Octave is great

Mathew Trujillo's picture

"I'd say there's a *big* difference between Matlab and Octave. Octave is Open Source, that means it's free as opposed to Matlab which you have to pay for. Very powerful."

Absolutely agree with this. I love Matlab but the flexibility that I have with Octave is awesome.

article submission service

Octave VS Matlab - Big Difference

Frank Bosco's picture

I'd say there's a *big* difference between Matlab and Octave. Octave is Open Source, that means it's free as opposed to Matlab which you have to pay for. Very powerful.

Coding Style

Terry Galarneau's picture

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 `.^='.

Octave Origins

Terry Galarneau's picture

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.

Almost VB Style

Rick Cole's picture

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

Jacob's picture

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

Newest Octave Release

Fran Callahan's picture

As of August 1, 2010 the newest version 3.3.52 is available in BETA for people interested in helping test. Be sure to download it and give it a spin: Octave Downloads.

Windows Version

Jason Becker's picture

MatLab is indeed a great high level program (language?) for doing precision math. It's available for Windows or Linux. I didn't see if Octave is also available for both.

mathlab is still better

johnnycwn's picture

I have used both programs and I think mathlab is still much than octave.
adjustable dumbbells

I am starting to learn about

John Sweeney's picture

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

Changes

John Palmer's picture

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

sipu1234's picture

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

Boota's picture

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

Matlab OR Octave

Jean.B's picture

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

I tried Octave

Router compare's picture

I tried Octave once , it's unbelievable .
very very useful for people who use Matrix (resolve Jordan and Gauss problem easily)

I wish our university had

Jerry Bloomberg's picture

I wish our university had Octave installed. But most machines in Ottawa are Windows.

I wish we had it too

Rikki's picture

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.

Rikki
Link Building Service

Great

kiran1234's picture

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

You have a lot of resources

betito's picture

You have many resources on this page.
I always consult linuxjournal so many things about my job, and my school.

If you need more info, send me an email.
Betito
Link Building Service

What I like!

glass top dining table's picture

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.

Thank you!

jayeola's picture

I used Matlab exclusively in my MSc. never knew that there was a Gnu'able version. Installed from my GNU/Linux's distrobution and taking it for a spin. Thanks :-)

jayeola

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState