# Visualize function with 4 variables

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

F = 2 - x - 0.5 y - 2 z - 1.5 t;


with all varialbes x, y, z, t in [0,1] range.

I would like to visualize F to see how it varies, maximum, minimum values, etc. Is there any way to do that?

• Clearly we run out of dimensions to plot so you need to decide what do you want to see, which is not really a Mathematica question. You can take a look about related, simpler cases of R^3->R functions., lookup related functions etc. When you are stuck with implementation of anything specific, let us know.
– Kuba
May 29, 2018 at 6:35
• You could, for each instance of time, visualize 4D like in what-are-the-possible-ways-of-visualizing-a-4d-function-in-mathematica Currently you have 5D, which is not possible. So you need to cut it down to 4D by showing each instance of time. May 29, 2018 at 6:35
• Well, something along the lines of Manipulate[ DensityPlot3D[ 2 - x - 0.5 y - 2 z - 1.5 t, {x, 0, 1}, {y, 0, 1}, {z, 0, 1}], {t, 0, 1} ] could be done, but its far from being beautiful. May 29, 2018 at 6:42
• @HenrikSchumacher: I've just tried it but it is hard to understand and get some meaningful infor. From the plot, how to know where is the origin, which axis y, y, z, t is? May 29, 2018 at 6:50
• You can add labels for the axes (and the exes themselves if you cannot see them, yet) with Manipulate[ DensityPlot3D[ 2 - x - 0.5 y - 2 z - 1.5 t, {x, 0, 1}, {y, 0, 1}, {z, 0, 1}, AxesLabel -> {"x", "y", "z"}, Boxed -> True, Axes -> True], {t, 0, 1}]. Click on the small plus-sign next to the slider control in the top in order to visualize the current value of t. As is, the coloring is not consistent for different values of t. This might get fixed with ColorFunctionScaling -> False (and by specifying a suitable ColorFunction) May 29, 2018 at 8:03

A proposal.

F = 2 - x - 1/2 y - 2 z - 3/2 t;

Minimize[{F, 0 <= x <= 1, 0 <= y <= 1, 0 <= z <= 1, 0 <= t <= 1}, {x, y, z, t}]

(*   {-3, {x -> 1, y -> 1, z -> 1, t -> 1}}   *)

Maximize[{F, 0 <= x <= 1, 0 <= y <= 1, 0 <= z <= 1, 0 <= t <= 1}, {x,y, z, t}]

(*   {2, {x -> 0, y -> 0, z -> 0, t -> 0}}   *)

tab = Table[
ContourPlot3D[F, {x, 0, 1}, {y, 0, 1}, {z, 0, 1},
Contours -> {-(29/10), -(5/2), -(21/10), -(17/10), -(13/10), -(9/
10), -(1/2), -(1/10), 3/10, 7/10, 11/10, 3/2, 19/10},
ContourStyle -> Table[Hue[.8 n/12], {n, 0, 12}],
AxesLabel -> {x, y, z},
PlotLabel -> {{"t = ", t}, {"Max=",
2 - (3 t)/2}, {"Min=", -(3/2) (1 + t)}}], {t, 0, 1, 1/20}];

ListAnimate[tab, 1, AnimationRunning -> False]


And here, how Maximum and Minimum varies with t.

Simplify[Maximize[{F, 0 <= x <= 1, 0 <= y <= 1, 0 <= z <= 1,
0 <= t <= 1}, {x, y, z}], {0 <= x <= 1, 0 <= y <= 1, 0 <= z <= 1,
0 <= t <= 1}]

(*   {2 - (3 t)/2, {x -> 0, y -> 0, z -> 0}}   *)

Simplify[Minimize[{F, 0 <= x <= 1, 0 <= y <= 1, 0 <= z <= 1,
0 <= t <= 1}, {x, y, z}], {0 <= x <= 1, 0 <= y <= 1, 0 <= z <= 1,
0 <= t <= 1}]

(*   {-(3/2) (1 + t), {x -> 1, y -> 1, z -> 1}}   *)


One approach is to assume you need to see the plot for only two coordinates, and just have two plots side by side with a Locator choosing which coordinate pair to use to "cut" the 4D space.

SetOptions[DensityPlot,
ImageSize -> 300
, PlotLegends -> Automatic
, PlotRange -> {-2, 2}
];

DynamicModule[
{
xx = 0.5,
yy = 0.5,
zz = 0.5,
tt = 0.5
},
Dynamic@Row[{
With[{
z = zz,
t = tt
},
Show[
DensityPlot[
2 - x - 0.5 y - 2 z - 1.5 t
, {x, 0, 1}
, {y, 0, 1}
, FrameLabel -> {"x", "y"}
, PlotLabel -> Row@{2 - x - 0.5 y - Sin[z] - 1.5 t}
],
Graphics@Locator[Dynamic[{xx, yy}]]
]
],
With[{
x = xx,
y = yy
},
Show[
DensityPlot[
2 - x - 0.5 y - 2 z - 1.5 t
, {z, 0, 1}
, {t, 0, 1}
, FrameLabel -> {"z", "t"}
, PlotLabel -> Row@{2 - x - 0.5 y - Sin[z] - 1.5 t}
],
Graphics@Locator[Dynamic[{zz, tt}]]
]]
}]
]


You may use DensityPlot3D and Manipulate.

DynamicModule is used to hold the min and max values of f for a given t. This is used in the OpacityFunction to enable exploration of the "density" for a given t.

With

f[x_, y_, z_, t_] := 2 - x - 0.5 y - 2 z - 1.5 t


Then

DynamicModule[{min, max},
Manipulate[
{min, max} =
Through[{Minimize, Maximize}[
{f[x, y, z, t], {{x}, {y}, {z}} \[Element] Interval[{0, 1}]},
{x, y, z}, Reals]
][[All, 1]];
DensityPlot3D[f[x, y, z, t], {x, 0, 1}, {y, 0, 1}, {z, 0, 1},
PlotLegends -> Automatic,
PlotPoints -> ControlActive[4, 40],
PerformanceGoal -> ControlActive["Speed", "Quality"],
OpacityFunction -> Interval[{min, First@o}, {Last@o, max}],
OpacityFunctionScaling -> False],
{{t, .5}, 0, 1, Appearance -> "Labeled"},
{{o, {-1, 1}, "Opacity"}, Dynamic@min, Dynamic@max, IntervalSlider,
Method -> "Push", MinIntervalSize -> .1}
]
]


Hope this helps.