在SymPy中一次解決非線性方程組和不等式系統(Solving a system of non linear equations and inequalities at once in SymPy)
我是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]
使用變量清單'lagVars':
>>> lagVars
(x1, x2, w0, w1, w2)
如您所見,我的系統包含方程和不等式。
試:
>>> solve(kunSys,lagVars)
得到:
NotImplementedError:
inequality has more than one symbol of interest
但是在分别解決方程和不等式時它可以正常工作:
>>> 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)
但這不是一個想要的結果。 我嘗試使用solveset()但它似乎也不起作用。 我google了很多,但未能找到答案。
題:
我該如何解決這個系統?
I'm new to SymPy and python and I was faced with a problem. I'm trying to solve a system '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
(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.
Question:
How do I solve this system?
原文:https://stackoverflow.com/questions/50269881
更新時間:2019-12-13 22:59
最滿意答案
SymPy目前不知道如何處理混合不等式和等式,但由于你的不等式隻是variable >= 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)]
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
>>> 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)]
2018-05-10
相關問答
隔離y表明我們都是y>=ax+b形式的不等式。 這意味着我們可以繪制使用圖的不等式,相等版本的函數以及函數在區間上獲得的最大值。 using Plots
f(x) = (2/5)x-6/5
g(x) = -x
X = -10:10
the_max = max(f(X[end]), g(X[1]))
plot(X, f, fill = (the_max, 0.5, :auto))
plot!(X, g, fill = (the_max, 0.5, :auto))
這給了我們 如果第二個方程有不等
...
有兩種選擇,您可以使用包含非線性求解器的日packages軟體包,用CI編寫。 我發現的唯一問題是你需要給它一個很好的初始估計。 第二個選擇是使用NLEQ或NLEQ2,我認為它是優越的(FORTRAN中的writtein,但很容易連結到類似于C的語言。但是,我剛剛找到了一些問題,有一個很好的網站,其中列出了可能的選項在http://plato.asu.edu/sub/zero.html There are two options for you, you can use the sundials
...
看起來這個子產品沒有包含在1.0.0版本中,但是隻能在目前版本中使用。 無論如何,檢查sympy repo的目前狀态可能是個好主意,因為它們的釋出很少。 根據檔案 ,這是如此簡單 git clone git://github.com/sympy/sympy.git
cd sympy
sudo python setupegg.py develop
(我不得不使用python3代替python因為我同時安裝了Python 2和Python 3系統,您也可以使用virtualenv來避免sudo 。)
...
這是Sympy的基本操作,你應該研究一下這個文檔。 隻是為了讓你開始: import sympy as sp
f_m, f_c, f_p = sp.var('f_m, f_c, f_p')
a0, a1, a2 = sp.var('a0:3')
dx = sp.var('dx')
eq1 = sp.Eq(f_m, a0 + a1*(-dx) + a2*(-dx)**2)
eq2 = sp.Eq(f_c, a0)
eq3 = sp.Eq(f_p, a0 + a1*(dx) + a2*(dx)**
...
SymPy目前不知道如何處理混合不等式和等式,但由于你的不等式隻是variable >= 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
...
我不相信你可以使用帶有限制的FindRoot (不等式等)。 對于非線性限制優化,您将需要研究以下内置函數 最大化[...] NMaximize [...] FindMaximum [...] 最小化[...] NMinimize [...] FindMinimum [...] 我不确定你對這個特殊問題想要哪一個。 以下是最大化實施的示例: Maximize[{(2 x + y - z)/(5 x - 7 y + 3),
0 <= x + y + z <= 1 && 1 <= x - y +
...
你的線性規劃是一個問題,你的等式和不等式是局限性,你希望最小化(然後最大化)表達式y 。 平等,不平等和表達都是線性的,是以這使得它成為線性規劃。 scipy軟體包使用scipy.optimize.linprog函數可以完成這種線性程式設計。 這裡是評論代碼做你想做的。 請注意,所有不平等都稍微改變,以包含相等性,這是y的最大值或最小值所必需的。 要找到y的最大值,代碼會找到-y的最小值,然後列印出該值的加法反比,因為linprog目标函數最小化。 最後, linprog的不等式限制必須“小于或等于”
...
我不确定要了解你的問題,但是你可以讓腳本像這樣工作: import math
import numpy as np
vars = [[1, 1], [1/2 + math.sqrt(5)/2, -math.sqrt(5)/2 + 1/2]]
outcomes = [1, 1]
solution = np.linalg.solve(vars, outcomes)
print("Solutions:", solution)
I am not sure to understand your qu
...
我在解決函數中使用了一些添加的标志: 當我這樣做時: import sympy as sy
sy.init_printing()
m, v0, k, g, R, u, v = sy.symbols('m v0 k g R u v') #Define Symbols
sy.solve((0.5*m*v0**2 - m*g*2*R - 0.5*m*v**2 - 0.5*k*m*u**2,
m*v0 + m*v - k*m*u, m*g - m*((u+v)**2)/R),
...
SciPy有一組可能适合您應用的非線性求解器。 它們是scipy.optimize的一部分,專為非線性系統而設計。 文檔可以在這裡找到。 有關如何使用求解器的深入讨論,請參閱此前關于該主題的SO讨論。 SciPy has a set of nonlinear solvers that would probably work for your application. They are part of scipy.optimize and are specifically designed for
...