集成函数时的ValueError

时间:2016-11-14 17:40:23

标签: python numpy

我对Python很陌生,所以这可能是一个非常基本的问题。我收到了错误

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

在我的以下代码中:

import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
from sympy import *
%matplotlib inline

x = y = z = np.linspace (0, 5)

#Define Integrals
P = 67.6*(np.sqrt (0.3*((1+z)**3)) + 0.7)

import scipy.integrate as integrate
from scipy.integrate import quad

A = quad(lambda x: ((1+x)*((P)**-3)), z, sp.oo)
B = quad(lambda y: ((1+y)*((P)**-3)), 0, sp.oo)

D = (P/67.6)*(a/b)

plt.plot(z, D, 'r')

plt.xlabel ('$z$'); plt.ylabel ('$D_+$')

plt.savefig('LinearGrowthFactor.png',dpi = 400)

我已经搜索了一段时间的答案,但我还没有发现任何我能理解并实际应用于我的代码的内容。错误出现在该行

A = quad(lambda x: ((1+x)*((P)**-3)), z, sp.oo)

因此同样的错误也可能出现在下面的行中。

编辑: 这是我收到的完整错误。

ValueError                                Traceback (most recent call last)
<ipython-input-90-94d843ab23dd> in <module>()
 13 from scipy.integrate import quad
 14 
---> 15 A = quad(lambda x: ((1+x)*((P)**-3)), z, sp.oo)
 16 B = quad(lambda y: ((1+y)*((P)**-3)), 0, sp.oo)
 17 

/Applications/anaconda/lib/python3.5/site-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
314     if (weight is None):
315         retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
--> 316                        points)
317     else:
318         retval = _quad_weight(func, a, b, args, full_output, epsabs, epsrel,

/Applications/anaconda/lib/python3.5/site-packages/scipy/integrate/quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
363 def _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points):
364     infbounds = 0
--> 365     if (b != Inf and a != -Inf):
366         pass   # standard integration
367     elif (b == Inf and a != -Inf):

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

0 个答案:

没有答案