gpt4 book ai didi

python - 欧拉-拉格朗日的 Sympy 隐式微分

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:18 29 4
gpt4 key购买 nike

我正在尝试使用 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

任何关于我如何实现我想要的东西的想法或任何帮助调试将不胜感激!

最佳答案

将隐式“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)

关于python - 欧拉-拉格朗日的 Sympy 隐式微分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57552872/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com