A new patch for that function.

- Corrected a logic error, added a compatibility f=str2funct(f) at the
beggining of the file.

- Changed function behavior to work with vectorized functions (improve
speed on loops).

- Added some documentation.

-- 
Joaquín Ignacio Aramendía <samsa...@gmail.com>

El lun, 31-10-2011 a las 16:59 +0000, Carnë Draug escribió:
> On 31 October 2011 15:20, Joaquín Ignacio Aramendía <samsa...@gmail.com> 
> wrote:
> > Hi there:
> > I've modified the deriv.m function (Seems to be a work in progress as it
> > won't work at all).
> > I changed some checks so the funcion passed can be a single-variable
> > function handle. Also fixed two bugs:
> > changed is_scalar to isscalar near line 27, and added N=1 as default is
> > missing.
> >
> > Further notice: I should consider putting the N parameter as the first
> > optional parameter, so you can take the Nth derivative of a function
> > with the default order (O) and step (h)
> 
> Hi,
> 
> I have applied these changes, as it appears that it was not working
> before. Thank you very much.
> 
> I have then made a few more changes of my own. If you plan to submit
> more patches to it, please checkout the latest development version.
> 
> Carnë
--- deriv.m	2011-11-01 09:51:11.909621187 -0300
+++ deriv_updated.m	2011-11-01 16:00:25.283627939 -0300
@@ -13,28 +13,39 @@
 ## for more details.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {dx =} deriv (@var{f}, @var{x0})
-## @deftypefnx {Function File} {dx =} deriv (@var{f}, @var{x0}, @var{h})
-## @deftypefnx {Function File} {dx =} deriv (@var{f}, @var{x0}, @var{h}, @var{O})
-## @deftypefnx {Function File} {dx =} deriv (@var{f}, @var{x0}, @var{h}, @var{O}, @var{N})
+## @deftypefn {Function File} {dx =} deriv (@var{f}, @var{X0})
+## @deftypefnx {Function File} {dx =} deriv (@var{f}, @var{X0}, @var{h})
+## @deftypefnx {Function File} {dx =} deriv (@var{f}, @var{X0}, @var{h}, @var{O})
+## @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 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.
+## @var{f} must be a function handle or the name of a function that takes a vector @var{X}
+## and returns a vector of the same size and orientation as @var{X}. @var{X0}
+## must be a vector or a scalar.
+##
+## @var{h} defines the step taken for the derivative calculation. It defaults to 1e-7.
+##
+## @var{0} defines the order of the calculation. Supported values are 2 (h^2 order)
+## or 4 (h^4 order). Defaults to 2.
+##
+## @var{N} defines the derivative order. Defaults to the 1st derivative of the
+## function. Can be up to the 4th derivative.
 ##
 ## Reference: Numerical Methods for Mathematics, Science, and Engineering by
 ## John H. Mathews.
 ## @end deftypefn
 
-function dx = deriv (f, x0, h = 0.0000001, O = 2, N = 1)
+function dx = deriv (f, X0, h = 0.0000001, O = 2, N = 1)
+
+  if (ischar(f))
+    f = str2func(f); # let's also support a string with str2func
 
   if (nargin < 2)
     error ("Not enough arguments.");
-  elseif (!isa (f, 'function_handle') || !ischar (f)) # let's also support a string with str2func
+  elseif (!isa (f, 'function_handle'))
     error ("The first argument 'f' must be a function handle.");
-  elseif (!isscalar (x0) || !isnumeric (x0))
-    error ("The second argument 'x0' must be a scalar.");
+  elseif (!isvector (X0) || !isnumeric (X0))
+    error ("The second argument 'X0' must be a numeric vector.");
   elseif (!isscalar (h) || !isnumeric (h))
     error ("The third argument 'h' must be a scalar.");
   elseif (!isscalar (O) || !isnumeric (O))
@@ -57,26 +68,26 @@
     case (2)
       switch N
         case (1)
-          dx = (feval(f,x0+h)-feval(f,x0-h))/(2*h);
+          dx = (feval(f,X0+h)-feval(f,X0-h))/(2*h);
         case (2)
-          dx = (feval(f,x0+h)-2*feval(f,x0)+feval(f,x0-h))/(h^2);
+          dx = (feval(f,X0+h)-2*feval(f,X0)+feval(f,X0-h))/(h^2);
         case (3)
-          dx = (feval(f,x0+2*h)-2*feval(f,x0+h)+2*feval(f,x0-h)-feval(f,x0-2*h))/(2*h^3);
+          dx = (feval(f,X0+2*h)-2*feval(f,X0+h)+2*feval(f,X0-h)-feval(f,X0-2*h))/(2*h^3);
         case (4)
-          dx = (feval(f,x0+2*h)-4*feval(f,x0+h)+6*feval(f,x0)-4*feval(f,x0-h)+feval(f,x0-2*h))/(h^4);
+          dx = (feval(f,X0+2*h)-4*feval(f,X0+h)+6*feval(f,X0)-4*feval(f,X0-h)+feval(f,X0-2*h))/(h^4);
         otherwise
           error("Only 1st,2nd,3rd or 4th order derivatives are acceptable.");
       endswitch
     case (4)
       switch N
         case (1)
-          dx = (-feval(f,x0+2*h)+8*feval(f,x0+h)-8*feval(f,x0-h)+feval(f,x0-2*h))/(12*h);
+          dx = (-feval(f,X0+2*h)+8*feval(f,X0+h)-8*feval(f,X0-h)+feval(f,X0-2*h))/(12*h);
         case (2)
-          dx = (-feval(f,x0+2*h)+16*feval(f,x0+h)-30*feval(f,x0)+16*feval(f,x0-h)-feval(f,x0-2*h))/(12*h^2);
+          dx = (-feval(f,X0+2*h)+16*feval(f,X0+h)-30*feval(f,X0)+16*feval(f,X0-h)-feval(f,X0-2*h))/(12*h^2);
         case (3)
-          dx = (-feval(f,x0+3*h)+8*feval(f,x0+2*h)-13*feval(f,x0+h)+13*feval(f,x0-h)-8*feval(f,x0-2*h)+feval(f,x0-3*h))/(8*h^3);
+          dx = (-feval(f,X0+3*h)+8*feval(f,X0+2*h)-13*feval(f,X0+h)+13*feval(f,X0-h)-8*feval(f,X0-2*h)+feval(f,X0-3*h))/(8*h^3);
         case (4)
-          dx = (-feval(f,x0+3*h)+12*feval(f,x0+2*h)-39*feval(f,x0+h)+56*feval(f,x0)-39*feval(f,x0-h)+12*feval(f,x0-2*h)-feval(f,x0-3*h))/(6*h^4);
+          dx = (-feval(f,X0+3*h)+12*feval(f,X0+2*h)-39*feval(f,X0+h)+56*feval(f,X0)-39*feval(f,X0-h)+12*feval(f,X0-2*h)-feval(f,X0-3*h))/(6*h^4);
         otherwise
           error("Only 1st,2nd,3rd or 4th order derivatives are acceptable.");
       endswitch
------------------------------------------------------------------------------
RSA&#174; Conference 2012
Save $700 by Nov 18
Register now&#33;
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to