1 Octave (Matlab clone)¶
1.1 Introduction¶
The Octave software claims to be mostly
compatible with Matlab; and this seems an accurate statement.
1.2 Installation¶
1.2.1 MS Windows¶
For MS Windows, download the latest version from http://sourceforge.net/project/showfiles.php?group_id=2888&package_id=40078. The latest version in January 2009 was 3.0.3, so you would download and execute octave-3.0.3-setup.exe. This is an executable file which will install Octave on your machine. You need to answer a few questions:
choose components -> stick with suggested default
graphics backend selection -> choose GnuPlot (JHandles doesn’t support xlabel, title and other commands yet)
choose install location -> stick with default
choose start menu folder -> stick with default
- last tested:
January 2009
1.2.2 Mac OS X¶
Basic octave installation on Mac OS X:
Starting from http://www.gnu.org/software/octave/download.html, scroll down to the Mac OS X section.
Click on ‘Octave Forge’ and then on the link labelled ‘Octave.app for Mac OS X’.
Assuming you have an Intel Mac, then you should select octave-3.0.3-i386.dmg (which points to http://downloads.sourceforge.net/octave/octave-3.0.3-i386.dmg?use_mirror=switch) (The particular version number may change, this is up-to-date as of March 2009.)
This will give you the right file. Make sure you read the ‘readme’ which suggest to install GNUPLOT which is already included in the disk image that you have downloaded.
This file is disk image, from which (once you have mounted them by double clicking on the dmg file) you can drag the octave (and Gnuplot) application into the application folder.
1.2.3 Linux/Unix¶
One can either download the source files (ending in .tar.gz or .tar.bz2) and compile from those (if you know how to do that). However, typically the package management for the linux distribution you are using does provides the octave software.
1.3 How do I start Octave?¶
On the University machines, the software should be located in Start->All Programs->Your School Software->Engineering Sciences->Octave.
If you only get a prompt (i.e. a mostly blank window with a blinking prompt), then type octave and press return. This will start octave.
1.4 Octave/Matlab compatibility issues¶
This section lists situations/commands where Octave behaves significantly different from Matlab and where this is relevant for the learning and teaching of Matlab at the School of Engineering Sciences.
Students are invited to report incompatibility issues not listed here (by email to fangohr@soton.ac.uk).
1.4.1 General observations¶
1.4.1.1 Windows¶
I have done a few tests (creation of vectors, plotting of x and
against y-vector, check that ode45 command exists) with version
3.0 on MS Windows, and Octave seems to behave exactly like Matlab
(although the ouput displayed on screen sometimes has a slightly different
format).
Hints:
to close a figure window, just press q (when the focus is on this window)
1.4.1.2 Mac OS X¶
The version I have tried on a Mac OS X system (although having the
same version number 3.0.0) did by default not have the octave package
odepkg installed and did therefore not know about the ode45
command (but has alternative commands such as lsode that we can use
instead). All octave packages can be downloaded from
http://octave.sourceforge.net/packages.html and installed with pkg
install FILENAME (see octave web pages for further instructions).
1.4.1.3 Linux/Unix¶
There is of course octave for Linux (originally, Octave was developed on Unix/Linux and only later ported to Windows and Mac OS X).
1.4.2 ODE45 - function handle¶
The ode45 function in Matlab can be called like this:
[x,y]=ode45('rhs',[0,2],1);
or like this:
[x,y]=ode45(@rhs,[0,2],1);
The first version passes the function name rhs as a string to the
ode45 function, the other one as a handle.
Octave only supports the second option, so in Octave the corresponding command has to read:
[x,y]=ode45(@rhs,[0,2],1);
1.4.3 ODE45 - default accuracy¶
Matlab’s default settings for allowed absolute and relative error tolerances (and related to that step size in the integration) differ from Octave’s. Octave has a larger step size. To reduce this, one can use the following settings
Reduce the maximum allowed step size (
MaxStep), and the initial step size:vopt = odeset("MaxStep",0.1,"InitialStep",0.1);
(It seems that the
MaxStepdoes not apply to the very first step – that’s why we need to set both.)Then pass this options object
voptto the ode45 command like this:vopt = odeset("MaxStep",0.1,"InitialStep",0.1); [ts, ys]=ode45(@my_ode1_rhs,timespan,y0,vopt);
Or reduce the absolute and/or relative tolerances to enforce smaller integration steps:
vopt = odeset("AbsTol",1e-9,"RelTol",1e-9); [ts, ys]=ode45(@my_ode1_rhs,timespan,y0,vopt);
All parameters can be combined, for example:
vopt = odeset("AbsTol",1e-9,"RelTol",1e-9,"MaxStep",0.1,"InitialStep",0.1);
[Tested with octave GNU Octave, version 3.0.3 on Mac OS X]
1.4.4 Editing m-files¶
A student reports that on Mac OS X, they cannot type edit myfile.m
to open an editor to edit the file. The edit command (in Matlab) will
start an editor instructing it to load the file myfile.m if it
exists, or creating an empty file and loading it into the editor if
the file does not yet exist. The default editor is part of the MATLAB
Graphical User Interface.
As there is no such GUI for Octave, the edit command in Octave (on any
operating system) is likely to try to invoke the default editor that
has been defined. On Unix (this includes Linux and Mac OS X), it is
common to set an environment variable with name
EDITOR to contain the name of the desired editor.
If you find that the edit myfile.m command does not work
satisfactorily, the simplest work around is not to use it. Instead you
can open the m-file directly in some text editor of your choice. You
need to make sure you save the file myfile.m, before you execute the file by
either typing myfile at the Octave prompt, or calling myfile()
if myfile.m contains a function.
You need to make sure the octave program is in the same working
directory as the file. You can use the commands pwd (Print Working
Directory) in octave to display the current directory and cd to
Change Directory:
octave:1> pwd
ans = /Users/fangohr
octave:2> cd tmp
octave:3> pwd
ans = /Users/fangohr/tmp
octave:4> cd ..
octave:5> pwd
ans = /Users/fangohr
octave:6>
Any plain text editor will do. On all platforms Emacs is available (although it might need some getting used to): We recommend XEmacs for Windows, Emacs or XEmacs for Linux and Aquamacs for Mac OS X. All of these support syntax highlighting for matlab files (and also for LaTeX files).
Another often used cross-platform editor is VIM.
You can also use Textedit on Mac OS X (which is very basic but comes
with Mac OS X preinstalled), edit on Windows (which is even more
basic) or any other source code text editor (see list of these on
Wikipedia entry for Source code editor).
1.4.5 Further resources¶
See also FAQ on Matlab compatibility at http://wiki.octave.org/FAQ#Differences_between_Octave_and_Matlab
and this Wiki entry http://en.wikibooks.org/wiki/MATLAB_Programming/Differences_between_Octave_and_MATLAB