未使用 sympy.nonlinsolve 找到非线性方程组的解
No Solutions Found for Nonlinear System of Equations Using sympy.nonlinsolve
我有一个非线性系统,我正在尝试使用 sympy 来解决。系统描述为:
-5.5*c_1*c_2 - 5.0*c_1 + 5.5*s_1*s_2 + 5.5
-5.5*c_1*s_2 - 5.5*c_2*s_1 - 5.0*s_1
c_1**2 + s_1**2 - 1.0
c_2**2 + s_2**2 - 1.0
运行sympy.solve(system, variables, domain=sympy.S.Reals, dict=True)
,返回如下解:
{s_1: -0.890723542830247, s_2: 0.890723542830247, c_1: 0.454545454545455, c_2: -0.454545454545455}
{s_1: 0.890723542830247, s_2: -0.890723542830247, c_1: 0.454545454545455, c_2: -0.454545454545455}
但是当 运行 sympy.nonlinsolve(system, variables)
时,没有返回任何解决方案。
为什么sympy.nonlinsolve找不到这个非线性系统的解?
还有其他功能我应该 运行 代替吗?
对于上下文,我正在努力解决 robotics inverse kinematics problem using symbolic algebra
可重现代码:
# python3.6
import sympy
from sympy import Symbol as Sym
s_1, s_2, c_1, c_2 = Sym("s_1", real=True), Sym("s_2", real=True), Sym("c_1", real=True), Sym("c_2", real=True)
p1 = -5.5*c_1*c_2 - 5.0*c_1 + 5.5*s_1*s_2 + 5.5
p2 = -5.5*c_1*s_2 - 5.5*c_2*s_1 - 5.0*s_1
p3 = 1.0*c_1**2 + 1.0*s_1**2 - 1.0
p4 = 1.0*c_2**2 + 1.0*s_2**2 - 1.0
system = [p1, p2, p3, p4]
variables = [s_1, s_2, c_1, c_2]
# {s_1: -0.890723542830247, s_2: 0.890723542830247, c_1: 0.454545454545455, c_2: -0.454545454545455}
# {s_1: 0.890723542830247, s_2: -0.890723542830247, c_1: 0.454545454545455, c_2: -0.454545454545455}
sols = sympy.solve(system, variables, domain=sympy.S.Reals, dict=True)
print(f"{len(sols)} solutions")
for sol in sols:
print(sol)
# 0 solutions
sols = sympy.nonlinsolve(system, variables)
print(f"{len(sols)} solutions")
for sol in sols:
print(sol)
如果您遵循错误消息的建议(至少在我使用的版本中)并将原始方程式 (eqs) 中的 Floats 更改为 Rational,您将得到一个解决方案:
>>> eqs=[nsimplify(i, rational=1) for i in eqs]
>>> ans = nonlinsolve(eqs,list(Tuple(*eqs).free_symbols))
>>> [[j.n(2) for j in i] for i in ans]
[[0.45, -0.45, -0.89, 0.89], [0.45, -0.45, 0.89, -0.89]]
我有一个非线性系统,我正在尝试使用 sympy 来解决。系统描述为:
-5.5*c_1*c_2 - 5.0*c_1 + 5.5*s_1*s_2 + 5.5
-5.5*c_1*s_2 - 5.5*c_2*s_1 - 5.0*s_1
c_1**2 + s_1**2 - 1.0
c_2**2 + s_2**2 - 1.0
运行sympy.solve(system, variables, domain=sympy.S.Reals, dict=True)
,返回如下解:
{s_1: -0.890723542830247, s_2: 0.890723542830247, c_1: 0.454545454545455, c_2: -0.454545454545455}
{s_1: 0.890723542830247, s_2: -0.890723542830247, c_1: 0.454545454545455, c_2: -0.454545454545455}
但是当 运行 sympy.nonlinsolve(system, variables)
时,没有返回任何解决方案。
为什么sympy.nonlinsolve找不到这个非线性系统的解?
还有其他功能我应该 运行 代替吗?
对于上下文,我正在努力解决 robotics inverse kinematics problem using symbolic algebra
可重现代码:
# python3.6
import sympy
from sympy import Symbol as Sym
s_1, s_2, c_1, c_2 = Sym("s_1", real=True), Sym("s_2", real=True), Sym("c_1", real=True), Sym("c_2", real=True)
p1 = -5.5*c_1*c_2 - 5.0*c_1 + 5.5*s_1*s_2 + 5.5
p2 = -5.5*c_1*s_2 - 5.5*c_2*s_1 - 5.0*s_1
p3 = 1.0*c_1**2 + 1.0*s_1**2 - 1.0
p4 = 1.0*c_2**2 + 1.0*s_2**2 - 1.0
system = [p1, p2, p3, p4]
variables = [s_1, s_2, c_1, c_2]
# {s_1: -0.890723542830247, s_2: 0.890723542830247, c_1: 0.454545454545455, c_2: -0.454545454545455}
# {s_1: 0.890723542830247, s_2: -0.890723542830247, c_1: 0.454545454545455, c_2: -0.454545454545455}
sols = sympy.solve(system, variables, domain=sympy.S.Reals, dict=True)
print(f"{len(sols)} solutions")
for sol in sols:
print(sol)
# 0 solutions
sols = sympy.nonlinsolve(system, variables)
print(f"{len(sols)} solutions")
for sol in sols:
print(sol)
如果您遵循错误消息的建议(至少在我使用的版本中)并将原始方程式 (eqs) 中的 Floats 更改为 Rational,您将得到一个解决方案:
>>> eqs=[nsimplify(i, rational=1) for i in eqs]
>>> ans = nonlinsolve(eqs,list(Tuple(*eqs).free_symbols))
>>> [[j.n(2) for j in i] for i in ans]
[[0.45, -0.45, -0.89, 0.89], [0.45, -0.45, 0.89, -0.89]]