2014-03-06 34 views
0

我是編程和科學計算的新手。下面是一些評估網格指數積分的代碼。積分是一個點的徑向距離的函數。我想將網格中多個點(具有定義的x,y座標)的貢獻相加。我從分析的角度認識到這是簡單的疊加,但我對如何構建循環來總結點的貢獻和最有效的方法感到困惑。如果任何人有任何建議或參考,將不勝感激。代碼中設置了電網和評估功能如下:Python:如何評估網格上的函數

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) 

回答

1

也許這樣的事情,可以適當

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)