Using Octave: Difference between revisions

From Octave
Jump to navigation Jump to search
(→‎Command evaluation: wiki markup)
(wiki markup for links)
Line 37: Line 37:
Many mathematical operators are available in addition to the standard
Many mathematical operators are available in addition to the standard
arithmetic.  Operations are floating-point.  Read more
arithmetic.  Operations are floating-point.  Read more
<a href="https://www.gnu.org/software/octave/doc/interpreter/Arithmetic.html">about elementary math</a>.</p><pre class="oct-code">x = 3/4*pi;
[https://www.gnu.org/software/octave/doc/interpreter/Arithmetic.html about elementary math].
y = sin(x)</pre><pre class="oct-code-output">y =  0.70711
 
</pre>
<pre class="oct-code">x = 3/4*pi;
y = sin(x)</pre>
<pre class="oct-code-output">y =  0.70711</pre>




Line 48: Line 50:
to as vectors.  Use a space or a comma <code>,</code> to separate elements in a row and
to as vectors.  Use a space or a comma <code>,</code> to separate elements in a row and
semicolon <code>;</code> to start a new row.  Read more
semicolon <code>;</code> to start a new row.  Read more
<a href="https://www.gnu.org/software/octave/doc/interpreter/Linear-Algebra.html">about matrices</a>.</p><pre class="oct-code">rowVec = [8 6 4]</pre><pre class="oct-code-output">rowVec =
[https://www.gnu.org/software/octave/doc/interpreter/Linear-Algebra.html about matrices].
<pre class="oct-code">rowVec = [8 6 4]</pre>
<pre class="oct-code-output">rowVec =
   8  6  4
   8  6  4
</pre><pre class="oct-code">columnVec = [8; 6; 4]</pre><pre class="oct-code-output">columnVec =
</pre>
 
<pre class="oct-code">columnVec = [8; 6; 4]</pre>
<pre class="oct-code-output">columnVec =
   8
   8
   6
   6
   4
   4
</pre><pre class="oct-code">mat = [8 6 4; 2 0 -2]</pre><pre class="oct-code-output">mat =
</pre>
 
<pre class="oct-code">mat = [8 6 4; 2 0 -2]</pre>
<pre class="oct-code-output">mat =
   8  6  4
   8  6  4
   2  0  -2
   2  0  -2
</pre><pre class="oct-code">size(mat)</pre><pre class="oct-code-output">ans =
</pre>
 
<pre class="oct-code">size(mat)</pre>
<pre class="oct-code-output">ans =
   2  3
   2  3
</pre><pre class="oct-code">length(rowVec)</pre><pre class="oct-code-output">ans =  3
</pre>
</pre>
<pre class="oct-code">length(rowVec)</pre>
<pre class="oct-code-output">ans =  3</pre>




Line 68: Line 83:
Many common linear algebra operations are simple to program using Octave’s
Many common linear algebra operations are simple to program using Octave’s
matrix syntax.  Read more
matrix syntax.  Read more
<a href="https://www.gnu.org/software/octave/doc/interpreter/Linear-Algebra.html">about linear algebra</a>.</p><pre class="oct-code">columnVec * rowVec</pre><pre class="oct-code-output">ans =
[https://www.gnu.org/software/octave/doc/interpreter/Linear-Algebra.html about linear algebra].
 
<pre class="oct-code">columnVec * rowVec</pre>
<pre class="oct-code-output">ans =
   64  48  32
   64  48  32
   48  36  24
   48  36  24
   32  24  16
   32  24  16
</pre><pre class="oct-code">rowVec * columnVec</pre><pre class="oct-code-output">ans =  116
</pre>
</pre><pre class="oct-code">columnVec<span class="string">'</span></pre><pre class="oct-code-output">ans =
 
<pre class="oct-code">rowVec * columnVec</pre>
<pre class="oct-code-output">ans =  116</pre>
 
<pre class="oct-code">columnVec'</pre>
<pre class="oct-code-output">ans =
   8  6  4
   8  6  4
</pre>
</pre>
Line 83: Line 106:
Octave is 1-indexed.  Matrix elements are accessed as
Octave is 1-indexed.  Matrix elements are accessed as
<code>matrix(rowNum, columnNum)</code>.  Read more
<code>matrix(rowNum, columnNum)</code>.  Read more
<a href="https://www.gnu.org/software/octave/doc/interpreter/Index-Expressions.html">about accessing elements</a>.</p><pre class="oct-code">mat(2,3)</pre><pre class="oct-code-output">ans = -2
[https://www.gnu.org/software/octave/doc/interpreter/Index-Expressions.html about accessing elements].
</pre>
 
<pre class="oct-code">mat(2,3)</pre>
<pre class="oct-code-output">ans = -2</pre>




Line 92: Line 117:
Octave supports <code>for</code> and <code>while</code> loops, as well as other control flow
Octave supports <code>for</code> and <code>while</code> loops, as well as other control flow
structures.  Read more
structures.  Read more
<a href="https://www.gnu.org/software/octave/doc/interpreter/Statements.html">about control flow</a>.</p><pre class="oct-code">x = zeros (50,1);
[https://www.gnu.org/software/octave/doc/interpreter/Statements.html about control flow].
<span class="keyword">for</span> i = 1:2:100 <span class="comment"># iterate from 1 to 100 with step size 2</span>
 
<pre class="oct-code">x = zeros (50,1);
for i = 1:2:100 # iterate from 1 to 100 with step size 2
   x(i) = i^2;
   x(i) = i^2;
<span class="keyword">endfor</span>
endfor


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




Line 112: Line 139:
operators <code>*</code>, <code>/</code>, and <code>^</code> all support element-wise operations writing
operators <code>*</code>, <code>/</code>, and <code>^</code> all support element-wise operations writing
a dot <code>.</code> before the operators.  Many other functions operate element-wise
a dot <code>.</code> 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>, <code>+</code>, <code>-</code>, etc.).  Read more
by default ([https://www.gnu.org/software/octave/doc/interpreter/XREFsin.html sin], <code>+</code>, <code>-</code>, etc.).  Read more
<a href="https://www.gnu.org/software/octave/doc/interpreter/Vectorization-and-Faster-Code-Execution.html">about vectorization</a>.</p><pre class="oct-code">i = 1:2:100;      <span class="comment"># create an array with 50-elements</span>
[https://www.gnu.org/software/octave/doc/interpreter/Vectorization-and-Faster-Code-Execution.html about vectorization].
x = i.^2;        <span class="comment"># each element is squared</span>
 
y = x + 9;        <span class="comment"># add 9 to each element</span>
<pre class="oct-code">i = 1:2:100;      # create an array with 50-elements
z = y./i;        <span class="comment"># divide each element in y by the corresponding value in i</span>
x = i.^2;        # each element is squared
w = sin (i / 10); <span class="comment"># take the sine of each element divided by 10</span>
y = x + 9;        # add 9 to each element
</pre><pre class="oct-code-output"></pre>
z = y./i;        # divide each element in y by the corresponding value in i
w = sin (i / 10); # take the sine of each element divided by 10
</pre>




Line 124: Line 153:
= Plotting =
= Plotting =


The function <a href="https://www.gnu.org/software/octave/doc/interpreter/XREFplot.html">plot</a> can be called with vector arguments to
The function [https://www.gnu.org/software/octave/doc/interpreter/XREFplot.html plot] can be called with vector arguments to
create 2D line and scatter plots.  Read more
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>.</p><pre class="oct-code">plot (i/10, w);
[https://www.gnu.org/software/octave/doc/interpreter/Two_002dDimensional-Plots.html about plotting].
 
<pre class="oct-code">plot (i/10, w);
title (<span class="string">'w = sin(i/10)'</span>);
title (<span class="string">'w = sin(i/10)'</span>);
xlabel (<span class="string">'i ÷ 10'</span>);
xlabel (<span class="string">'i ÷ 10'</span>);
ylabel (<span class="string">'w'</span>);</pre><pre class="oct-code-output"></pre><img src="octave_basics-1.png" alt="octave_basics-1.png">
ylabel (<span class="string">'w'</span>);</pre>
 
<img src="octave_basics-1.png" alt="octave_basics-1.png">




Line 136: Line 169:


Strings are simply arrays of characters.  Strings can be composed using
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
C-style formatting with [https://www.gnu.org/software/octave/doc/interpreter/XREFsprintf.html sprintf] or
<a href="https://www.gnu.org/software/octave/doc/interpreter/XREFfprintf.html">fprintf</a>.  Read more
[https://www.gnu.org/software/octave/doc/interpreter/XREFfprintf.html fprintf].  Read more
<a href="https://www.gnu.org/software/octave/doc/interpreter/Strings.html">about strings</a>.</p><pre class="oct-code">firstString = <span class="string">"hello world"</span>;
[https://www.gnu.org/software/octave/doc/interpreter/Strings.html about strings].
 
<pre class="oct-code">firstString = <span class="string">"hello world"</span>;
secondString = <span class="string">"!"</span>;
secondString = <span class="string">"!"</span>;
[firstString, secondString] <span class="comment"># concatenate both strings</span>
[firstString, secondString] <span class="comment"># concatenate both strings</span>
Line 150: Line 185:


Conditional statements can be used to create branching logic in your code.
Conditional statements can be used to create branching logic in your code.
Read more
Read more [https://www.gnu.org/software/octave/doc/interpreter/The-if-Statement.html in the manual].
<a href="https://www.gnu.org/software/octave/doc/interpreter/The-if-Statement.html">in the manual</a>.</p><pre class="oct-code"><span class="comment"># Print 'Foo'      if divisible by 7,</span>
 
<pre class="oct-code"><span class="comment"># Print 'Foo'      if divisible by 7,</span>
<span class="comment">#      'Fizz'    if divisible by 3,</span>
<span class="comment">#      'Fizz'    if divisible by 3,</span>
<span class="comment">#      'Buzz'    if divisible by 5,</span>
<span class="comment">#      'Buzz'    if divisible by 5,</span>
Line 168: Line 204:
   <span class="keyword">endif</span>
   <span class="keyword">endif</span>
   fprintf(<span class="string">"i=%g: %s \n"</span>, i, outputString);
   fprintf(<span class="string">"i=%g: %s \n"</span>, i, outputString);
<span class="keyword">endfor</span></pre><pre class="oct-code-output">i=1:   
<span class="keyword">endfor</span></pre>
 
<pre class="oct-code-output">i=1:   
i=2:   
i=2:   
i=3: Fizz  
i=3: Fizz  
Line 194: Line 232:
= Getting Help =
= 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
The [https://www.gnu.org/software/octave/doc/interpreter/XREFhelp.html help] and [https://www.gnu.org/software/octave/doc/interpreter/XREFdoc.html doc] commands can be invoked at the
Octave prompt to print documentation for any function.</p><pre class="pre-code">help plot
Octave prompt to print documentation for any function.
 
<pre class="pre-code">help plot
doc plot</pre>
doc plot</pre>


Line 203: Line 243:


Community-developed packages can be added from the
Community-developed packages can be added from the
<a href="http://octave.sourceforge.net/index.html">Octave Forge</a> website to extend
[http://octave.sourceforge.net/index.html Octave Forge] website to extend
the functionality of Octave’s core library.  (Matlab users: Forge packages
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
act similarly to Matlab’s toolboxes.)  The [https://www.gnu.org/software/octave/doc/interpreter/XREFpkg.html pkg] command is used
to manage these packages.  For example, to use the image processing library
to manage these packages.  For example, to use the image processing library
from the Forge, use:</p><pre class="pre-code">pkg install -forge image <span class="comment"># install package</span>
from the Forge, use:
 
<pre class="pre-code">pkg install -forge image <span class="comment"># install package</span>
pkg load image          <span class="comment"># load new functions into workspace</span>
pkg load image          <span class="comment"># load new functions into workspace</span>
</pre><p><a href="https://www.gnu.org/software/octave/doc/interpreter/Packages.html">Read more about packages</a>.</p>
</pre>
 
[https://www.gnu.org/software/octave/doc/interpreter/Packages.html Read more about packages].


[[Category:Tutorials]]
[[Category:Tutorials]]

Revision as of 11:37, 28 October 2016

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 about variables.

a = 1;

Comments

# or % start a comment line, that continues to the end of the line. Read more about comments.

Command evaluation

The output of every command is printed to the console unless terminated with a semicolon ;. The disp command can be used to print output anywhere. 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 printed
disp(t);
 100

Elementary math

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

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 about 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


Linear Algebra

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

columnVec * rowVec
ans =
   64   48   32
   48   36   24
   32   24   16
rowVec * columnVec
ans =  116
columnVec'
ans =
   8   6   4


Accessing Elements

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

mat(2,3)
ans = -2


Control flow wih loops

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

x = zeros (50,1);
for i = 1:2:100 # iterate from 1 to 100 with step size 2
  x(i) = i^2;
endfor

y = zeros (50,1);
k = 1;
step = 2;
while (k <= (100-step))
  y(i) = k^2;
  k = k + step;
endwhile


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 (sin, +, -, etc.). Read more about vectorization.

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


Plotting

The function plot can be called with vector arguments to create 2D line and scatter plots. Read more about plotting.

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 sprintf or fprintf. Read more about strings.

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 in the manual.

<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 help and doc 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 Octave Forge website to extend the functionality of Octave’s core library. (Matlab users: Forge packages act similarly to Matlab’s toolboxes.) The pkg 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>

Read more about packages.