# Why does Plot3D omit parts of the surface at kinks?

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):

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?

-
Increasing the number of PlotPoints seems to reduce the gap. –  b.gatessucks Apr 27 '12 at 10:30
@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. –  celtschk Apr 27 '12 at 10:37

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]


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]


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]


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]


-
Thank you, that gives exactly what I want. –  celtschk Apr 27 '12 at 10:43
Correction: Almost exactly: The grid lines are not drawn in the filled gap, which gets very visible for large gaps. –  celtschk Apr 27 '12 at 10:46
@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.) –  Szabolcs Apr 27 '12 at 10:47
Yeah, I meant the mesh. Exclusions->None plus sufficiently high MaxRecursion indeed did the trick. Thank you again. –  celtschk Apr 27 '12 at 11:02
@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. –  Szabolcs Apr 27 '12 at 11:04