Editing Using Octave

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
First, follow the installation instructions for:
First, follow the [https://www.gnu.org/software/octave/doc/interpreter/Installation.html installation guide]
* [[Octave for macOS|macOS]]
to install GNU Octave on your system. Then, launch the interactive prompt by
* [[Octave for GNU/Linux|GNU/Linux]] and [[Octave for other Unix systems|other Unix systems]]
typing <code>octave</code> in a terminal or by clicking the icon in the programs menu.
* [[Octave_for_Microsoft_Windows|Microsoft Windows]]
For further guidance, see the manual page on
or consult the [https://www.gnu.org/software/octave/doc/interpreter/Installation.html GNU Octave manual] to install GNU Octave on your system.
[https://www.gnu.org/software/octave/doc/interpreter/Running-Octave.html Running Octave].
Then, start the GNU Octave by clicking the icon in the programs menu or launch the interactive prompt by typing <code>octave</code> in a terminal.
 
See the manual page on [https://www.gnu.org/software/octave/doc/interpreter/Running-Octave.html running Octave].


[[File:GNU Octave screenshot.png|thumb|center|500px|The GNU Octave graphical user interface (GUI).]]


= Variable Assignment =
= Variable Assignment =


Assign values to variables with <code>=</code> (Note: assignment is ''pass-by-value'').
Assign values to variables with <code>=</code> (Note: assignment is <i>pass-by-value</i>).
Read more [https://www.gnu.org/software/octave/doc/interpreter/Variables.html about variables].
Read more
<a href="https://www.gnu.org/software/octave/doc/interpreter/Variables.html">about variables</a>.</p><pre class="oct-code">a = 1;</pre><pre class="oct-code-output"></pre>


<syntaxhighlight lang="octave">a = 1;</syntaxhighlight>


= Comments =
= Comments =


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


= Command evaluation =
= Command evaluation =


The output of every command is printed to the console unless terminated with
The output of every command is printed to the console unless terminated with
a semicolon <code>;</code>.  The [https://www.gnu.org/software/octave/doc/interpreter/XREFdisp.html disp] command can be used to print output
a semicolon <code>;</code>.  The <a href="https://www.gnu.org/software/octave/doc/interpreter/XREFdisp.html">disp</a> command can be used to print output
anywhere.  Use [https://www.gnu.org/software/octave/doc/interpreter/XREFquit.html exit] or [https://www.gnu.org/software/octave/doc/interpreter/XREFquit.html quit] to quit the console.
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 [https://www.gnu.org/software/octave/doc/interpreter/Simple-Examples.html about command evaluation].
Read more
<a href="https://www.gnu.org/software/octave/doc/interpreter/Simple-Examples.html">about command evaluation</a>.</p><pre class="oct-code">t = 99 + 1  <span class="comment"># prints 't = 100'</span>
t = 99 + 1; <span class="comment"># nothing is printed</span>
disp(t);</pre><pre class="oct-code-output">t =  100
100
</pre>


<syntaxhighlight lang="octave">t = 99 + 1  # prints 't = 100'</syntaxhighlight>
<syntaxhighlight lang="text">t =  100</syntaxhighlight>


<syntaxhighlight lang="octave">t = 99 + 1; # nothing is printed
disp(t);</syntaxhighlight>
<syntaxhighlight lang="text"> 100</syntaxhighlight>


= Elementary math =
= Elementary math =
Line 39: Line 40:
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
[https://www.gnu.org/software/octave/doc/interpreter/Arithmetic.html about elementary math].
<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;
 
y = sin(x)</pre><pre class="oct-code-output">y =  0.70711
<syntaxhighlight lang="octave">x = 3/4 * pi;
</pre>
y = sin (x)</syntaxhighlight>
<syntaxhighlight lang="text">y =  0.70711</syntaxhighlight>
 




Line 53: Line 51:
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
[https://www.gnu.org/software/octave/doc/interpreter/Linear-Algebra.html about matrices].
<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 =
<syntaxhighlight lang="octave">rowVec = [8 6 4]</syntaxhighlight>
<syntaxhighlight lang="text">rowVec =
   8  6  4
   8  6  4
</syntaxhighlight>
</pre><pre class="oct-code">columnVec = [8; 6; 4]</pre><pre class="oct-code-output">columnVec =
 
<syntaxhighlight lang="octave">columnVec = [8; 6; 4]</syntaxhighlight>
<syntaxhighlight lang="text">columnVec =
   8
   8
   6
   6
   4
   4
</syntaxhighlight>
</pre><pre class="oct-code">mat = [8 6 4; 2 0 -2]</pre><pre class="oct-code-output">mat =
 
<syntaxhighlight lang="octave">mat = [8 6 4; 2 0 -2]</syntaxhighlight>
<syntaxhighlight lang="text">mat =
   8  6  4
   8  6  4
   2  0  -2
   2  0  -2
</syntaxhighlight>
</pre><pre class="oct-code">size(mat)</pre><pre class="oct-code-output">ans =
 
<syntaxhighlight lang="octave">size(mat)</syntaxhighlight>
<syntaxhighlight lang="text">ans =
   2  3
   2  3
</syntaxhighlight>
</pre><pre class="oct-code">length(rowVec)</pre><pre class="oct-code-output">ans =  3
 
</pre>
<syntaxhighlight lang="octave">length(rowVec)</syntaxhighlight>
<syntaxhighlight lang="text">ans =  3</syntaxhighlight>




Line 86: Line 71:
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
[https://www.gnu.org/software/octave/doc/interpreter/Linear-Algebra.html about linear algebra].
<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 =
 
<syntaxhighlight lang="octave">columnVec * rowVec</syntaxhighlight>
<syntaxhighlight lang="text">ans =
   64  48  32
   64  48  32
   48  36  24
   48  36  24
   32  24  16
   32  24  16
</syntaxhighlight>
</pre><pre class="oct-code">rowVec * columnVec</pre><pre class="oct-code-output">ans =  116
</pre><pre class="oct-code">columnVec<span class="string">'</span></pre><pre class="oct-code-output">ans =
  8  6  4
</pre>


<syntaxhighlight lang="octave">rowVec * columnVec</syntaxhighlight>
<syntaxhighlight lang="text">ans =  116</syntaxhighlight>


<syntaxhighlight lang="octave">columnVec'</syntaxhighlight>
<syntaxhighlight lang="text">ans =
  8  6  4
</syntaxhighlight>


= Accessing Elements =
= Accessing Elements =
Line 107: Line 86:
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
[https://www.gnu.org/software/octave/doc/interpreter/Index-Expressions.html about accessing elements].
<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
</pre>


<syntaxhighlight lang="octave">mat(2,3)</syntaxhighlight>
<syntaxhighlight lang="text">ans = -2</syntaxhighlight>




 
= Control flow wih loops =
= Control flow with loops =


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
[https://www.gnu.org/software/octave/doc/interpreter/Statements.html about control flow].
<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);
 
<span class="keyword">for</span> i = 1:2:100 <span class="comment"># iterate from 1 to 100 with step size 2</span>
<syntaxhighlight lang="octave">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;
endfor
<span class="keyword">endfor</span>


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


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




Line 152: Line 127:
= Plotting =
= Plotting =


The function [https://www.gnu.org/software/octave/doc/interpreter/XREFplot.html plot] can be called with vector arguments to
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
create 2D line and scatter plots.  Read more
[https://www.gnu.org/software/octave/doc/interpreter/Two_002dDimensional-Plots.html about plotting].
<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);
title (<span class="string">'w = sin(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">


<syntaxhighlight lang="octave">plot (i / 10, w);
title ('w = sin (i / 10)');
xlabel ('i / 10');
ylabel ('w');</syntaxhighlight>


[[File:Using octave-1.png|thumb|center]]


= Strings =
= Strings =


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 [https://www.gnu.org/software/octave/doc/interpreter/XREFsprintf.html sprintf] or
C-style formatting with <a href="https://www.gnu.org/software/octave/doc/interpreter/XREFsprintf.html">sprintf</a> or
[https://www.gnu.org/software/octave/doc/interpreter/XREFfprintf.html fprintf].  Read more
<a href="https://www.gnu.org/software/octave/doc/interpreter/XREFfprintf.html">fprintf</a>.  Read more
[https://www.gnu.org/software/octave/doc/interpreter/Strings.html about strings].
<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>;
 
secondString = <span class="string">"!"</span>;
<syntaxhighlight lang="octave">firstString = "hello world";
[firstString, secondString] <span class="comment"># concatenate both strings</span>
secondString = "!";
</pre><pre class="oct-code-output">ans = hello world!
[firstString, secondString] # concatenate both strings
</pre><pre class="oct-code">fprintf (<span class="string">"%s %.10f \n"</span>, <span class="string">"The number is:"</span>, 10)</pre><pre class="oct-code-output">The number is: 10.0000000000  
</syntaxhighlight>
</pre>
<syntaxhighlight lang="text">ans = hello world!</syntaxhighlight>
 
<syntaxhighlight lang="octave">fprintf ("%s %.10f \n", "The number is:", 10)</syntaxhighlight>
<syntaxhighlight lang="text">The number is: 10.0000000000</syntaxhighlight>




Line 184: Line 153:


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 [https://www.gnu.org/software/octave/doc/interpreter/The-if-Statement.html in the manual].
Read more
 
<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>
<syntaxhighlight lang="octave"># Print 'Foo'      if divisible by 7,
<span class="comment">#      'Fizz'    if divisible by 3,</span>
#      'Fizz'    if divisible by 3,
<span class="comment">#      'Buzz'    if divisible by 5,</span>
#      'Buzz'    if divisible by 5,
<span class="comment">#      'FizzBuzz' if divisible by 3 and 5</span>
#      'FizzBuzz' if divisible by 3 and 5
<span class="keyword">for</span> i = 1:1:20
for i = 1:1:20
   outputString = <span class="string">""</span>;
   outputString = "";
   <span class="keyword">if</span> (rem (i, 3) == 0)  <span class="comment"># rem is the remainder function</span>
   if (rem (i, 3) == 0)  # rem is the remainder function
     outputString = [outputString, <span class="string">"Fizz"</span>];
     outputString = [outputString, "Fizz"];
   <span class="keyword">endif</span>
   endif
   <span class="keyword">if</span> (rem (i, 5) == 0)
   if (rem (i, 5) == 0)
     outputString = [outputString, <span class="string">"Buzz"</span>];
     outputString = [outputString, "Buzz"];
   <span class="keyword">elseif</span> (rem(i,7) == 0)
   elseif (rem(i,7) == 0)
     outputString = <span class="string">"Foo"</span>;
     outputString = "Foo";
   <span class="keyword">else</span>
   else
     outputString = outputString;
     outputString = outputString;
   endif
   <span class="keyword">endif</span>
   fprintf("i=%g: %s \n", i, outputString);
   fprintf(<span class="string">"i=%g: %s \n"</span>, i, outputString);
endfor</syntaxhighlight>
<span class="keyword">endfor</span></pre><pre class="oct-code-output">i=1:   
 
<syntaxhighlight lang="text">i=1:   
i=2:   
i=2:   
i=3: Fizz  
i=3: Fizz  
Line 225: Line 191:
i=19:   
i=19:   
i=20: Buzz  
i=20: Buzz  
</syntaxhighlight>
</pre>




Line 231: Line 197:
= Getting Help =
= Getting Help =


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
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.
Octave prompt to print documentation for any function.</p><pre class="pre-code">help plot
doc plot</pre>


<syntaxhighlight lang="octave">help plot
doc plot</syntaxhighlight>




 
= Octave forge packages =
= Octave packages =


Community-developed packages can be added from the
Community-developed packages can be added from the
[https://gnu-octave.github.io/packages/ Octave Packages] website to extend
<a href="http://octave.sourceforge.net/index.html">Octave Forge</a> website to extend
the functionality of Octave’s core library.  (Matlab users: Packages
the functionality of Octave’s core library.  (Matlab users: Forge packages
act similarly to Matlab’s toolboxes.)  The [https://www.gnu.org/software/octave/doc/interpreter/XREFpkg.html pkg] command is used
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 visit its [https://gnu-octave.github.io/packages/image/ page on Octave Packages,] copy the install command and run it in octave
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>
<syntaxhighlight lang="octave">pkg install "https://downloads.sourceforge.net/project/octave/Octave%20Forge%20Packages/Individual%20Package%20Releases/image-2.14.0.tar.gz" # install package
pkg load image          <span class="comment"># load new functions into workspace</span>
pkg load image          # load new functions into workspace
</pre><p><a href="https://www.gnu.org/software/octave/doc/interpreter/Packages.html">Read more about packages</a>.</p>
</syntaxhighlight>
 
[https://www.gnu.org/software/octave/doc/interpreter/Packages.html Read more about packages].
 
= Octave User Codes =
 
There are also User Codes available for GNU Octave which are not part of the core program or any of the packages.
 
[[:Category:User Codes|See Category User Codes]].


[[Category:Tutorials]]
[[Category:Tutorials]]
Please note that all contributions to Octave may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Octave:Copyrights for details). Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)