changeset 8716:a9a4b8737f9a octave-forge

deriv: also accept a string as function and use str2func
author carandraug
date Mon, 31 Oct 2011 17:24:06 +0000
parents 3ed012a0f558
children da32bc7df365
files main/optim/inst/deriv.m
diffstat 1 files changed, 8 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/main/optim/inst/deriv.m	Mon Oct 31 16:59:24 2011 +0000
+++ b/main/optim/inst/deriv.m	Mon Oct 31 17:24:06 2011 +0000
@@ -19,8 +19,9 @@
 ## @deftypefnx {Function File} {dx =} deriv (@var{f}, @var{x0}, @var{h}, @var{O}, @var{N})
 ## Calculate derivate of function @var{f}.
 ##
-## @var{f} must be a function handle and @var{x0} a scalar.The optional arguments
-## @var{h}, @var{O} and @var{N} default to 1e-7, 2, and 1 respectively.
+## @var{f} must be a function handle or the name of a function while @var{x0}
+## must be a scalar.The optional arguments @var{h}, @var{O} and @var{N} default
+## to 1e-7, 2, and 1 respectively.
 ##
 ## Reference: Numerical Methods for Mathematics, Science, and Engineering by
 ## John H. Mathews.
@@ -30,7 +31,7 @@
 
   if (nargin < 2)
     error ("Not enough arguments.");
-  elseif (!isa (f, 'function_handle'))
+  elseif (!isa (f, 'function_handle') || !ischar (f)) # let's also support a string with str2func
     error ("The first argument 'f' must be a function handle.");
   elseif (!isscalar (x0) || !isnumeric (x0))
     error ("The second argument 'x0' must be a scalar.");
@@ -48,6 +49,10 @@
     warning("Ignoring arguements beyond the 5th.");
   endif
 
+  if (ischar (f))
+    f = str2func (f);
+  endif
+
   switch O
     case (2)
       switch N