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