Cannot differentiate wrt a complicated variable in Sympy

僤鯓⒐⒋嵵緔 提交于 2021-02-16 05:27:38

问题


(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()?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/25188084/cannot-differentiate-wrt-a-complicated-variable-in-sympy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!