Calculating numerical integrals from numpy arrays

Asked

Viewed 217 times

2

I have the triple integral of the figure. I used the documentation of the scipy to solve her.

inserir a descrição da imagem aqui

Now I want to exchange the values of the upper limits of x, y and z, which in this problem are the values 2, 3 and 2 for arrays.

Type where 2(x) I want a numpy array [2,3,4,5], where the value is 3 (y) I want the np.array [5,6,7,8] and where 2(z) I want the np.array [0,1,2,3].

How do I make this replacement?

Below follows my code:

from scipy import integrate
f = lambda z, y, x: (12*x)*(y**2)*(z**3)
a = integrate.tplquad(f, -1, 2, lambda x: 0, lambda x: 3,
                  lambda x, y: 0, lambda x, y: 2)


b = np.array([2,3,4,5])
c = np.array([5,6,7,8])
d = np.array([0,1,2,3])

g = integrate.tplquad(f, -1, b, lambda x: 0, lambda x: c,
                  lambda x, y: 0, lambda x, y: d)
  • nquad serves? https://docs.scipy.org/doc/scipy-1.2.1/reference/generated/scipy.integrate.nquad.html#scipy.integrate.nquad

1 answer

0


You can simply encapsulate your code in a separate function that receives the list of limits per parameter:

def calcular_integrais_triplas(f, limites):
    resultados = []

    for limite in limites:
        (r, _) = integrate.tplquad(f, *limite)
        resultados.append(r)

    return resultados

From there below you create the function you want to integrate and the list of limits:

f = lambda x, y, z: 12*x*(y*y)*(z*z*z)

limites = [
    (0, 2, 0, 3, -1, 2),
    (0, 3, 0, 3, -1, 2),
    (0, 4, 0, 3, -1, 2),
    (0, 5, 0, 3, -1, 2),
]

Note that each line in the list of limits is a tuple that contains the limits exactly in the order that the function integrate.tplquad accurate.

Once done, just call your new function and you will get back a list with the respective results:

resultados = calcular_integrais_triplas(f, limites)

print(*resultados, sep='\n')

The program prints the following in the terminal:

648.0000000000001
3280.5
10368.000000000002
25312.5

That are precisely the results of its integral using the limits specified in the list of limits.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.