GNU Octave is a programming language focused on scientific and mathematical computation. It’s particularly good when working with arrays (scalars, vectors, matrices and higher-dimensional arrays) and is generally seen as a free alternative to MATLAB.
Octave was initially developed by John W Eaton and first released in 1993 (although early prototypes had already existed for a few years by then). It is named after Octave Levenspiel, a former professor of chemical engineering at Oregon State University who had taught Eaton.
macOS
Ubuntu
On Ubuntu 22.04 you can run the following from the terminal:
$ sudo apt-get install octave
Don’t install from the Snap Store as this produces lots of warnings when you try to run it. For the record, installing from the Snap Store involves installing from the Ubuntu Software programme or from the terminal via the following command:
# Don't install this way
$ sudo snap install octave
Once installed, you can open Octave in interactive mode from the terminal with:
$ octave
This will also tell you what version you have but, for the record, this information can also be found by running:
$ octave --version
Exit this Octave interactive mode with:
exit()
Check where it has been installed to with:
$ which octave
It will probably be located here:
/usr/bin/octave
If you installed it from the Snap Store (hopefully you didn’t) it will be here:
/snap/bin/octave
.m
extension. This helps with MATLAB compatibility.octave {filename}.m
C:\octave\Octave-6.4.0\bin\octave-cli {filename}.m
or similar (Octave-6.4.0
should be replaced with the version of Octave you have installed)%
. The hash #
also works but is non-standard.disp()
function if you want to explicitly display something on the screenans
: this will always be equal to whatever the output of the previous statement waspwd
: the path of your present working directory, usually the folder where your script is locatedpi
: equal to the circle constant (3.14159265…)eps
: the smallest difference between two numbers that Octave can handle (2.2204e-16)realmax
: the largest number that Octave can handle (1.7977e+308)realmiin
: the smallest number that Octave can handle (2.2251e-308)inf
or Inf
: infinitynan
or NaN
: not-a-number; nullclear
: clears the memory of all stored variables and resets the special variables to their default valuesclc
: clears the command window (but has no effect on the stored variables)close
: closes all open plot windowsclc clear close
at the start of your scripts to ensure that your working area is cleanaddpath()
functionZero-dimensional arrays (which most of use would refer to as ‘numbers’!) can be used to do maths much as one would expect:
a = 1;
b = 2;
% Addition
a + b
% Subtraction
a - b
% Multiplication
a * b
% Right division
a / b
% Left division
a \ b
% Exponentiation
a^b
## ans = 3
## ans = -1
## ans = 2
## ans = 0.5000
## ans = 2
## ans = 1
Basic mathematical functions:
x = 10;
% Absolute value
abs(x)
% Exponential function
exp(x)
% Natural logarithm
log(x)
% Logarithm, base 10
log10(x)
% Square root
sqrt(x)
% Trigonometric functions using radians
sin(x)
cos(x)
tan(x)
% Trigonometric functions using degrees
sind(x)
cosd(x)
tand(x)
## ans = 10
## ans = 2.2026e+04
## ans = 2.3026
## ans = 1
## ans = 3.1623
## ans = -0.5440
## ans = -0.8391
## ans = 0.6484
## ans = 0.1736
## ans = 0.9848
## ans = 0.1763
Running the input()
function will:
current_age = input('How old are you? ');
disp('Your current age:'), disp(current_age)
age_next_year = current_age + 1;
disp('Your age next year:'), disp(age_next_year)
How old are you? 31
Your current age:
31
Your age next year:
32