I am working on a problem where I want to sample a scalar function f[x,y,z].
The problem is how to obtain a mesh-grid in 3D (A grid of points in x,y, and z). In order to study this function.
Of course, I´ve read previous questions in 2D Simulate MATLAB's meshgrid function. And also an incomplete answered was reported here Mathematica implementation of meshgrid in 3D.
Please, How can I solve and fix this code?
Clear["Global`*"];
meshgrid[x_List, y_List, z_list] := {ConstantArray[x, Length[x]],
Transpose@ConstantArray[y, Length[y]],
Transpose@ConstantArray[z, Length[z]]}
{xx, yy, zz} =
meshgrid[Range[-2, 2, .1], Range[-4, 4, .2], Range[-6, 6, .2] ]
c = xx*Exp[-xx^2 - yy^2] + 2 zz;
pts = Flatten[{xx, yy, zz, c}];
ListPlot3D[pts, PlotRange -> All, AxesLabel -> Automatic,
ImagePadding -> 20, Mesh -> 35, InterpolationOrder -> 2,
ColorFunction -> "Rainbow", Boxed -> False]
The code output is
Set::shape: Lists {xx,yy,zz} and meshgrid[{-2.,-1.9,-1.8,-1.7,-1.6,-1.5,-1.4,-1.3,-1.2,-1.1,-1.,-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0.,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.},{-4.,-3.8,<<38>>,4.},{-6.,-5.8,-5.6,<<45>>,3.6,3.8,<<11>>}] are not the same shape.
ListPlot3D: {xx,yy,zz,2.71828^(-1. xx^2-1. yy^2) xx+2. zz} must be a valid array or a list of valid arrays.
Flatten[Table[{xx,yy,zz,c},{xx,-2,2,0.1},{yy,-4,4,0.2},{zz,-6,6,0.2}],2]
gives you tabulated values of functionc
at grid points in 3D. Then you may visualize this withListDensityPlot3D
. $\endgroup$meshgrid
does. $\endgroup$