SciPy集成错误:只能将size-1数组转换为Python标量

时间:2019-03-21 10:20:20

标签: python scipy integral

我有以下简单的代码在其中使用SciPy的集成(涉及Bessel函数)。

import matplotlib.pyplot as plt
import numpy as np
import scipy.integrate as integrate
import scipy.special as sp
from scipy.special import jn


U = np.arange(0.0, 10.0, 0.1)
#U = np.linspace(0,10,100)


def intgl(U):
    #intgl = integrate.quad(lambda x: jn(1,x)/(x*(1.0+np.exp(U*x*0.5))), 0.1, 1000)
    intgl, err = integrate.quad(lambda x: sp.jv(1,x)/(x*(1.0+np.exp(U*x*0.5))), 0, 100)
    return intgl


Del = U - 4.0 + 8.0*intgl(U)



print ("U, Del=",U, Del)
plt.plot(U,Del)
plt.show()

但是,我遇到错误消息:

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    Del = U - 4.0 + 8.0*intgl(U)
  File "test.py", line 14, in intgl
    intgl, err = integrate.quad(lambda x: sp.jv(1,x)/(x*(1.0+np.exp(U*x*0.5))), 0, 100)
  File "/usr/local/lib/python3.7/site-packages/scipy/integrate/quadpack.py", line 341, in quad
    points)
  File "/usr/local/lib/python3.7/site-packages/scipy/integrate/quadpack.py", line 448, in _quad
    return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
TypeError: only size-1 arrays can be converted to Python scalars

可能会发生什么,如何解决?希望我正确调用函数intgl

0 个答案:

没有答案