在SymPy中一次解决非线性方程组和不等式系统

时间:2021-12-24 12:36:56

I'm new to SymPy and python and I was faced with a problem. I'm trying to solve a system 'kunSys':

我是SymPy和python的新手,我遇到了一个问题。我正在尝试解决系统'kunSys':

>>> kunSys
[-w0 + w1 - 8*x1 + 20,
-2*w0 + w2 - 8*x2 + 4,
w0*(-x1 - 2*x2 + 2),
w1*x1,
w2*x2,
w0 >= 0,
w1 >= 0,
w2 >= 0]

With a list of variables 'lagVars':

使用变量列表'lagVars':

>>> lagVars
(x1, x2, w0, w1, w2)

As you can see, my system contains both eqations and inequalities.

如您所见,我的系统包含方程和不等式。

Trying:

试:

>>> solve(kunSys,lagVars)

Get:

得到:

NotImplementedError: 
inequality has more than one symbol of interest

But it works fine when solving eqations and inequalities separately:

但是在分别解决方程和不等式时它可以正常工作:

>>> kunSys[:5]
[-w0 + w1 - 8*x1 + 20,
-2*w0 + w2 - 8*x2 + 4,
w0*(-x1 - 2*x2 + 2),
w1*x1,
w2*x2]

>>> solve(kunSys[:5],lagVars)
[(0, 0, 0, -20, -4),
(0, 1/2, 0, -20, 0),
(0, 1, -2, -22, 0),
(2, 0, 4, 0, 4),
(11/5, -1/10, 12/5, 0, 0),
(5/2, 0, 0, 0, -4),
(5/2, 1/2, 0, 0, 0)]

>>> kunSys[5:]
[w0 >= 0, w1 >= 0, w2 >= 0]

>>> solve(kunSys[5:],lagVars)
(0 <= w0) & (0 <= w1) & (0 <= w2) & (w0 < oo) & (w1 < oo) & (w2 < oo)

But this is not a wanted result. I tried to use solveset() but it doesn't seem to work also. I googled a lot, but failed to find the answer.

但这不是一个想要的结果。我尝试使用solveset()但它似乎也不起作用。我google了很多,但未能找到答案。

Question:

题:

How do I solve this system?

我该如何解决这个系统?

1 个解决方案

#1


1  

SymPy presently doesn't know how to handle mixed inequalities and equalities, but since your inequalities are just variable >= 0, you can work around this by just defining those symbols to be nonnegative. solve will then filter the solutions based on that

SymPy目前不知道如何处理混合不等式和等式,但由于你的不等式只是变量> = 0,你可以通过将这些符号定义为非负的来解决这个问题。然后,解决方案将基于此过滤解决方案

>>> w0, w1, w2 = symbols('w0:3', nonnegative=True)
>>> x1, x2 = symbols("x1 x2")
>>> solve([-w0 + w1 - 8*x1 + 20,
... -2*w0 + w2 - 8*x2 + 4,
... w0*(-x1 - 2*x2 + 2),
... w1*x1,
... w2*x2], (w0, w1, w2, x1, x2))
[(0, 0, 0, 5/2, 1/2), (12/5, 0, 0, 11/5, -1/10), (4, 0, 4, 2, 0)]

#1


1  

SymPy presently doesn't know how to handle mixed inequalities and equalities, but since your inequalities are just variable >= 0, you can work around this by just defining those symbols to be nonnegative. solve will then filter the solutions based on that

SymPy目前不知道如何处理混合不等式和等式,但由于你的不等式只是变量> = 0,你可以通过将这些符号定义为非负的来解决这个问题。然后,解决方案将基于此过滤解决方案

>>> w0, w1, w2 = symbols('w0:3', nonnegative=True)
>>> x1, x2 = symbols("x1 x2")
>>> solve([-w0 + w1 - 8*x1 + 20,
... -2*w0 + w2 - 8*x2 + 4,
... w0*(-x1 - 2*x2 + 2),
... w1*x1,
... w2*x2], (w0, w1, w2, x1, x2))
[(0, 0, 0, 5/2, 1/2), (12/5, 0, 0, 11/5, -1/10), (4, 0, 4, 2, 0)]