Martin-Yiu
Martin-Yiu

Reputation: 23

TypeError: only size-1 arrays can be converted to Python scalars when trying to do mutiple integration in arrays

I need to get the result of definite integral with a set of upper limit from an array, but there is error when I try to run it.

E_midpoint = np.linspace(-8,-0.52,1000)
m_e = 0.510998950

rate = []
err = []

for i in range(len(E_midpoint)-1):
    def integrand_f(E_e):                      
        p_e = np.sqrt(np.power(E_midpoint,2)-np.power(m_e,2))  
        E_m = m_e - E_midpoint[i]
        f_not_int = E_e*np.power(E_m-E_e,2)/m_e
        return f_not_int

    rate_x, err_x =quad(integrand_f, m_e, m_e - E_midpoint[i])
    rate.append(rate_x)
    err.append(err_x)

Then it have the error "TypeError: only size-1 arrays can be converted to Python scalars" for quad.

    TypeError                                 Traceback (most recent call last)
<ipython-input-21-6d8ed5a39407> in <module>
     62         return f_not_int
     63 
---> 64     rate_x, err_x =quad(integrand_f, m_e, m_e - E_midpoint[i])
     65     rate.append(rate_x)
     66     err.append(err_x)

~\anaconda3\lib\site-packages\scipy\integrate\quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
    349 
    350     if weight is None:
--> 351         retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
    352                        points)
    353     else:

~\anaconda3\lib\site-packages\scipy\integrate\quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
    461     if points is None:
    462         if infbounds == 0:
--> 463             return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
    464         else:
    465             return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)

TypeError: only size-1 arrays can be converted to Python scalars

May I know how can I get an array of definite integration output for a function with a set of upper limit array E_midpoint?

Upvotes: 0

Views: 42

Answers (0)

Related Questions