# How to sample numerically a function using a mesh-grid function in 3D?

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.

• Try to search stackexchange for meshgrid 3D... Aug 6, 2019 at 8:10
• @Ulrich Neumann. This problem has not been explained before, I have searched where you recommend me. indeed I also put the links related to my questions (look them up). Aug 6, 2019 at 9:04
• @irondonio, 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 function c at grid points in 3D. Then you may visualize this with ListDensityPlot3D.
– Alx
Aug 6, 2019 at 9:46
• "This problem has not been explained before" perhaps because this problem has not been explained before -- that is, no one asking the question has taken the trouble to describe what the problem is, what meshgrid does. Aug 6, 2019 at 10:52

This might work better:

{xx, yy, zz} = Transpose[Tuples[{Range[-2, 2, .1], Range[-4, 4, .2], Range[-6, 6, .2]}]];
c = xx*Exp[-xx^2 - yy^2] + 2 zz;
pts = Transpose[{xx, yy, zz, c}];


In the end you try to plot a 4D-list with ListPlot3D. That is not possible. Instead, you may use

ListDensityPlot3D[pts,
AxesLabel -> {"x", "y", "z"},
ColorFunction -> "Rainbow"
]


or

ListSliceDensityPlot3D[pts,
AxesLabel -> {"x", "y", "z"},
ColorFunction -> "Rainbow"
]


However, if you want to plot only the function , then you don't have to mess around with meshes anyways. You can simply use

SliceDensityPlot3D[
x*Exp[-x^2 - y^2] + 2 z,
{x, -2, 2},
{y, -4, 4},
{z, -6, 6},
AxesLabel -> {"x", "y", "z"},
ColorFunction -> "Rainbow"
]


• Thanks, @Henrik Schumacher, I can't understand your proposal is very advanced for my knowledge in Mathematica Please, could you be so kind as to take a minute and show the complete solution. You're right I can't graph using ListPlot3D. Thanks Aug 6, 2019 at 15:53

Looking for "meshgrid 3d" in stackexchange gives a bunch of answers

One of them "Creating a three dimensional grid" offers

meshgrid3D[xxx_List, yyy_List, zzz_List] := Table[#, {x, xxx}, {y, yyy}, {z, zzz}] & /@ {x, y, z}


as Mathematica version of matlab meshgrid

• Thanks@Ulrich Neumann, I implemented your code but it doesn't work meshgrid3D[xxx_List, yyy_List, zzz_List] := Table[c, {x, xxx}, {y, yyy}, {z, zzz}] & /@ {x, y, z} {xxx, yyy, zzz} = {Range[-2, 2, .1], Range[-4, 4, .2], Range[-6, 6, .2]}; c = xx*Exp[-xx^2 - yy^2] + zz; What it still wrong ? Thanks Aug 6, 2019 at 15:43
• There is nothing wrong. What you call c evaluates to a matrix of same size as xx,yy,zz`. The question remains why you want to use meshgrid formalism from matlab, which is matrix orientated. List -concept of Mathematica is much more easy to apply I think! Aug 7, 2019 at 7:08
• ...from this answer by @JasonB. (["And always give proper credit to the author and site [sic] where you found the text, including a direct link to it."](mathematica.stackexchange.com/help/referencing)) Aug 7, 2019 at 16:43