简体   繁体   中英

Cannot differentiate wrt a complicated variable in Sympy

(I understand the title makes this sound like a duplicate question to Sympy Can't differentiate wrt the variable , but I'm fairly certain it is quite different. I apologize in advance if I am mistaken)

I'm trying to solve the Double Pendulum using Hamiltonian Mechanics, but Sympy is having trouble taking one of the derivatives. The code is below, but it's much easier to read the iPython Notebook over at nbviewer.ipython.org

from __future__ import division
from sympy import *
init_session()
r_1,r_2,l_1,l_2, m_1, m_2, rdot_1, rdot_2,g = symbols("r_1 r_2 l_1 l_2 m_1 m_2 \dot{r_1} \dot{r_2} g")
phi_1, phi_2 = symbols("phi_1 phi_2", cls=Function)
phi_1 = phi_1(t)
phi_2 = phi_2(t)

r_1 = Matrix([l_1 * sin(phi_1), -l_1 * cos(phi_1)])
rdot_1 = r_1.diff(t)
r_2 = Matrix([r_1[0] + l_2 * sin(phi_2), r_1[1] -l_2 * cos(phi_2)])
rdot_2 = r_2.diff(t)

T = (1/2) * m_1 * rdot_1.T * rdot_1 + (1/2) * m_2 * rdot_2.T * rdot_2
T = T[0]

U = -g * ((m_1 * r_1[1]) + (m_2 * r_2[1]))

L = symbols("\mathcal{L}")
L = T - U

H = symbols("\mathcal{H}")
H = T + U

p_1, p_2 = symbols("p_1 p_2")
p_1 = L.diff(phi_1.diff(t))
p_2 = L.diff(phi_2.diff(t))

eq1_1 = p_1.diff(t) + H.diff(phi_1)
eq1_2 = p_2.diff(t) + H.diff(phi_2)
eq2_1 = phi_1.diff(t) - H.diff(p_1)

The last line yields the following error:

ValueError                                Traceback (most recent call last)
<ipython-input-96-83bfafadd08f> in <module>()
----> 1 eq2_1 = phi_1.diff(t) - H.diff(p_1)
      2 eq2_2 = phi_2.diff(t) - H.diff(p_2)

/Library/Python/2.7/site-packages/sympy/core/expr.pyc in diff(self, *symbols, **assumptions)
   2773         new_symbols = list(map(sympify, symbols))  # e.g. x, 2, y, z
   2774         assumptions.setdefault("evaluate", True)
-> 2775         return Derivative(self, *new_symbols, **assumptions)
   2776 
   2777     ###########################################################################

/Library/Python/2.7/site-packages/sympy/core/function.pyc in __new__(cls, expr, *variables, **assumptions)
   1027                 from sympy.utilities.misc import filldedent
   1028                 raise ValueError(filldedent('''
-> 1029                 Can\'t differentiate wrt the variable: %s, %s''' % (v, count)))
   1030 
   1031             if all_zero and not count == 0:
ValueError: 
Can't differentiate wrt the variable:
1.0*l_1*(l_1*m_1*Derivative(phi_1(t), t) +
l_1*m_2*Derivative(phi_1(t), t) + l_2*m_2*cos(phi_1(t) -
phi_2(t))*Derivative(phi_2(t), t)), 1

Is the expression too complicated? Or am I misunderstanding how to use diff()?

Did you perhaps inadvertently overwrite p_1?

p_1, p_2 = symbols("p_1 p_2")
p_1 = L.diff(phi_1.diff(t))

You created a symbol but then destroyed it by creating a Python variable with the same name, so when you try to differentiate wrt p_1 you are (as Aaron pointed out) differentiating wrt an expression, not a symbol that you created.

It's essentially the same problem. The second argument to diff (or the only argument if called as a method) should be a single Symbol, not an expression. In this case, your differentiation "variable" is an entire expression, which doesn't even make sense mathematically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM