为什么我定义的函数不接受数组作为输入



我正在处理二重积分,内部积分有可变边界。我写了一个函数,使用SciPy的四次积分,可以计算这个积分。然而,我想要的只是计算内部积分,这样我只剩下一个关于某个变量的单一的、未计算的积分。然后,我想画出这个"中途"评估的二重积分与该变量的范围,这样我就可以看到一些趋势。然而,当我输入该变量的数组(它只是0-10000,但增量为1(时,它会提供以下错误消息:

"ValueError:包含多个元素的数组的真值不明确。请使用.any((或.all((">

我以前定义过允许我输入数组的函数(无论数组中有多少点,它都会输出该函数(,所以我不确定为什么现在会出现这个消息。我认为这与我在定义函数时使用SciPy的"quad"集成有关。我该如何绕过它以便输入数组?

import numpy as np
from scipy import integrate
from scipy.integrate import quad
import matplotlib.pyplot as plt

#This is the array of values of the variable I ultimately want to have the function plotted 
against
timearray = np.arange(0,10000,1)
#This below defines the general function, that is with respect to two variables (p and t)
def thomtest(p,t):
d = 3.086e22
c = 2.998e10
return (3*((np.cos(p + (2*t/(d*p))))**2))/(8*(t+((p**2)*d/(2*c))))

#The function below evaluates just the inner-integral
def phib(t):
d = 3.086e22
c = 2.998e10
return quad(thomtest,0.00001*(c*t)/d,np.pi, args=(t))[0]

#This evaluates the outer-integral, giving me the complete numerical answer
doubleintegral = quad(phib,0,((3.086e22)/(2.998e10)))

#This below is what gives me the error message: "ValueError: The truth 
#value of an array with more than one element is ambiguous.
#Use a.any() or a.all()". Apparently I cannot input an array
print(phib(timearray))

附件是完整错误信息的图片

In [2]: doubleintegral                                                                                               
Out[2]: (0.9892936902920587, 1.3643899787751934e-08)
In [3]: phib(0)                                                                                                      
Out[3]: 6.377997354641736e-10
In [4]: phib(np.arange(0,5))                                                                                         
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-89b7b858c2f5> in <module>
----> 1 phib(np.arange(0,5))
<ipython-input-1-4c64e69e4f62> in phib(t)
10     d = 3.086e22
11     c = 2.998e10
---> 12     return quad(thomtest,0.00001*(c*t)/d,np.pi, args=(t))[0]
13 
14 doubleintegral = quad(phib,0,((3.086e22)/(2.998e10)))
/usr/local/lib/python3.6/dist-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
336 
337     # check the limits of integration: int_a^b, expect a < b
--> 338     flip, a, b = b < a, min(a, b), max(a, b)
339 
340     if weight is None:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [5]:       

当它测试积分边界,确保第一个小于第二个时,就会出现错误。

但是对于数组t,该测试是多值的,不能使用:

In [6]: d = 3.086e22  
...: c = 2.998e10                                                                                                 
In [7]: 0.00001*(c*np.arange(5))/d                                                                                   
Out[7]: 
array([0.00000000e+00, 9.71484122e-18, 1.94296824e-17, 2.91445237e-17,
3.88593649e-17])

对于quad,您必须为t的每个元素运行一次代码phibdoubleintegral。在numpy中,我们试图避免迭代,但quad仅适用于标量边界。所以需要好的旧迭代。

相关内容

  • 没有找到相关文章

最新更新