网格网格与X、Y坐标的混淆

2024-06-28 15:40:39 发布

您现在位置:Python中文网/ 问答频道 /正文

import numpy as np
def L2Norm(f, x, y, d=0.00001):
    dudx = (f(x+d,y) - f(x-d,y)) / (2*d) 
    dudy = (f(x,y+d) - f(x,y-d)) / (2*d)
    L2Norm = np.float128(np.sqrt(dudx**2 + dudy**2))
return L2Norm

def f(x,y):
    return np.float128((1.0 + np.sin(np.pi*x))*((3.0 + np.cos(2.011*y))**2)*np.e**((-x**2)-((y**2)/4)))

# 20 points in X direction
x = np.linspace(-1.0,1.0,20)
# 40 points in Y direction
y = np.linspace(-2.0,2.0,40)
[X,Y] = np.meshgrid(x,y)
L2_Norm = L2Norm(f, X, Y)
print L2_Norm[37,13]

我的功能在上面。基本上,我希望调用函数L2Norm,得到一个在X方向从0到19的数组,或者第一个索引在第二个索引从0到39。在

相反,似乎第一个索引现在对应于我期望y的位置。在

我可以打电话 L2_规范[39,19] 但不是 L2_规范[19,39] 为什么会这样?在


Tags: inimport规范normreturndefnppoints
1条回答
网友
1楼 · 发布于 2024-06-28 15:40:39

如果调用L2_Norm.shape,您将看到数组的维数是(40,20),因此错误状态是,L2_Norm[19,39]超出范围,因为39>;该维度中的最大索引为19。请看一下X和{}的形状,看看L2}Norm为什么会有这样的形状如果你查看meshgrid文档,原因很明显:

For vectors x, y with lengths Nx=len(x) and Ny=len(y), return X, Y where X and Y are (Ny, Nx) shaped arrays with the elements of x and y repeated to fill the matrix along the first dimension for x, the second for y.

相关问题 更多 >