我不太明白如何在 Python 中使用 SymPy 求解我的方程組。我試過使用solve()and nsolve()。solve()沒有給我答案,它只是站著走,nsolve()給了我這個錯誤。
ValueError: Could not find root within given tolerance. (7378.8100001153525709 > 2.16840434497100886801e-19) Try another starting point or tweak arguments.
這是我嘗試使用時寫的solve():
from sympy import *
theta_1, theta_2, theta_3 = symbols('theta_1 theta_2 theta_3')
eq1 = -136.2*sin(theta_2)*sin(theta_3)*cos(theta_1) 136.2*cos(theta_1)*cos(theta_2)*cos(theta_3) 222.1*cos(theta_1)*cos(theta_2)
eq2 = -136.2*sin(theta_1)*sin(theta_2)*cos(theta_3) 136.2*sin(theta_1)*cos(theta_2)*cos(theta_3) 222.1*sin(theta_1)*cos(theta_2)
eq3 = 136.2*sin(theta_2)*cos(theta_3) 222.1*sin(theta_2) 136.2*sin(theta_3)*cos(theta_2) 100.9
eq1 = Eq(eq1, 0)
eq2 = Eq(eq2, (-323.90))
eq3 = Eq(eq3, 176.71)
ans = solve((eq1, eq2, eq3), (theta_1, theta_2, theta_3))
print(ans)
這是我嘗試使用時寫的nsolve():
from sympy import *
theta_1, theta_2, theta_3 = symbols('theta_1 theta_2 theta_3')
eq1 = -136.2*sin(theta_2)*sin(theta_3)*cos(theta_1) 136.2*cos(theta_1)*cos(theta_2)*cos(theta_3) 222.1*cos(theta_1)*cos(theta_2)
eq2 = -136.2*sin(theta_1)*sin(theta_2)*cos(theta_3) 136.2*sin(theta_1)*cos(theta_2)*cos(theta_3) 222.1*sin(theta_1)*cos(theta_2)
eq3 = 136.2*sin(theta_2)*cos(theta_3) 222.1*sin(theta_2) 136.2*sin(theta_3)*cos(theta_2) 100.9
ans = nsolve((eq1, eq2, eq3), (theta_1, theta_2, theta_3), (0, -323.9, 176.71))
print(ans)
我想找到 theta1、theta2 和 theta3 的運算式。最好采用 arctan2 的形式。有誰知道我該怎么做?或者為什么這不起作用?
uj5u.com熱心網友回復:
我假設您的輸入應該是以度為單位的角度。nsolve如果您以弧度給出輸入,這里可以給您答案:
In [48]: p = (pi/180).evalf()
In [49]: nsolve((eq1, eq2, eq3), (theta_1, theta_2, theta_3), (0, -323.9*p, 176.71*p))
Out[49]:
?-1.5707963267949 ?
? ?
?-9.36640725122338?
? ?
?-2.49008907692194?
我們可以將您的系統簡化為具有如下有理系數的多項式系統:
In [60]: c1, c2, c3, s1, s2, s3 = symbols('c1:4 s1:4')
...: rep = {cos(theta_1): c1, cos(theta_2): c2, cos(theta_3): c3,
...: sin(theta_1): s1, sin(theta_2): s2, sin(theta_3): s3}
...:
...: eqs = [eq.subs(rep) for eq in [eq1, eq2, eq3]] [
...: c1**2 s1**2 - 1, c2**2 s2**2 - 1, c3**2 s3**2 - 1]
...: eqs = [nsimplify(eq) for eq in eqs]
In [61]: for eq in eqs:
...: print(eq)
...:
Eq(681*c1*c2*c3/5 - 681*c1*s2*s3/5 2221*c1/10, 0)
Eq(681*c2*c3*s1/5 - 681*c3*s1*s2/5 2221*s1/10, -3239/10)
Eq(681*c2*s3/5 681*c3*s2/5 2221*s2/10 1009/10, 17671/100)
c1**2 s1**2 - 1
c2**2 s2**2 - 1
c3**2 s3**2 - 1
從那里您可以使用 SymPy 的groebner函式來計算多項式系統解的 Groebner 基。為了對稱,我們將添加一個分隔元素 t:
In [62]: gb = groebner(eqs, order='grevlex').fglm('lex')
這就像一個新的分離方程串列,其解是您想要的不同 theta 的 cos 和 sin 值。
最終方程是一個 20 次多項式,c3它的解可以代入其他方程,得到系統的完整解。多項式有 4 個實根,每個根都是較小的 8 次不可約多項式的根。由于 Abel-Ruffini,根不能有根式運算式,但可以用數值計算它們:
In [70]: c3num = [r.n() for r in real_roots(gb[-1])]
In [71]: c3num
Out[71]: [-0.795172954862362, -0.552080421425289, 0.702791508766261, 0.99923774387104]
每一個都可以用來解決 Groebner 基礎的其余部分:
In [73]: for c3n in c3num:
...: for eq in gb[:-1]:
...: print(solve([eq.subs(c3, c3n)], dict=True))
...: print(solve([c3 - c3n], dict=True))
...: print()
...:
[{s1: -1.00000000000000}]
[{s2: -0.0583375690202956}]
[{s3: -0.606382694211788}]
[{c1: 0}]
[{c1: 0.0}]
[{c2: -0.998296913792728}]
[{c3: -0.795172954862362}]
[{s1: -1.00000000000000}]
[{s2: 0.881316344165583}]
[{s3: 0.833790866034178}]
[{c1: 0}]
[{c1: 0.0}]
[{c2: -0.472526720395763}]
[{c3: -0.552080421425289}]
[{s1: -1.00000000000000}]
[{s2: -0.0656752871541357}]
[{s3: 0.711395878031908}]
[{c1: 0}]
[{c1: 0.0}]
[{c2: 0.997841047763359}]
[{c3: 0.702791508766261}]
[{s1: -1.00000000000000}]
[{s2: 0.226102985521720}]
[{s3: -0.0390375619754195}]
[{c1: -3.05175781250000e-5}, {c1: 3.05175781250000e-5}]
[{c1: 0.0}]
[{c2: 0.974103403161280}]
[{c3: 0.999237743871040}]
最后一種情況似乎在數值上不一致,但這可能是舍入誤差,因為這在數值上非常不穩定,也許解應該是 c1=0。我認為發生這種情況是因為你的方程是一個稍微退化的情況。
The above gives all solutions for the cos and sin of your angles and you can use atan2 to get from there to the angles themselves. You can use nsolve to refine any of the above as accurately as you want. The first of the 4 cases is the one found by nsolve above.
If you wanted an explicit analytic expression here then it seems unlikely.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/441742.html
上一篇:Javascript-如何計算動態資料集每一頁的分頁范圍數?
下一篇:以恒定速率移動球與角度
