GNU Octave Introduction

“The only way to learn mathematics is to do mathematics.” - Paul Halmos

Photo by Dan-Cristian Pădureț on Unsplash

My go to Open Source mathematics solution is GNU Octave. It is comparable to MATLAB and has much of MATLAB's functionality, sans the high price tag and licensing fees associated with MATLAB's numerous add ons — do not get me started on the inefficiencies of the MATLAB C/C++ compiler! Don't get me wrong, those MATLAB libraries are powerful and Octave does not have the resources of MathWorks, but I find it is often powerful enough to suit my needs.

I'm doing this tutorial on Windows 11 with Windows Subsystem for Linux 2.0, running Ubuntu 20.04. Much of this tutorial owes its existence to this document, from this page.

Installation

At a WSL 2 Ubuntu prompt enter:

sudo apt install octave

Note that I have to explicitly specify "octave --gui" when starting up in WSL 2 with Ubuntu 20.04 but you may not have to. Command line options are listed here. If you prefer, you can just fire up the CLI.

See this stackoverflow article to switch between GUI engines for octave in WSL 2. The one that worked for me was gnuplotqt was not working for some reason.

Further tips for Windows development with Octave can be found here.

Matrices & Vectors

Vectors and Matrices are core data types of Octave (and MATLAB). Resources on them are all over the Internet and are not covered here. Octave knows basic arithmetic, ignores most white space, and recognizes a LOT of vector and matrix operations. Vector procedures such as dot product, cross product, normal, and vector projection are supported. Matrix operations include multiplication, transposition, determinants, and eigenvalues. All arithmetic operations in Octave are assumed to be matrix operations unless specified otherwise.

Plots

A picture is worth a thousand words. I have found this to be true again and again in my career so we will spend most of this tutorial on plotting with Octave. The bulk of this text is from section 1.4 of this document.

Let's start by plotting the graph of the function sin(x) on the interval [0; 2]. Like a typical graphing calculator, Octave will simply plot a series of points and connect the dots to represent the curve. The process is less automated in Octave (but in the end, much more powerful). We begin by creating a vector of x-values.

>> x = linspace (0, 2*pi, 50) ;

Notice the format linspace (start val , end val, n). This creates a row vector of 50 evenly spaced values beginning at 0 and going up to 2. The smaller the increment, the smoother the curve will look. In this case, 50 points should be suitable. The semicolon at the end of the line is to suppress the output to the screen, since we don't need to see all the values in the vector. Now, we want to create a vector of the corresponding y-values. Use this command:

>> y = sin( x ) ;

Now, to plot the function, use the plot command:

>> plot(x , y ) ;

You should see the graph of f(x) = sin(x) pop up in a new window. You may wish to customize it a little bit. For example, the x-axis extends too far. We can set the window with the axis command. The window is controlled by a vector of the form [Xmin Xmax Ymin Ymax]. Let's set the axes to match the domain and range of the function.

>> axis ( [ 0 2*pi 1 1 ] ) ;

Sine Wave

We may want to change the color (to, say, red) or make the line thicker. We can add a grid to help guide our eye. In addition, a graph should usually be labeled with a title, axis labels, and legend. Try these options to get the improved graph:

>> plot (x , y , 'r' , 'linewidth' , 3)

>> grid on

>> xlabel('x') ;

>> ylabel ('y') ;

>> title('Sine graph');

>> legend ('y=sin(x)') ;

Now, let's try plotting points. The procedure is essentially the same, but we use an option to specify the marker we want. Some marker options are o, +, or *. We will plot the set of points f(1; 1); (2; 2); (3; 5); (4; 4)g using circles as our marker. First, clear the variables from the workspace and clear any existing graphs. Then define a vector of x-values and a vector of y-values and use the plot command.

>> clear; clf;

>> x = [ 1 2 3 4 ]

>> y = [ 1 2 5 4 ]

>> plot(x , y , 'o')

Figure of the Scatter Plot

Now suppose we want to graph the line y = 1.2x on the same set of axes (this is the line of best for this data). To add to our current graph we need to use the command hold on. Then any new plots will be drawn onto the current axes. We can switch back later with hold off.

>> hold on

>> plot(x , 1.2*x)

Now we should see four points and the graph of the line. Alternately, we can create multiple plots within a single plot command. Try this, for example:

>> clear; clf;

>> x = [ 1 2 3 4 ] ;

>> y1 = [ 1 2 5 4 ] ;

>> y2 = 1.2* x ;

>> plot (x , y1 , 'o' , x , y2)

>> axis( [ 0 5 0 6 ] ) ;

>> grid on ;

>> legend ( 'data points' , 'regressionline' ) ;

Notice that sets of input and output variables come in pairs, followed by any options that apply to that pair.

Elementwise Operations

Octave makes extensive use of elementwise operations which are covered here and here.

An important consideration when working with a more complex function like, say, y = x^2 sin(x), is that Octave will regard the product and exponent as matrix operations, unless we indicate otherwise. The same is true for division. To avoid errors when we are evaluating a function at a numeric input vector, we need to use the elementwise versions of exponentiation, multiplication, and division between variables. This is done by preceding the operation in question with a period, as in .^ or .* or ./.

These commands are incorrect and will cause errors:

>> x = linspace (10 , 10 , 100) ;

>> plot (x , x^2* sin(x))
% incorrect syntax : not elementwise operations

error: for A^b, A must be a square matrix . Use .^ for elementwise power.

error: evaluating argument list element number 2

But this will do the trick:

>> x = linspace(10 , 10 , 100) ;

>> plot(x , x.^2.*sin(x))
% correct : elementwise exponent and product

Plot of y =x^2 sin(x)

It is important to remember to use elementwise multiplication, exponentiation, and division, except when you are actually intending to execute a matrix operation. Failing to do so is the source of many errors and considerable frustration for beginning Octave users.

Here are links on Octave resources for plotting.

https://octave.sourceforge.io/octave/function/plot.html

https://octave.org/doc/v6.4.0/Plot-Annotations.html

3D plots

Some Octave 3D plots are at:

https://octave.org/doc/v4.2.1/Three_002dDimensional-Plots.html

http://www.malinc.se/math/octave/threeden.php

https://www.youtube.com/watch?v=sfTmLwOTE8Y

https://www.youtube.com/watch?v=3wOAH8toqQ

Saving Plots

One way to save plots is from the Octave GUI via an .m file. To do so open up the GUI and create a new file in Editor (not Command) window:

%excercise 1.4

x = [-1 1 2 3]
y = [5 4 2.5 0]

plot(x, y, '^', 'linewidth', 3)
grid on
axis([-2 4 -1 6]);
title("Scatter Plot")

Save the file and hit the "Save File and Run" indicated in the figure below:

Figure indicating where “Run” is located in the Editor Octave GUI

This should result in a plot window popping up. In that window select "Export to image" as shown in the figure. When the prompt comes up enter "scatterPlot.png" to export a PNG file.

Figure highlighting “Export to image” in a plot popup window.

Resources

A github link is provided for all the code in this tutorial here. Note you will need a github account to access those files.

Credits

https://techpiezo.com/linux/install-gnu-octave-in-ubuntu-20-04-lts/

https://en.wikibooks.org/wiki/Octave_Programming_Tutorial/Getting_started

https://octave.org/doxygen/stable/

https://www.yanivplan.com/files/tutorial5plot.pdf

Previous
Previous

GNU Readline

Next
Next

CMake Introduction