Function integration returns “only length-1 arrays can be converted to Python scalars”
我正试图将一个函数整合到一个给定的范围内,该范围显示了零angular(θ)方向上的粒子流与粒子能量E的关系。我尝试了几种方法并得到了不同的错误,但最后仍然有两种错误。我对Python的了解是有限的,我尝试学习新的做事方式,但我已经使用该功能好几天了,但没有成功。
此刻我的功能如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from numpy import radians, cos, arange from scipy.integrate import quad def integral(self): theta=0 E = arange(1, 5000, 1) costh = cos(radians(theta)) a = 18 / (E * costh + 145) b = (E + 2.7 / costh)**-2.7 c = (E + 5) / (E + 5 / costh) return a*b*c*1**4 A = quad(integral, 500, 1000) |
将" quad "应用于这样的函数将返回:
TypeError: only length-1 arrays can be converted to Python scalars
如果我不将" self "作为参数放入函数中,它将返回:
TypeError: integral() takes 0 positional arguments but 1 was given
有人对如何绕过此方法有想法吗?
该函数必须返回
1 2 | def integral(x): return x * x |
然后它起作用了。当然,这不是您所需要的,但这是
您可能想在这里查看有关如何使用
一些建议:
- 缩进使用四个空格(而不是示例中的5)
- 不需要在结果周围加上括号。删除以更清楚。