1
$\begingroup$

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}, 
   PlotLabel -> TraditionalForm[h[x]]],
  Plot[Abs[h[x]], {x, -3, 1}, 
   PlotLabel -> TraditionalForm[Abs@h[x]]]}, Frame -> All]

enter image description here

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

enter image description here

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

enter image description here

This nicely shows the four real roots and behaves as expected.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Kuba Jul 1 '14 at 10:15
  • $\begingroup$ @Kuba thank you very much for such a prompt and clear response...a Homer Simpson...doh... $\endgroup$ – ubpdqn Jul 1 '14 at 10:21
  • $\begingroup$ You can also do h[u_] = pol1^2 - pol2^2 /. x -> u in your definition of h, i.e. Set, rather than SetDelayed. $\endgroup$ – Mark McClure Jul 1 '14 at 10:27
  • $\begingroup$ @kguler thanks Evaluate works either as option or function works...thanks $\endgroup$ – ubpdqn Jul 1 '14 at 10:27
  • $\begingroup$ @MarkMcClure thank you learned a lot in the past few minutes... $\endgroup$ – ubpdqn Jul 1 '14 at 10:31
2
$\begingroup$

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

enter image description here

$\endgroup$
1
$\begingroup$

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

plot

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – ubpdqn Jul 1 '14 at 12:12

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.