octave: Calling a Function by its Name

 
 9.1 Calling a Function by its Name
 ==================================
 
 The ‘feval’ function allows you to call a function from a string
 containing its name.  This is useful when writing a function that needs
 to call user-supplied functions.  The ‘feval’ function takes the name of
 the function to call as its first argument, and the remaining arguments
 are given to the function.
 
    The following example 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
DONTPRINTYET  function really was a function, etc.  SeePredicates for Numeric
 Objects, for a list of predicates for numeric objects, and *noteDONTPRINTYET  function really was a function, etc.  SeePredicates for Numeric
 Objects, for a list of predicates for numeric objects, and See
 Status of Variables, for a description of the ‘exist’ function.
 
  -- : feval (NAME, ...)
      Evaluate the function named NAME.
 
      Any arguments after the first are passed as inputs to the named
      function.  For example,
 
           feval ("acos", -1)
                ⇒ 3.1416
 
      calls the function ‘acos’ with the argument ‘-1’.
 
      The function ‘feval’ can also be used with function handles of any
      sort (SeeFunction Handles).  Historically, ‘feval’ was the
      only way to call user-supplied functions in strings, but function
      handles are now preferred due to the cleaner syntax they offer.
      For example,
 
           F = @exp;
           feval (F, 1)
               ⇒ 2.7183
           F (1)
               ⇒ 2.7183
 
      are equivalent ways to call the function referred to by F.  If it
      cannot be predicted beforehand whether F is a function handle,
      function name in a string, or inline function then ‘feval’ can be
      used instead.
 
    A similar function ‘run’ exists for calling user script files, that
 are not necessarily on the user path
 
  -- : run SCRIPT
  -- : run ("SCRIPT")
      Run SCRIPT in the current workspace.
 
      Scripts which reside in directories specified in Octave’s load
      path, and which end with the extension ‘".m"’, can be run simply by
      typing their name.  For scripts not located on the load path, use
      ‘run’.
 
      The filename SCRIPT can be a bare, fully qualified, or relative
      filename and with or without a file extension.  If no extension is
      specified, Octave will first search for a script with the ‘".m"’
      extension before falling back to the script name without an
      extension.
 
      Implementation Note: If SCRIPT includes a path component, then
      ‘run’ first changes the working directory to the directory where
      SCRIPT is found.  Next, the script is executed.  Finally, ‘run’
      returns to the original working directory unless ‘script’ has
      specifically changed directories.
 
DONTPRINTYET       See also: Seepath XREFpath, Seeaddpath XREFaddpath, *noteDONTPRINTYET       See also: Seepath XREFpath, Seeaddpath XREFaddpath, See
      source XREFsource.