-3

我想编写一个函数,它创建一个在网格内有一个球的 3D 网格。应该是3D的。我找到了这个例子,这正是我想要的,但我不知道如何将它添加到函数 m 文件中。

这是我的代码:

function kgrid = makeGrid(Nx, dx, Ny, dy); 

% create the computational grid
Nx = 64;            % number of grid points in the x direction
Ny = 64;            % number of grid points in the y direction
Nz = 64;            % number of grid points in the z direction
dx = 0.1e-3;        % grid point spacing in the x direction [m]
dy = 0.1e-3;        % grid point spacing in the y direction [m]
dz = 0.1e-3;        % grid point spacing in the z direction [m]

kgrid = makeGrid(Nx, dx, Ny, dy, Nz, dz);

end
4

1 回答 1

1

看了例子之后,那个网站上说,那makeGrid是一个功能。该函数是第 3 方开源 Matlab 工具箱的一部分。
如果你有那个工具箱(为了下载显然你需要在网站上登录),你应该有这个功能makeGrid

如果你不这样做,你可以试试 Matlab 的功能meshgrid

xgv = linspace(0,1,64); % this will give you 64 points between 0 and 1
ygv = linspace(0,1,64);
zgv = linspace(0,1,64);

或者

xgv = 0:1e-4:1; % this will give you a spacing of 1e-4 between the gridpoints
ygv = 0:1e-4:1;
zgv = 0:1e-4:1;

然后使用上述任何一种meshgrid

[X,Y,Z] = meshgrid(xgv,ygv,zgv);
于 2013-05-31T13:47:02.490 回答