Sympy解决了保持运行,不停止
我正在尝试使用Lagrange的方程式对三重摆进行模拟。我使用sympy.solve求解三个质量的三个线性方程系统,但是代码保持运行超过一个小时,并且不会停止。有人知道会发生什么吗?当系统具有两个方程式时,可以很好地工作。
t, g = smp.symbols('t g')
m1, m2, m3 = smp.symbols('m1, m2, m3')
L1, L2, L3 = smp.symbols('L1, L2, L3')
the1, the2, the3 = smp.symbols(r'\theta_1, \theta_2, \theta_3', cls=smp.Function)
the1 = the1(t)
the2 = the2(t)
the3 = the3(t)
the1_d = smp.diff(the1, t)
the2_d = smp.diff(the2, t)
the3_d = smp.diff(the3, t)
the1_dd = smp.diff(the1_d, t)
the2_dd = smp.diff(the2_d, t)
the3_dd = smp.diff(the3_d, t)
x1 = L1*smp.sin(the1)
y1 = -L1*smp.cos(the1)
x2 = L1*smp.sin(the1)+L2*smp.sin(the2)
y2 = -L1*smp.cos(the1)-L2*smp.cos(the2)
x3 = L3*smp.sin(the3)+L1*smp.sin(the1)+L2*smp.sin(the2)
y3 = -L3*smp.cos(the3)-L1*smp.cos(the1)-L2*smp.cos(the2)
# Kinetic
T1 = 1/2 * m1 * (smp.diff(x1, t)**2 + smp.diff(y1, t)**2)
T2 = 1/2 * m2 * (smp.diff(x2, t)**2 + smp.diff(y2, t)**2)
T3 = 1/2 * m3 * (smp.diff(x3, t)**2 + smp.diff(y3, t)**2)
T = T1+T2+T3
# Potential
V1 = m1*g*y1
V2 = m2*g*y2
V3 = m3*g*y3
V = V1 + V2 + V3
# Lagrangian
L = T-V
LE1 = smp.diff(L, the1) - smp.diff(smp.diff(L, the1_d), t)
LE2 = smp.diff(L, the2) - smp.diff(smp.diff(L, the2_d), t)
LE3 = smp.diff(L, the3) - smp.diff(smp.diff(L, the3_d), t)
sols = smp.solve([LE1, LE2, LE3], (the1_dd, the2_dd, the3_dd), simplify=False, rational=False)
I'm trying to do a simulation of a triple pendulum using Lagrange's equations. I use sympy.solve to solve the three linear equation system for the three masses, but the code keeps running for more than an hour and does not stop. Anybody knows what can happen? When the system has two equations works perfectly.
t, g = smp.symbols('t g')
m1, m2, m3 = smp.symbols('m1, m2, m3')
L1, L2, L3 = smp.symbols('L1, L2, L3')
the1, the2, the3 = smp.symbols(r'\theta_1, \theta_2, \theta_3', cls=smp.Function)
the1 = the1(t)
the2 = the2(t)
the3 = the3(t)
the1_d = smp.diff(the1, t)
the2_d = smp.diff(the2, t)
the3_d = smp.diff(the3, t)
the1_dd = smp.diff(the1_d, t)
the2_dd = smp.diff(the2_d, t)
the3_dd = smp.diff(the3_d, t)
x1 = L1*smp.sin(the1)
y1 = -L1*smp.cos(the1)
x2 = L1*smp.sin(the1)+L2*smp.sin(the2)
y2 = -L1*smp.cos(the1)-L2*smp.cos(the2)
x3 = L3*smp.sin(the3)+L1*smp.sin(the1)+L2*smp.sin(the2)
y3 = -L3*smp.cos(the3)-L1*smp.cos(the1)-L2*smp.cos(the2)
# Kinetic
T1 = 1/2 * m1 * (smp.diff(x1, t)**2 + smp.diff(y1, t)**2)
T2 = 1/2 * m2 * (smp.diff(x2, t)**2 + smp.diff(y2, t)**2)
T3 = 1/2 * m3 * (smp.diff(x3, t)**2 + smp.diff(y3, t)**2)
T = T1+T2+T3
# Potential
V1 = m1*g*y1
V2 = m2*g*y2
V3 = m3*g*y3
V = V1 + V2 + V3
# Lagrangian
L = T-V
LE1 = smp.diff(L, the1) - smp.diff(smp.diff(L, the1_d), t)
LE2 = smp.diff(L, the2) - smp.diff(smp.diff(L, the2_d), t)
LE3 = smp.diff(L, the3) - smp.diff(smp.diff(L, the3_d), t)
sols = smp.solve([LE1, LE2, LE3], (the1_dd, the2_dd, the3_dd), simplify=False, rational=False)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,通常在Sympy中避免使用浮子更好,因此将
1/2
更改为Rational(1,2)
或s.half
或s(1)/2
等。此处涉及的表达式很长且复杂。我将用普通的符号替换所有功能和衍生物,以便我们可以更好地看到它:
我还使用Trigsimp在那里使用Trig减少了Trig,这确实需要一些时间。 (如果您确切地知道您要寻找的简化类型,您可以通过各种方式加快速度。)
现在它们更简单,让我们看一下这些方程式:
因此,我们想解决双点符号和我们可以看到这只是线性的,因此我们可以将其转换为矩阵:
直接计算逆向恰好有点慢,但是我们可以使用
atchugate
:需要一段时间才能简化这一点。但实际上并没有变得更简单。 SO字符限制使我无法粘贴以下
简化(SOL)
的结果。Firstly it is generally better in SymPy to avoid the use of floats so change
1/2
toRational(1, 2)
orS.Half
orS(1)/2
etc.The expressions involved here are quite long and complicated. I'm going to replace all the functions and derivatives with plain symbols so that we can see it a bit better:
I also used trigsimp there to reduce using trig which does take some time. (There are various ways you can speed that up if you know exactly what type of simplification you are looking for.)
Now that they are a bit simpler let's take a look at these equations:
So we want to solve for the double dot symbols and we can see that it's all just linear so we can convert this into matrices:
Directly computing the inverse here happens to be a bit slow but we can do it faster using
adjugate
:It takes a while to simplify this but doesn't really get much simpler. The SO character limit prevents me from pasting the result of
simplify(sol)
below though.