comparison examples.md @ 43:a1cb50997055

Reviewed and reformatted some files.
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Wed, 31 Aug 2016 01:08:06 +0200
parents 21fd6254edc3
children 171ca967fcc9 7609e2a6faef
comparison
equal deleted inserted replaced
42:ecee119c2378 43:a1cb50997055
3 title: Examples 3 title: Examples
4 permalink: /examples/ 4 permalink: /examples/
5 menu: true 5 menu: true
6 --- 6 ---
7 7
8 ### Using Octave 8 ### Using Octave
9 9
10 First, follow the [installation guide]({{ site.baseurl/install/ }}) to install Octave on your system. Then, launch the interactive prompt by typing `octave` in a terminal or by clicking the icon in the programs menu. For further guidance, see the manual page on [Running Octave]({{site.docs_url}}Running-Octave.html). 10 First, follow the [installation guide]({{ site.baseurl/install/ }}) to install Octave on your system. Then, launch the interactive prompt by typing `octave` in a terminal or by clicking the icon in the programs menu. For further guidance, see the manual page on [Running Octave]({{site.docs_url}}Running-Octave.html).
11 11
12 ### Language Basics 12 ### Language Basics
13 13
33 100 33 100
34 {% endhighlight %} 34 {% endhighlight %}
35 35
36 ###### [Math]({{site.docs_url}}Arithmetic.html) 36 ###### [Math]({{site.docs_url}}Arithmetic.html)
37 37
38 Many mathematical operators are available in addition to the standard arithmetic. Operations are floating-point. 38 Many mathematical operators are available in addition to the standard arithmetic.
39 Operations are floating-point.
39 {% highlight matlab %} 40 {% highlight matlab %}
40 x = 3/4*pi; 41 x = 3/4*pi;
41 y = sin(x) 42 y = sin(x)
42 0.70711 43 0.70711
43 {% endhighlight %} 44 {% endhighlight %}
44 45
45 ###### [Matrices]({{site.docs_url}}Matrices.html) 46 ###### [Matrices]({{site.docs_url}}Matrices.html)
46 47
47 Arrays in Octave are called matrices. One-dimensional matrices are referred to as vectors. Use `space` or `,` to separate elements and `;` to start a new row. 48 Arrays in Octave are called matrices.
49 One-dimensional matrices are referred to as vectors.
50 Use `space` or `,` to separate elements and `;` to start a new row.
48 51
49 {% highlight matlab %} 52 {% highlight matlab %}
50 rowVec = [8 6 4] 53 rowVec = [8 6 4]
51 8 6 4 54 8 6 4
52 columnVec = [8; 6; 4] 55 columnVec = [8; 6; 4]
105 end 108 end
106 {% endhighlight %} 109 {% endhighlight %}
107 110
108 ###### [Vectorization]({{site.docs_url}}Vectorization-and-Faster-Code-Execution.html) 111 ###### [Vectorization]({{site.docs_url}}Vectorization-and-Faster-Code-Execution.html)
109 112
110 For-loops can often be replaced or simplified using vector syntax. The operators `*`,`/`,`^`,`%` all support element-wise operations using `.`. Many other functions operate element-wise by default (`sin`,`+`,`-`, etc.). 113 For-loops can often be replaced or simplified using vector syntax.
114 The operators `*`,`/`,`^`,`%` all support element-wise operations using `.`.
115 Many other functions operate element-wise by default (`sin`,`+`,`-`, etc.).
111 116
112 {% highlight matlab %} 117 {% highlight matlab %}
113 i = 1:2:100; % create a 50-element array 118 i = 1:2:100; % create a 50-element array
114 x = i.^2; % each element is squared 119 x = i.^2; % each element is squared
115 y = x + 9; % add 9 to each element 120 y = x + 9; % add 9 to each element
131 <img src="{{site.baseurl}}/img/sinePlot.png" alt="sine plot" style="height: 20rem; width: auto;" /> 136 <img src="{{site.baseurl}}/img/sinePlot.png" alt="sine plot" style="height: 20rem; width: auto;" />
132 137
133 138
134 ###### [Strings]({{site.docs_url}}Strings.html) 139 ###### [Strings]({{site.docs_url}}Strings.html)
135 140
136 Strings are simply arrays of characters. Strings can be composed using `printf`-style formatting with `sprintf` and `fprintf`. 141 Strings are simply arrays of characters.
142 Strings can be composed using `printf`-style formatting with `sprintf` and `fprintf`.
137 {% highlight matlab %} 143 {% highlight matlab %}
138 firstString = 'hello world'; 144 firstString = 'hello world';
139 secondString = '!'; 145 secondString = '!';
140 [firstString,secondString] 146 [firstString,secondString]
141 hello world! 147 hello world!
159 end 165 end
160 if (rem(i,5) == 0) 166 if (rem(i,5) == 0)
161 outputString = [outputString, 'Buzz']; 167 outputString = [outputString, 'Buzz'];
162 elseif (rem(i,7) == 0) 168 elseif (rem(i,7) == 0)
163 outputString = 'Foo'; 169 outputString = 'Foo';
164 else 170 else
165 outputString = outputString; 171 outputString = outputString;
166 end 172 end
167 fprintf('i=%g: %s \n',i,outputString); 173 fprintf('i=%g: %s \n',i,outputString);
168 end 174 end
169 {% endhighlight %} 175 {% endhighlight %}
186 doc plot 192 doc plot
187 {% endhighlight %} 193 {% endhighlight %}
188 194
189 ###### [Packages]({{site.docs_url}}Packages.html) 195 ###### [Packages]({{site.docs_url}}Packages.html)
190 196
191 Community-developed packages can be added from the [Octave Forge](http://octave.sourceforge.net/index.html) to extend the functionality of Octave's core library. (Matlab users: Forge packages act similarly to Matlab's toolboxes.) The `pkg` command is used to manage these packages. For example, to use the image processing library from the Forge, use: 197 Community-developed packages can be added from the
198 [Octave Forge](http://octave.sourceforge.net/index.html)
199 to extend the functionality of Octave's core library.
200 (Matlab users: Forge packages act similarly to Matlab's toolboxes.)
201 The `pkg` command is used to manage these packages.
202 For example, to use the image processing library from the Forge, use:
192 203
193 {% highlight matlab %} 204 {% highlight matlab %}
194 pkg install -forge image % install package 205 pkg install -forge image % install package
195 pkg load image % load new functions into workspace 206 pkg load image % load new functions into workspace
196 {% endhighlight %} 207 {% endhighlight %}
197 208
198 <div id="assignmentModal" class="reveal-modal tiny" data-reveal aria-hidden="true" role="dialog"> 209 <div id="assignmentModal" class="reveal-modal tiny" data-reveal aria-hidden="true" role="dialog">
199 {% highlight matlab %} 210 {% highlight matlab %}
200 a = 1 211 a = 1
201 b = a 212 b = a