I want to create a three dimensional grid (x,y,z) in mathematica and plot a function f(x,y,z) on it. I am not worried about the plotting part now; for that I will probably use ListContourPlot3D or something like that. But, I am having problem in creating the grid itself. In matlab, one can achieve this by Meshgrid command which takes either two or three variable. There are many nice ways to reproduce the two variable meshgrid equivalent in mathematica as discussed in this stackexchange question: Simulate MATLAB's meshgrid function
However, what should be the equivalent for 3D case? I am trying a combination of Riffle and Partition, but it's getting too long and confusing. Any help will be much appreciated. Thanks!!
Answer
There are better ways to make this list that don't involve emulating MATLAB, but what you want is straightforward:
meshgrid3D[xxx_List, yyy_List, zzz_List] :=
Table[#, {x, xxx}, {y, yyy}, {z, zzz}] & /@ {x, y, z}
{xxx, yyy, zzz} =
meshgrid3D[Range[-2, 2, .1], Range[-2, 2, .1], Range[-2, 2, .1]];
ListContourPlot3D[xxx^2 + yyy^2 - zzz^2, Contours -> {0},
Mesh -> None]
For example, it is better to use
grid3D = Table[{x, y, z}, {x, -2, 2, .1}, {y, -2, 2, .1}, {z, -2,
2, .1}];
func[x_, y_, z_] := x^2 + y^2 - z^2;
ListContourPlot3D[
Apply[func,
grid3D, {3}], Contours -> {0}, Mesh -> None]
Comments
Post a Comment