我正在尝试使用sympy的idiff函数对某些表达式执行隐式微分。
在本例中,rdot
是 dr
/ds,其中 s 是仿射参数。我想对相同的仿射参数 s 对Ltdot
、Lphidot
和Lrdot
进行隐式微分。
import numpy as np
from sympy import *
from sympy.physics.mechanics import *
#definition of variables
s = dynamicsymbols('s')
r = Function('r')(s)
rdot = Function('rdot')(s)
t = Function('t')(s)
tdot = Function('tdot')(s)
phi = Function('phi')(s)
phidot = Function('phidot')(s)
def F(x):
return 1-(1/x)
# Largangian
def L(a,b,c, adot, bdot, cdot, photon = true): #r,t,phi
return F(a)*(bdot)**2 - adot**2/F(a) - (a*cdot)**2
L = L(r, t, phi, rdot, tdot, phidot, photon = True)
Lt = diff(L, t)
Ltdot = diff(L, tdot)
Lphi = diff(L, phi)
Lphidot = diff(L, phidot)
Lr = diff(L, r)
Lrdot = diff(L, rdot)
#E-L equations printed to be used to solve equations
print('d/ds(', Ltdot, ') =', Lt) #EL1
print('d/ds(', Lphidot, ') =', Lphi) #EL2
print('d/ds(', Lrdot, ') =', Lr) #EL3
#FIX THIIISSSSS------------------------------------------------
LHS_EL1 = idiff(Ltdot, [t, tdot], s)
LHS_EL2 = idiff(Lphidot, [phi, phidot], s)
LHS_EL3 = idiff(Lrdot, [r, rdot], s)
#i want to do implicit differentiation wrt to affine parameter s, same that r is differentiated by to make rdot!!
print('d/ds(', LHS_EL1, ') =', Lt) #EL1 finalised
print('d/ds(', LHS_EL2, ') =', Lphi) #EL2 finalised
print('d/ds(', LHS_EL3, ') =', Lr) #EL3 finalised
我收到以下错误消息:
Traceback (most recent call last):
File "/Users/myname/PycharmProjects/untitled/.idea/14.1.py", line 53, in <module>
LHS_EL1 = idiff(Ltdot, [t, tdot], s)
File "/Users/myname/PycharmProjects/untitled/venv/lib/python3.6/site-packages/sympy/geometry/util.py", line 589, in idiff
yp = solve(eq.diff(x), dydx)[0].subs(derivs)
IndexError: list index out of range
任何关于我如何实现我想要的东西或任何帮助调试的想法将不胜感激!
将隐式的"t"作为s
的"时间"变量并且t
是函数t(s)
有点令人困惑。当你区分 wrtt
时,你的意思是"函数('t'("还是"s.args[0]"?如果是后者,那么如果T = s.args[0]
那么
>>> diff(L, T)
2*(1 - 1/r(s(t)))*tdot(s(t))*Derivative(s(t),
t)*Derivative(tdot(s(t)), s(t)) -
2*phidot(s(t))**2*r(s(t))*Derivative(r(s(t)), s(t))*Derivative(s(t),
t) - 2*phidot(s(t))*r(s(t))**2*Derivative(phidot(s(t)),
s(t))*Derivative(s(t), t) + tdot(s(t))**2*Derivative(r(s(t)),
s(t))*Derivative(s(t), t)/r(s(t))**2 -
2*rdot(s(t))*Derivative(rdot(s(t)), s(t))*Derivative(s(t), t)/(1 -
1/r(s(t))) + rdot(s(t))**2*Derivative(r(s(t)), s(t))*Derivative(s(t),
t)/((1 - 1/r(s(t)))**2*r(s(t))**2)