Editing Using Octave
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 41: | Line 39: | ||
[https://www.gnu.org/software/octave/doc/interpreter/Arithmetic.html about elementary math]. | [https://www.gnu.org/software/octave/doc/interpreter/Arithmetic.html about elementary math]. | ||
< | <pre class="oct-code">x = 3/4*pi; | ||
y = sin (x)</ | y = sin(x)</pre> | ||
< | <pre class="oct-code-output">y = 0.70711</pre> | ||
Line 54: | Line 51: | ||
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]. | [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 = | ||
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 = | ||
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 = | ||
2 3 | 2 3 | ||
</ | </pre> | ||
< | <pre class="oct-code">length(rowVec)</pre> | ||
< | <pre class="oct-code-output">ans = 3</pre> | ||
Line 88: | Line 85: | ||
[https://www.gnu.org/software/octave/doc/interpreter/Linear-Algebra.html about linear algebra]. | [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 class="oct-code">columnVec'</pre> | ||
< | <pre class="oct-code-output">ans = | ||
8 6 4 | 8 6 4 | ||
</ | </pre> | ||
= Accessing Elements = | = Accessing Elements = | ||
Line 109: | Line 108: | ||
[https://www.gnu.org/software/octave/doc/interpreter/Index-Expressions.html about accessing elements]. | [https://www.gnu.org/software/octave/doc/interpreter/Index-Expressions.html about accessing elements]. | ||
< | <pre class="oct-code">mat(2,3)</pre> | ||
< | <pre class="oct-code-output">ans = -2</pre> | ||
= 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 120: | Line 119: | ||
[https://www.gnu.org/software/octave/doc/interpreter/Statements.html about control flow]. | [https://www.gnu.org/software/octave/doc/interpreter/Statements.html about control flow]. | ||
< | <pre class="oct-code">x = zeros (50,1); | ||
for i = 1:2:100 # iterate from 1 to 100 with step size 2 | for i = 1:2:100 # iterate from 1 to 100 with step size 2 | ||
x(i) = i^2; | x(i) = i^2; | ||
Line 128: | Line 127: | ||
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</ | endwhile</pre> | ||
= Vectorization = | = Vectorization = | ||
Line 141: | Line 142: | ||
[https://www.gnu.org/software/octave/doc/interpreter/Vectorization-and-Faster-Code-Execution.html about vectorization]. | [https://www.gnu.org/software/octave/doc/interpreter/Vectorization-and-Faster-Code-Execution.html about vectorization]. | ||
< | <pre class="oct-code">i = 1:2:100; # create an array with 50-elements | ||
x = i.^2; # each element is squared | x = i.^2; # each element is squared | ||
y = x + 9; # add 9 to each element | y = x + 9; # add 9 to each element | ||
z = y./i; # divide each element in y by the corresponding value in i | 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 | w = sin (i / 10); # take the sine of each element divided by 10 | ||
</ | </pre> | ||
Line 156: | Line 157: | ||
[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]. | ||
< | <pre class="oct-code">plot (i/10, w); | ||
title ('w = sin (i / 10)'); | title (<span class="string">'w = sin(i/10)'</span>); | ||
xlabel ('i | xlabel (<span class="string">'i ÷ 10'</span>); | ||
ylabel ('w');</ | ylabel (<span class="string">'w'</span>);</pre> | ||
<img src="octave_basics-1.png" alt="octave_basics-1.png"> | |||
= Strings = | = Strings = | ||
Line 170: | Line 173: | ||
[https://www.gnu.org/software/octave/doc/interpreter/Strings.html about strings]. | [https://www.gnu.org/software/octave/doc/interpreter/Strings.html about strings]. | ||
< | <pre class="oct-code">firstString = <span class="string">"hello world"</span>; | ||
secondString = "!"; | secondString = <span class="string">"!"</span>; | ||
[firstString, secondString] # concatenate both strings | [firstString, secondString] <span class="comment"># concatenate both strings</span> | ||
</ | </pre><pre class="oct-code-output">ans = hello world! | ||
< | </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 | ||
</pre> | |||
< | |||
< | |||
Line 186: | Line 187: | ||
Read more [https://www.gnu.org/software/octave/doc/interpreter/The-if-Statement.html in the manual]. | Read more [https://www.gnu.org/software/octave/doc/interpreter/The-if-Statement.html in the manual]. | ||
< | <pre class="oct-code"><span class="comment"># Print 'Foo' if divisible by 7,</span> | ||
# 'Fizz' if divisible by 3, | <span class="comment"># 'Fizz' if divisible by 3,</span> | ||
# 'Buzz' if divisible by 5, | <span class="comment"># 'Buzz' if divisible by 5,</span> | ||
# 'FizzBuzz' if divisible by 3 and 5 | <span class="comment"># 'FizzBuzz' if divisible by 3 and 5</span> | ||
for i = 1:1:20 | <span class="keyword">for</span> i = 1:1:20 | ||
outputString = ""; | outputString = <span class="string">""</span>; | ||
if (rem (i, 3) == 0) # rem is the remainder function | <span class="keyword">if</span> (rem (i, 3) == 0) <span class="comment"># rem is the remainder function</span> | ||
outputString = [outputString, "Fizz"]; | outputString = [outputString, <span class="string">"Fizz"</span>]; | ||
endif | <span class="keyword">endif</span> | ||
if (rem (i, 5) == 0) | <span class="keyword">if</span> (rem (i, 5) == 0) | ||
outputString = [outputString, "Buzz"]; | outputString = [outputString, <span class="string">"Buzz"</span>]; | ||
elseif (rem(i,7) == 0) | <span class="keyword">elseif</span> (rem(i,7) == 0) | ||
outputString = "Foo"; | outputString = <span class="string">"Foo"</span>; | ||
else | <span class="keyword">else</span> | ||
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</ | <span class="keyword">endfor</span></pre> | ||
< | <pre class="oct-code-output">i=1: | ||
i=2: | i=2: | ||
i=3: Fizz | i=3: Fizz | ||
Line 225: | Line 226: | ||
i=19: | i=19: | ||
i=20: Buzz | i=20: Buzz | ||
</ | </pre> | ||
Line 234: | Line 235: | ||
Octave prompt to print documentation for any function. | Octave prompt to print documentation for any function. | ||
< | <pre class="pre-code">help plot | ||
doc plot</ | doc plot</pre> | ||
= 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: | |||
< | <pre class="pre-code">pkg install -forge image <span class="comment"># install package</span> | ||
pkg load image # load new functions into workspace | pkg load image <span class="comment"># load new functions into workspace</span> | ||
</ | </pre> | ||
[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]] |