簡體   English   中英

在Python中解決非線性方程組(scipy.optimize.fsolve)

[英]Solve a system of non-linear equations in Python (scipy.optimize.fsolve)

我正在嘗試解決以下簡單的非線性方程組( Source(第二個示例) ):

(I) y - x^2 = 7 - 5x
(II) 4y - 8x = -21

它應該只有一個解(x = 3.5,y = 1.75)。

我當前使用scipy堆棧的方法如下:

from scipy.optimize import fsolve

def equations(p):
    x, y = p
    return (y - x**2 -7 + 5*x, 4*y - 8*x + 21)

x, y =  fsolve(equations, (5, 5))

print(equations((x, y)))

並產生以下結果(不是結果):

(0.0, 0.0)

我已經嘗試過不同的初始估算,但是它沒有提供正確的解決方案。

我的方法有什么問題? 我想念什么嗎?

提前致謝!

這工作得很好:

In [1]: %paste
from scipy.optimize import fsolve

def equations(p):
    x, y = p
    return (y - x**2 -7 + 5*x, 4*y - 8*x + 21)

x, y =  fsolve(equations, (5, 5))

print(equations((x, y)))

## -- End pasted text --
(0.0, 0.0)

In [2]: x
Out[2]: 3.5000000414181831

In [3]: y
Out[3]: 1.7500000828363667

equations(x, y)(0, 0)表示y - x**2 -7 + 5*x4*y - 8*x + 21均為0。

也許您感到困惑並打算print(x, y)而不是print(equations(x, y))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM