quadpack.error:提供的函数不会在python中返回有效的float

时间:2014-03-29 10:30:51

标签: python numpy

我想将(z)和make(x)和(y)的函数集成为参数。我的目标是在不同的位置(x,y)得到积分的结果。在这种情况下,我应该得到16个积分值,它们对应于(x1,y1),(x1,y2)......,(x2, y1)......等等。 这是代码:

 import numpy as np
 import math
 import scipy.integrate

 a = 5
 b = 6
 xn=np.linspace(0,3,4)
 yn=np.linspace(3,6,4)

 x,y=np.ix_(xn,yn)


 def fun(z,x,y):
     model=(x**2/a**2+y**2/b**2+z**2/a**2)**0.5
     #print(model)
     return model

 def int(x,y):
     int=scipy.integrate.quad(fun,0,10,args=(x,y,))[0]
     print (int)
     return int
 integral = int(x,y)
 print (integral)

但我收到此错误消息:

 ....
 int=scipy.integrate.quad(model,0,10,args=(x,y,))[0]
 File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-  
 packages/scipy/integrate/quadpack.py", line 254, in quad
 retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
 File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site- 
 packages/scipy/integrate/quadpack.py", line 319, in _quad
 return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
 quadpack.error: Supplied function does not return a valid float.

请有人告诉我如何修复此错误,并提前感谢您。

1 个答案:

答案 0 :(得分:3)

如错误所示,您的fun函数应返回float,而是返回2D数组:

[[ 1.11803399  1.20185043  1.30170828  1.41421356]
 [ 1.13578167  1.21837779  1.31698308  1.42828569]
 [ 1.18743421  1.26666667  1.36177988  1.46969385]
 [ 1.26885775  1.34329611  1.43333333  1.53622915]]

您正在集成的函数应在参数空间中的某个点评估为单个数字。

如果您尝试进行N维积分,可能需要查看scipy.integrate.nquad