Mercurial > hg > octave-lyh
comparison WWW/readme.html @ 2594:64dcc39370e3
[project @ 1996-12-14 16:28:46 by jwe]
author | jwe |
---|---|
date | Sat, 14 Dec 1996 16:28:46 +0000 |
parents | 140fd45c1b84 |
children | 843a5f07e9c7 |
comparison
equal
deleted
inserted
replaced
2593:3190d595d0da | 2594:64dcc39370e3 |
---|---|
24 other numerical experiments. It may also be used as a batch-oriented | 24 other numerical experiments. It may also be used as a batch-oriented |
25 language. | 25 language. |
26 </p> | 26 </p> |
27 | 27 |
28 <h2><a name="Language Features">Language Features</a></h2> | 28 <h2><a name="Language Features">Language Features</a></h2> |
29 <p> | |
30 The best way to introduce Octave's language is probably to show a few | |
31 simple examples. | |
32 </p> | |
33 | |
34 <p> | |
35 If you are new to Octave, I recommend that you try these examples to | |
36 begin learning Octave by using it. Lines marked with | |
37 <tt>octave:13></tt> are lines you type, ending each with a carriage | |
38 return. Octave will respond with an answer, or by displaying a graph. | |
39 </p> | |
40 | |
41 <h3>Creating a Matrix</h3> | |
42 | |
43 <p> | |
44 To create a new matrix and store it in a variable so that it you can | |
45 refer to it later, type the command | |
46 | |
47 <pre> | |
48 octave:1> a = [ 1, 1, 2; 3, 5, 8; 13, 21, 34 ] | |
49 </pre> | |
50 | |
51 Octave will respond by printing the matrix in neatly aligned columns. | |
52 Ending a command with a semicolon tells Octave to not print the result | |
53 of a command. For example | |
54 | |
55 <pre> | |
56 octave:2> b = rand (3, 2); | |
57 </pre> | |
58 | |
59 will create a 3 row, 2 column matrix with each element set to a random | |
60 value between zero and one. | |
61 </p> | |
62 | |
63 <p> | |
64 To display the value of any variable, simply type the name of the | |
65 variable. For example, to display the value stored in the matrix | |
66 <tt>b</tt>, type the command | |
67 | |
68 <pre> | |
69 octave:3> b | |
70 </pre> | |
71 | |
72 <h3>Matrix Arithmetic</h3> | |
73 | |
74 <p> | |
75 Octave has a convenient operator notation for performing matrix | |
76 arithmetic. For example, to multiply the matrix <tt>a</tt> by a | |
77 scalar value, type the command | |
78 | |
79 <pre> | |
80 octave:4> 2 * a | |
81 </pre> | |
82 | |
83 <p> | |
84 To multiply the two matrices <tt>a</tt> and <tt>b</tt>, type the | |
85 command | |
86 | |
87 <pre> | |
88 octave:5> a * b | |
89 </pre> | |
90 | |
91 <p> | |
92 To form the matrix product <tt>transpose (a) * a</tt>, type the command | |
93 | |
94 <pre> | |
95 octave:6> a' * a | |
96 </pre> | |
97 </p> | |
98 | |
99 <h3>Solving Linear Equations</h3> | |
100 | |
101 <p> | |
102 To solve the set of linear equations <tt>Ax = b</tt> use the left | |
103 division operator, <tt>\</tt>: | |
104 | |
105 <pre> | |
106 octave:7> a \ b | |
107 </pre> | |
108 | |
109 This is conceptually equivalent to <tt>inv (A) * b</tt>, but avoids | |
110 computing the inverse of a matrix directly. | |
111 </p> | |
112 | |
113 <p> | |
114 If the coefficient matrix is singular, Octave will print a warning | |
115 message and compute a minimum norm solution. | |
116 </p> | |
117 | |
118 <h3>Integrating Differential Equations</h3> | |
119 | |
120 <p> | |
121 Octave has built-in functions for solving nonlinear differential | |
122 equations of the form | |
123 | |
124 <pre> | |
125 dx | |
126 -- = f (x, t) | |
127 dt | |
128 </pre> | |
129 | |
130 with the initial condition | |
131 | |
132 <pre> | |
133 x(t = t0) = x0 | |
134 </pre> | |
135 </p> | |
136 | |
137 <p> | |
138 For Octave to integrate equations of this form, you must first provide a | |
139 definition of the function <tt> f (x, t)</tt>. This is | |
140 straightforward, and may be accomplished by entering the function body | |
141 directly on the command line. For example, the following commands | |
142 define the right hand side function for an interesting pair of | |
143 nonlinear differential equations. Note that while you are entering a | |
144 function, Octave responds with a different prompt, to indicate that it | |
145 is waiting for you to complete your input. | |
146 | |
147 <pre> | |
148 octave:8> function xdot = f (x, t) | |
149 > | |
150 > r = 0.25; | |
151 > k = 1.4; | |
152 > a = 1.5; | |
153 > b = 0.16; | |
154 > c = 0.9; | |
155 > d = 0.8; | |
156 > | |
157 > xdot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1)); | |
158 > xdot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2); | |
159 > | |
160 > endfunction | |
161 </pre> | |
162 </p> | |
163 | |
164 <p> | |
165 Given the initial condition | |
166 | |
167 <pre> | |
168 x0 = [1; 2]; | |
169 </pre> | |
170 | |
171 and the set of output times as a column vector (note that the first | |
172 output time corresponds to the initial condition given above) | |
173 | |
174 <pre> | |
175 t = linspace (0, 50, 200)'; | |
176 </pre> | |
177 | |
178 it is easy to integrate the set of differential equations: | |
179 | |
180 <pre> | |
181 x = lsode ("f", x0, t); | |
182 </pre> | |
183 | |
184 <p> | |
185 The function <tt>lsode</tt> uses the Livermore Solver for Ordinary | |
186 Differential Equations, described in A. C. Hindmarsh, <em>ODEPACK, a | |
187 Systematized Collection of ODE Solvers</em>, in: Scientific Computing, | |
188 R. S. Stepleman et al. (Eds.), North-Holland, Amsterdam, 1983, pages | |
189 55-64. | |
190 </p> | |
191 | |
192 <h3>Producing Graphical Output</h3> | |
193 | |
194 <p> | |
195 To display the solution of the previous example graphically, use the | |
196 command | |
197 | |
198 <pre> | |
199 plot (t, x) | |
200 </pre> | |
201 </p> | |
202 | |
203 <p> | |
204 If you are using the X Window System, Octave will automatically create | |
205 a separate window to display the plot. If you are using a terminal that | |
206 supports some other graphics commands, you will need to tell Octave what | |
207 kind of terminal you have. Type the command | |
208 </p> | |
209 | |
210 <pre> | |
211 gset term | |
212 </pre> | |
213 | |
214 to see a list of the supported terminal types. Octave uses | |
215 <tt>gnuplot</tt> to display graphics, and can display graphics on any | |
216 terminal that is supported by <tt>gnuplot</tt>. | |
217 | |
218 <p> | |
219 To capture the output of the plot command in a file rather than sending | |
220 the output directly to your terminal, you can use a set of commands like | |
221 this | |
222 | |
223 <pre> | |
224 gset term postscript | |
225 gset output "foo.ps" | |
226 replot | |
227 </pre> | |
228 | |
229 This will work for other types of output devices as well. Octave's | |
230 <tt>gset</tt> command is really just piped to the <tt>gnuplot</tt> | |
231 subprocess, so that once you have a plot on the screen that you like, | |
232 you should be able to do something like this to create an output file | |
233 suitable for your graphics printer. | |
234 </p> | |
235 | |
236 <p> | |
237 Or, you can eliminate the intermediate file by using commands like this | |
238 | |
239 <pre> | |
240 gset term postscript | |
241 gset output "|lpr -Pname_of_your_graphics_printer" | |
242 replot | |
243 </pre> | |
244 | |
245 <h3>Editing What You Have Typed</h3> | |
246 | |
247 <p> | |
248 At the Octave prompt, you can recall, edit, and reissue previous | |
249 commands using Emacs- or vi-style editing commands. The default | |
250 keybindings use Emacs-style commands. | |
251 </p> | |
252 | |
253 <h3>Getting Help</h3> | |
254 | |
255 <p> | |
256 Octave has an extensive help facility. The same documentation that is | |
257 available in printed form is also available from the Octave prompt, | |
258 because both forms of the documentation are created from the same input | |
259 file. | |
260 </p> | |
261 | |
262 <p> | |
263 In order to get good help you first need to know the name of the command | |
264 that you want to use. This name of the function may not always be | |
265 obvious, but a good place to start is to just type <tt>help</tt>. | |
266 This will show you all the operators, reserved words, functions, | |
267 built-in variables, and function files. You can then get more | |
268 help on anything that is listed by simply including the name as an | |
269 argument to help. For example, | |
270 | |
271 <pre> | |
272 help plot | |
273 </pre> | |
274 | |
275 will display the help text for the <tt>plot</tt> function. | |
276 </p> | |
277 | |
278 <p> | |
279 The complete text of the manual is availabe from Octave's command line | |
280 using the command <tt>help -i</tt>. Because it is written in Texinfo, | |
281 it is also possible to put | |
282 <a href="http://www.che.wisc.edu/cgi-bin/info2www?(octave)">the manual | |
283 on the WWW</a>. | |
284 </p> | |
29 | 285 |
30 <h2><a name="Distribution Terms">Distribution Terms</a></h2> | 286 <h2><a name="Distribution Terms">Distribution Terms</a></h2> |
31 <p> | 287 <p> |
32 Octave is free software; you can redistribute it and/or modify it | 288 Octave is free software; you can redistribute it and/or modify it |
33 under the terms of the | 289 under the terms of the |