0

I have the following triple integral:

the integral

the data are: t 1D array(size 124) , pr 1D array(size 10), lat 1D array(241) and lon 1D array(size 480) V 4D array (124, 10, 241, 480)

I want to integrate v over t, pr and lon at each lat So, I used the following code:

def M(T, lam, P, V, phi):

    return integrate.tplquad(V*R*np.cos(phi), 0, T,
                         lambda T: 0, lambda T: lam,
                         lambda T,lam: 0, lambda T,lam: P)[0]

for i in range(lat.shape[0]):
    a = M(t, lon, pr, v[:, :, i, :], lat[i])

But I got error:

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

How tplquad works is still confusing for me. Any help with doing the integral with tplquad or other functions?

Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22
Mag
  • 11
  • 2
  • your immediate problem is that the first argument to `tplquad` should be a function, e.g. `lambda t, lam, p: …`. that said, computing integrals is kind of expensive and you're better off simplifying as much analytically as possible – Sam Mason Oct 22 '18 at 17:38
  • "you're better off simplifying as much analytically as possible" could you explain more? – Mag Oct 24 '18 at 17:30
  • integration by quadrature is computationally expensive (the function needs to be evaluated at a lot of points) and this scales particularly badly with the dimensionality of the problem. rearranging the maths so that you can do integration over less variables is strongly recommended. you should be moving as many terms out of the integration as possible – Sam Mason Oct 25 '18 at 09:20
  • your first problem is that the first parameter to `tplquad` should be a function, not a value. can you edit your question to set `t`, `pr` to something reasonable, e.g. `t = np.linspace(1, 2, 124)` – Sam Mason Oct 25 '18 at 09:24
  • I've been trying to answer this and I think you just want to be iterating over the array elements rather than performing numerical integration – Sam Mason Oct 25 '18 at 13:57
  • I used the trapezoidal method by defining the trapezoidal formula as a function and iterate over array elements and I think it works properly until now and gives good results but which is better for the runtime to do all iterations for three integrals in one function or to split it into 3 functions. Because I will run it for big amount of data. – Mag Oct 27 '18 at 11:24

0 Answers0