Simple Examples | This is an overview over Octaves numerical features. Here you will learn how to work with matrices, how to integrate differential equations, how to plot functions, how to edit what you have typed, how to get help and how to work with script or function files. Octave for OS/2 Homepage. Read Me! History of Changes.

If you are new to Octave, I recommend that you try these examples to begin learning Octave by using it. Lines marked with octave:13> are lines you type, ending each with a carriage return. Octave will respond with an answer, or by displaying a graph.

1. Creating a Matrix

   To create a new matrix and store it in a variable so that it you can refer to it later, type the command

       octave:1> a = [ 1, 1, 2; 3, 5, 8; 13, 21, 34 ]

   Octave will respond by printing the matrix in neatly aligned columns. Ending a command with a semicolon tells Octave to not print the result of a command. For example

       octave:2> b = rand (3, 2);

   will create a 3 row, 2 column matrix with each element set to a random value between zero and one.

   To display the value of any variable, simply type the name of the variable. For example, to display the value stored in the matrix b, type the command

       octave:3> b

2. Matrix Arithmetic

   Octave has a convenient operator notation for performing matrix arithmetic. For example, to multiply the matrix a by a scalar value, type the command

       octave:4> 2 * a

   To multiply the two matrices a and b, type the command

       octave:5> a * b

   To form the matrix product a' * a, type the command

       octave:6> a' * a

3. Solving Linear Equations

   To solve the set of linear equations Ax = b, use the left division operator, \:

   This is conceptually equivalent to inv(A) * b, but avoids computing the inverse of a matrix directly. If the coefficient matrix is singular, Octave will print a warning message and compute a minimum norm solution.

4. Integrating Differential Equations

   Octave has built-in functions for solving nonlinear differential equations of the form

       dx / dt = f (x, t)

   with the initial condition

       x(t = t0) = x0

   For Octave to integrate equations of this form, you must first provide a definition of the function f(x,t).

   This is straightforward, and may be accomplished by entering the function body directly on the command line. For example, the following commands define the right hand side function for an interesting pair of nonlinear differential equations. Note that while you are entering a function, Octave responds with a different prompt, to indicate that it is waiting for you to complete your input.

       octave:8> function xdot = f (x, t)
       >
       >  r = 0.25;
       >  k = 1.4;
       >  a = 1.5;
       >  b = 0.16;
       >  c = 0.9;
       >  d = 0.8;
       >
       >  xdot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1));
       >  xdot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2);
       >
       > endfunction

   Given the initial condition

       x0 = [1; 2];

   and the set of output times as a column vector (note that the first output time corresponds to the initial condition given above)

       t = linspace (0, 50, 200)';

   it is easy to integrate the set of differential equations:

       x = lsode ("f", x0, t);

   The function lsode uses the Livermore Solver for Ordinary Differential Equations, described in A. C. Hindmarsh, ODEPACK, a Systematized Collection of ODE Solvers, in: Scientific Computing, R. S. Stepleman et al. (Eds.), North-Holland, Amsterdam, 1983, pages 55--64.

5. Producing Graphical Output

   To display the solution of the previous example graphically, use the command

       plot (t, x)

   | Integrating the above differential equation. The plot command will start Gnuplot, which will display the plot in a seperate window. The Gnuplot window. To see the left screenshot in its original (large) size, please click on the picture. If you are using the X Window System, Octave will automatically create a separate window to display the plot. If you are using a terminal that supports some other graphics commands, you will need to tell Octave what kind of terminal you have. Type the command
       gset term
   to see a list of the supported terminal types. Octave uses gnuplot to display graphics, and can display graphics on any terminal that is supported by gnuplot.

   To capture the output of the plot command in a file rather than sending the output directly to your terminal, you can use a set of commands like this

       gset term postscript
       gset output "foo.ps"
       replot

   This will work for other types of output devices as well. Octave's gset command is really just piped to the gnuplot subprocess, so that once you have a plot on the screen that you like, you should be able to do something like this to create an output file suitable for your graphics printer.

   Or, you can eliminate the intermediate file by using commands like this

       gset term postscript
       gset output "|lpr -Pname_of_your_graphics_printer"
       replot

6. Editing What You Have Typed

   At the Octave prompt, you can recall, edit, and reissue previous commands using Emacs- or vi-style editing commands. The default keybindings use Emacs-style commands. For example, to recall the previous command, type Control-P (usually written C-p for short). C-p gets its name from the fact that you type it by holding down the CTRL key and then pressing p. Doing this will normally bring back the previous line of input. C-n will bring up the next line of input, C-b will move the cursor backward on the line, C-f will move the cursor forward on the line, etc.

7. Getting Help

   Octave has an extensive help facility. The same documentation that is available in printed form is also available from the Octave prompt, because both forms of the documentation are created from the same input file. In order to get good help you first need to know the name of the command that you want to use. This name of the function may not always be obvious, but a good place to start is to just type help. This will show you all the operators, reserved words, functions, built-in variables, and function files. You can then get more help on anything that is listed by simply including the name as an argument to help. For example,

       help plot

   will display the help text for the plot function. Octave sends output that is too long to fit on one screen through a pager like less or more. Type a carriage return to advance one line, a space character to advance one page, and q to exit the pager.

8. Help via Info

   | The part of Octave's help facility that allows you to read the complete text of the printed manual from within Octave uses a program called Info. When you invoke Info you will be put into a menu driven program that contains the entire Octave manual. If you type help -i asi Octave will open a second buffer and start the Emacs info browser. The buffer with the Emacs info browser. To see the left screenshot in its original (large) size, please click on the picture.

9. Function and Script files

   Except for simple one-shot programs, it is not practical to have to define all the functions you need each time you need them. Instead, you will normally want to save them in a file so that you can easily edit them, and save them for use at a later time.

   | Octave does not require you to load function definitions from files before using them. You simply need to put the function definitions in a place where Octave can find them. When Octave encounters an identifier that is undefined, it first looks for variables or functions that are already compiled and currently listed in its symbol table. If it fails to find a definition there, it searches the list of directories specified by the built-in variable LOADPATH for files ending in .m that have the same base name as the undefined identifier (The .m suffix was chosen for compatibility with Matlab). Once Octave finds a file with a name that matches, the contents of the file are read. If it defines a single function, it is compiled and executed. To see the left screenshot in its original (large) size, please click on the picture.

   A script file is a file containing (almost) any sequence of Octave commands. It is read and evaluated just as if you had typed each command at the Octave prompt, and provides a convenient way to perform a sequence of commands that do not logically belong inside a function.

   Unlike a function file, a script file must not begin with the keyword function. If it does, Octave will assume that it is a function file, and that it defines a single function that should be evaluated as soon as it is defined.

   A script file also differs from a function file in that the variables named in a script file are not local variables, but are in the same scope as the other variables that are visible on the command line.

   Even though a script file may not begin with the function keyword, it is possible to define more than one function in a single script file and load (but not execute) all of them at once. To do this, the first token in the file (ignoring comments and other white space) must be something other than function.