Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用brentq求两条直线的根_Python_Numpy_Scipy - Fatal编程技术网

Python 用brentq求两条直线的根

Python 用brentq求两条直线的根,python,numpy,scipy,Python,Numpy,Scipy,我试图写一个函数,当y值大约为零时,返回一些数据的x值。我有两个列表可以输入函数,例如,对于x值为[1,4,5],对于y值为[-3,5,9]。我编写了这个函数,使用插值,然后使用索引在y值接近零时首先查找y值的索引,并使用它查找此时的x值:请注意:我添加的图形和y=0线仅用于说明目的 def root(xs, ys): xfine = np.linspace(min(xs), max(xs), 10000) y0 = inter.interp1d(xs, ys, kind = '

我试图写一个函数,当y值大约为零时,返回一些数据的x值。我有两个列表可以输入函数,例如,对于x值为[1,4,5],对于y值为[-3,5,9]。我编写了这个函数,使用插值,然后使用索引在y值接近零时首先查找y值的索引,并使用它查找此时的x值:请注意:我添加的图形和y=0线仅用于说明目的

def root(xs, ys):
    xfine = np.linspace(min(xs), max(xs), 10000)
    y0 = inter.interp1d(xs, ys, kind = 'linear')
    f1 = xfine, y0(xfine)
    x2fine = np.linspace(min(xs), max(xs), 10000)
    y2 = np.linspace(0,0, 10000)
    f2 = x2fine, y2
    pl.plot(xfine, y0(xfine))
    pl.plot(x2fine, y2)
    pl.show()
    closest = min(abs(y0(xfine)))
    xindex = numpy.searchsorted(y0(xfine), closest)
    print round(xfine[xindex], 3)

这似乎给了我正确的答案,但我被告知应该在函数中使用brentq。然而,我只得到上面提到的数据,我发誓brentq需要输入两个连续函数,不是吗?我怎样才能让brentq只使用一个新的数字而不是一个函数来实现这一点呢?

尽管您可以在插值函数上使用brentq,但由于您已经在使用插值,只需使用它来反转函数即可:

finv = inter.interp1d(y, x)
print (finv(0))