简体   繁体   English

Python:如何在网格上求值函数

[英]Python: how to evaluate a function on a grid

I'm new to programming and scientific computing. 我是编程和科学计算的新手。 Below is some code for evaluating an exponential integral over a grid. 下面是一些用于评估网格上指数积分的代码。 The integral is a function of radial distance from a point. 积分是到点的径向距离的函数。 I would like to sum the contribution from multiple points (with defined x, y coordinates) over the grid. 我想总结网格上多个点(定义了x,y坐标)的贡献。 I realize analytically this is simple superposition, but I'm confused over how to construct the loop summing the contribution from the points and the most efficient approach. 从分析上我意识到这是简单的叠加,但是我对如何构造循环总结点和最有效方法的贡献感到困惑。 If anyone has any suggestions or references, it will be much appreciated. 如果有人有任何建议或参考,将不胜感激。 The code setting up the grid and evaluating the function is below: 设置网格和评估功能的代码如下:

S=.0004
xi0 = 1.0
dx = 10.0
side = 100.0
points = 500
spacing = side/points
x1 = side/2 + dx/2
y1 = side/2
x2 = side/2 - dx/2
y2 = side/2
xi = empty([points,points],float)
for i in range(points):
y = spacing*i
for j in range(points):
    x = spacing*j
    r1 = sqrt((x-x1)**2+(y-y1)**2)
    r2 = sqrt((x-x2)**2+(y-y2)**2)
    u = (r1*r1*S)
    xi[i,j] = expn(1,u)

Maybe something like this can be appropriate 也许这样的事情可能是适当的

x = np.linspace(0, side, points)
y = np.linspace(0, side, points)
r1 = np.sqrt((x-x1)**2 + (y-y1)**2)
r2 = np.sqrt((x-x2)**2 + (y-y2)**2)
u = (r1 * r2 * s)
xi = np.exp(u)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM