eulerlagrang的辛隐微分法

2024-04-26 11:00:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用sympy的idiff函数对一些表达式进行隐式微分。你知道吗

在这种情况下,rdot是dr/ds,其中s是仿射参数。我想对LtdotLphidotLrdot对相同的仿射参数执行隐式微分

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

任何关于我如何能达到我想要的想法或任何帮助调试将不胜感激!你知道吗


Tags: todsdifffunctionprintsympyphilhs
1条回答
网友
1楼 · 发布于 2024-04-26 11:00:36

将隐式的't'作为s的“时间”变量,t作为函数t(s)有点混乱。区分wrtt是指“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)

相关问题 更多 >