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