NumPy does not provide general functionality for computing derivatives. However, it can handle a simple special case of polynomials:
>>> p = numpy.poly1d([1, 0, 1]) >>> print p 2 1 x + 1 >>> q = p.deriv() >>> print q 2 x >>> q(5) 10
If you want to calculate the derivative numerically, you can avoid using the central difference coefficients for the vast majority of applications. For a derivative at one point, the formula will look like
x = 5.0 eps = numpy.sqrt(numpy.finfo(float).eps) * (1.0 + x) print (p(x + eps) - p(x - eps)) / (2.0 * eps * x)
if you have an x abscissa array with a corresponding array of y function values, you can calculate the approximations of the derivatives using
numpy.diff(y) / numpy.diff(x)
Sven Marnach Mar 26 '12 at 17:09 2012-03-26 17:09
source share