| GNU Octave Manual by John W. Eaton Paperback (6"x9"), 324 pages, 4 figures ISBN 0954161726 RRP £19.99 ($29.99) |
9 Evaluation
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.
- Built-in Function: eval (command)
- Parse the string command and evaluate it as if it were an Octave
program, returning the last value computed. The command is
evaluated in the current context, so any results remain available after
evalreturns. For example,eval ("a = 13") -| a = 13 => 13In this case, the value of the evaluated expression is printed and it is also returned returned from
eval. Just as with any other expression, you can turn printing off by ending the expression in a semicolon. For example,eval ("a = 13;") => 13In this example, the variable
ahas been given the value 13, but the value of the expression is not printed. You can also turn off automatic printing for all expressions executed byevalusing the variabledefault_eval_print_flag.
- Built-in Variable: default_eval_print_flag
- If the value of this variable is nonzero, Octave prints the results of
commands executed by
evalthat do not end with semicolons. If it is zero, automatic printing is suppressed. The default value is 1.
- Built-in Function: feval (name, ...)
- Evaluate the function named name. Any arguments after the first
are passed on to the named function. For example,
feval ("acos", -1) => 3.1416calls the function
acoswith the argument ‘-1’.The function
fevalis 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 (likeEXTERNALin Fortran). Instead, you must refer to functions by name, and usefevalto 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.
function result = newtroot (fname, x)
# usage: newtroot (fname, x)
#
# fname : a string naming a function f(x).
# x : initial guess
delta = tol = sqrt (eps);
maxit = 200;
fx = feval (fname, x);
for i = 1:maxit
if (abs (fx) < tol)
result = x;
return;
else
fx_new = feval (fname, x + delta);
deriv = (fx_new - fx) / delta;
x = x - fx / deriv;
fx = fx_new;
endif
endfor
result = x;
endfunction
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.3 Predicates for Numeric Objects, for example,
for a list of predicates for numeric objects, and See section 7.2 Status of Variables, for a description of the exist function.
| ISBN 0954161726 | GNU Octave Manual | See the print edition |