Philippe
Philippe

Reputation: 65

Error when I try to use nonlinsolve to solve a system of nonlinear equations

I'm trying to use SymPy, which I've never done before, to solve a system of nonlinear equations, but I can't get it to work.

Here is what I have tried:

from sympy import *

a, b, c, d, e, f, x, y, z, g0, g1, g2 = symbols('a:f x:z g:3')
system = [(a * (x - y*z))/(b * (1 - z**2)) - g1,
(a * (y - x*z))/(c * (1 - z**2)) - g2,
f - d * (a * (x - y*z))/(b * (1 - z**2)) - e * (a * (y - x*z))/(c * (1 - z**2)) - g0]
nonlinsolve(system, [x, y, z])

What I want are the values of x, y and z in terms of the other symbols that solve the system. But I get the following error message:

Traceback (most recent call last):
  File "/Users/phl43/Desktop/test.py", line 7, in <module>
    nonlinsolve(system, [x, y, z])
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sympy/solvers/solveset.py", line 3053, in nonlinsolve
    res = _handle_positive_dimensional(polys, symbols, denominators)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sympy/solvers/solveset.py", line 2801, in _handle_positive_dimensional
    denominators)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sympy/solvers/solveset.py", line 2719, in substitution
    old_result, solveset_real)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sympy/solvers/solveset.py", line 2708, in _solve_using_known_values
    result.remove(res)
ValueError: list.remove(x): x not in list

Any idea how to make this work?

Upvotes: 0

Views: 550

Answers (1)

smichr
smichr

Reputation: 19047

See what you can learn by solving the system manually without checking:

>>> r = solve(system, dict=True, manual=True, check=False)
>>> r
[{x: -y, z: -1}, {x: y, z: 1}, {x: (b*g1 + c*g2*z)/a, y: (b*g1*z + c*g2)/a}]

The first two solutions are not going to work because that would lead to division by 0. The last solution sets the first two equations to 0 while the last solution satisfies the first two equations but these make the last expression independent of z:

>>> [simplify(i.subs(r[-1])) for i in r]
[0, 0, -d*g1 - e*g2 + f - g0]

So z can be anything provided that g0 = f -d*g1 - e*g2. It is an ill-posed set of equations for solving for x, y, and z.

Upvotes: 1

Related Questions