Mercurial > hg > octave-lyh
comparison doc/interpreter/intro.texi @ 2653:e7908588548a
[project @ 1997-02-01 16:53:52 by jwe]
author | jwe |
---|---|
date | Sat, 01 Feb 1997 16:57:10 +0000 |
parents | 31d5588dbb61 |
children | 18192eea4973 |
comparison
equal
deleted
inserted
replaced
2652:69613a17f51a | 2653:e7908588548a |
---|---|
4 | 4 |
5 @node Introduction, Invoking Octave, Preface, Top | 5 @node Introduction, Invoking Octave, Preface, Top |
6 @chapter A Brief Introduction to Octave | 6 @chapter A Brief Introduction to Octave |
7 @cindex introduction | 7 @cindex introduction |
8 | 8 |
9 This manual documents how to run, install and port Octave, and how | 9 This manual documents how to run, install and port Octave, and how to |
10 to report bugs. | 10 report bugs. |
11 | 11 |
12 Octave is a high-level language, primarily intended for numerical | 12 Octave is a high-level language, primarily intended for numerical |
13 computations. It provides a convenient command line interface for | 13 computations. It provides a convenient command line interface for |
14 solving linear and nonlinear problems numerically, and for performing | 14 solving linear and nonlinear problems numerically, and for performing |
15 other numerical experiments. It may also be used as a batch-oriented | 15 other numerical experiments. It may also be used as a batch-oriented |
28 @c always use more funding. | 28 @c always use more funding. |
29 | 29 |
30 @menu | 30 @menu |
31 * Running Octave:: | 31 * Running Octave:: |
32 * Simple Examples:: | 32 * Simple Examples:: |
33 * Comments:: | 33 * Conventions:: |
34 * Executable Octave Programs:: | |
35 * Errors:: | |
36 @end menu | 34 @end menu |
37 | 35 |
38 @node Running Octave, Simple Examples, Introduction, Introduction | 36 @node Running Octave, Simple Examples, Introduction, Introduction |
39 @section Running Octave | 37 @section Running Octave |
40 | 38 |
51 | 49 |
52 @cindex exiting octave | 50 @cindex exiting octave |
53 @cindex quitting octave | 51 @cindex quitting octave |
54 To exit Octave, type @samp{quit}, or @samp{exit} at the Octave prompt. | 52 To exit Octave, type @samp{quit}, or @samp{exit} at the Octave prompt. |
55 | 53 |
56 @deftypefn {Built-in Function} {} exit (@var{status}) | |
57 @deftypefnx {Built-in Function} {} quit (@var{status}) | |
58 Exit the current Octave session. If the optional integer value | |
59 @var{status} is supplied, pass that value to the operating system as the | |
60 Octave's exit status. | |
61 @end deftypefn | |
62 | |
63 On systems that support job control, you can suspend Octave by sending | 54 On systems that support job control, you can suspend Octave by sending |
64 it a @code{SIGTSTP} signal, usually by typing @kbd{C-z}. | 55 it a @code{SIGTSTP} signal, usually by typing @kbd{C-z}. |
65 | 56 |
66 @node Simple Examples, Comments, Running Octave, Introduction | 57 @node Simple Examples, Conventions, Running Octave, Introduction |
67 @section Simple Examples | 58 @section Simple Examples |
68 | 59 |
69 The following chapters describe all of Octave's features in detail, but | 60 The following chapters describe all of Octave's features in detail, but |
70 before doing that, it might be helpful to give a sampling of some of its | 61 before doing that, it might be helpful to give a sampling of some of its |
71 capabilities. | 62 capabilities. |
353 Octave sends output that is too long to fit on one screen through a | 344 Octave sends output that is too long to fit on one screen through a |
354 pager like @code{less} or @code{more}. Type a carriage return to | 345 pager like @code{less} or @code{more}. Type a carriage return to |
355 advance one line, a space character to advance one page, and @samp{q} to | 346 advance one line, a space character to advance one page, and @samp{q} to |
356 exit the pager. | 347 exit the pager. |
357 | 348 |
358 @unnumberedsubsubsec Help via Info | |
359 | |
360 The part of Octave's help facility that allows you to read the complete | 349 The part of Octave's help facility that allows you to read the complete |
361 text of the printed manual from within Octave uses a program called | 350 text of the printed manual from within Octave normally uses a separate |
362 Info. When you invoke Info you will be put into a menu driven program | 351 program called Info. When you invoke Info you will be put into a menu |
363 that contains the entire Octave manual. Help for using Info is provided | 352 driven program that contains the entire Octave manual. Help for using |
364 in this manual in @ref{Using Info}. | 353 Info is provided in this manual in @ref{Help}. |
365 | 354 |
366 @node Comments, Executable Octave Programs, Simple Examples, Introduction | 355 @node Conventions, , Simple Examples, Introduction |
367 @section Comments in Octave Programs | 356 @section Conventions |
368 @cindex @samp{#} | 357 |
369 @cindex @samp{%} | 358 This section explains the notational conventions that are used in this |
370 @cindex comments | 359 manual. You may want to skip this section and refer back to it later. |
371 @cindex use of comments | 360 |
372 @cindex documenting Octave programs | 361 @menu |
373 @cindex programs, documenting | 362 * Fonts:: |
374 | 363 * Evaluation Notation:: |
375 A @dfn{comment} is some text that is included in a program for the sake | 364 * Printing Notation:: |
376 of human readers, and that is not really part of the program. Comments | 365 * Error Messages:: |
377 can explain what the program does, and how it works. Nearly all | 366 * Format of Descriptions:: |
378 programming languages have provisions for comments, because programs are | 367 @end menu |
379 typically hard to understand without them. | 368 |
380 | 369 @node Fonts, Evaluation Notation, Conventions, Conventions |
381 In the Octave language, a comment starts with either the sharp sign | 370 @subsection Fonts |
382 character, @samp{#}, or the percent symbol @samp{%} and continues to the | 371 @cindex fonts |
383 end of the line. The Octave interpreter ignores the rest of a | 372 |
384 line following a sharp sign or percent symbol. For example, we could | 373 Examples of Octave code appear in this font or form: @code{svd (a)}. |
385 have put the following into the function @code{f}: | 374 Names that represent arguments or metasyntactic variables appear |
386 | 375 in this font or form: @var{first-number}. |
387 @smallexample | 376 |
388 function xdot = f (x, t) | 377 @node Evaluation Notation, Printing Notation, Fonts, Conventions |
389 | 378 @subsection Evaluation Notation |
390 # usage: f (x, t) | 379 @cindex evaluation notation |
391 # | 380 @cindex documentation notation |
392 # This function defines the right-hand-side functions for a set of | 381 |
393 # nonlinear differential equations. | 382 In the examples in this manual, results from expressions that you |
394 | 383 evaluate are indicated with @samp{@result{}}. For example, |
395 r = 0.25 | 384 |
396 | 385 @example |
397 and so on... | 386 @group |
398 | 387 sqrt (2) |
399 endfunction | 388 |
400 @end smallexample | 389 @result{} 1.4142 |
401 | 390 @end group |
402 The @code{help} command (@pxref{Help}) is able to find the first block | 391 @end example |
403 of comments in a function (even those that are composed directly on the | 392 |
404 command line). This means that users of Octave can use the same | 393 @noindent |
405 commands to get help for built-in functions, and for functions that you | 394 You can read this as ``@code{sqrt (2)} evaluates to 1.4142''. |
406 have defined. For example, after defining the function @code{f} above, | 395 |
407 the command | 396 In some cases, matrix values that are returned by expressions are |
408 | 397 displayed like this |
409 @example | 398 |
410 help f | 399 @example |
411 @end example | 400 @group |
412 | 401 [1, 2; 3, 4] == [1, 3; 2, 4] |
413 @noindent | 402 |
414 produces the output | 403 @result{} [ 1, 0; 0, 1 ] |
415 | 404 @end group |
416 @smallexample | 405 @end example |
417 usage: f (x, t) | 406 |
418 | 407 @noindent |
419 This function defines the right-hand-side functions for a set of | 408 and in other cases, they are displayed like this |
420 nonlinear differential equations. | 409 |
421 @end smallexample | 410 @example |
422 | 411 @group |
423 Although it is possible to put comment lines into keyboard-composed | 412 eye (3) |
424 throw-away Octave programs, it usually isn't very useful, because the | 413 |
425 purpose of a comment is to help you or another person understand the | 414 @result{} 1 0 0 |
426 program at a later time. | 415 0 1 0 |
427 | 416 0 0 1 |
428 @node Executable Octave Programs, Errors, Comments, Introduction | 417 @end group |
429 @section Executable Octave Programs | 418 @end example |
430 @cindex executable scripts | 419 |
431 @cindex scripts, executable | 420 @noindent |
432 @cindex self contained programs | 421 in order to clearly show the structure of the result. |
433 @cindex program, self contained | 422 |
434 @cindex @samp{#!} | 423 Sometimes to help describe one expression, another expression is |
435 | 424 shown that produces identical results. The exact equivalence of |
436 Once you have learned Octave, you may want to write self-contained | 425 expressions is indicated with @samp{@equiv{}}. For example, |
437 Octave scripts, using the @samp{#!} script mechanism. You can do this | 426 |
438 on GNU systems and on many Unix systems @footnote{The @samp{#!} | 427 @example |
439 mechanism works on Unix systems derived from Berkeley Unix, System V | 428 @group |
440 Release 4, and some System V Release 3 systems.} | 429 rot90 ([1, 2; 3, 4], -1) |
441 | 430 @equiv{} |
442 For example, you could create a text file named @file{hello}, containing | 431 rot90 ([1, 2; 3, 4], 3) |
443 the following lines: | 432 @equiv{} |
444 | 433 rot90 ([1, 2; 3, 4], 7) |
445 @example | 434 @end group |
446 @group | 435 @end example |
447 #! @value{OCTAVEHOME}/bin/octave -qf | 436 |
448 | 437 @node Printing Notation, Error Messages, Evaluation Notation, Conventions |
449 # a sample Octave program | 438 @subsection Printing Notation |
450 printf ("Hello, world!\n"); | 439 @cindex printing notation |
451 @end group | 440 |
452 @end example | 441 Many of the examples in this manual print text when they are |
453 | 442 evaluated. Examples in this manual indicate printed text with |
454 @noindent | 443 @samp{@print{}}. The value that is returned by evaluating the |
455 After making this file executable (with the @code{chmod} command), you | 444 expression (here @code{1}) is displayed with @samp{@result{}} and |
456 can simply type: | 445 follows on a separate line. |
457 | 446 |
458 @example | 447 @example |
459 hello | 448 @group |
460 @end example | 449 printf ("foo %s\n", "bar") |
461 | 450 |
462 @noindent | 451 @print{} foo bar |
463 at the shell, and the system will arrange to run Octave @footnote{The | 452 |
464 line beginning with @samp{#!} lists the full file name of an interpreter | 453 @result{} 1 |
465 to be run, and an optional initial command line argument to pass to that | 454 @end group |
466 interpreter. The operating system then runs the interpreter with the | 455 @end example |
467 given argument and the full argument list of the executed program. The | 456 |
468 first argument in the list is the full file name of the Octave program. | 457 @node Error Messages, Format of Descriptions, Printing Notation, Conventions |
469 The rest of the argument list will either be options to Octave, or data | 458 @subsection Error Messages |
470 files, or both. The @code{-qf} option is usually specified in | 459 @cindex error message notation |
471 stand-alone Octave programs to prevent them from printing the normal | 460 |
472 startup message, and to keep them from behaving differently depending on | 461 Some examples signal errors. This normally displays an error message |
473 the contents of a particular user's @file{~/.octaverc} file. | 462 on your terminal. Error messages are shown on a line starting with |
474 @xref{Invoking Octave}.} as if you had typed: | 463 @samp{@error{}}. Note that @samp{@error{}} itself does not appear on |
475 | 464 your terminal. |
476 @example | 465 |
477 octave hello | 466 @example |
478 @end example | 467 struct_elements ([1, 2; 3, 4]) |
479 | 468 @error{} struct_elements: wrong type argument `matrix' |
480 @noindent | 469 @end example |
481 Self-contained Octave scripts are useful when you want to write a | 470 |
482 program which users can invoke without knowing that the program is | 471 @node Format of Descriptions, , Error Messages, Conventions |
483 written in the Octave language. | 472 @subsection Format of Descriptions |
484 | 473 @cindex description format |
485 @node Errors, , Executable Octave Programs, Introduction | 474 |
486 @section Errors | 475 Functions, commands, and variables are described in this manual in a |
487 | 476 uniform format. The first line of a description contains the name of |
488 There are two classes of errors that Octave produces when it encounters | 477 the item followed by its arguments, if any. |
489 input that it is unable to understand, or when it is unable to perform | 478 @ifinfo |
490 an action. | 479 The category---function, variable, or whatever---appears at the |
491 | 480 beginning of the line. |
492 A @dfn{parse error} occurs if Octave cannot understand something you | 481 @end ifinfo |
493 have typed. For example, if you misspell a keyword, | 482 @iftex |
494 | 483 The category---function, variable, or whatever---is printed next to the |
495 @example | 484 right margin. |
496 octave:13> functon y = f (x) y = x^2; endfunction | 485 @end iftex |
497 @end example | 486 The description follows on succeeding lines, sometimes with examples. |
498 | 487 |
499 @noindent | 488 @menu |
500 Octave will respond immediately with a message like this: | 489 * A Sample Function Description:: |
501 | 490 * A Sample Command Description:: |
502 @example | 491 * A Sample Variable Description:: |
503 parse error: | 492 @end menu |
504 | 493 |
505 functon y = f (x) y = x^2; endfunction | 494 @node A Sample Function Description, A Sample Command Description, Format of Descriptions, Format of Descriptions |
506 ^ | 495 @subsubsection A Sample Function Description |
507 @end example | 496 @cindex function descriptions |
508 | 497 |
509 @noindent | 498 In a function description, the name of the function being described |
510 For most parse errors, Octave uses a caret (@samp{^}) to mark the point | 499 appears first. It is followed on the same line by a list of parameters. |
511 on the line where it was unable to make sense of your input. In this | 500 The names used for the parameters are also used in the body of the |
512 case, Octave generated an error message because the keyword | 501 description. |
513 @code{function} was misspelled. Instead of seeing @samp{function f}, | 502 |
514 Octave saw two consecutive variable names, which is invalid in this | 503 Here is a description of an imaginary function @code{foo}: |
515 context. It marked the error at the @code{y} because the first name by | 504 |
516 itself was accepted as valid input. | 505 @deftypefn {Function} {} foo (@var{x}, @var{y}, @dots{}) |
517 | 506 The function @code{foo} subtracts @var{x} from @var{y}, then adds the |
518 Another class of error message occurs occurs at evaluation time. These | 507 remaining arguments to the result. If @var{y} is not supplied, then the |
519 errors are called @dfn{run-time errors}, or sometimes | 508 number 19 is used by default. |
520 @dfn{evaluation errors} because they occur when your program is being | 509 |
521 @dfn{run}, or @dfn{evaluated}. For example, if after correcting the | 510 @example |
522 mistake in the previous function definition, you type | 511 @group |
523 | 512 foo (1, [3, 5], 3, 9) |
524 @example | 513 |
525 octave:13> f () | 514 @result{} [ 14, 16 ] |
526 @end example | 515 |
527 | 516 foo (5) |
528 @noindent | 517 |
529 Octave will respond with | 518 @result{} 14 |
530 | 519 @end group |
531 @example | 520 @end example |
532 @group | 521 |
533 error: `x' undefined near line 1 column 24 | 522 More generally, |
534 error: evaluating expression near line 1, column 24 | 523 |
535 error: evaluating assignment expression near line 1, column 22 | 524 @example |
536 error: called from `f' | 525 @group |
537 @end group | 526 foo (@var{w}, @var{x}, @var{y}, @dots{}) |
538 @end example | 527 @equiv{} |
539 | 528 @var{x} - @var{w} + @var{y} + @dots{} |
540 This error message has several parts, and gives you quite a bit of | 529 @end group |
541 information to help you locate the source of the error. The messages | 530 @end example |
542 are generated from the point of the innermost error, and provide a | 531 @end deftypefn |
543 traceback of enclosing expressions and function calls. | 532 |
544 | 533 Any parameter whose name contains the name of a type (e.g., |
545 In the example above, the first line indicates that a variable named | 534 @var{integer}, @var{integer1} or @var{matrix}) is expected to be of that |
546 @samp{x} was found to be undefined near line 1 and column 24 of some | 535 type. Parameters named @var{object} may be of any type. Parameters |
547 function or expression. For errors occurring within functions, lines | 536 with other sorts of names (e.g., @var{new_file}) are discussed |
548 from the beginning of the file containing the function definition. For | 537 specifically in the description of the function. In some sections, |
549 errors occurring at the top level, the line number indicates the input | 538 features common to parameters of several functions are described at the |
550 line number, which is usually displayed in the prompt string. | 539 beginning. |
551 | 540 |
552 The second and third lines in the example indicate that the error | 541 Functions in Octave may be defined in several different ways. The |
553 occurred within an assignment expression, and the last line of the error | 542 catagory name for functions may include another name that indicates the |
554 message indicates that the error occurred within the function @samp{f}. | 543 way that the function is defined. These additional tags include |
555 If the function @samp{f} had been called from another function, for | 544 |
556 example, @samp{g}, the list of errors would have ended with one more | 545 @table @asis |
557 line: | 546 @item Built-in Function |
558 | 547 The function described is written in a language like C++, C, or Fortran, |
559 @example | 548 and is part of the compiled Octave binary. |
560 error: called from `g' | 549 |
561 @end example | 550 @item Loadable Function |
562 | 551 The function described is written in a language like C++, C, or Fortran. |
563 These lists of function calls usually make it fairly easy to trace the | 552 On systems that support dynamic linking of user-supplied functions, it |
564 path your program took before the error occurred, and to correct the | 553 may be automatically linked while Octave is running, but only if it is |
565 error before trying again. | 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 | |
577 @end example | |
578 | |
579 @noindent | |
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 | |
584 | |
585 @node A Sample Variable Description, , A Sample Command Description, Format of Descriptions | |
586 @subsubsection A Sample Variable Description | |
587 @cindex variable descriptions | |
588 | |
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. | |
594 | |
595 Here is a description of the imaginary user option | |
596 @code{do_what_i_mean_not_what_i_say}. | |
597 | |
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 | |
603 | |
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. |