0

我正在使用双积分,并且内部积分具有可变界限。我编写了一个函数,使用 SciPy 的四元积分,它允许我评估这个积分。但是,我想要的只是评估内部积分,这样我剩下的就是一个关于某个变量的单个、未评估的积分。然后我想绘制这个“中途”评估的双积分与该变量的范围,以便我可以看到一些趋势。但是,当我输入该变量的数组时(它只是 0-10000,但增量为 1,它会提供以下错误消息:

“ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.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))

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

4

1 回答 1

1
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]:       

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

但是对于 array 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你必须运行你的代码,phib并且doubleintegral为. t我们numpy尽量避免迭代,但quad只适用于标量边界。所以需要良好的旧迭代。

于 2020-03-14T00:01:27.367 回答