scipy – Difference between Sympy and Numpy solvers

scipy – Difference between Sympy and Numpy solvers

In numpy version function G(array([pi/4])) returns an empty array:

>> G(array([pi/4]))  
array([], dtype=float64)

The problem is in line:

return diff(F(theta,phi,phi0,H0),phi)

numpy.diff calculates differences between consecutive element of the arrays, whereas sympy.diff calculates a derivative. You can modify your own F_phi function to return derivative calculated analytically (if you know the solution) or numerically. For numerical solution you can use:

def F_phi(theta,phi,phi0,H0, eps=1e-12):
    return (F(theta,phi+eps,phi0,H0) - F(theta,phi,phi0,H0))/eps

and analytical solution (calculated with sympy):

def F_phi(theta, phi, phi0, H0):
    return -H0*a*t*(-sin(phi)*sin(phi0)*sin(theta) - sin(phi)*sin(theta)*cos(phi0)) + 4*t*sin(2*phi)*sin(theta)**4*cos(2*phi)

Please remember that numerical solution wont be as precise as analytical. Therefore, there might be still differences between sympy (analytical) and numpy (numerical) approaches.

scipy – Difference between Sympy and Numpy solvers

Leave a Reply

Your email address will not be published.