以下是我从数字分析教科书中获得的Matlab代码。它用于离散拉普拉斯算子并基于所选域(在这种情况下为单位平方)设置矩阵。我想知道如何修改此代码以更改域的维度。即一个4 x 5的矩形。我并没有真正使用Matlab,但我很想看到不同域的结果。
N = nx * ny;
N5 = 5 * N;
irow = zeros(N5, 1);
icol = irow;
NZA = irow;
index = 0;
row = 0;
for j = 1:ny
for k = 1:nx
row = row + 1;
if j > 1
index = index + 1;
NZA (index) = -1.0;
irow(index) = row;
icol(index) = row - nx; % South
end
if k > 1
index = index + 1;
NZA (index) = -1.0;
irow(index) = row;
icol(index) = row - 1; % West
end
index = index + 1;
NZA (index) = 4.0;
irow(index) = row;
icol(index) = row; % P (self)
if k < nx
index = index + 1;
NZA (index) = -1.0;
irow(index) = row;
icol(index) = row + 1; % East
end
if j < ny
index = index + 1;
NZA (index) = -1.0;
irow(index) = row;
icol(index) = row + nx; % North
end
end
end
icol = icol(1:index);
irow = irow(1:index);
NZA = NZA(1:index);
A = sparse (irow, icol, NZA, N, N);
答案 0 :(得分:2)
看起来你可以通过设置nx和ny来改变大小。
答案 1 :(得分:0)
我不知道修改上面的代码,但我认为你可以得到一个代表unit square的mxn
点网格,如下所示:
%user input
m=4; % points in x-direction
n=5; %points in y-direction
%code
[x,y]=meshgrid(0:1/m:1,0:1/n:1);
%visualization
allPointsOnGrid=[x(:) y(:)];
scatter(allPointsOnGrid(:,1),allPointsOnGrid(:,2));
这将生成一个单位正方形,在x方向上有m+1
个点,在y方向上有n+1
个点。