Using Octave: Difference between revisions
Updated the package section to use Octave Packages instead of Forge (as forge is being deprecated
(→Command evaluation: wiki markup) |
(Updated the package section to use Octave Packages instead of Forge (as forge is being deprecated) |
||
(10 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
First, follow the [https://www.gnu.org/software/octave/doc/interpreter/Installation.html | First, follow the installation instructions for: | ||
to install GNU Octave on your system. | * [[Octave for macOS|macOS]] | ||
typing <code>octave</code> in a terminal | * [[Octave for GNU/Linux|GNU/Linux]] and [[Octave for other Unix systems|other Unix systems]] | ||
* [[Octave_for_Microsoft_Windows|Microsoft Windows]] | |||
[https://www.gnu.org/software/octave/doc/interpreter/Running-Octave.html | or consult the [https://www.gnu.org/software/octave/doc/interpreter/Installation.html 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 <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 = | ||
Line 27: | Line 29: | ||
<syntaxhighlight lang="octave">t = 99 + 1 # prints 't = 100'</syntaxhighlight> | <syntaxhighlight lang="octave">t = 99 + 1 # prints 't = 100'</syntaxhighlight> | ||
<syntaxhighlight>t = 100</syntaxhighlight> | <syntaxhighlight lang="text">t = 100</syntaxhighlight> | ||
<syntaxhighlight lang="octave">t = 99 + 1; # nothing is printed | <syntaxhighlight lang="octave">t = 99 + 1; # nothing is printed | ||
disp(t);</syntaxhighlight> | disp(t);</syntaxhighlight> | ||
<syntaxhighlight> 100</syntaxhighlight> | <syntaxhighlight lang="text"> 100</syntaxhighlight> | ||
= Elementary math = | = Elementary math = | ||
Line 37: | Line 39: | ||
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]. | |||
y = sin(x)</ | |||
</ | <syntaxhighlight lang="octave">x = 3/4 * pi; | ||
y = sin (x)</syntaxhighlight> | |||
<syntaxhighlight lang="text">y = 0.70711</syntaxhighlight> | |||
Line 48: | Line 53: | ||
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]. | |||
<syntaxhighlight lang="octave">rowVec = [8 6 4]</syntaxhighlight> | |||
<syntaxhighlight lang="text">rowVec = | |||
8 6 4 | 8 6 4 | ||
</ | </syntaxhighlight> | ||
<syntaxhighlight lang="octave">columnVec = [8; 6; 4]</syntaxhighlight> | |||
<syntaxhighlight lang="text">columnVec = | |||
8 | 8 | ||
6 | 6 | ||
4 | 4 | ||
</ | </syntaxhighlight> | ||
<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> | ||
<syntaxhighlight lang="octave">size(mat)</syntaxhighlight> | |||
<syntaxhighlight lang="text">ans = | |||
2 3 | 2 3 | ||
</ | </syntaxhighlight> | ||
</ | |||
<syntaxhighlight lang="octave">length(rowVec)</syntaxhighlight> | |||
<syntaxhighlight lang="text">ans = 3</syntaxhighlight> | |||
Line 68: | Line 86: | ||
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]. | |||
<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> | ||
<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 83: | Line 107: | ||
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]. | |||
</ | |||
<syntaxhighlight lang="octave">mat(2,3)</syntaxhighlight> | |||
<syntaxhighlight lang="text">ans = -2</syntaxhighlight> | |||
= Control flow | = 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]. | |||
<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 | |||
y = zeros (50,1); | y = zeros (50,1); | ||
k = 1; | k = 1; | ||
step = 2; | step = 2; | ||
while (k <= 100) | |||
y( | y(k) = k^2; | ||
k = k + step; | k = k + step; | ||
endwhile</syntaxhighlight> | |||
= Vectorization = | = Vectorization = | ||
Line 112: | Line 138: | ||
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 ( | by default ([https://www.gnu.org/software/octave/doc/interpreter/XREFsin.html sin], <code>+</code>, <code>-</code>, etc.). Read more | ||
[https://www.gnu.org/software/octave/doc/interpreter/Vectorization-and-Faster-Code-Execution.html about vectorization]. | |||
x = i.^2; | |||
y = x + 9; | <syntaxhighlight lang="octave">i = 1:2:100; # create an array with 50-elements | ||
z = y./i; | x = i.^2; # each element is squared | ||
w = sin (i / 10); | 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 | |||
</syntaxhighlight> | |||
Line 124: | Line 152: | ||
= Plotting = | = Plotting = | ||
The function | 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 | ||
[https://www.gnu.org/software/octave/doc/interpreter/Two_002dDimensional-Plots.html about plotting]. | |||
<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 | C-style formatting with [https://www.gnu.org/software/octave/doc/interpreter/XREFsprintf.html sprintf] or | ||
[https://www.gnu.org/software/octave/doc/interpreter/XREFfprintf.html fprintf]. Read more | |||
[https://www.gnu.org/software/octave/doc/interpreter/Strings.html about strings]. | |||
secondString | |||
[firstString, secondString] | <syntaxhighlight lang="octave">firstString = "hello world"; | ||
< | secondString = "!"; | ||
</ | [firstString, secondString] # concatenate both strings | ||
</ | </syntaxhighlight> | ||
<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 150: | Line 184: | ||
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]. | ||
<syntaxhighlight lang="octave"># Print 'Foo' if divisible by 7, | |||
# 'Fizz' if divisible by 3, | |||
# 'Buzz' if divisible by 5, | |||
# 'FizzBuzz' if divisible by 3 and 5 | |||
outputString | for i = 1:1:20 | ||
outputString = ""; | |||
outputString = [outputString, | if (rem (i, 3) == 0) # rem is the remainder function | ||
outputString = [outputString, "Fizz"]; | |||
endif | |||
outputString = [outputString, | if (rem (i, 5) == 0) | ||
outputString = [outputString, "Buzz"]; | |||
outputString | elseif (rem(i,7) == 0) | ||
outputString = "Foo"; | |||
else | |||
outputString = outputString; | outputString = outputString; | ||
endif | |||
fprintf( | fprintf("i=%g: %s \n", i, outputString); | ||
endfor</syntaxhighlight> | |||
<syntaxhighlight lang="text">i=1: | |||
i=2: | i=2: | ||
i=3: Fizz | i=3: Fizz | ||
Line 188: | Line 225: | ||
i=19: | i=19: | ||
i=20: Buzz | i=20: Buzz | ||
</ | </syntaxhighlight> | ||
Line 194: | Line 231: | ||
= Getting Help = | = Getting Help = | ||
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.< | Octave prompt to print documentation for any function. | ||
doc plot</ | |||
<syntaxhighlight lang="octave">help plot | |||
doc plot</syntaxhighlight> | |||
= Octave | = 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 | |||
the functionality of Octave’s core library. (Matlab users: | the functionality of Octave’s core library. (Matlab users: Packages | ||
act similarly to Matlab’s toolboxes.) The | 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 visit its [https://gnu-octave.github.io/packages/image/ page on Octave Packages,] copy the install command and run it in octave | ||
pkg load image | <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 # load new functions into workspace | |||
</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]] |