Using Octave: Difference between revisions

Jump to navigation Jump to search
429 bytes removed ,  28 October 2016
Code examples in wiki markup
(wiki markup for links)
(Code examples in wiki markup)
Line 39: 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;
<syntaxhighlight lang="octave">x = 3/4 * pi;
y = sin(x)</pre>
y = sin (x)</syntaxhighlight>
<pre class="oct-code-output">y =  0.70711</pre>
<syntaxhighlight>y =  0.70711</syntaxhighlight>
 




Line 51: Line 52:
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>
<syntaxhighlight lang="octave">rowVec = [8 6 4]</syntaxhighlight>
<pre class="oct-code-output">rowVec =
<syntaxhighlight>rowVec =
   8  6  4
   8  6  4
</pre>
</syntaxhighlight>


<pre class="oct-code">columnVec = [8; 6; 4]</pre>
<syntaxhighlight lang="octave">columnVec = [8; 6; 4]</syntaxhighlight>
<pre class="oct-code-output">columnVec =
<syntaxhighlight>columnVec =
   8
   8
   6
   6
   4
   4
</pre>
</syntaxhighlight>


<pre class="oct-code">mat = [8 6 4; 2 0 -2]</pre>
<syntaxhighlight lang="octave">mat = [8 6 4; 2 0 -2]</syntaxhighlight>
<pre class="oct-code-output">mat =
<syntaxhighlight>mat =
   8  6  4
   8  6  4
   2  0  -2
   2  0  -2
</pre>
</syntaxhighlight>


<pre class="oct-code">size(mat)</pre>
<syntaxhighlight lang="octave">size(mat)</syntaxhighlight>
<pre class="oct-code-output">ans =
<syntaxhighlight>ans =
   2  3
   2  3
</pre>
</syntaxhighlight>


<pre class="oct-code">length(rowVec)</pre>
<syntaxhighlight lang="octave">length(rowVec)</syntaxhighlight>
<pre class="oct-code-output">ans =  3</pre>
<syntaxhighlight>ans =  3</syntaxhighlight>




Line 85: Line 86:
[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>
<syntaxhighlight lang="octave">columnVec * rowVec</syntaxhighlight>
<pre class="oct-code-output">ans =
<syntaxhighlight>ans =
   64  48  32
   64  48  32
   48  36  24
   48  36  24
   32  24  16
   32  24  16
</pre>
</syntaxhighlight>


<pre class="oct-code">rowVec * columnVec</pre>
<syntaxhighlight lang="octave">rowVec * columnVec</syntaxhighlight>
<pre class="oct-code-output">ans =  116</pre>
<syntaxhighlight>ans =  116</syntaxhighlight>


<pre class="oct-code">columnVec'</pre>
<syntaxhighlight lang="octave">columnVec'</pre>
<pre class="oct-code-output">ans =
<syntaxhighlight>ans =
   8  6  4
   8  6  4
</pre>
</syntaxhighlight>




Line 108: Line 109:
[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>
<syntaxhighlight lang="octave">mat(2,3)</syntaxhighlight>
<pre class="oct-code-output">ans = -2</pre>
<syntaxhighlight>ans = -2</syntaxhighlight>




Line 119: Line 120:
[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);
<syntaxhighlight lang="octave">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 130: Line 131:
   y(i) = k^2;
   y(i) = k^2;
   k = k + step;
   k = k + step;
endwhile</pre>
endwhile</syntaxhighlight>




Line 142: Line 143:
[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
<syntaxhighlight lang="octave">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>
</syntaxhighlight>




Line 157: 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].


<pre class="oct-code">plot (i/10, w);
<syntaxhighlight lang="octave">plot (i/10, w);
title (<span class="string">'w = sin(i/10)'</span>);
title ('w = sin(i/10)');
xlabel (<span class="string">'i ÷ 10'</span>);
xlabel ('i ÷ 10');
ylabel (<span class="string">'w'</span>);</pre>
ylabel ('w');</syntaxhighlight>


<img src="octave_basics-1.png" alt="octave_basics-1.png">
<img src="octave_basics-1.png" alt="octave_basics-1.png">
Line 173: Line 174:
[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>;
<syntaxhighlight lang="octave">firstString = "hello world";
secondString = <span class="string">"!"</span>;
secondString = "!";
[firstString, secondString] <span class="comment"># concatenate both strings</span>
[firstString, secondString] # concatenate both strings
</pre><pre class="oct-code-output">ans = hello world!
</syntaxhighlight>
</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>ans = hello world!</syntaxhighlight>
</pre>
 
<syntaxhighlight lang="octave">fprintf ("%s %.10f \n", "The number is:", 10)</syntaxhighlight>
<syntaxhighlight>The number is: 10.0000000000</syntaxhighlight>




Line 187: Line 190:
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>
<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;
   <span class="keyword">endif</span>
   endif
   fprintf(<span class="string">"i=%g: %s \n"</span>, i, outputString);
   fprintf("i=%g: %s \n", i, outputString);
<span class="keyword">endfor</span></pre>
endfor</syntaxhighlight>


<pre class="oct-code-output">i=1:   
<syntaxhighlight>i=1:   
i=2:   
i=2:   
i=3: Fizz  
i=3: Fizz  
Line 226: Line 229:
i=19:   
i=19:   
i=20: Buzz  
i=20: Buzz  
</pre>
</syntaxhighlight>




Line 235: Line 238:
Octave prompt to print documentation for any function.
Octave prompt to print documentation for any function.


<pre class="pre-code">help plot
<syntaxhighlight lang="octave">help plot
doc plot</pre>
doc plot</syntaxhighlight>




Line 249: Line 252:
from the Forge, use:
from the Forge, use:


<pre class="pre-code">pkg install -forge image <span class="comment"># install package</span>
<syntaxhighlight lang="octave">pkg install -forge image # install package
pkg load image          <span class="comment"># load new functions into workspace</span>
pkg load image          # load new functions into workspace
</pre>
</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]]

Navigation menu