我正在尝试使用sympy的idiff函数对某些表达式执行隐式微分。
在这种情况下,rdot
是d r
/ 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
任何对我如何实现自己想要的想法或任何帮助调试的想法将不胜感激!
答案 0 :(得分:0)
将隐式的“ t”作为s
的“时间”变量并将t
用作函数t(s)
有点令人困惑。当您区分t
时,是指“ Function('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)