GNU Octave: a free, open source MATLAB-like language for numerical computing

I tend to use Python with the Numpy, SciPy, and Matplotlib stack whenever I have to do scientific computing. For statistical computing I use R whenever this Python stack does not provide the necessary features. However, I want to draw readers’ attention to another tool for free, open-source numerical computing: GNU Octave (hereafter called “Octave”), which is an interactive language closely related to MATLAB. In fact, most MATLAB code can be run in Octave with no modification; for MATLAB code that does need a change to run in Octave, the necessary modification is usually slight. MATLAB is common in engineering and scientific environments, but it costs money. For budget-constrained startups or non-profits Octave is a viable alternative. This post introduces Octave and shows its operation in both a statistical and control system design role.

Octave is covered by the GNU General Public License, which enables programmers to modify it as needed, provided the modifications are made publicly available. This license also makes Octave effectively free in the financial sense. The program runs in both Linux and Windows, although the Windows setup is more complicated since you have to use Cygwin, MinGW, or Visual Studio.

Octave has a full range of plotting abilities that MATLAB users will be familiar with. The program automatically uses a third-party program, Gnuplot, to create the plots. For example, a 3D plot of a geodesic sphere rendered by Octave is shown below [1]:

gnuplot_class_1_icosahedron_frequency_2

As another example, a Nyquist plot of a dynamic system is:

my_nyquist

Like MATLAB, Octave has many toolkits, which are published as part of Octave-Forge [2], a central repository for Octave packages. Examples of such toolkits are packages for control systems analysis/design and fuzzy logic.

Under the hood, Octave uses the BLAS and LAPACK libraries to facilitate linear algebra computations, in much the same way R and Python’s NumPy package do.

Recent versions of Octave come with a GUI. Using the GUI to show the iconic “sombrero” plot with code given by [3]:

octave_gui

Statistical Calculations with Octave

Since I’m a statistician in training, I thought I’d demonstrate a few of Octave’s basic statistical tools. First, a box plot of samples from two normal distributions:

[source language=”matlab”]
a = normrnd(20, 3, 100, 1);
b = normrnd(22, 3, 100, 1);
boxplot([a, b]);
title("Comparison of Samples From Two Normal Distributions")
xlabel("Distribution")
ylabel("Sample")
[/source]

boxplot

Conducting a t-test on the two sets of samples from normal distributions:

t_test

Conducting linear regression on simulated data:

[source language=”matlab”]
x = 0:0.01:10;
noise = normrnd(0, 2, 1, length(x));
hist(noise);
title("Noise");
y = 2*x + noise + 5;
figure;
hold on;
scatter(x, y);
F = polyfit(x, y, 1);
a = F(1);
b = F(2);
y_pred = a*x + b;
plot(x, y_pred, ‘r’);
title("Scatter Plot and Regression Line");
hold off;
[/source]

histogram

scatter

Control Systems Calculations with Octave

I also have experience designing control systems, which Octave is well suited for. Here is an example of a PID controller’s step response implemented in Octave’s control system toolkit:

[source language=”matlab”]
pkg load control
Kp = 4
Kd = 1
Ki = 1
P=tf([1], [1 Kp/Kd Ki/Kd])
step(P, 20)
[/source]

pid

Creating the Nyquist plot shown above for this system:

[source language=”matlab”]
nyquist(P)
[/source]

my_nyquist

Conclusion

Octave is a viable, free alternative to MATLAB for many scientific computing applications.

References

1. http://octave-dome.sourceforge.net/ (I have stopped working on this. Use pyDome instead).
2. http://octave.sourceforge.net/
3. http://en.wikipedia.org/wiki/GNU_Octave

Leave a Reply

Your email address will not be published. Required fields are marked *