Reputation: 600
I have a non linear system I am trying to solve for using sympy. The system is described as:
-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
Running sympy.solve(system, variables, domain=sympy.S.Reals, dict=True)
, the following solutions are returned:
{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}
However when running sympy.nonlinsolve(system, variables)
, no solutions are returned.
Why does sympy.nonlinsolve fail to find solutions to this nonlinear system?
Are there any other functions I should be running instead?
For context, I am working on solving the robotics inverse kinematics problem using symbolic algebra
Reproducible code:
# 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)
Upvotes: 1
Views: 1315
Reputation: 19029
If you follow the advice of the error message (at least in the version I am using) and change the Floats to Rational in your original equations (eqs) you will get a solution:
>>> 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]]
Upvotes: 2