我有一个清单:
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))
当我尝试运行此代码时,出现此错误:
Traceback (most recent call last):
File "<ipython-input-88-ca3005760f4b>", line 19, in <module>
plt.plot(F(x))
File "<ipython-input-88-ca3005760f4b>", line 14, in F
y,err = integrate.quad(f,0,val)
File "C:\Anaconda2\lib\site-packages\scipy\integrate\quadpack.py", line 311,
in quad points)
File "C:\Anaconda2\lib\site-packages\scipy\integrate\quadpack.py", line 376,
in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
error: Supplied function does not return a valid float.
任何人都可以帮助我理解为什么我会收到此错误吗?
您遇到此错误是因为
f
中的这两行:
else:
res = q2[:10]
在这里,您将从函数返回列表的一部分。
quad
不喜欢这样;根据其文档,它需要一个返回双精度值(即Python浮点数)的函数,而不是列表。
虽然您的问题中没有给出您的预期输出,但将该行更改为
res = q2[0]
或
q2
中的任何其他任意索引可纠正错误。