comparison examples.md @ 23:f37d56cd8195

add examples page
author Alex Krolick <whokilledtheelectricmonk@gmail.com>
date Sun, 15 Nov 2015 21:49:15 -0800
parents
children 21fd6254edc3
comparison
equal deleted inserted replaced
22:3c8941fa46da 23:f37d56cd8195
1 ---
2 layout: page
3 title: Examples
4 permalink: /examples/
5 menu: true
6 ---
7
8 ### Using Octave
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).
11
12 ### Language Basics
13
14 Click the headings below to see a manual page related to each topic
15 {: .panel}
16
17 ###### [Variable Assignment]({{site.docs_url}}Variables.html)
18
19 Assign values to variables with `=` (Note: assignment is <a href="#" data-reveal-id="assignmentModal">pass-by-value</a>)
20
21 ###### [Comments]({{site.docs_url}}Comments.html)
22
23 `%` starts a comment block that continues to the end of the line.
24
25 ###### [Evaluation]({{site.docs_url}}Simple-Examples.html)
26
27 The output of every command is printed to the console unless terminated with `;`. The `disp()` command can be used to print output anywhere. Use `exit` to quit the console.
28
29 {% highlight matlab %}
30 t = 99 + 1 % prints 't = 100'
31 t = 99 + 1; % nothing is printed
32 disp(t);
33 100
34 {% endhighlight %}
35
36 ###### [Math]({{site.docs_url}}Arithmetic.html)
37
38 Many mathematical operators are available in addition to the standard arithmetic. Operations are floating-point.
39 {% highlight matlab %}
40 x = 3/4*pi;
41 y = sin(x)
42 0.70711
43 {% endhighlight %}
44
45 ###### [Matrices]({{site.docs_url}}Matrices.html)
46
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
49 {% highlight matlab %}
50 rowVec = [8 6 4]
51 8 6 4
52 columnVec = [8; 6; 4]
53 8
54 6
55 4
56 mat = [8 6 4; 2 0 -2]
57 8 6 4
58 2 0 -2
59 size(mat)
60 2 3
61 length(rowVec)
62 3
63 {% endhighlight %}
64
65 ###### [Linear Algebra]({{site.docs_url}}Linear-Algebra.html)
66
67 Many common linear algebra operations are simple to program using Octave's matrix syntax.
68
69 {% highlight matlab %}
70 columnVec * rowVec
71 64 48 32
72 48 36 24
73 32 24 16
74 rowVec * columnVec
75 116
76 columnVec'
77 8 6 4
78 {% endhighlight %}
79
80 ###### [Accessing Elements]({{site.docs_url}}Index-Expressions.html)
81
82 Octave is 1-indexed. Matrix elements are accessed as `matrix(rowNum, columnNum)`.
83
84 {% highlight matlab %}
85 mat(2,3)
86 -2
87 {% endhighlight %}
88
89 ###### [Iteration]({{site.docs_url}}Statements.html)
90
91 Octave supports `for` and `while` loops, as well as other control flow structures.
92
93 {% highlight matlab %}
94 x = zeros(50,1);
95 for i=1:2:100 % iterate from 1 to 100 with step size 2
96 x(i) = i^2;
97 end
98
99 y = zeros(50,1);
100 k = 1;
101 step = 2;
102 while k<=(100-step)
103 y(i) = k^2;
104 k = k + step;
105 end
106 {% endhighlight %}
107
108 ###### [Vectorization]({{site.docs_url}}Vectorization-and-Faster-Code-Execution.html)
109
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.).
111
112 {% highlight matlab %}
113 i = 1:2:100; % create a 50-element array
114 x = i.^2; % each element is squared
115 y = x + 9; % add 9 to each element
116 z = y./i; % divide each element in y by the corresponding value in i
117 w = sin(i/10); % take the sine of each element ÷ 10
118 {% endhighlight %}
119
120 ###### [Plotting]({{site.docs_url}}Two_002dDimensional-Plots.html)
121
122 `plot` can be called with vector arguments to create 2D line and scatter plots.
123
124 {% highlight matlab %}
125 plot(w,i/10);
126 title('w = sin(i/10)';
127 xlabel('i ÷ 10');
128 ylabel('w');
129 {% endhighlight %}
130
131 <img src="{{site.baseurl}}/img/sinePlot.png" alt="sine plot" style="height: 20rem; width: auto;" />
132
133
134 ###### [Strings]({{site.docs_url}}Strings.html)
135
136 Strings are simply arrays of characters. Strings can be composed using `printf`-style formatting with `sprintf` and `fprintf`.
137 {% highlight matlab %}
138 firstString = 'hello world';
139 secondString = '!';
140 [firstString,secondString]
141 hello world!
142 fprintf('%s %.10f \n', 'The number is:', 10)
143 The number is: 10.0000000000
144 {% endhighlight %}
145
146 ###### [If-else]({{site.docs_url}}The-if-Statement.html)
147
148 Conditional statements can be used to create branching logic in your code.
149
150 {% highlight matlab %}
151 % Print 'Foo' if divisible by 7,
152 % 'Fizz' if divisible by 3,
153 % 'Buzz' if divisible by 5,
154 % 'FizzBuzz' if divisible by 3 and 5
155 for (i = 1:1:100)
156 outputString = '';
157 if (rem(i,3) == 0) % rem is the remainder function
158 outputString = [outputString, 'Fizz'];
159 end
160 if (rem(i,5) == 0)
161 outputString = [outputString, 'Buzz'];
162 elseif (rem(i,7) == 0)
163 outputString = 'Foo';
164 else
165 outputString = outputString;
166 end
167 fprintf('i=%g: %s \n',i,outputString);
168 end
169 {% endhighlight %}
170
171 {% highlight text %}
172 ...
173 i=12: Fizz
174 i=13:
175 i=14: Foo
176 i=15: FizzBuzz
177 ...
178 {% endhighlight %}
179
180 ###### [Help]({{site.docs_url}}Simple-Examples.html#Help-and-Documentation)
181
182 The `help` and `doc` commands can be invoked at the Octave prompt to print documentation for any function.
183
184 {% highlight matlab %}
185 help plot
186 doc plot
187 {% endhighlight %}
188
189 ###### [Packages]({{site.docs_url}}Packages.html)
190
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. The `pkg` command is used to manage these packages. For example, to use the image processing library from the Forge, use:
192
193 {% highlight matlab %}
194 pkg install -forge image % install package
195 pkg load image % load new functions into workspace
196 {% endhighlight %}
197
198 <div id="assignmentModal" class="reveal-modal tiny" data-reveal aria-hidden="true" role="dialog">
199 {% highlight matlab %}
200 a = 1
201 b = a
202 b = 2
203 a == b % returns 0 (false)
204 {% endhighlight %}
205 <a class="close-reveal-modal" aria-label="Close">&#215;</a>
206 </div>