2013-05-30 51 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);