aymenbh
aymenbh

Reputation: 161

Sympy Can't differentiate wrt the variable

I am trying to evaluate a function (second derivative of another one) but Sympy seems to have a difficulty to do that... ?

from sympy import *
from sympy import Symbol

# Symbols
theta = Symbol('theta')
phi = Symbol('phi')
phi0 = Symbol('phi0')
H0 = Symbol('H0')
# Constants
a = 0.05 
t = 100*1e-9
b = 0.05**2/(8*pi*1e-7)
c = 0.001/(4*pi*1e-7)
phi0 = 60*pi/180 
H0 = -0.03/(4*pi*1e-7)
def m(theta,phi):
    return Matrix([[sin(theta)*cos(phi), sin(theta)*cos(phi), cos(phi)]])
def h(phi0):
    return Matrix([[cos(phi0), sin(phi0), 0]])
def k(theta,phi,phi0):
    return m(theta,phi).dot(h(phi0))
def F(theta,phi,phi0,H0): 
    return -(t*a*H0)*k(theta,phi,phi0)+b*t*(cos(theta)**2)+c*t*(sin(2*theta)**2)+t*sin(theta)**4*sin(2*phi)**2
def F_theta(theta,phi,phi0,H0):
    return simplify(diff(F(theta,phi,phi0,H0),theta))
def F_thetatheta(theta,phi,phi0,H0):
    return simplify(diff(F_theta(theta,phi,phi0,H0),theta))

print F_thetatheta(theta,phi,phi0,H0), F_thetatheta(pi/2,phi,phi0,H0)

As seen below, the general function is evaluated but when I try to replace theta by pi/2 or another value, it does not work.

(4.0e-7*pi*sin(theta)**4*cos(2*phi)**2 - 4.0e-7*pi*sin(theta)**4 + 0.00125*sin(theta)**2 - 0.0001875*sqrt(3)*sin(theta)*cos(phi) - 0.0001875*sin(theta)*cos(phi) + 1.2e-6*pi*cos(2*phi)**2*cos(theta)**4 - 1.2e-6*pi*cos(2*phi)**2*cos(theta)**2 - 1.2e-6*pi*cos(theta)**4 + 1.2e-6*pi*cos(theta)**2 + 0.004*cos(2*theta)**2 - 0.002625)/pi
Traceback (most recent call last):
File "Test.py", line 46, in <module>
print F_thetatheta(theta,phi,phi0,H0), F_thetatheta(pi/2,phi,phi0,H0)
File "Test.py", line 29, in F_thetatheta
return simplify(diff(F_theta(theta,phi,phi0,H0),theta))
File "Test.py", line 27, in F_theta
return simplify(diff(F(theta,phi,phi0,H0),theta))
File "/usr/lib64/python2.7/site-packages/sympy/core/function.py", line 1418, in diff
return Derivative(f, *symbols, **kwargs)
File "/usr/lib64/python2.7/site-packages/sympy/core/function.py", line 852, in __new__
Can\'t differentiate wrt the variable: %s, %s''' % (v, count)))
ValueError: 
Can't differentiate wrt the variable: pi/2, 1

Upvotes: 1

Views: 3063

Answers (1)

jorgeca
jorgeca

Reputation: 5522

The error means you can not differentiate with respect to a number, pi/2. Ie, you derive with respect to a variable (x, y...), not a number.

In an expression with several variables, you can substitute one of them (or more) by its value (or another expression) by using subs:

F_thetatheta(theta,phi,phi0,H0).subs(theta, pi/2)

Then, to evaluate it to the desired accuracy you can use evalf. Compare the two results:

F_thetatheta(theta,phi,phi0,H0).evalf(50, subs={theta:pi/2, phi:0})
F_thetatheta(theta,phi,phi0,H0).subs({theta: pi/2, phi:0})

You should probably have a look at the sympy documentation or follow the tutorial. The documentation is very good, and you can even try the examples in the browser and evaluate code.

Upvotes: 3

Related Questions