提问人:Zack Eriksen 提问时间:11/2/2023 最后编辑:Mike 'Pomax' KamermansZack Eriksen 更新时间:11/2/2023 访问量:72
如何在网格网格上应用积分函数
How to apply an integration function over a meshgrid
问:
为了简单起见,假设我有一个形式的函数:
我想在 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 中的四边形函数,但这只允许我计算一个确定的积分,而不是在整个网格上积分。
答:
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)
谢谢!
评论
x
quad
quad