2013-05-30 49 views
-3

我想写一个函数,它在网格中创建一个带球的3D网格。它应该是3D。 我发现this example,这正是我想要的,但我不知道如何将它添加到函数m文件中。Matlab:如何创建3D网格?

这是我的代码:

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 

回答

1

看着the example后,它说,该网站上,那makeGrid是一个函数。该功能是3rd party, open source, Matlab toolbox的一部分。
如果您有该工具箱(显然,您需要在网站上登录才能下载),则应具有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); 

OR

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);