I tried computing the value of the derivative of a function using both sympy and scipy. The value computed using scipy is varying with changing the value of the parameter 'dx' for the derivative function is scipy. The code is given below
import sympy as smp
import numpy as np
import scipy as sp
x=smp.symbols('x',real=True)
f=smp.exp(-smp.sin(x**2))*smp.sin(2**x)*smp.log(3*smp.sin(x)**2/x)
smpval=smp.diff(f,x,4).subs([(x,4)]).evalf()
def f(x):
return sp.exp(-sp.sin(x**2))*sp.sin(2**x)*sp.log(3*sp.sin(x)**2/x)
from scipy.misc import derivative
spval=derivative(f, x0=4,dx=1e-6,n=4,order=5)
print(smpval)
print(spval)
The values I am getting are smpval=-73035.8044625845 and spval=15154544286.133389. Why there is so much deviation and how do one can confirm which value is correct?
As for the documentation, the function scipy.misc.derivative.
Ultimately it is calculating
sum(weights[k]*f(x0+(k-order//2)*dx) for k in range(order))/dx**n
For small values it converges to the derivative, but in practice if you use too small values you the roundoff errors will dominate. So you have to find a reasonable value of dx
for your purpose. And, as you already know you can calculate the derivatives symbolically.