1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Try to search stackexchange for meshgrid 3D... $\endgroup$ – Ulrich Neumann Aug 6 at 8:10
  • $\begingroup$ @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). $\endgroup$ – irondonio Aug 6 at 9:04
  • 1
    $\begingroup$ @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. $\endgroup$ – Alx Aug 6 at 9:46
  • 1
    $\begingroup$ "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. $\endgroup$ – Michael E2 Aug 6 at 10:52
2
$\begingroup$

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"
 ]

enter image description here

or

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

enter image description here

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"
 ]

enter image description here

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – irondonio Aug 6 at 15:53
1
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – irondonio Aug 6 at 15:43
  • $\begingroup$ 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! $\endgroup$ – Ulrich Neumann Aug 7 at 7:08
  • $\begingroup$ ...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)) $\endgroup$ – Michael E2 Aug 7 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.