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