[英]Sympy solve keeps running and doesn't stop
我正在嘗試使用拉格朗日方程模擬三重擺。 我使用 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)
首先,在 SymPy 中通常最好避免使用浮點數,因此將1/2
更改為Rational(1, 2)
或S.Half
或S(1)/2
等。
這里涉及的表達式相當長且復雜。 我將用普通符號替換所有函數和衍生物,以便我們可以更好地看到它:
t1, t2, t3 = symbols('t1:4')
dt1, dt2, dt3 = symbols('tdot1:4')
ddt1, ddt2, ddt3 = symbols('tddot1:4')
rep = {
the1:t1, the2:t2, the3:t3,
the1_d:dt1, the2_d:dt2, the3_d:dt3,
the1_dd:ddt1, the2_dd:ddt2, the3_dd:ddt3,
}
eqs = [eq.subs(rep).trigsimp() for eq in [LE1, LE2, LE3]]
syms = [ddt1, ddt2, ddt3]
我還在那里使用了 trigsimp 來減少使用 trig ,這確實需要一些時間。 (如果您確切地知道您正在尋找哪種類型的簡化,則有多種方法可以加快速度。)
現在它們更簡單了,讓我們來看看這些方程:
In [4]: for eq in eqs: pprint(eq)
⎛ 2 2 2 ⎞
-L₁⋅⎝L₁⋅m₁⋅ẗ₁ + L₁⋅m₂⋅ẗ₁ + L₁⋅m₃⋅ẗ₁ + L₂⋅m₂⋅ẗ₂⋅cos(t₁ - t₂) + L₂⋅m₂⋅ṫ₂ ⋅sin(t₁ - t₂) + L₂⋅m₃⋅ẗ₂⋅cos(t₁ - t₂) + L₂⋅m₃⋅ṫ₂ ⋅sin(t₁ - t₂) + L₃⋅m₃⋅ẗ₃⋅cos(t₁ - t₃) + L₃⋅m₃⋅ṫ₃ ⋅sin(t₁ - t₃) + g⋅m₁⋅sin(t₁) + g⋅m₂⋅sin(t₁) + g⋅m₃⋅sin(t₁)⎠
⎛ 2 2 2 ⎞
L₂⋅⎝-L₁⋅m₂⋅ẗ₁⋅cos(t₁ - t₂) + L₁⋅m₂⋅ṫ₁ ⋅sin(t₁ - t₂) - L₁⋅m₃⋅ẗ₁⋅cos(t₁ - t₂) + L₁⋅m₃⋅ṫ₁ ⋅sin(t₁ - t₂) - L₂⋅m₂⋅ẗ₂ - L₂⋅m₃⋅ẗ₂ - L₃⋅m₃⋅ẗ₃⋅cos(t₂ - t₃) - L₃⋅m₃⋅ṫ₃ ⋅sin(t₂ - t₃) - g⋅m₂⋅sin(t₂) - g⋅m₃⋅sin(t₂)⎠
⎛ 2 2 ⎞
L₃⋅m₃⋅⎝-L₁⋅ẗ₁⋅cos(t₁ - t₃) + L₁⋅ṫ₁ ⋅sin(t₁ - t₃) - L₂⋅ẗ₂⋅cos(t₂ - t₃) + L₂⋅ṫ₂ ⋅sin(t₂ - t₃) - L₃⋅ẗ₃ - g⋅sin(t₃)⎠
所以我們想解決雙點符號,我們可以看到它都是線性的,所以我們可以將其轉換為矩陣:
In [5]: A, b = linear_eq_to_matrix(eqs, syms)
In [6]: A
Out[6]:
⎡ -L₁⋅(L₁⋅m₁ + L₁⋅m₂ + L₁⋅m₃) -L₁⋅(L₂⋅m₂⋅cos(t₁ - t₂) + L₂⋅m₃⋅cos(t₁ - t₂)) -L₁⋅L₃⋅m₃⋅cos(t₁ - t₃)⎤
⎢ ⎥
⎢L₂⋅(-L₁⋅m₂⋅cos(t₁ - t₂) - L₁⋅m₃⋅cos(t₁ - t₂)) L₂⋅(-L₂⋅m₂ - L₂⋅m₃) -L₂⋅L₃⋅m₃⋅cos(t₂ - t₃)⎥
⎢ ⎥
⎢ 2 ⎥
⎣ -L₁⋅L₃⋅m₃⋅cos(t₁ - t₃) -L₂⋅L₃⋅m₃⋅cos(t₂ - t₃) -L₃ ⋅m₃ ⎦
In [7]: b
Out[7]:
⎡ ⎛ 2 2 2 ⎞⎤
⎢L₁⋅⎝L₂⋅m₂⋅ṫ₂ ⋅sin(t₁ - t₂) + L₂⋅m₃⋅ṫ₂ ⋅sin(t₁ - t₂) + L₃⋅m₃⋅ṫ₃ ⋅sin(t₁ - t₃) + g⋅m₁⋅sin(t₁) + g⋅m₂⋅sin(t₁) + g⋅m₃⋅sin(t₁)⎠⎥
⎢ ⎥
⎢ ⎛ 2 2 2 ⎞ ⎥
⎢ -L₂⋅⎝L₁⋅m₂⋅ṫ₁ ⋅sin(t₁ - t₂) + L₁⋅m₃⋅ṫ₁ ⋅sin(t₁ - t₂) - L₃⋅m₃⋅ṫ₃ ⋅sin(t₂ - t₃) - g⋅m₂⋅sin(t₂) - g⋅m₃⋅sin(t₂)⎠ ⎥
⎢ ⎥
⎢ ⎛ 2 2 ⎞ ⎥
⎣ -L₃⋅m₃⋅⎝L₁⋅ṫ₁ ⋅sin(t₁ - t₃) + L₂⋅ṫ₂ ⋅sin(t₂ - t₃) - g⋅sin(t₃)⎠ ⎦
在這里直接計算逆有點慢,但我們可以使用adjugate
更快地完成:
In [8]: sol = A.adjugate()*b/A.det()
In [9]: sol
Out[9]:
⎡ ⎛ 2 2 2 2 2 2 2 2 2⎞ ⎛ 2 2 2 ⎞ ⎛ 2 2 2 2 2 ⎞ ⎛ 2 2 2
⎢ L₁⋅⎝L₂ ⋅L₃ ⋅m₂⋅m₃ - L₂ ⋅L₃ ⋅m₃ ⋅cos (t₂ - t₃) + L₂ ⋅L₃ ⋅m₃ ⎠⋅⎝L₂⋅m₂⋅ṫ₂ ⋅sin(t₁ - t₂) + L₂⋅m₃⋅ṫ₂ ⋅sin(t₁ - t₂) + L₃⋅m₃⋅ṫ₃ ⋅sin(t₁ - t₃) + g⋅m₁⋅sin(t₁) + g⋅m₂⋅sin(t₁) + g⋅m₃⋅sin(t₁)⎠ - L₂⋅⎝- L₁⋅L₂⋅L₃ ⋅m₂⋅m₃⋅cos(t₁ - t₂) - L₁⋅L₂⋅L₃ ⋅m₃ ⋅cos(t₁ - t₂) + L₁⋅L₂⋅L₃ ⋅m₃ ⋅cos(t₁ - t₃)⋅cos(t₂ - t₃)⎠⋅⎝L₁⋅m₂⋅ṫ₁ ⋅sin(t₁ - t₂) + L₁⋅m₃⋅ṫ₁ ⋅sin(t₁ - t₂) - L₃⋅m₃⋅ṫ₃ ⋅sin(t₂ -
⎢ ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
⎢ 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
⎢ - L₁ ⋅L₂ ⋅L₃ ⋅m₁⋅m₂⋅m₃ + L₁ ⋅L₂ ⋅L₃ ⋅m₁⋅m₃ ⋅cos (t₂ - t₃) - L₁ ⋅L₂ ⋅L₃ ⋅m₁⋅m₃ + L₁ ⋅L₂ ⋅L₃ ⋅m₂ ⋅m₃⋅cos (t₁ - t₂) - L₁ ⋅L₂ ⋅L₃ ⋅m₂ ⋅m₃ + 2⋅L₁ ⋅L₂ ⋅L₃ ⋅m₂⋅m₃ ⋅cos (t₁ - t₂) - 2⋅L₁ ⋅L₂ ⋅L₃ ⋅m₂⋅m₃ ⋅cos(t₁ - t₂)⋅cos(t₁ - t₃)⋅cos(t₂ - t₃) + L₁ ⋅L₂ ⋅L₃ ⋅m₂⋅m₃ ⋅cos (t₁ - t₃) + L₁ ⋅L₂ ⋅L₃ ⋅m₂⋅m₃ ⋅cos (t₂ - t₃) - 2⋅L₁ ⋅L₂
⎢
⎢ ⎛ 2 2 2 2 2 ⎞ ⎛ 2 2 2 ⎞ ⎛ 2 2 2 2 2 2 2 2 2 2 2⎞ ⎛ 2 2 2
⎢ L₁⋅⎝- L₁⋅L₂⋅L₃ ⋅m₂⋅m₃⋅cos(t₁ - t₂) - L₁⋅L₂⋅L₃ ⋅m₃ ⋅cos(t₁ - t₂) + L₁⋅L₂⋅L₃ ⋅m₃ ⋅cos(t₁ - t₃)⋅cos(t₂ - t₃)⎠⋅⎝L₂⋅m₂⋅ṫ₂ ⋅sin(t₁ - t₂) + L₂⋅m₃⋅ṫ₂ ⋅sin(t₁ - t₂) + L₃⋅m₃⋅ṫ₃ ⋅sin(t₁ - t₃) + g⋅m₁⋅sin(t₁) + g⋅m₂⋅sin(t₁) + g⋅m₃⋅sin(t₁)⎠ - L₂⋅⎝L₁ ⋅L₃ ⋅m₁⋅m₃ + L₁ ⋅L₃ ⋅m₂⋅m₃ - L₁ ⋅L₃ ⋅m₃ ⋅cos (t₁ - t₃) + L₁ ⋅L₃ ⋅m₃ ⎠⋅⎝L₁⋅m₂⋅ṫ₁ ⋅sin(t₁ - t₂) + L₁⋅m₃⋅ṫ₁ ⋅sin(t₁ - t₂) - L₃⋅m₃⋅ṫ₃ ⋅sin(t₂ - t₃) - g⋅
⎢ ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
⎢ 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
⎢ - L₁ ⋅L₂ ⋅L₃ ⋅m₁⋅m₂⋅m₃ + L₁ ⋅L₂ ⋅L₃ ⋅m₁⋅m₃ ⋅cos (t₂ - t₃) - L₁ ⋅L₂ ⋅L₃ ⋅m₁⋅m₃ + L₁ ⋅L₂ ⋅L₃ ⋅m₂ ⋅m₃⋅cos (t₁ - t₂) - L₁ ⋅L₂ ⋅L₃ ⋅m₂ ⋅m₃ + 2⋅L₁ ⋅L₂ ⋅L₃ ⋅m₂⋅m₃ ⋅cos (t₁ - t₂) - 2⋅L₁ ⋅L₂ ⋅L₃ ⋅m₂⋅m₃ ⋅cos(t₁ - t₂)⋅cos(t₁ - t₃)⋅cos(t₂ - t₃) + L₁ ⋅L₂ ⋅L₃ ⋅m₂⋅m₃ ⋅cos (t₁ - t₃) + L₁ ⋅L₂ ⋅L₃ ⋅m₂⋅m₃ ⋅cos (t₂ - t₃) - 2⋅L₁ ⋅L
⎢
⎢ ⎛ 2 2 2 2 2 2 ⎞ ⎛ 2 2 2 ⎞ ⎛ 2 2 2 ⎞ ⎛ 2 2
⎢L₁⋅⎝L₁⋅L₂ ⋅L₃⋅m₂⋅m₃⋅cos(t₁ - t₂)⋅cos(t₂ - t₃) - L₁⋅L₂ ⋅L₃⋅m₂⋅m₃⋅cos(t₁ - t₃) + L₁⋅L₂ ⋅L₃⋅m₃ ⋅cos(t₁ - t₂)⋅cos(t₂ - t₃) - L₁⋅L₂ ⋅L₃⋅m₃ ⋅cos(t₁ - t₃)⎠⋅⎝L₂⋅m₂⋅ṫ₂ ⋅sin(t₁ - t₂) + L₂⋅m₃⋅ṫ₂ ⋅sin(t₁ - t₂) + L₃⋅m₃⋅ṫ₃ ⋅sin(t₁ - t₃) + g⋅m₁⋅sin(t₁) + g⋅m₂⋅sin(t₁) + g⋅m₃⋅sin(t₁)⎠ - L₂⋅⎝L₁⋅m₂⋅ṫ₁ ⋅sin(t₁ - t₂) + L₁⋅m₃⋅ṫ₁ ⋅sin(t₁ - t₂) - L₃⋅m₃⋅ṫ₃ ⋅sin(t₂ - t₃) - g⋅m₂⋅sin(t₂) - g⋅m₃⋅sin(t₂)⎠⋅⎝- L₁ ⋅L₂⋅L₃⋅m₁⋅m₃⋅cos(t₂ - t₃) + L₁ ⋅L₂⋅L₃⋅m₂⋅m₃⋅cos(t₁ - t₂)⋅cos(t₁ -
⎢─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
⎢ 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
⎣ - L₁ ⋅L₂ ⋅L₃ ⋅m₁⋅m₂⋅m₃ + L₁ ⋅L₂ ⋅L₃ ⋅m₁⋅m₃ ⋅cos (t₂ - t₃) - L₁ ⋅L₂ ⋅L₃ ⋅m₁⋅m₃ + L₁ ⋅L₂ ⋅L₃ ⋅m₂ ⋅m₃⋅cos (t₁ - t₂) - L₁ ⋅L₂ ⋅L₃ ⋅m₂ ⋅m₃ + 2⋅L₁ ⋅L₂ ⋅L₃ ⋅m₂⋅m₃ ⋅cos (t₁ - t₂) - 2⋅L₁ ⋅L₂ ⋅L₃ ⋅m₂⋅m₃ ⋅cos(t₁ - t₂)⋅cos(t₁ - t₃)⋅cos(t₂ - t₃) + L₁ ⋅L₂ ⋅L₃ ⋅m₂⋅m₃ ⋅cos (t₁ - t₃) + L₁ ⋅L₂ ⋅L₃ ⋅m₂⋅m₃ ⋅cos (t₂ - t₃) - 2⋅L₁ ⋅L
⎞ ⎛ 2 2 ⎞ ⎛ 2 2 2 2 2 2 ⎞ ⎤
t₃) - g⋅m₂⋅sin(t₂) - g⋅m₃⋅sin(t₂)⎠ - L₃⋅m₃⋅⎝L₁⋅ṫ₁ ⋅sin(t₁ - t₃) + L₂⋅ṫ₂ ⋅sin(t₂ - t₃) - g⋅sin(t₃)⎠⋅⎝L₁⋅L₂ ⋅L₃⋅m₂⋅m₃⋅cos(t₁ - t₂)⋅cos(t₂ - t₃) - L₁⋅L₂ ⋅L₃⋅m₂⋅m₃⋅cos(t₁ - t₃) + L₁⋅L₂ ⋅L₃⋅m₃ ⋅cos(t₁ - t₂)⋅cos(t₂ - t₃) - L₁⋅L₂ ⋅L₃⋅m₃ ⋅cos(t₁ - t₃)⎠ ⎥
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ⎥
2 2 2 2 2 2 3 2 2 2 2 3 2 2 2 3 2 2 2 2 3 2 2 2 2 3 ⎥
⋅L₃ ⋅m₂⋅m₃ + L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⋅cos (t₁ - t₂) - 2⋅L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⋅cos(t₁ - t₂)⋅cos(t₁ - t₃)⋅cos(t₂ - t₃) + L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⋅cos (t₁ - t₃) + L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⋅cos (t₂ - t₃) - L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⎥
⎥
⎞ ⎛ 2 2 ⎞ ⎛ 2 2 2 2 2 2 2 ⎞ ⎥
m₂⋅sin(t₂) - g⋅m₃⋅sin(t₂)⎠ - L₃⋅m₃⋅⎝L₁⋅ṫ₁ ⋅sin(t₁ - t₃) + L₂⋅ṫ₂ ⋅sin(t₂ - t₃) - g⋅sin(t₃)⎠⋅⎝- L₁ ⋅L₂⋅L₃⋅m₁⋅m₃⋅cos(t₂ - t₃) + L₁ ⋅L₂⋅L₃⋅m₂⋅m₃⋅cos(t₁ - t₂)⋅cos(t₁ - t₃) - L₁ ⋅L₂⋅L₃⋅m₂⋅m₃⋅cos(t₂ - t₃) + L₁ ⋅L₂⋅L₃⋅m₃ ⋅cos(t₁ - t₂)⋅cos(t₁ - t₃) - L₁ ⋅L₂⋅L₃⋅m₃ ⋅cos(t₂ - t₃)⎠ ⎥
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ⎥
2 2 2 2 2 2 3 2 2 2 2 3 2 2 2 3 2 2 2 2 3 2 2 2 2 3 ⎥
₂ ⋅L₃ ⋅m₂⋅m₃ + L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⋅cos (t₁ - t₂) - 2⋅L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⋅cos(t₁ - t₂)⋅cos(t₁ - t₃)⋅cos(t₂ - t₃) + L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⋅cos (t₁ - t₃) + L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⋅cos (t₂ - t₃) - L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⎥
⎥
2 2 2 2 2 ⎞ ⎛ 2 2 ⎞ ⎛ 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2⎞⎥
t₃) - L₁ ⋅L₂⋅L₃⋅m₂⋅m₃⋅cos(t₂ - t₃) + L₁ ⋅L₂⋅L₃⋅m₃ ⋅cos(t₁ - t₂)⋅cos(t₁ - t₃) - L₁ ⋅L₂⋅L₃⋅m₃ ⋅cos(t₂ - t₃)⎠ - L₃⋅m₃⋅⎝L₁⋅ṫ₁ ⋅sin(t₁ - t₃) + L₂⋅ṫ₂ ⋅sin(t₂ - t₃) - g⋅sin(t₃)⎠⋅⎝L₁ ⋅L₂ ⋅m₁⋅m₂ + L₁ ⋅L₂ ⋅m₁⋅m₃ - L₁ ⋅L₂ ⋅m₂ ⋅cos (t₁ - t₂) + L₁ ⋅L₂ ⋅m₂ - 2⋅L₁ ⋅L₂ ⋅m₂⋅m₃⋅cos (t₁ - t₂) + 2⋅L₁ ⋅L₂ ⋅m₂⋅m₃ - L₁ ⋅L₂ ⋅m₃ ⋅cos (t₁ - t₂) + L₁ ⋅L₂ ⋅m₃ ⎠⎥
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────⎥
2 2 2 2 2 2 3 2 2 2 2 3 2 2 2 3 2 2 2 2 3 2 2 2 2 3 ⎥
₂ ⋅L₃ ⋅m₂⋅m₃ + L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⋅cos (t₁ - t₂) - 2⋅L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⋅cos(t₁ - t₂)⋅cos(t₁ - t₃)⋅cos(t₂ - t₃) + L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⋅cos (t₁ - t₃) + L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⋅cos (t₂ - t₃) - L₁ ⋅L₂ ⋅L₃ ⋅m₃ ⎦
簡化它需要一段時間,但實際上並沒有變得更簡單。 SO 字符限制使我無法在下面粘貼simplify(sol)
的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.