繁体   English   中英

python SciPy错误brentq

[英]python SciPy error brentq

我正在尝试寻找功能的根源

from scipy.optimize import brentq
import matplotlib.pyplot as plt
from numpy.polynomial import Polynomial,Legendre
def f():
    return Legendre.basis(3).linspace()

brentq(f,1,2)

但我不断收到错误

TypeError: f() takes 0 positional arguments but 1 was given

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

看一下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

而且,如果您只想将其应用于Legendre多项式,则无需使用brentq 您可以调用Legendre.basis(3)roots()方法:

In [115]: leg3 = Legendre.basis(3)

In [116]: leg3.roots()
Out[116]: array([ -7.74596669e-01,  -8.32938319e-17,   7.74596669e-01])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM