6778
|
1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton |
3294
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
4167
|
5 @node Introduction |
3294
|
6 @chapter A Brief Introduction to Octave |
|
7 @cindex introduction |
|
8 |
6620
|
9 This manual documents how to install, run, and use GNU Octave, and how |
3294
|
10 to report bugs. |
|
11 |
|
12 GNU Octave is a high-level language, primarily intended for numerical |
|
13 computations. It provides a convenient command line interface for |
|
14 solving linear and nonlinear problems numerically, and for performing |
|
15 other numerical experiments. It may also be used as a batch-oriented |
|
16 language. |
|
17 |
|
18 GNU Octave is also freely redistributable software. You may |
|
19 redistribute it and/or modify it under the terms of the GNU General |
|
20 Public License as published by the Free Software Foundation. The GPL is |
|
21 included in this manual in @ref{Copying}. |
|
22 |
|
23 This document corresponds to Octave version @value{VERSION}. |
|
24 |
5775
|
25 @c FIXME -- add explanation about how and why Octave was written. |
3294
|
26 @c |
5775
|
27 @c FIXME -- add a sentence or two explaining that we could |
3294
|
28 @c always use more funding. |
|
29 |
|
30 @menu |
|
31 * Running Octave:: |
|
32 * Simple Examples:: |
|
33 * Conventions:: |
|
34 @end menu |
|
35 |
4167
|
36 @node Running Octave |
3294
|
37 @section Running Octave |
|
38 |
|
39 On most systems, the way to invoke Octave is with the shell command |
|
40 @samp{octave}. Octave displays an initial message and then a prompt |
|
41 indicating it is ready to accept input. You can begin typing Octave |
|
42 commands immediately afterward. |
|
43 |
|
44 If you get into trouble, you can usually interrupt Octave by typing |
|
45 @kbd{Control-C} (usually written @kbd{C-c} for short). @kbd{C-c} gets |
|
46 its name from the fact that you type it by holding down @key{CTRL} and |
|
47 then pressing @key{c}. Doing this will normally return you to Octave's |
|
48 prompt. |
|
49 |
|
50 @cindex exiting octave |
|
51 @cindex quitting octave |
|
52 To exit Octave, type @kbd{quit}, or @kbd{exit} at the Octave prompt. |
|
53 |
|
54 On systems that support job control, you can suspend Octave by sending |
|
55 it a @code{SIGTSTP} signal, usually by typing @kbd{C-z}. |
|
56 |
4167
|
57 @node Simple Examples |
3294
|
58 @section Simple Examples |
|
59 |
|
60 The following chapters describe all of Octave's features in detail, but |
|
61 before doing that, it might be helpful to give a sampling of some of its |
|
62 capabilities. |
|
63 |
|
64 If you are new to Octave, I recommend that you try these examples to |
|
65 begin learning Octave by using it. Lines marked with @samp{octave:13>} |
|
66 are lines you type, ending each with a carriage return. Octave will |
|
67 respond with an answer, or by displaying a graph. |
|
68 |
6623
|
69 @subsection Creating a Matrix |
3294
|
70 |
|
71 To create a new matrix and store it in a variable so that it you can |
|
72 refer to it later, type the command |
|
73 |
|
74 @example |
6620
|
75 octave:1> A = [ 1, 1, 2; 3, 5, 8; 13, 21, 34 ] |
3294
|
76 @end example |
|
77 |
|
78 @noindent |
|
79 Octave will respond by printing the matrix in neatly aligned columns. |
|
80 Ending a command with a semicolon tells Octave to not print the result |
|
81 of a command. For example |
|
82 |
|
83 @example |
6620
|
84 octave:2> B = rand (3, 2); |
3294
|
85 @end example |
|
86 |
|
87 @noindent |
|
88 will create a 3 row, 2 column matrix with each element set to a random |
|
89 value between zero and one. |
|
90 |
|
91 To display the value of any variable, simply type the name of the |
|
92 variable. For example, to display the value stored in the matrix |
6620
|
93 @code{B}, type the command |
3294
|
94 |
|
95 @example |
6620
|
96 octave:3> B |
3294
|
97 @end example |
|
98 |
6623
|
99 @subsection Matrix Arithmetic |
3294
|
100 |
|
101 Octave has a convenient operator notation for performing matrix |
|
102 arithmetic. For example, to multiply the matrix @code{a} by a scalar |
|
103 value, type the command |
|
104 |
|
105 @example |
6620
|
106 octave:4> 2 * A |
3294
|
107 @end example |
|
108 |
6620
|
109 @noindent |
3294
|
110 To multiply the two matrices @code{a} and @code{b}, type the command |
|
111 |
|
112 @example |
6620
|
113 octave:5> A * B |
3294
|
114 @end example |
|
115 |
6620
|
116 @noindent |
|
117 and to form the matrix product |
3294
|
118 @iftex |
|
119 @tex |
6620
|
120 $@code{A}^T@code{A}$, |
3294
|
121 @end tex |
|
122 @end iftex |
6620
|
123 @ifnottex |
|
124 @code{transpose (A) * A}, |
|
125 @end ifnottex |
3294
|
126 type the command |
|
127 |
|
128 @example |
6620
|
129 octave:6> A' * A |
3294
|
130 @end example |
|
131 |
6623
|
132 @subsection Solving Linear Equations |
3294
|
133 |
6620
|
134 To solve the set of linear equations @code{A@var{x} = b}, |
3294
|
135 use the left division operator, @samp{\}: |
|
136 |
|
137 @example |
6620
|
138 octave:7> A \ b |
3294
|
139 @end example |
|
140 |
|
141 @noindent |
|
142 This is conceptually equivalent to |
|
143 @iftex |
|
144 @tex |
6620
|
145 $@code{A}^{-1}@code{b}$, |
3294
|
146 @end tex |
|
147 @end iftex |
6620
|
148 @ifnottex |
3294
|
149 @code{inv (a) * b}, |
6620
|
150 @end ifnottex |
3294
|
151 but avoids computing the inverse of a matrix directly. |
|
152 |
|
153 If the coefficient matrix is singular, Octave will print a warning |
|
154 message and compute a minimum norm solution. |
|
155 |
6623
|
156 @subsection Integrating Differential Equations |
3294
|
157 |
|
158 Octave has built-in functions for solving nonlinear differential |
|
159 equations of the form |
|
160 @iftex |
|
161 @tex |
|
162 $$ |
|
163 {dx \over dt} = f(x,t), \qquad x(t=t_0) = x_0 |
|
164 $$ |
|
165 @end tex |
|
166 @end iftex |
6620
|
167 @ifnottex |
3294
|
168 |
|
169 @example |
|
170 @group |
|
171 dx |
|
172 -- = f (x, t) |
|
173 dt |
|
174 @end group |
|
175 @end example |
|
176 |
|
177 @noindent |
|
178 with the initial condition |
|
179 |
|
180 @example |
|
181 x(t = t0) = x0 |
|
182 @end example |
6620
|
183 @end ifnottex |
3294
|
184 |
|
185 @noindent |
|
186 For Octave to integrate equations of this form, you must first provide a |
|
187 definition of the function |
|
188 @iftex |
|
189 @tex |
|
190 $f (x, t)$. |
|
191 @end tex |
|
192 @end iftex |
6620
|
193 @ifnottex |
3294
|
194 @code{f(x,t)}. |
6620
|
195 @end ifnottex |
3294
|
196 This is straightforward, and may be accomplished by entering the |
|
197 function body directly on the command line. For example, the following |
|
198 commands define the right hand side function for an interesting pair of |
|
199 nonlinear differential equations. Note that while you are entering a |
|
200 function, Octave responds with a different prompt, to indicate that it |
|
201 is waiting for you to complete your input. |
|
202 |
|
203 @example |
|
204 @group |
|
205 octave:8> function xdot = f (x, t) |
|
206 > |
|
207 > r = 0.25; |
|
208 > k = 1.4; |
|
209 > a = 1.5; |
|
210 > b = 0.16; |
|
211 > c = 0.9; |
|
212 > d = 0.8; |
|
213 > |
|
214 > xdot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1)); |
|
215 > xdot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2); |
|
216 > |
|
217 > endfunction |
|
218 @end group |
|
219 @end example |
|
220 |
|
221 @noindent |
|
222 Given the initial condition |
|
223 |
|
224 @example |
|
225 x0 = [1; 2]; |
|
226 @end example |
|
227 |
|
228 @noindent |
|
229 and the set of output times as a column vector (note that the first |
|
230 output time corresponds to the initial condition given above) |
|
231 |
|
232 @example |
|
233 t = linspace (0, 50, 200)'; |
|
234 @end example |
|
235 |
|
236 @noindent |
|
237 it is easy to integrate the set of differential equations: |
|
238 |
|
239 @example |
|
240 x = lsode ("f", x0, t); |
|
241 @end example |
|
242 |
|
243 @noindent |
|
244 The function @code{lsode} uses the Livermore Solver for Ordinary |
|
245 Differential Equations, described in A. C. Hindmarsh, @cite{ODEPACK, a |
|
246 Systematized Collection of ODE Solvers}, in: Scientific Computing, R. S. |
|
247 Stepleman et al. (Eds.), North-Holland, Amsterdam, 1983, pages 55--64. |
|
248 |
6623
|
249 @subsection Producing Graphical Output |
3294
|
250 |
|
251 To display the solution of the previous example graphically, use the |
|
252 command |
|
253 |
|
254 @example |
|
255 plot (t, x) |
|
256 @end example |
|
257 |
6620
|
258 @noindent |
|
259 If you are using a graphical user interface, Octave will automatically create |
6331
|
260 a separate window to display the plot. |
|
261 |
6620
|
262 To save a plot once it has been displayed on the screen, use the print |
6331
|
263 command. For example, |
3294
|
264 |
|
265 @example |
6331
|
266 print -deps foo.eps |
3294
|
267 @end example |
|
268 |
|
269 @noindent |
6331
|
270 will create a fille called @file{foo.eps} that contains a rendering of |
|
271 the current plot. The command |
3294
|
272 |
|
273 @example |
6331
|
274 help print |
3294
|
275 @end example |
|
276 |
|
277 @noindent |
6331
|
278 explains more options for the @code{print} command and provides a list |
6620
|
279 of additional output file formats. |
3294
|
280 |
6623
|
281 @subsection Editing What You Have Typed |
3294
|
282 |
|
283 At the Octave prompt, you can recall, edit, and reissue previous |
|
284 commands using Emacs- or vi-style editing commands. The default |
|
285 keybindings use Emacs-style commands. For example, to recall the |
6620
|
286 previous command, press @kbd{Control-p} (usually written @kbd{C-p} for |
|
287 short). Doing this will normally bring back the previous line of input. |
|
288 @kbd{C-n} will bring up the next line of input, @kbd{C-b} will move |
|
289 the cursor backward on the line, @kbd{C-f} will move the cursor forward |
|
290 on the line, etc. |
3294
|
291 |
|
292 A complete description of the command line editing capability is given |
|
293 in this manual in @ref{Command Line Editing}. |
|
294 |
6623
|
295 @subsection Help and Documentation |
3294
|
296 |
|
297 Octave has an extensive help facility. The same documentation that is |
|
298 available in printed form is also available from the Octave prompt, |
|
299 because both forms of the documentation are created from the same input |
|
300 file. |
|
301 |
|
302 In order to get good help you first need to know the name of the command |
|
303 that you want to use. This name of the function may not always be |
|
304 obvious, but a good place to start is to just type @code{help}. |
|
305 This will show you all the operators, reserved words, functions, |
6620
|
306 built-in variables, and function files. An alternative is to search the |
|
307 documentation using the @code{lookfor} function. This function is |
|
308 described in @ref{Getting Help}. |
|
309 |
|
310 Once you know the name of the function you wish to use, you can get more |
|
311 help on the function by simply including the name as an argument to help. |
|
312 For example, |
3294
|
313 |
|
314 @example |
|
315 help plot |
|
316 @end example |
|
317 |
|
318 @noindent |
|
319 will display the help text for the @code{plot} function. |
|
320 |
|
321 Octave sends output that is too long to fit on one screen through a |
|
322 pager like @code{less} or @code{more}. Type a @key{RET} to advance one |
|
323 line, a @key{SPC} to advance one page, and @key{q} to exit the pager. |
|
324 |
|
325 The part of Octave's help facility that allows you to read the complete |
|
326 text of the printed manual from within Octave normally uses a separate |
|
327 program called Info. When you invoke Info you will be put into a menu |
|
328 driven program that contains the entire Octave manual. Help for using |
|
329 Info is provided in this manual in @ref{Getting Help}. |
|
330 |
4167
|
331 @node Conventions |
3294
|
332 @section Conventions |
|
333 |
|
334 This section explains the notational conventions that are used in this |
|
335 manual. You may want to skip this section and refer back to it later. |
|
336 |
|
337 @menu |
|
338 * Fonts:: |
|
339 * Evaluation Notation:: |
|
340 * Printing Notation:: |
|
341 * Error Messages:: |
|
342 * Format of Descriptions:: |
|
343 @end menu |
|
344 |
4167
|
345 @node Fonts |
3294
|
346 @subsection Fonts |
|
347 @cindex fonts |
|
348 |
|
349 Examples of Octave code appear in this font or form: @code{svd (a)}. |
|
350 Names that represent arguments or metasyntactic variables appear |
|
351 in this font or form: @var{first-number}. Commands that you type at the |
|
352 shell prompt sometimes appear in this font or form: |
|
353 @samp{octave --no-init-file}. Commands that you type at the Octave |
|
354 prompt sometimes appear in this font or form: @kbd{foo --bar --baz}. |
|
355 Specific keys on your keyboard appear in this font or form: @key{ANY}. |
|
356 @cindex any key |
|
357 |
4167
|
358 @node Evaluation Notation |
3294
|
359 @subsection Evaluation Notation |
|
360 @cindex evaluation notation |
|
361 @cindex documentation notation |
|
362 |
|
363 In the examples in this manual, results from expressions that you |
|
364 evaluate are indicated with @samp{@result{}}. For example, |
|
365 |
|
366 @example |
|
367 @group |
|
368 sqrt (2) |
|
369 @result{} 1.4142 |
|
370 @end group |
|
371 @end example |
|
372 |
|
373 @noindent |
|
374 You can read this as ``@code{sqrt (2)} evaluates to 1.4142''. |
|
375 |
|
376 In some cases, matrix values that are returned by expressions are |
|
377 displayed like this |
|
378 |
|
379 @example |
|
380 @group |
|
381 [1, 2; 3, 4] == [1, 3; 2, 4] |
|
382 @result{} [ 1, 0; 0, 1 ] |
|
383 @end group |
|
384 @end example |
|
385 |
|
386 @noindent |
|
387 and in other cases, they are displayed like this |
|
388 |
|
389 @example |
|
390 @group |
|
391 eye (3) |
|
392 @result{} 1 0 0 |
|
393 0 1 0 |
|
394 0 0 1 |
|
395 @end group |
|
396 @end example |
|
397 |
|
398 @noindent |
|
399 in order to clearly show the structure of the result. |
|
400 |
|
401 Sometimes to help describe one expression, another expression is |
|
402 shown that produces identical results. The exact equivalence of |
|
403 expressions is indicated with @samp{@equiv{}}. For example, |
|
404 |
|
405 @example |
|
406 @group |
|
407 rot90 ([1, 2; 3, 4], -1) |
|
408 @equiv{} |
|
409 rot90 ([1, 2; 3, 4], 3) |
|
410 @equiv{} |
|
411 rot90 ([1, 2; 3, 4], 7) |
|
412 @end group |
|
413 @end example |
|
414 |
4167
|
415 @node Printing Notation |
3294
|
416 @subsection Printing Notation |
|
417 @cindex printing notation |
|
418 |
|
419 Many of the examples in this manual print text when they are |
|
420 evaluated. Examples in this manual indicate printed text with |
|
421 @samp{@print{}}. The value that is returned by evaluating the |
|
422 expression (here @code{1}) is displayed with @samp{@result{}} and |
|
423 follows on a separate line. |
|
424 |
|
425 @example |
|
426 @group |
|
427 printf ("foo %s\n", "bar") |
|
428 @print{} foo bar |
|
429 @result{} 1 |
|
430 @end group |
|
431 @end example |
|
432 |
4167
|
433 @node Error Messages |
3294
|
434 @subsection Error Messages |
|
435 @cindex error message notation |
|
436 |
|
437 Some examples signal errors. This normally displays an error message |
|
438 on your terminal. Error messages are shown on a line starting with |
|
439 @code{error:}. |
|
440 |
|
441 @example |
|
442 @group |
|
443 struct_elements ([1, 2; 3, 4]) |
|
444 error: struct_elements: wrong type argument `matrix' |
|
445 @end group |
|
446 @end example |
|
447 |
4167
|
448 @node Format of Descriptions |
3294
|
449 @subsection Format of Descriptions |
|
450 @cindex description format |
|
451 |
|
452 Functions, commands, and variables are described in this manual in a |
|
453 uniform format. The first line of a description contains the name of |
|
454 the item followed by its arguments, if any. |
|
455 @ifinfo |
|
456 The category---function, variable, or whatever---appears at the |
|
457 beginning of the line. |
|
458 @end ifinfo |
|
459 @iftex |
|
460 The category---function, variable, or whatever---is printed next to the |
|
461 right margin. |
|
462 @end iftex |
|
463 The description follows on succeeding lines, sometimes with examples. |
|
464 |
|
465 @menu |
|
466 * A Sample Function Description:: |
|
467 * A Sample Command Description:: |
|
468 * A Sample Variable Description:: |
|
469 @end menu |
|
470 |
4167
|
471 @node A Sample Function Description |
3294
|
472 @subsubsection A Sample Function Description |
|
473 @cindex function descriptions |
|
474 |
|
475 In a function description, the name of the function being described |
|
476 appears first. It is followed on the same line by a list of parameters. |
|
477 The names used for the parameters are also used in the body of the |
|
478 description. |
|
479 |
|
480 Here is a description of an imaginary function @code{foo}: |
|
481 |
|
482 @deftypefn {Function} {} foo (@var{x}, @var{y}, @dots{}) |
|
483 The function @code{foo} subtracts @var{x} from @var{y}, then adds the |
|
484 remaining arguments to the result. If @var{y} is not supplied, then the |
|
485 number 19 is used by default. |
|
486 |
|
487 @example |
|
488 @group |
|
489 foo (1, [3, 5], 3, 9) |
|
490 @result{} [ 14, 16 ] |
|
491 foo (5) |
|
492 @result{} 14 |
|
493 @end group |
|
494 @end example |
|
495 |
|
496 More generally, |
|
497 |
|
498 @example |
|
499 @group |
|
500 foo (@var{w}, @var{x}, @var{y}, @dots{}) |
|
501 @equiv{} |
|
502 @var{x} - @var{w} + @var{y} + @dots{} |
|
503 @end group |
|
504 @end example |
|
505 @end deftypefn |
|
506 |
|
507 Any parameter whose name contains the name of a type (e.g., |
|
508 @var{integer}, @var{integer1} or @var{matrix}) is expected to be of that |
|
509 type. Parameters named @var{object} may be of any type. Parameters |
|
510 with other sorts of names (e.g., @var{new_file}) are discussed |
|
511 specifically in the description of the function. In some sections, |
|
512 features common to parameters of several functions are described at the |
|
513 beginning. |
|
514 |
|
515 Functions in Octave may be defined in several different ways. The |
|
516 catagory name for functions may include another name that indicates the |
|
517 way that the function is defined. These additional tags include |
|
518 |
|
519 @table @asis |
6620
|
520 @item Function File |
|
521 @cindex function file |
|
522 The function described is defined using Octave commands stored in a text |
|
523 file. @xref{Function Files}. |
|
524 |
3294
|
525 @item Built-in Function |
|
526 @cindex built-in function |
|
527 The function described is written in a language like C++, C, or Fortran, |
|
528 and is part of the compiled Octave binary. |
|
529 |
|
530 @item Loadable Function |
|
531 @cindex loadable function |
|
532 The function described is written in a language like C++, C, or Fortran. |
|
533 On systems that support dynamic linking of user-supplied functions, it |
|
534 may be automatically linked while Octave is running, but only if it is |
|
535 needed. @xref{Dynamically Linked Functions}. |
|
536 |
|
537 @item Mapping Function |
|
538 @cindex mapping function |
|
539 The function described works element-by-element for matrix and vector |
|
540 arguments. |
|
541 @end table |
|
542 |
4167
|
543 @node A Sample Command Description |
3294
|
544 @subsubsection A Sample Command Description |
|
545 @cindex command descriptions |
|
546 |
|
547 Command descriptions have a format similar to function descriptions, |
|
548 except that the word `Function' is replaced by `Command. Commands are |
|
549 functions that may called without surrounding their arguments in |
|
550 parentheses. For example, here is the description for Octave's |
|
551 @code{cd} command: |
|
552 |
|
553 @deffn {Command} cd dir |
|
554 @deffnx {Command} chdir dir |
|
555 Change the current working directory to @var{dir}. For example, |
|
556 @kbd{cd ~/octave} changes the current working directory to |
|
557 @file{~/octave}. If the directory does not exist, an error message is |
|
558 printed and the working directory is not changed. |
|
559 @end deffn |
|
560 |
4167
|
561 @node A Sample Variable Description |
3294
|
562 @subsubsection A Sample Variable Description |
|
563 @cindex variable descriptions |
|
564 |
|
565 A @dfn{variable} is a name that can hold a value. Although any variable |
|
566 can be set by the user, @dfn{built-in variables} typically exist |
|
567 specifically so that users can change them to alter the way Octave |
|
568 behaves (built-in variables are also sometimes called @dfn{user |
|
569 options}). Ordinary variables and built-in variables are described |
|
570 using a format like that for functions except that there are no |
|
571 arguments. |
|
572 |
|
573 Here is a description of the imaginary variable |
|
574 @code{do_what_i_mean_not_what_i_say}. |
|
575 |
|
576 @defvr {Built-in Variable} do_what_i_mean_not_what_i_say |
|
577 If the value of this variable is nonzero, Octave will do what you |
|
578 actually wanted, even if you have typed a completely different and |
|
579 meaningless list of commands. |
|
580 @end defvr |
|
581 |
|
582 Other variable descriptions have the same format, but `Built-in |
|
583 Variable' is replaced by `Variable', for ordinary variables, or |
|
584 `Constant' for symbolic constants whose values cannot be changed. |