First, follow the installation instructions for:
- macOS
- GNU/Linux and other Unix systems
- Microsoft Windows
or consult the GNU Octave manual to install GNU Octave on your system.Then, start the GNU Octave by clicking the icon in the programs menu or launch the interactive prompt by typing octave
in a terminal.See the manual page on running Octave.
The GNU Octave graphical user interface (GUI).
Contents
- 1 Variable Assignment
- 2 Comments
- 3 Command evaluation
- 4 Elementary math
- 5 Matrices
- 6 Linear Algebra
- 7 Accessing Elements
- 8 Control flow with loops
- 9 Vectorization
- 10 Plotting
- 11 Strings
- 12 If-else
- 13 Getting Help
- 14 Octave packages
- 15 Octave User Codes
Assign values to variables with =
(Note: assignment is pass-by-value).Read more about variables.
a = 1;
#
or %
start a comment line, that continues to the end of the line.Read more about comments.
The output of every command is printed to the console unless terminated witha semicolon ;
. The disp command can be used to print outputanywhere. Use exit or quit to quit the console.Read more about command evaluation.
t = 99 + 1 # prints 't = 100'
t = 100
t = 99 + 1; # nothing is printeddisp(t);
100
Many mathematical operators are available in addition to the standardarithmetic. Operations are floating-point. Read moreabout elementary math.
x = 3/4 * pi;y = sin (x)
y = 0.70711
Arrays in Octave are called matrices. One-dimensional matrices are referredto as vectors. Use a space or a comma ,
to separate elements in a row andsemicolon ;
to start a new row. Read moreabout matrices.
rowVec = [8 6 4]
rowVec = 8 6 4
columnVec = [8; 6; 4]
columnVec = 8 6 4
mat = [8 6 4; 2 0 -2]
mat = 8 6 4 2 0 -2
size(mat)
ans = 2 3
length(rowVec)
ans = 3
Many common linear algebra operations are simple to program using Octave’smatrix syntax. Read moreabout linear algebra.
columnVec * rowVec
ans = 64 48 32 48 36 24 32 24 16
rowVec * columnVec
ans = 116
columnVec'
ans = 8 6 4
Octave is 1-indexed. Matrix elements are accessed asmatrix(rowNum, columnNum)
. Read moreabout accessing elements.
mat(2,3)
ans = -2
Octave supports for
and while
loops, as well as other control flowstructures. Read moreabout control flow.
x = zeros (50,1);for i = 1:2:100 # iterate from 1 to 100 with step size 2 x(i) = i^2;endfory = zeros (50,1);k = 1;step = 2;while (k <= 100) y(k) = k^2; k = k + step;endwhile
For-loops can often be replaced or simplified using vector syntax. Theoperators *
, /
, and ^
all support element-wise operations writinga dot .
before the operators. Many other functions operate element-wiseby default (sin, +
, -
, etc.). Read moreabout vectorization.
i = 1:2:100; # create an array with 50-elementsx = i.^2; # each element is squaredy = x + 9; # add 9 to each elementz = y./i; # divide each element in y by the corresponding value in iw = sin (i / 10); # take the sine of each element divided by 10
The function plot can be called with vector arguments tocreate 2D line and scatter plots. Read moreabout plotting.
plot (i / 10, w);title ('w = sin (i / 10)');xlabel ('i / 10');ylabel ('w');
Strings are simply arrays of characters. Strings can be composed usingC-style formatting with sprintf orfprintf. Read moreabout strings.
firstString = "hello world";secondString = "!";[firstString, secondString] # concatenate both strings
ans = hello world!
fprintf ("%s%.10f \n", "The number is:", 10)
The number is: 10.0000000000
Conditional statements can be used to create branching logic in your code.Read more in the manual.
# Print 'Foo' if divisible by 7,# 'Fizz' if divisible by 3,# 'Buzz' if divisible by 5,# 'FizzBuzz' if divisible by 3 and 5for i = 1:1:20 outputString = ""; if (rem (i, 3) == 0) # rem is the remainder function outputString = [outputString, "Fizz"]; endif if (rem (i, 5) == 0) outputString = [outputString, "Buzz"]; elseif (rem(i,7) == 0) outputString = "Foo"; else outputString = outputString; endif fprintf("i=%g: %s \n", i, outputString);endfor
i=1: i=2: i=3: Fizz i=4: i=5: Buzz i=6: Fizz i=7: Foo i=8: i=9: Fizz i=10: Buzz i=11: i=12: Fizz i=13: i=14: Foo i=15: FizzBuzz i=16: i=17: i=18: Fizz i=19: i=20: Buzz
The help and doc commands can be invoked at theOctave prompt to print documentation for any function.
help plotdoc plot
Community-developed packages can be added from theOctave Packages website to extendthe functionality of Octave’s core library. (Matlab users: Packagesact similarly to Matlab’s toolboxes.) The pkg command is usedto manage these packages. For example, to use the image processing library visit its page on Octave Packages, copy the install command and run it in octave
pkg install "https://downloads.sourceforge.net/project/octave/Octave%20Forge%20Packages/Individual%20Package%20Releases/image-2.14.0.tar.gz" # install packagepkg load image # load new functions into workspace
There are also User Codes available for GNU Octave which are not part of the core program or any of the packages.
See Category User Codes.