[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Normally, you evaluate expressions simply by typing them at the Octave prompt, or by asking Octave to interpret commands that you have saved in a file.
Sometimes, you may find it necessary to evaluate an expression that has
been computed and stored in a string, or use a string as the name of a
function to call. The eval
and feval
functions allow you
to do just that, and are necessary in order to evaluate commands that
are not known until run time, or to write functions that will need to
call user-supplied functions.
eval
returns.
feval ("acos", -1) => 3.1416 |
calls the function acos
with the argument `-1'.
The function feval
is necessary in order to be able to write
functions that call user-supplied functions, because Octave does not
have a way to declare a pointer to a function (like C) or to declare a
special kind of variable that can be used to hold the name of a function
(like EXTERNAL
in Fortran). Instead, you must refer to functions
by name, and use feval
to call them.
Here is a simple-minded function using feval
that finds the root
of a user-supplied function of one variable using Newton's method.
Note that this is only meant to be an example of calling user-supplied
functions and should not be taken too seriously. In addition to using a
more robust algorithm, any serious code would check the number and type
of all the arguments, ensure that the supplied function really was a
function, etc. See See section 4.4 Predicates for Numeric Objects, for example,
for a list of predicates for numeric objects, and See section 9.3 Status of Variables, for a description of the exist
function.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |