As the documentation says for derivative
:
derivative(func, x0, dx=1.0, n=1, args=(), order=3) Find the n-th derivative of a function at point x0. Given a function, use a central difference formula with spacing `dx` to compute the n-th derivative at `x0`.
You did not specify dx
, so it used the default value of 1
, which is too large here. For instance:
In [1]: from scipy.misc import derivative In [2]: derivative(lambda x: 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2, -2, dx=1) Out[2]: -39.0 In [3]: derivative(lambda x: 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2, -2, dx=0.5) Out[3]: -22.5 In [4]: derivative(lambda x: 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2, -2, dx=0.1) Out[4]: -17.220000000000084 In [5]: derivative(lambda x: 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2, -2, dx=0.01) Out[5]: -17.0022000000003 In [6]: derivative(lambda x: 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2, -2, dx=1e-5) Out[6]: -17.000000001843318
Alternatively, you can increase the order:
In [7]: derivative(lambda x: 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2, -2, dx=1, order=5) Out[7]: -17.0
Taking numerical derivatives is always a bit troublesome.
source share