Mathematics Science and Engineering

GNU Octave Getting Started

Install and Get Started with GNU Octave

Octave started out as a software for chemistry class. GNU Octave has now evolved to much more that that. Octave has moved from a piece of software. It is now a high level programming language for numerical computations. The goal is to use mathematics to visualize chemical reactions. Since it is a mathematical software, it now has many modules for different purposes. In this article you will learn how to install it, get started and add modules. There are many modules available, ranging from chemistry through mechanics to quantum mechanics. Because of its wide use GNU Octave has many packages to enhance your experience.

Installation of GNU Octave on Ubuntu

1. Update apt before you start.

$ sudo apt update

2. Install the basic Octave package.

$ sudo apt install octave

You should now have the possibility to program mathematics functions. As soon as you define a more specific purpose for your use of Octave, you can add modules. But let’s not get ahead of ourselves.

When you start the GUI you have three tabs that show. To start with, it seems easy to use Octave. All you need to do is type in numbers with regular operators between; 5+2 [enter] the result is ‘ans: 7’. To create more advanced functions you need to put some more thought into the process. Since Octave supports so many functions you will be forgiven for seeing an uphill struggle to get into the functions. But don’t worry, the steps will be gentle.

When you type a function, the values have to be in brackets as you will see below. Start by getting familiar with the programming language. If you use the command line, things are simple enough to get started. You can actually do regular calculus on the command line, like this;

>> 5 + 2
ans = 7
>> 5 + 2;
>>
>> log (10)
ans = 2.3026
>> log (10);
>> x = log(10);
>>

Note, in the sequence above how adding a semicolon at the end renders no result. Also note that the last line uses an x. The x is now available as a variable for further operations. To use several statements for a graph you need the semicolon at the end otherwise the line executes and all info is lost. As you will see below, this is essential for more complex graphs.

Simply adding and subtracting is not extremely useful but combine to start making variables and creating plots and you have a powerful utility. Creating plots is surprisingly simple, the procedure is a few steps long. Basic graphs can be created using a very simple sequence.

>> x = linspace(0, 15, 100);
>> y = int8(x);
>> plot (x , y);

The result will look like this:


As you can see, drawing one plot requires a few lines of code. You can keep creating all the functions you can imagine with command line only but keeping track of the equations becomes a bottleneck.

It is also much harder to use existing work from others. This is where functions come in, many of them are built-in and some are available on octave-forge.

When your project expands the number of lines, you will need to create script files. Built into the GUI you have an editor, create a file to use for your experiments and try a few things out.

Any line that start with a hash (#) is a comment. If you need to create your own function, use the New Function… menu option to create it. The file you create is prefilled with comment fields in the top and a function definition.

function [retval] = Bloch_Sphere (input1, input2)
endfunction

The file Bloch_Sphere contained this code. Here you need to fill in your functions, of course. If you rename the function but not the file, Octave cannot run your function. Inside the function the code is similar to C, so you can use ‘If, then, else’ statements. What differs is that there is support for mathematical functions and data types. The data types include matrices and imaginary numbers.

An example function looks like this:

function result = factorial( n )
if( n == 0 )
result = 1;
return;
else
result = prod( 1:n );
endif
endfunction

And you call it with:

>> x = factorial (n);

This can also be added to your own code as a variable and re-used.

If you are using emacs, there is also a plugin available. For a description how to activate it, see the Octave wiki. For the impatient people, use a built-in function. At the command prompt type sombrero. You are presented with the official logo of Octave.

>>sombrero
>>help sombrero

GNU Octave Modules

The many modules for Octave which makes things complicated but it is all worth it in the end. When you run Ubuntu or many of the other main distributions you have most packages available in your repositories.  The modules on the web needs to be installed using the ‘pkg’ command.

>>pkg install financial-0.5.1.tar.gz

error: the following dependencies were unsatisfied:

financial needs io >= 1.0.18

Many times this will result in an error because of dependencies, you can see one example above. The financial package depends on the io package.  To solve this use your regular package manager to install it.

$ sudo apt install octave-io

Then try again, with your first package.

>> pkg install financial-0.5.1.tar.gz

For information about changes from the previous versions of the financial package, run ‘news financial’.

After that is done, you will need to load it also.

>>pkg load financial

The package itself has functions and documentation inside. You can find the list of functions on the octave webpage.

Conclusion

The octave package is comprehensive and works very well for displaying your mathematical functions as graphs. The documentation could be more accessible. If you have problems, look-up each package its own web page. Use the list of functions and use the online descriptions when you are experimenting.

About the author

Mats Tage Axelsson

Mats Tage Axelsson

I am a freelance writer for Linux magazines. I enjoy finding out what is possible under Linux and how we can all chip in to improve it. I also cover renewable energy and the new way the grid operates. You can find more of my writing on my blog.