3294
|
1 @c Copyright (C) 1996, 1997 John W. Eaton |
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
|
5 @node Evaluation, Statements, Expressions, Top |
|
6 @chapter Evaluation |
|
7 |
|
8 Normally, you evaluate expressions simply by typing them at the Octave |
|
9 prompt, or by asking Octave to interpret commands that you have saved in |
|
10 a file. |
|
11 |
|
12 Sometimes, you may find it necessary to evaluate an expression that has |
|
13 been computed and stored in a string, or use a string as the name of a |
|
14 function to call. The @code{eval} and @code{feval} functions allow you |
|
15 to do just that, and are necessary in order to evaluate commands that |
|
16 are not known until run time, or to write functions that will need to |
|
17 call user-supplied functions. |
|
18 |
|
19 @deftypefn {Built-in Function} {} eval (@var{command}) |
|
20 Parse the string @var{command} and evaluate it as if it were an Octave |
|
21 program, returning the last value computed. The @var{command} is |
|
22 evaluated in the current context, so any results remain available after |
|
23 @code{eval} returns. For example, |
|
24 |
|
25 @example |
|
26 @group |
|
27 eval ("a = 13") |
|
28 @print{} a = 13 |
|
29 @result{} 13 |
|
30 @end group |
|
31 @end example |
|
32 |
|
33 In this case, the value of the evaluated expression is printed and it is |
|
34 also returned returned from @code{eval}. Just as with any other |
|
35 expression, you can turn printing off by ending the expression in a |
|
36 semicolon. For example, |
|
37 |
|
38 @example |
|
39 eval ("a = 13;") |
|
40 @result{} 13 |
|
41 @end example |
|
42 |
|
43 In this example, the variable @code{a} has been given the value 13, but |
|
44 the value of the expression is not printed. You can also turn off |
|
45 automatic printing for all expressions executed by @code{eval} using the |
|
46 variable @code{default_eval_print_flag}. |
|
47 @end deftypefn |
|
48 |
|
49 @defvr {Built-in Variable} default_eval_print_flag |
|
50 If the value of this variable is nonzero, Octave prints the results of |
|
51 commands executed by @code{eval} that do not end with semicolons. If it |
|
52 is zero, automatic printing is suppressed. The default value is 1. |
|
53 @end defvr |
|
54 |
|
55 @deftypefn {Built-in Function} {} feval (@var{name}, @dots{}) |
|
56 Evaluate the function named @var{name}. Any arguments after the first |
|
57 are passed on to the named function. For example, |
|
58 |
|
59 @example |
|
60 feval ("acos", -1) |
|
61 @result{} 3.1416 |
|
62 @end example |
|
63 |
|
64 @noindent |
|
65 calls the function @code{acos} with the argument @samp{-1}. |
|
66 |
|
67 The function @code{feval} is necessary in order to be able to write |
|
68 functions that call user-supplied functions, because Octave does not |
|
69 have a way to declare a pointer to a function (like C) or to declare a |
|
70 special kind of variable that can be used to hold the name of a function |
|
71 (like @code{EXTERNAL} in Fortran). Instead, you must refer to functions |
|
72 by name, and use @code{feval} to call them. |
|
73 @end deftypefn |
|
74 |
|
75 Here is a simple-minded function using @code{feval} that finds the root |
|
76 of a user-supplied function of one variable using Newton's method. |
|
77 |
|
78 @example |
|
79 @group |
|
80 @cindex Fordyce, A. P. |
|
81 @findex newtroot |
|
82 function result = newtroot (fname, x) |
|
83 |
|
84 # usage: newtroot (fname, x) |
|
85 # |
|
86 # fname : a string naming a function f(x). |
|
87 # x : initial guess |
|
88 |
|
89 delta = tol = sqrt (eps); |
|
90 maxit = 200; |
|
91 fx = feval (fname, x); |
|
92 for i = 1:maxit |
|
93 if (abs (fx) < tol) |
|
94 result = x; |
|
95 return; |
|
96 else |
|
97 fx_new = feval (fname, x + delta); |
|
98 deriv = (fx_new - fx) / delta; |
|
99 x = x - fx / deriv; |
|
100 fx = fx_new; |
|
101 endif |
|
102 endfor |
|
103 |
|
104 result = x; |
|
105 |
|
106 endfunction |
|
107 @end group |
|
108 @end example |
|
109 |
|
110 Note that this is only meant to be an example of calling user-supplied |
|
111 functions and should not be taken too seriously. In addition to using a |
|
112 more robust algorithm, any serious code would check the number and type |
|
113 of all the arguments, ensure that the supplied function really was a |
|
114 function, etc. See @xref{Predicates for Numeric Objects}, for example, |
|
115 for a list of predicates for numeric objects, and @xref{Status of |
|
116 Variables}, for a description of the @code{exist} function. |