Octave

Overview

Octave is a mathematical program that is geared towards linear algebra. Or by it’s own start-up message, ‘[An] interactive language for numerical computations’. It is very similar to Matlab, except it is open source/free to use! The core program is operated with a command line, but thankfully someone wrote GUI Octave, a graphical front-end to Octave which makes it nicer to use. GUI Octave can be downloaded from it’s website, here.

The Basics

Some of the mathematical operations that Octave supports are:

  • All the usual mathematical operators (+, -, *, /, ^)
  • Truth statements (==, ~=, &&, ||)
  • Assignments (x = 2)
  • String assignments (b = ‘hello’)
    Note: single quotes for strings!
  • C printf syntax (sprintf(‘String text %u’, <operator>))
  • Matricies (A = [1 2; 3 4; 5 6])
  • Quick matrix generation (e.g. a matrix full of ones: ones(2,3), random numbers: rand(3,3))
  • Graphs (hist(x))

Saving Files

To save a binary file:

To save a readable (text) file:

The binary file is not human-readable, but stores the data in a more compressed manner than the text file.
System
Command chaining <command 1>; <command 2>; <command 3>; …
Display on screen dis(‘This comment will be displayed on screen’)
Closes Octave exit (or quit)
Comments %

Matrix Indexing Options

A(1,2) – Gets the element in the 1st row and 2nd column
A(2,:) – Gets everything in the 2nd row
A(:) – Put all the elements of A into a single vector (concantenates the elements column by column)

Matrix Manipulation

Note, matrixs are 1-indexed rather than 0 indexed

Element Wise

Addition A+1
Subtraction A-1
Multiplication A .* B
Division A ./ B
Log log(A)
Exponent exp(V)
Comparison A<3

Matrix Specific

Transpose A’
Per Column Maximum max(A,[],1)
Per Row Maximum max(A, [], 2)

Maximum of A max(max(A))
Row-wise Sum sum(A,1)
Flip Vertically flipud(A)
Pseudo-Inverse pinv(A)
Conditional Statements And Loops
Octave supports conditions statement and loops in a similar way to the programming language C
For statement
for i=1:10
v(i)=i;
end;
While statement
while i<= 10.
v(i) = i
i = i+1;
end;
If statement
if i == 10,
break;
end;

Plotting

Plot 2 variables against each other plot(x, y)
Hold the plot (to allow multiple plots on top of each other) hold on
Label The x-axis xlabel(‘X Label Name’)
Label The y-axis ylabel(‘Y Label Name’)
legend(‘legend for plot’)
title(‘My Plot Title’)
Print A Graph print -dpng ‘myPlot.png’
Close graph close
Multiple Graphs figure(1); plot(t, yq); figure(2); plot(t, y2)
Plot Small Graphs On One Figure subplot(grid rows, grid columns, current one to access)
Visually Plot Matrix In Colour imagesc(A)
Visually plot matrix in greyscale with legend imagesc(A), colorbar, colormap grey;

Functions

Functions can return more than one variable. This is unusual for a computer language! Yet again, very similar to Matlab…
Save in file called function name.m
function[y1, y2]=doubleAndTripleTheNumber(x1)
To call
<function name>(<parameters…>)
Add a path to search for functions addpath(‘Path string’)
Vectorization
Vectorization is the way of writing simpler and more efficient code for linear algebra manipulation.
e.g.

Posted: November 26th, 2018 at 8:47 am