如何在网格网格上应用积分函数

How to apply an integration function over a meshgrid

提问人:Zack Eriksen 提问时间:11/2/2023 最后编辑:Mike 'Pomax' KamermansZack Eriksen 更新时间:11/2/2023 访问量:72

问:

为了简单起见,假设我有一个形式的函数:

Equation link

我想在 x-y 网格上评估这个函数:

x = np.linspace(0,1,50)
y = np.linspace(0,1,50)
xx, yy = np.meshgrid(x, y)

where 用作积分的上限 (x_2),但我不确定最佳方法。我想我正在寻找合适的集成功能来允许我做到这一点。我知道这个例子可以很容易地通过解析求解,然后在整个网格上执行,但我的真实例子更复杂,我想学习适当的数值方法。我真的很感激任何建议。提前致谢!xx

我尝试使用 scipy.integrate 中的四边形函数,但这只允许我计算一个确定的积分,而不是在整个网格上积分。

python numpy 数学

评论

0赞 Mike 'Pomax' Kamermans 11/2/2023
你的积分极限是什么意思?什么是 x₂?
0赞 Zack Eriksen 11/2/2023
@Mike 'Pomax“ Kamermans,很抱歉这不清楚,这将是我用来制作网格的数组中从 0-1 开始的所有值。x
0赞 hpaulj 11/2/2023
是的,的集成边界必须是标量。因此,您可以对每对标量边界进行迭代和应用一次。quadquad
0赞 Simon Goater 11/2/2023
我不知道在 python 中执行此操作的最佳方法,但一种方法是将所有 y 值的 x 积分相加,取终点值的一半(@y=0,y=1),然后乘以 dy。

答:

1赞 Zack Eriksen 11/2/2023 #1

我能够弄清楚!使用 I 可以启动一个接受网格作为输入的函数:@np.vectorize

# vectorize, to implement function over xy meshgrid
@np.vectorize

# define function
def z(x,y):
    
    # define function to be integrated
    def integrand(x):
         return x**3
        
    # integrate function
    ans,error = quad(integrand,0,x)

    # calculate full function, with integrated terms
    z = ans + y
    
    # return solution
    return(G)

# define meshgrid
x = np.linspace(0,1,50)
y = np.linspace(0,1,50)

XX, YY = np.meshgrid(T,P)

# implement function across meshgrid
z_out = z(XX,YY)

谢谢!