Using scipy.integrate.quad to perform 3D integral

后端 未结 2 924
眼角桃花
眼角桃花 2021-01-24 08:21

Motivation for the question

I\'m trying to integrate a function f(x,y,z) over all space.

I have tried using scipy.integrate.tplquad & scipy.integrate.nquad

相关标签:
2条回答
  • 2021-01-24 08:30

    Here is an example with nested call to quad performing the integration giving 1/8th of the sphere volume:

    import numpy as np
    from scipy.integrate import quad
    
    def fz(x, y):
        return quad( lambda z:1, 0, np.sqrt(x**2+y**2) )[0]
    
    def fy(x):
        return quad( fz, 0, np.sqrt(1-x**2), args=(x, ) )[0]
    
    def fx():
        return quad( fy, 0, 1 )[0]
    
    fx()
    >>> 0.5235987755981053
    
    4/3*np.pi/8
    >>> 0.5235987755982988
    
    0 讨论(0)
  • 2021-01-24 08:46

    I'm trying to integrate a function f(x,y,z) over all space.

    First of all you'll have to ask yourself why the integral should converge at all. Does it have a factor exp(-r) or exp(-r^2)? In both of these cases, quadpy (a project of mine has something for you), e.g.,

    import quadpy
    
    scheme = quadpy.e3r2.stroud_secrest_10a()
    val = scheme.integrate(lambda x: x[0]**2)
    print(val)
    
    2.784163998415853
    
    0 讨论(0)
提交回复
热议问题