Mercurial > web-octave
annotate examples.md @ 92:7609e2a6faef
maint: revert changeset 40 (2ff331fce678) .gitignore -> .hgignore.
author | Kai T. Ohlhus <k.ohlhus@gmail.com> |
---|---|
date | Tue, 01 Nov 2016 00:28:55 +0100 |
parents | a1cb50997055 |
children |
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 |
92
7609e2a6faef
maint: revert changeset 40 (2ff331fce678) .gitignore -> .hgignore.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
43
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 |
92
7609e2a6faef
maint: revert changeset 40 (2ff331fce678) .gitignore -> .hgignore.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
43
diff
changeset
|
38 Many mathematical operators are available in addition to the standard arithmetic. Operations are floating-point. |
23
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
39 {% highlight matlab %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
40 x = 3/4*pi; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
41 y = sin(x) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
42 0.70711 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
43 {% endhighlight %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
44 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
45 ###### [Matrices]({{site.docs_url}}Matrices.html) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
46 |
92
7609e2a6faef
maint: revert changeset 40 (2ff331fce678) .gitignore -> .hgignore.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
43
diff
changeset
|
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. |
23
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
48 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
49 {% highlight matlab %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
50 rowVec = [8 6 4] |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
51 8 6 4 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
52 columnVec = [8; 6; 4] |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
53 8 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
54 6 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
55 4 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
56 mat = [8 6 4; 2 0 -2] |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
57 8 6 4 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
58 2 0 -2 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
59 size(mat) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
60 2 3 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
61 length(rowVec) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
62 3 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
63 {% endhighlight %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
64 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
65 ###### [Linear Algebra]({{site.docs_url}}Linear-Algebra.html) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
66 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
67 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
|
68 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
69 {% highlight matlab %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
70 columnVec * rowVec |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
71 64 48 32 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
72 48 36 24 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
73 32 24 16 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
74 rowVec * columnVec |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
75 116 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
76 columnVec' |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
77 8 6 4 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
78 {% endhighlight %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
79 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
80 ###### [Accessing Elements]({{site.docs_url}}Index-Expressions.html) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
81 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
82 Octave is 1-indexed. Matrix elements are accessed as `matrix(rowNum, columnNum)`. |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
83 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
84 {% highlight matlab %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
85 mat(2,3) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
86 -2 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
87 {% endhighlight %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
88 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
89 ###### [Iteration]({{site.docs_url}}Statements.html) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
90 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
91 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
|
92 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
93 {% highlight matlab %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
94 x = zeros(50,1); |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
95 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
|
96 x(i) = i^2; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
97 end |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
98 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
99 y = zeros(50,1); |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
100 k = 1; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
101 step = 2; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
102 while k<=(100-step) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
103 y(i) = k^2; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
104 k = k + step; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
105 end |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
106 {% endhighlight %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
107 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
108 ###### [Vectorization]({{site.docs_url}}Vectorization-and-Faster-Code-Execution.html) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
109 |
92
7609e2a6faef
maint: revert changeset 40 (2ff331fce678) .gitignore -> .hgignore.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
43
diff
changeset
|
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.). |
23
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
111 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
112 {% highlight matlab %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
113 i = 1:2:100; % create a 50-element array |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
114 x = i.^2; % each element is squared |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
115 y = x + 9; % add 9 to each element |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
116 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
|
117 w = sin(i/10); % take the sine of each element รท 10 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
118 {% endhighlight %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
119 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
120 ###### [Plotting]({{site.docs_url}}Two_002dDimensional-Plots.html) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
121 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
122 `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
|
123 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
124 {% highlight matlab %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
125 plot(w,i/10); |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
126 title('w = sin(i/10)'; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
127 xlabel('i รท 10'); |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
128 ylabel('w'); |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
129 {% endhighlight %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
130 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
131 <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
|
132 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
133 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
134 ###### [Strings]({{site.docs_url}}Strings.html) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
135 |
92
7609e2a6faef
maint: revert changeset 40 (2ff331fce678) .gitignore -> .hgignore.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
43
diff
changeset
|
136 Strings are simply arrays of characters. 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
|
137 {% highlight matlab %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
138 firstString = 'hello world'; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
139 secondString = '!'; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
140 [firstString,secondString] |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
141 hello world! |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
142 fprintf('%s %.10f \n', 'The number is:', 10) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
143 The number is: 10.0000000000 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
144 {% endhighlight %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
145 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
146 ###### [If-else]({{site.docs_url}}The-if-Statement.html) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
147 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
148 Conditional statements can be used to create branching logic in your code. |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
149 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
150 {% highlight matlab %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
151 % Print 'Foo' if divisible by 7, |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
152 % 'Fizz' if divisible by 3, |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
153 % 'Buzz' if divisible by 5, |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
154 % 'FizzBuzz' if divisible by 3 and 5 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
155 for (i = 1:1:100) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
156 outputString = ''; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
157 if (rem(i,3) == 0) % rem is the remainder function |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
158 outputString = [outputString, 'Fizz']; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
159 end |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
160 if (rem(i,5) == 0) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
161 outputString = [outputString, 'Buzz']; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
162 elseif (rem(i,7) == 0) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
163 outputString = 'Foo'; |
92
7609e2a6faef
maint: revert changeset 40 (2ff331fce678) .gitignore -> .hgignore.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
43
diff
changeset
|
164 else |
23
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
165 outputString = outputString; |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
166 end |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
167 fprintf('i=%g: %s \n',i,outputString); |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
168 end |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
169 {% endhighlight %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
170 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
171 {% highlight text %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
172 ... |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
173 i=12: Fizz |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
174 i=13: |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
175 i=14: Foo |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
176 i=15: FizzBuzz |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
177 ... |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
178 {% endhighlight %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
179 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
180 ###### [Help]({{site.docs_url}}Simple-Examples.html#Help-and-Documentation) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
181 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
182 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
|
183 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
184 {% highlight matlab %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
185 help plot |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
186 doc plot |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
187 {% endhighlight %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
188 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
189 ###### [Packages]({{site.docs_url}}Packages.html) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
190 |
92
7609e2a6faef
maint: revert changeset 40 (2ff331fce678) .gitignore -> .hgignore.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
43
diff
changeset
|
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: |
23
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
192 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
193 {% highlight matlab %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
194 pkg install -forge image % install package |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
195 pkg load image % load new functions into workspace |
92
7609e2a6faef
maint: revert changeset 40 (2ff331fce678) .gitignore -> .hgignore.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
43
diff
changeset
|
196 {% endhighlight %} |
23
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
197 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
198 <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
|
199 {% highlight matlab %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
200 a = 1 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
201 b = a |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
202 b = 2 |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
203 a == b % returns 0 (false) |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
204 {% endhighlight %} |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
205 <a class="close-reveal-modal" aria-label="Close">×</a> |
f37d56cd8195
add examples page
Alex Krolick <whokilledtheelectricmonk@gmail.com>
parents:
diff
changeset
|
206 </div> |