如何用Python求解非线性方程组?

问题描述 投票:0回答:1

我正在尝试用 Python 求解这个非线性方程组:

2y3 + y2 - y5 - x4 + 2x3 - x2 = 0
(-4x3 + 6x2 - 2x) / (5y4 - 6y2 - 2y) = 1

这是我的代码:

from sympy import symbols, Eq, solve
x, y = symbols('x y')
eq1 = Eq(2*y**3 + y**2 - y**5 - x**4 + 2*x**3 - x**2, 0)
eq2 = Eq((-4*x**3 + 6*x**2 - 2*x)/(5*y**4 - 6*y**2 - 2*y), 1)
points = solve([eq1, eq2], [x,y])

我知道这个方程组有 5 个解,但是这段代码我没有得到任何解。有人知道我该如何继续吗?

顺便说一句,第二个方程是第一个方程的导数 dy/dx,所以基本上我试图找到路边 eq1 中切线斜率为 1 的所有点。第一个方程的图形是这个。

Graph of equation 1

我已经用 Geogebra 解决了这个问题并得到了 5 个答案。我正在尝试使用 Python,想看看是否可以使用任何 Python 库来解决它。

Solution using Geogebra

python math sympy nonlinear-equation
1个回答
0
投票

可以通过这些方程的 Groebner 基找到解(5 个中的 3 个,其中 eq2 的分母不为 0)(尽管我不完全清楚如何处理基中存在 2 个 x-y 方程):

>>> from sympy import *

# your equations go here, then

>>> n, d = fraction((eq2.lhs-eq2.rhs).normal())
>>> sols = list(groebner((eq1,n),x,y))
>>> yy = real_roots(sols[-1])
>>> xy=list(sorted(set([(s.n(4),yi.n(4)) for e in sols[:2] for yi in yy for s in solve(e.subs(y,yi.n()))])))
>>> [d.subs(dict(zip((x,y),i))) for i in xy]
[6.697, 0, -0.1917, -0.2850, 0, -0.2850, -0.1917, 6.697]
>>> xy = [xy[i] for i in range(len(xy)) if i not in (1,4)]
>>> [i for i in xy if all(abs(v)<1e-4 for v in Tuple(eq1.lhs,n).subs(dict(zip((x,y),i))))]
[(-0.7575, 1.453), (0.2267, -0.5028), (1.106, -0.5731)]

这些只是概念验证,精度较低。

© www.soinside.com 2019 - 2024. All rights reserved.