# Unexpected Plot3D behaviour

The following illustrates what appears to be an incorrect Plot3D for this polynomial. I have forced the function to complex argument, changed ranges. In contrast, ListPLot3D behaves as expected.

Is there something I am missing?

pol1 = x + 1 - x^2 - 2 + 3 x + 4 x^2;
pol2 = -2 Sqrt[x (1 - x^2)];
h[u_] := pol1^2 - pol2^2 /. x -> u


2D plots:

GraphicsColumn[{Plot[h[x], {x, -3, 1},
Plot[Abs[h[x]], {x, -3, 1},
PlotLabel -> TraditionalForm[Abs@h[x]]]}, Frame -> All] Plot3D:

Plot3D[Abs@h[x + I y], {x, -3, 1}, {y, -1, 1}, Mesh -> None,
PlotPoints -> 100, PlotRange -> {0, 20},
PlotLabel -> Row[{"Plot3D of ", TraditionalForm[Abs@h[z]]}]] This appears to just be a translation of the real valued function along the y-axis and not the (or perhaps my) intended outcome...this can be easily confirmed by looking at adding even the smallest imaginary component to any of the 4 real roots.

In contrast ListPlot3D:

sol = Solve[h[x] == 0, x];
pts = {#, 0, 0} & /@ (x /. sol);
Show[ListPlot3D[
Flatten[Table[{i, j, Abs@h[i + j I]}, {i, -3, 1, 0.1}, {j, -1, 1,
0.1}], 1], MeshFunctions -> (#2 &), Mesh -> {{0}},
MeshStyle -> {Red, Thick}, PlotRange -> {-1, 20}],
Graphics3D[{PointSize[0.04], Point[pts]}],
PlotLabel -> Row[{"ListPlot3D of ", TraditionalForm[Abs@h[z]]}]] This nicely shows the four real roots and behaves as expected.

• I don't have to parse it now but it seems you have some scoping problem there. Try ClearAll[h]; Function[h[x_] := #^2 - #2^2][pol1, pol2] to define h. Then it works for me like you want. – Kuba Jul 1 '14 at 10:15
• @Kuba thank you very much for such a prompt and clear response...a Homer Simpson...doh... – ubpdqn Jul 1 '14 at 10:21
• You can also do h[u_] = pol1^2 - pol2^2 /. x -> u in your definition of h, i.e. Set, rather than SetDelayed. – Mark McClure Jul 1 '14 at 10:27
• @kguler thanks Evaluate works either as option or function works...thanks – ubpdqn Jul 1 '14 at 10:27
• @MarkMcClure thank you learned a lot in the past few minutes... – ubpdqn Jul 1 '14 at 10:31

Adding the option Evaluated->True

Plot3D[Abs@h[x + I y], {x, -3, 1}, {y, -1, 1}, Mesh -> None,
PlotPoints -> 100, PlotRange -> {0, 20},
PlotLabel -> Row[{"Plot3D of ", TraditionalForm[Abs@h[z]]}]
Evaluated -> True]


gives You are missing something rather basic -- the difference between Set and SetDelayed. If you were to change your definition of h to use Set, all would be well.

pol1 = x + 1 - x^2 - 2 + 3 x + 4 x^2;
pol2 = -2 Sqrt[x (1 - x^2)];
h[u_] = (pol1^2 - pol2^2 /. x -> u);
Plot3D[Abs@h[x + I y], {x, -3, 1}, {y, -1, 1},
Mesh -> None, PlotPoints -> 100, PlotRange -> {0, 20},
PlotLabel -> Row[{"Plot3D of ", TraditionalForm[Abs@h[z]]}]] • yes as both kgulers Evaluate (forcing evaluation for a delayed setting) and MarkMcClure's comment have identified the pitfall of my penchant for SetDelay over Set...embarrassing but instructive lesson as they often are...have +1 your answer – ubpdqn Jul 1 '14 at 12:12