1 ## Copyright (C) 2000 Ben Sapp <bsapp@nua.lampf.lanl.gov>
2 ## Copyright (C) 2011 Joaquín Ignacio Aramendía <samsagax@gmail.com>
3 ## Copyright (C) 2011 Carnë Draug <carandraug+dev@gmail.com>
5 ## This program is free software; you can redistribute it and/or modify it under
6 ## the terms of the GNU General Public License as published by the Free Software
7 ## Foundation; either version 3 of the License, or (at your option) any later
10 ## This program is distributed in the hope that it will be useful, but WITHOUT
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 ## You should have received a copy of the GNU General Public License along with
16 ## this program; if not, see <http://www.gnu.org/licenses/>.
19 ## @deftypefn {Function File} {@var{dx} =} deriv (@var{f}, @var{x0})
20 ## @deftypefnx {Function File} {@var{dx} =} deriv (@var{f}, @var{x0}, @var{h})
21 ## @deftypefnx {Function File} {@var{dx} =} deriv (@var{f}, @var{x0}, @var{h}, @var{O})
22 ## @deftypefnx {Function File} {@var{dx} =} deriv (@var{f}, @var{x0}, @var{h}, @var{O}, @var{N})
23 ## Calculate derivate of function @var{f}.
25 ## @var{f} must be a function handle or the name of a function that takes @var{x0}
26 ## and returns a variable of equal length and orientation. @var{x0} must be a
27 ## numeric vector or scalar.
29 ## @var{h} defines the step taken for the derivative calculation. Defaults to 1e-7.
31 ## @var{O} defines the order of the calculation. Supported values are 2 (h^2 order)
32 ## or 4 (h^4 order). Defaults to 2.
34 ## @var{N} defines the derivative order. Defaults to the 1st derivative of the
35 ## function. Can be up to the 4th derivative.
37 ## Reference: Numerical Methods for Mathematics, Science, and Engineering by
41 function dx = deriv (f, x0, h = 0.0000001, O = 2, N = 1)
44 f = str2func(f); # let's also support a string with str2func
48 error ("Not enough arguments.");
49 elseif (!isa (f, 'function_handle'))
50 error ("The first argument 'f' must be a function handle.");
51 elseif (!isvector (x0) || !isnumeric (x0))
52 ## a scalar is 1x1 therefore counts as a vector too
53 error ("The second argument 'x0' must be a numeric vector.");
54 elseif (!isscalar (h) || !isnumeric (h))
55 error ("The third argument 'h' must be a scalar.");
56 elseif (!isscalar (O) || !isnumeric (O))
57 error ("The fourth argument 'O' must be a scalar.");
58 elseif (O != 2 && O != 4)
59 error ("Only order 2 or 4 is supported.");
60 elseif (!isscalar (N) || !isnumeric (N))
61 error ("The fifth argument 'N' must be a scalar.");
62 elseif ((N > 4) || (N < 1))
63 error("Only 1st,2nd,3rd or 4th order derivatives are acceptable.");
65 warning("Ignoring arguements beyond the 5th.");
72 dx = (feval(f,x0+h)-feval(f,x0-h))/(2*h);
74 dx = (feval(f,x0+h)-2*feval(f,x0)+feval(f,x0-h))/(h^2);
76 dx = (feval(f,x0+2*h)-2*feval(f,x0+h)+2*feval(f,x0-h)-feval(f,x0-2*h))/(2*h^3);
78 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);
80 error("Only 1st,2nd,3rd or 4th order derivatives are acceptable.");
85 dx = (-feval(f,x0+2*h)+8*feval(f,x0+h)-8*feval(f,x0-h)+feval(f,x0-2*h))/(12*h);
87 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);
89 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);
91 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);
93 error("Only 1st,2nd,3rd or 4th order derivatives are acceptable.");
96 error ("Only order 2 or 4 is supported.");