Editing Using Octave
Jump to navigation
Jump to search
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 | First, follow the [https://www.gnu.org/software/octave/doc/interpreter/Installation.html installation guide] | ||
to install GNU Octave on your system. Then, launch the interactive prompt by | |||
typing <code>octave</code> in a terminal or by clicking the icon in the programs menu. | |||
For further guidance, see the manual page on | |||
[https://www.gnu.org/software/octave/doc/interpreter/Running-Octave.html Running Octave]. | |||
Then, | |||
= Variable Assignment = | = Variable Assignment = | ||
Line 29: | Line 27: | ||
<syntaxhighlight lang="octave">t = 99 + 1 # prints 't = 100'</syntaxhighlight> | <syntaxhighlight lang="octave">t = 99 + 1 # prints 't = 100'</syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>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 | <syntaxhighlight> 100</syntaxhighlight> | ||
= Elementary math = | = Elementary math = | ||
Line 43: | Line 41: | ||
<syntaxhighlight lang="octave">x = 3/4 * pi; | <syntaxhighlight lang="octave">x = 3/4 * pi; | ||
y = sin (x)</syntaxhighlight> | y = sin (x)</syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>y = 0.70711</syntaxhighlight> | ||
Line 55: | Line 53: | ||
[https://www.gnu.org/software/octave/doc/interpreter/Linear-Algebra.html about matrices]. | [https://www.gnu.org/software/octave/doc/interpreter/Linear-Algebra.html about matrices]. | ||
<syntaxhighlight lang="octave">rowVec = [8 6 4]</syntaxhighlight> | <syntaxhighlight lang="octave">rowVec = [8 6 4]</syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>rowVec = | ||
8 6 4 | 8 6 4 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="octave">columnVec = [8; 6; 4]</syntaxhighlight> | <syntaxhighlight lang="octave">columnVec = [8; 6; 4]</syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>columnVec = | ||
8 | 8 | ||
6 | 6 | ||
Line 67: | Line 65: | ||
<syntaxhighlight lang="octave">mat = [8 6 4; 2 0 -2]</syntaxhighlight> | <syntaxhighlight lang="octave">mat = [8 6 4; 2 0 -2]</syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>mat = | ||
8 6 4 | 8 6 4 | ||
2 0 -2 | 2 0 -2 | ||
Line 73: | Line 71: | ||
<syntaxhighlight lang="octave">size(mat)</syntaxhighlight> | <syntaxhighlight lang="octave">size(mat)</syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>ans = | ||
2 3 | 2 3 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="octave">length(rowVec)</syntaxhighlight> | <syntaxhighlight lang="octave">length(rowVec)</syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>ans = 3</syntaxhighlight> | ||
Line 89: | Line 87: | ||
<syntaxhighlight lang="octave">columnVec * rowVec</syntaxhighlight> | <syntaxhighlight lang="octave">columnVec * rowVec</syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>ans = | ||
64 48 32 | 64 48 32 | ||
48 36 24 | 48 36 24 | ||
Line 96: | Line 94: | ||
<syntaxhighlight lang="octave">rowVec * columnVec</syntaxhighlight> | <syntaxhighlight lang="octave">rowVec * columnVec</syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>ans = 116</syntaxhighlight> | ||
<syntaxhighlight lang="octave">columnVec'</ | <syntaxhighlight lang="octave">columnVec'</pre> | ||
<syntaxhighlight | <syntaxhighlight>ans = | ||
8 6 4 | 8 6 4 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Accessing Elements = | = Accessing Elements = | ||
Line 110: | Line 110: | ||
<syntaxhighlight lang="octave">mat(2,3)</syntaxhighlight> | <syntaxhighlight lang="octave">mat(2,3)</syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>ans = -2</syntaxhighlight> | ||
= Control flow | = Control flow wih 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 | ||
Line 128: | Line 128: | ||
k = 1; | k = 1; | ||
step = 2; | step = 2; | ||
while (k <= 100) | while (k <= (100-step)) | ||
y( | y(i) = k^2; | ||
k = k + step; | k = k + step; | ||
endwhile</syntaxhighlight> | endwhile</syntaxhighlight> | ||
= Vectorization = | = Vectorization = | ||
Line 156: | Line 158: | ||
[https://www.gnu.org/software/octave/doc/interpreter/Two_002dDimensional-Plots.html about plotting]. | [https://www.gnu.org/software/octave/doc/interpreter/Two_002dDimensional-Plots.html about plotting]. | ||
<syntaxhighlight lang="octave">plot (i / 10, w); | <syntaxhighlight lang="octave">plot (i/10, w); | ||
title ('w = sin (i / 10)'); | title ('w = sin(i/10)'); | ||
xlabel ('i | xlabel ('i ÷ 10'); | ||
ylabel ('w');</syntaxhighlight> | ylabel ('w');</syntaxhighlight> | ||
<img src="octave_basics-1.png" alt="octave_basics-1.png"> | |||
= Strings = | = Strings = | ||
Line 174: | Line 178: | ||
[firstString, secondString] # concatenate both strings | [firstString, secondString] # concatenate both strings | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>ans = hello world!</syntaxhighlight> | ||
<syntaxhighlight lang="octave">fprintf ("%s %.10f \n", "The number is:", 10)</syntaxhighlight> | <syntaxhighlight lang="octave">fprintf ("%s %.10f \n", "The number is:", 10)</syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>The number is: 10.0000000000</syntaxhighlight> | ||
Line 205: | Line 209: | ||
endfor</syntaxhighlight> | endfor</syntaxhighlight> | ||
<syntaxhighlight | <syntaxhighlight>i=1: | ||
i=2: | i=2: | ||
i=3: Fizz | i=3: Fizz | ||
Line 239: | Line 243: | ||
= Octave packages = | = Octave forge packages = | ||
Community-developed packages can be added from the | Community-developed packages can be added from the | ||
[ | [http://octave.sourceforge.net/index.html Octave Forge] website to extend | ||
the functionality of Octave’s core library. (Matlab users: | 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 [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: | |||
<syntaxhighlight lang="octave">pkg install | <syntaxhighlight lang="octave">pkg install -forge image # install package | ||
pkg load image # load new functions into workspace | pkg load image # load new functions into workspace | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[https://www.gnu.org/software/octave/doc/interpreter/Packages.html Read more about packages]. | [https://www.gnu.org/software/octave/doc/interpreter/Packages.html Read more about packages]. | ||
[[Category:Tutorials]] | [[Category:Tutorials]] |