python SciPy错误brentq

时间:2018-01-28 00:19:12

标签: python scipy

我试图找到函数的根源

MainIntent

但我一直收到错误

this.handler.state = states.STAGE_TWO

我不知道它有什么问题,因为据我所知,brentq的格式是正确的。

1 个答案:

答案 0 :(得分:2)

查看brentq docstring中的示例。函数f必须接受一个参数,并返回单个值。

例如,

In [106]: import numpy as np

In [107]: from scipy.optimize import brentq

In [108]: from numpy.polynomial import Legendre

In [109]: def f(x):
     ...:     return Legendre.basis(3)(x)
     ...: 

In [110]: brentq(f, 0.5, 1)
Out[110]: 0.7745966692411781

在这种情况下,不需要定义f,因为Legendre.basis(3)返回的对象是可调用的对象,可以直接传递给brentq

In [111]: brentq(Legendre.basis(3), 0.5, 1)
Out[111]: 0.7745966692411781

如果您只对将其应用于勒让德多项式感兴趣,则无需使用brentq。您可以调用roots()的{​​{1}}方法:

Legendre.basis(3)