Using Octave

From Octave
Revision as of 11:05, 28 October 2016 by Siko1056 (talk | contribs) (Create a short beginners tutorial page from a publish document. First wiki markup.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

First, follow the installation guide to install GNU Octave on your system. Then, launch the interactive prompt by typing octave in a terminal or by clicking the icon in the programs menu. For further guidance, see the manual page on Running Octave.


Variable Assignment

Assign values to variables with = (Note: assignment is pass-by-value). Read more

<a href="https://www.gnu.org/software/octave/doc/interpreter/Variables.html">about variables</a>.

a = 1;


Comments

# or % start a comment line, that continues to the end of the line. Read more <a href="https://www.gnu.org/software/octave/doc/interpreter/Comments.html">about comments</a>.


Command evaluation

The output of every command is printed to the console unless terminated with a semicolon ;. The <a href="https://www.gnu.org/software/octave/doc/interpreter/XREFdisp.html">disp</a> command can be used to print output anywhere. Use <a href="https://www.gnu.org/software/octave/doc/interpreter/XREFquit.html">exit</a> or <a href="https://www.gnu.org/software/octave/doc/interpreter/XREFquit.html">quit</a> to quit the console. Read more

<a href="https://www.gnu.org/software/octave/doc/interpreter/Simple-Examples.html">about command evaluation</a>.

t = 99 + 1  <span class="comment"># prints 't = 100'</span>
t = 99 + 1; <span class="comment"># nothing is printed</span>
disp(t);
t =  100
100


Elementary math

Many mathematical operators are available in addition to the standard arithmetic. Operations are floating-point. Read more

<a href="https://www.gnu.org/software/octave/doc/interpreter/Arithmetic.html">about elementary math</a>.

x = 3/4*pi;
y = sin(x)
y =  0.70711


Matrices

Arrays in Octave are called matrices. One-dimensional matrices are referred to as vectors. Use a space or a comma , to separate elements in a row and semicolon ; to start a new row. Read more

<a href="https://www.gnu.org/software/octave/doc/interpreter/Linear-Algebra.html">about matrices</a>.

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


Linear Algebra

Many common linear algebra operations are simple to program using Octave’s matrix syntax. Read more

<a href="https://www.gnu.org/software/octave/doc/interpreter/Linear-Algebra.html">about linear algebra</a>.

columnVec * rowVec
ans =
  64   48   32
  48   36   24
  32   24   16
rowVec * columnVec
ans =  116
columnVec<span class="string">'</span>
ans =
  8   6   4


Accessing Elements

Octave is 1-indexed. Matrix elements are accessed as matrix(rowNum, columnNum). Read more

<a href="https://www.gnu.org/software/octave/doc/interpreter/Index-Expressions.html">about accessing elements</a>.

mat(2,3)
ans = -2


Control flow wih loops

Octave supports for and while loops, as well as other control flow structures. Read more

<a href="https://www.gnu.org/software/octave/doc/interpreter/Statements.html">about control flow</a>.

x = zeros (50,1);
<span class="keyword">for</span> i = 1:2:100 <span class="comment"># iterate from 1 to 100 with step size 2</span>
  x(i) = i^2;
<span class="keyword">endfor</span>

y = zeros (50,1);
k = 1;
step = 2;
<span class="keyword">while</span> (k <= (100-step))
  y(i) = k^2;
  k = k + step;
<span class="keyword">endwhile</span>


Vectorization

For-loops can often be replaced or simplified using vector syntax. The operators *, /, and ^ all support element-wise operations writing a dot . before the operators. Many other functions operate element-wise by default (<a href="https://www.gnu.org/software/octave/doc/interpreter/XREFsin.html">sin</a>, +, -, etc.). Read more

<a href="https://www.gnu.org/software/octave/doc/interpreter/Vectorization-and-Faster-Code-Execution.html">about vectorization</a>.

i = 1:2:100;      <span class="comment"># create an array with 50-elements</span>
x = i.^2;         <span class="comment"># each element is squared</span>
y = x + 9;        <span class="comment"># add 9 to each element</span>
z = y./i;         <span class="comment"># divide each element in y by the corresponding value in i</span>
w = sin (i / 10); <span class="comment"># take the sine of each element divided by 10</span>


Plotting

The function <a href="https://www.gnu.org/software/octave/doc/interpreter/XREFplot.html">plot</a> can be called with vector arguments to create 2D line and scatter plots. Read more

<a href="https://www.gnu.org/software/octave/doc/interpreter/Two_002dDimensional-Plots.html">about plotting</a>.

plot (i/10, w);
title (<span class="string">'w = sin(i/10)'</span>);
xlabel (<span class="string">'i ÷ 10'</span>);
ylabel (<span class="string">'w'</span>);

<img src="octave_basics-1.png" alt="octave_basics-1.png">


Strings

Strings are simply arrays of characters. Strings can be composed using C-style formatting with <a href="https://www.gnu.org/software/octave/doc/interpreter/XREFsprintf.html">sprintf</a> or <a href="https://www.gnu.org/software/octave/doc/interpreter/XREFfprintf.html">fprintf</a>. Read more

<a href="https://www.gnu.org/software/octave/doc/interpreter/Strings.html">about strings</a>.

firstString = <span class="string">"hello world"</span>;
secondString = <span class="string">"!"</span>;
[firstString, secondString] <span class="comment"># concatenate both strings</span>
ans = hello world!
fprintf (<span class="string">"%s %.10f \n"</span>, <span class="string">"The number is:"</span>, 10)
The number is: 10.0000000000 


If-else

Conditional statements can be used to create branching logic in your code. Read more

<a href="https://www.gnu.org/software/octave/doc/interpreter/The-if-Statement.html">in the manual</a>.

<span class="comment"># Print 'Foo'      if divisible by 7,</span>
<span class="comment">#       'Fizz'     if divisible by 3,</span>
<span class="comment">#       'Buzz'     if divisible by 5,</span>
<span class="comment">#       'FizzBuzz' if divisible by 3 and 5</span>
<span class="keyword">for</span> i = 1:1:20
  outputString = <span class="string">""</span>;
  <span class="keyword">if</span> (rem (i, 3) == 0)  <span class="comment"># rem is the remainder function</span>
    outputString = [outputString, <span class="string">"Fizz"</span>];
  <span class="keyword">endif</span>
  <span class="keyword">if</span> (rem (i, 5) == 0)
    outputString = [outputString, <span class="string">"Buzz"</span>];
  <span class="keyword">elseif</span> (rem(i,7) == 0)
    outputString = <span class="string">"Foo"</span>;
  <span class="keyword">else</span>
    outputString = outputString;
  <span class="keyword">endif</span>
  fprintf(<span class="string">"i=%g: %s \n"</span>, i, outputString);
<span class="keyword">endfor</span>
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


Getting Help

The <a href="https://www.gnu.org/software/octave/doc/interpreter/XREFhelp.html">help</a> and <a href="https://www.gnu.org/software/octave/doc/interpreter/XREFdoc.html">doc</a> commands can be invoked at the

Octave prompt to print documentation for any function.

help plot
doc plot


Octave forge packages

Community-developed packages can be added from the <a href="http://octave.sourceforge.net/index.html">Octave Forge</a> website to extend the functionality of Octave’s core library. (Matlab users: Forge packages act similarly to Matlab’s toolboxes.) The <a href="https://www.gnu.org/software/octave/doc/interpreter/XREFpkg.html">pkg</a> command is used to manage these packages. For example, to use the image processing library

from the Forge, use:

pkg install -forge image <span class="comment"># install package</span>
pkg load image           <span class="comment"># load new functions into workspace</span>

<a href="https://www.gnu.org/software/octave/doc/interpreter/Packages.html">Read more about packages</a>.