首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matlab网格生成器

Matlab网格生成器
EN

Stack Overflow用户
提问于 2014-02-21 21:39:53
回答 2查看 242关注 0票数 0

下面是我从我的数值分析教材中得到的Matlab代码。它被用来离散一个拉普拉斯,并建立一个基于所选区域的矩阵(在本例中是单位平方)。我想知道如何修改这段代码以更改域的维度。即4x5矩形。我并不经常使用Matlab,但我很想看到不同领域的结果。

代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-21 23:36:18

看起来您可以通过设置nx和ny来更改大小。

票数 2
EN

Stack Overflow用户

发布于 2014-02-21 23:34:30

我不知道如何修改上面的代码,但我认为您可以得到一个表示mxn单位方格点的网格,如下所示:

代码语言:javascript
复制
%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));

这将产生一个单位正方形,m+1点在x方向,n+1点在y方向.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21945718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档