# What are the possible ways of visualizing a 4D function in Mathematica?

I have a function $F$ that maps the xyz space to a set of reals, more clearly:

$c = F[x,y,z]$

Where $c$,$x$,$y$ and $z$ are reals.

What are the possible ways of visualizing this 3d function in Mathematica? (if possible, please post a how-to-do-it)

-
I have something that could be interesting. I'll put it up tomorrow. – rcollyer Feb 14 '13 at 3:09
@h3now you could check en.wikipedia.org/wiki/Parallel_coordinates to get some other ideas? – Lou Feb 14 '13 at 8:29
Contour plot. You can plot the surfaces along which $F$ is constant. – becko Aug 27 '14 at 20:32

One possible way is to use Graphics3D with Point and color points by function value so it's like density plot 3d. For example,

xyz = Flatten[
Table[{i, j, k}, {i, 1, 10, .35}, {j, 1, 10, .35}, {k, 1,
10, .35}], 2];

f[x_, y_, z_] := x^2 y Cos[z]

Graphics3D[
Point[xyz, VertexColors -> (Hue /@ Rescale[f[##] & @@@ xyz])],
Axes -> True, AxesLabel -> {x, y, z}]


Another possible choice is just thinking one parameter as time variable and use Manipulate:

Manipulate[Plot3D[f[x, y, z], {x, 1, 10}, {y, 1, 10}], {z, 1, 10}]


There should be many other way to visualize 4d data, but it's really depending on what you want to see and how you want to visualize.

Like amr suggested, you can also use Image3D or Raster3D:

values = Rescale[
Table[f[i, j, k], {i, 1, 10, .2}, {j, 1, 10, .2}, {k, 1, 10, .2}]];

Graphics3D[Raster3D[values, ColorFunction -> Hue]]


Image3D[values, ColorFunction -> Hue]


Image3D[values]


-
I was thinking something like this but with the Image3D functionality from Version 9. Not sure what the difference might be but it may look "smoother." I only have Mathematica 8 at the moment though so I can't test it. – amr Feb 14 '13 at 2:59
@amr yes, that will be other way. I added examples. – halmir Feb 14 '13 at 3:27
Thank you, great answer. Just so you know I needed this to visualize the error in a 3d position-estimate in the xyz space, so I'll most likely try something like your first example. – JLagana Feb 18 '13 at 14:47

A typical way of visualizing functions of the form $f(x,y,z)$ is in terms of level sets. One uses ContourPlot3D in Mathematica. Here I show it in conjunction with the function's gradient field, which may be omitted.

Manipulate[
Show[
ContourPlot3D[f == c, {x, -2, 2}, {y, -2, 2}, {z, -2, 2},
ContourStyle -> Opacity[0.5]],
ControlActive[{}, VectorPlot3D[Evaluate[D[f, {{x, y, z}}]], {x, -2, 2}, {y, -2, 2}, {z, -2, 2}]]
],
{{f, x^2 + x y z + z^4}, InputField},
{{c, 0.1}, -0.25, 5, Appearance -> "Labeled"}]


You mention in a comment visualizing error. I wasn't sure exactly what you were after, but you can plot contours plus or minus a given error in the value of f fairly easily.

Manipulate[
Show[
ContourPlot3D[f, {x, -2, 2}, {y, -2, 2}, {z, -2, 2},
Contours -> c + {-dc, 0, dc},
ContourStyle -> {Directive[Opacity[0.3], Red], Opacity[0.3],
Directive[Opacity[0.3], Blue]}, Mesh -> None],
ControlActive[{}, VectorPlot3D[Evaluate[D[f, {{x, y, z}}]], {x, -2, 2}, {y, -2, 2}, {z, -2, 2}]]
],
{{f, x^2 + x y z + z^4}, InputField},
{{c, 0.6}, -0.25, 5, Appearance -> "Labeled"},
{{dc, 0.5}, 0, 1., Appearance -> "Labeled"}]


-