Miscellaneous notes on octave

From Notes_Wiki
Revision as of 05:53, 10 November 2012 by Saurabh (talk | contribs) (Created page with "=Miscellaneous notes on octave= ==Creating shell scripts with octave== To create shell scripts with octave you can use the normal 'hash bang' operator on top of script to sp...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Miscellaneous notes on octave

Creating shell scripts with octave

To create shell scripts with octave you can use the normal 'hash bang' operator on top of script to specify that script should be interpreted by octave and not by some other program. For example below script will print list of arguments passed to shell script

#! /usr/local/bin/octave -qf
    printf ("%s", program_name ());
    arg_list = argv ();
    for i = 1:nargin
        printf (" %s", arg_list{i});
    endfor
    printf ("\n");

Note the options '-qf' supplied to octave so that we do not get normal start-up messages and users personal octave initialization files are not used.


Comments in octave program

We can include comments in octave program with lines beginning with '#' or '%' sign. For example

function xdot = f (x, t)
     
# usage: f (x, t)
#
# This function defines the right hand
# side functions for a set of nonlinear
# differential equations.
     
r = 0.25;
...
endfunction

One can then use 'help f' command to see the help on function f which print first comment block. This can be useful to see help on user-defined functions.


Built-in variables

There are few variables names used by octave for its functioning. The list and brief description about these variable names is available at http://www.gnu.org/software/octave/doc/interpreter/Summary-of-Built_002din-Variables.html#Summary-of-Built_002din-Variables Values of some of these variables can also be taken from environment variables or can also be specified through command line. See http://www.gnu.org/software/octave/doc/interpreter/Defaults-from-the-Environment.html#Defaults-from-the-Environment for more information on setting values through command line or environment variables.



Useful tips

  • It is better to store array of strings in cell arrays then in matrix as all rows of matrix must have same number of columns. Hence matrix would have number of columns same as length of largest string. This is not required when we store array of strings in cell arrays.
  • In octave while calling functions we pass arguments by value and not by reference. Octave will not make local copy of variable unless function called tries to modify the value. Hence passing large matrices to function will not lead them to being copied, unless we try to modify those matrices inside function body.
  • Limited recursion is allowed in octave. The max recursion depth can be seen/controlled by max_recursion_depth() function.