17
$\begingroup$

When doing

Plot3D[Max[p^2-q^2,0],{p,0,1},{q,-1,1},PlotStyle->White]

the result looks like this (with Mathematica 8.0.0.0):

enter image description here

As you can easily see, at the "kink" where the function starts going above 0, Mathematica leaves a gap in the surface.

Why does it do that, and more importantly, what can I do about it?

$\endgroup$
2
  • $\begingroup$ Increasing the number of PlotPoints seems to reduce the gap. $\endgroup$ Apr 27, 2012 at 10:30
  • $\begingroup$ @b.gatessucks: True, but it also increases the memory needed, and doesn't really close the gap. And I've got other cases (with more complicated functions) where the gap gets much larger than in this example. Increasing the number of points isn't really practical in those cases. $\endgroup$
    – celtschk
    Apr 27, 2012 at 10:37

1 Answer 1

17
$\begingroup$

The reason for this is automatic exclusion detection:

Here the discontinuity in the derivative is ugly:

Plot3D[Max[p^2 - q^2, 0], {p, 0, 1}, {q, -1, 1}, 
 Exclusions -> None]

Mathematica graphics

We can get Mathematica to auto-detect it, and compute the contours precisely, making the plot smooth around the discontinuity:

Plot3D[Max[p^2 - q^2, 0], {p, 0, 1}, {q, -1, 1}, 
 Exclusions -> Automatic]

Mathematica graphics

The side effect is a gap, as you noticed. Often one would prefer this gap to be filled with the same style as the plot. It can be accomplished by setting ExclusionsStyle, like this:

Plot3D[Max[p^2 - q^2, 0], {p, 0, 1}, {q, -1, 1}, 
 Exclusions -> Automatic, ExclusionsStyle -> Automatic]

Mathematica graphics

EDIT: As @celtschk notes, mesh lines or contour lines are not drawn inside the excluded region. This is very visible for large exclusions and can be prevented by forbidding exclusions with Exclusions -> None. Then the plot can be made smoother by increasing MaxRecursion and PlotPoints. Here's an example:

Plot3D[UnitStep[y], {x, -1, 1}, {y, -1, 1}, Exclusions -> None, MaxRecursion -> 5]

Mathematica graphics

$\endgroup$
5
  • $\begingroup$ Thank you, that gives exactly what I want. $\endgroup$
    – celtschk
    Apr 27, 2012 at 10:43
  • $\begingroup$ Correction: Almost exactly: The grid lines are not drawn in the filled gap, which gets very visible for large gaps. $\endgroup$
    – celtschk
    Apr 27, 2012 at 10:46
  • $\begingroup$ @celtschk You mean the mesh, right? (Mesh). That's true, the workaround would be Exclusions -> None and increasing MaxRecursion and PlotPloints manually until you get the right result. (I.e. the same thing you did.) $\endgroup$
    – Szabolcs
    Apr 27, 2012 at 10:47
  • $\begingroup$ Yeah, I meant the mesh. Exclusions->None plus sufficiently high MaxRecursion indeed did the trick. Thank you again. $\endgroup$
    – celtschk
    Apr 27, 2012 at 11:02
  • $\begingroup$ @celtschk The refinement in Mma's adaptive plotting is based on the curvature, so it'll converge fast around a discontinuity of the derivative (where the curvature is infinite). MaxRecursion just controls the number of allowed recursive refinement steps. $\endgroup$
    – Szabolcs
    Apr 27, 2012 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.