fsolve在求解简单代数方程时返回不必要的警告

2024-09-27 07:25:11 发布

您现在位置:Python中文网/ 问答频道 /正文

使用fsolve,我想找到x,在f的情况下,f(x)=0

y=1.
def f(x):
    return ((x-y)**2-y**2)**2

如我们所见,函数有一个x=0的最小值,对于这个值,我们正好有f(x=0)=0。 当我尝试用fsolve进行求解时,它返回一个非常接近于零的值,但也会返回一个警告:

from scipy.optimize import fsolve
y=1
def f(x):
    return ((x-y)**2-y**2)**2

print fsolve(f,x0=0.001,xtol=1e-6)

输出为

RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last ten iterations.
  warnings.warn(msg, RuntimeWarning)
[1.55543489e-16]

所以,结果是令人满意的,但我如何才能避免警告,为什么它返回它,考虑到它也提供了一个体面的结果?你知道吗


Tags: the函数fromimport警告returndef情况

热门问题