| |
- deriv(*positional_inputs, **keyword_inputs)
- Calculate the derivative along a single dimension.
Calling Sequence:
Result = deriv([x_in,] y_in, missing=1e+20, algorithm='default')
Positional Input Arguments:
* x_in: Abscissa values of y_in to take with respect to. If
not set, the derivative of y_in is take with respect to unit
abscissa intervals. Numeric array of same shape and size as
y_in. Must be monotonic and with no duplicate values.
Optional. First positional argument out of two, if present.
* y_in: Ordinate values, to take the derivative with respect
to. Numeric array vector of rank 1. Required. Second posi-
tional argument, if x_in is present; only positional argument
if x_in is absent.
Keyword Input Arguments:
* missing: If y_in and/or x_in has missing values, this is the
missing value value. Scalar. Default is 1e+20.
* algorithm: Name of the algorithm to use. String scalar.
Default is 'default'. Possible values include:
+ 'default': Default method (currently set to 'order1').
+ 'order1': First-order finite-differencing (backward and
forward differencing used at the endpoints, and centered
differencing used everywhere else). If abscissa intervals
are irregular, differencing will be correspondingly asym-
metric.
Output Result:
* Derivative of y_in with respect to x_in (or unit interval
abscissa, if x_in is not given). Numeric array of same shape
and size as y_in. If there are missing values, those elements
in the output are set to the value in |missing|. For instance,
if y_in is only one element, a one-element vector is returned
as the derivative with the value of |missing|. If there are
missing values in the output due to math errors and |missing|
is set to None, output will fill those missing values with the
MA default value of 1e+20.
References:
* Press, W. H., et al. (1992): Numerical Recipes in Fortran
77: The Art of Scientific Computing. New York, NY: Cambridge
University Press, pp. 180-184.
* Wang, Y. (1999): "Numerical Differentiation," Introduction to
MHD Numerical Simulation in Space, ESS265: Instrumentation,
Data Processing and Data Analysis in Space Physics (UCLA).
URL: http://www-ssc.igpp.ucla.edu/personnel/russell/ESS265/
Ch10/ylwang/node21.html.
Example with one argument, no missing values, using the default
method:
>>> from deriv import deriv
>>> import Numeric as N
>>> y = N.sin(N.arange(8))
>>> dydx = deriv(y)
>>> ['%.7g' % dydx[i] for i in range(4)]
['0.841471', '0.4546487', '-0.3501755', '-0.83305']
>>> true = N.cos(N.arange(8)) #- Compare with exact solution
>>> ['%.7g' % true[i] for i in range(4)]
['1', '0.5403023', '-0.4161468', '-0.9899925']
Example with two arguments with missing values, using first-
order differencing:
>>> x = N.arange(8)/(2.*N.pi)
>>> y = N.sin(x)
>>> y[3] = 1e20 #- Set an element to missing value
>>> dydx = deriv(x, y, missing=1e20, algorithm='order1')
>>> ['%.7g' % dydx[i] for i in range(5)]
['0.9957836', '0.9831985', '1e+20', '0.8844179', '1e+20']
>>> true = N.cos(x) #- Compare with exact solution
>>> ['%.7g' % true[i] for i in range(5)]
['1', '0.9873616', '0.9497657', '0.8881628', '0.8041098']
|