使用integrate.quad进行集成时出现无效的浮点错误

时间:2017-05-30 00:11:53

标签: python-2.7 scipy integration

我有一个清单:

EventLog

我正在尝试整合。 我做了以下事情:

q1 = [0,1.9488e-06,1.5473e-05,5.1829e-05,0.00012192,0.00023633,0.00040526,0.00063862,0.00094596,0.0013365,0.0018192,0.0024025,0.0030949,0.0039041,0.0048379,0.0059036]

当我尝试运行此代码时出现此错误:

def f(x):
    if (np.abs(x)<1e-10):
        res = x
    else:
        res = q2[:10]
    return res
x = np.arange(0,10,0.001)
def F(x):
    res = np.zeros_like(x)
    for i,val in enumerate (x):
        y,err = integrate.quad(f,0,val)
        res[i] = y
    return res     
plt.plot(F(x))

任何人都可以帮助我理解为什么我会收到此错误吗?

1 个答案:

答案 0 :(得分:0)

由于f中的这两行:

,您出现此错误
    else:
        res = q2[:10]

在这里,您将从该函数返回一个列表片段。 quad并不喜欢这样;根据{{​​3}},它需要一个返回double的函数(即Python float),而不是列表。

虽然问题中未给出您的预期输出,但请将该行更改为

        res = q2[0]

q2中的任何其他任意索引都会纠正错误。