用python的库 scipy 求二重(多重)积分

scipy求一重积分:点击跳转.

sympy求积分:点击跳转.

问题1:求解如下二重积分

I = y = 0 1 / 2 x = 0 1 2 y x y   d x d y . I = \int_{y=0}^{1/2}\int_{x=0}^{1-2y} xy~dxdy .
程序1,如下:

from scipy.integrate import dblquad
area = dblquad(lambda x, y: x*y, 0, 0.5, 
               lambda x: 0, lambda x: 1-2*x)
print(area)
结果:(0.010416666666666668, 4.101620128472366e-16)

程序2,如下:

from scipy import integrate
def f(x, y):
    return x*y

def bounds_y():
    return [0, 0.5]

def bounds_x(y):
    return [0, 1-2*y]

print(integrate.nquad(f, [bounds_x, bounds_y]))

结果:0.010416666666666668, 4.101620128472366e-16)

问题2:求解如下三重积分

I = 1 2 2 3 0 1 x y z   d x d y d z . I = \int_{1}^{2}\int_{2}^{3}\int_{0}^{1} xyz~dxdydz .

程序1,如下

from scipy import integrate
f = lambda z, y, x: x*y*z
print(integrate.tplquad(f, 1, 2, lambda x: 2, lambda x: 3,
                  lambda x, y: 0, lambda x, y: 1))
结果:(1.8750000000000002, 3.324644794257407e-14)

程序2,如下

import scipy.integrate as integrate
def f(x,y,z):
    return x*y*z
def bounds_z():
    return [1, 2]
def bounds_y(*args):
    return [2, 3]
def bounds_x(*args):
    return [0, 1]
result = integrate.nquad(f, [bounds_x, bounds_y, bounds_z])
print(result)

结果:(1.8750000000000002, 3.324644794257407e-14)

问题3:求解如下四重积分

I = 0 1 / 2 0 3 0 3 0 3 x y z t   d x d y d z d t . I = \int_{0}^{1/2}\int_{0}^{3}\int_{0}^{3}\int_{0}^{3} xyzt~dxdydzdt .
程序,如下

import scipy.integrate as integrate
def f(x,y,z,t):
    return x*y*z*t
def bounds_z(*args):
    return [0, 1/2]
def bounds_y(*args):
    return [0, 3]
def bounds_x(*args):
    return [0, 3]
def bounds_t(*args)
    return [0,3]
result = integrate.nquad(f, [bounds_x, bounds_y, bounds_z, bounds_t])
print(result)
结果:(0.005208333333333334, 4.092713794698356e-16)
发布了23 篇原创文章 · 获赞 32 · 访问量 4476

猜你喜欢

转载自blog.csdn.net/t4ngw/article/details/105739379
今日推荐