I discovered an idiosyncrasy in how Plot3D works with an irregular region, in Mathematica 10: Plot3D first numerically evaluates the function at various points along the boundaries of the plot. If no point returns a number, an empty plot is returned even if the function may return values in its interior.

Consider the following toy example:

f[x_?NumericQ, y_?NumericQ] := 
  If[x == 0. || y == 0. || 1 - x - y == 0.,
    Indeterminate, 1]

g[x_?NumericQ, y_?NumericQ] := 1;

And now try plotting

Plot3D[f[x, y], {x, 0, 1}, {y, 0, 1 - x}]
(*Empty plot*)

Plot3D[g[x, y], {x, 0, 1}, {y, 0, 1 - x}]
(*Not empty plot*)

In the example above, f is like my real-case extremely complicated numerically-defined function; it chokes right on the boundary, and returns Indeterminate there. But I'd like to generate a plot for it.

A makeshift solution is to slightly shift the domain specification: Plot3D[f[x, y], {x, .0001, 1}, {y, .0001, 1 - x}]. Then a plot is displayed. But I find this unacceptable, because I can't automate this in any reasonable way.

A less makeshift solution is to generate a Table of values, and then ListPlot3D the result: data=Flatten[Table[f[x, y], {x, 0, 1, .05}, {y, 0, 1 - x, .05}], 1]; ListPlot3D[data]. This doesn't work so well because I lose the adaptive sampling algorithms of Plot3D.

Is there an option (documented or undocumented) that toggles how Plot3D works so that it tries a different set of points, before giving up? What else can I try?

  • $\begingroup$ In my opinion, the example you present is too artificial to act as surrogate of your real problem. The solution for example is simply to replace f`` with g`, but nothing so simple would probably work for the real case. $\endgroup$ – m_goldberg Jul 2 '17 at 22:59
  • $\begingroup$ @m_goldberg g is unavailable. It's in the question for illustrative purposes only. $\endgroup$ – QuantumDot Jul 2 '17 at 23:05
  • 1
    $\begingroup$ Plot3D[f[x, y] /. Indeterminate | Undefined :> f @@ Rescale[{x, y}, {0, 1}, {Sqrt@$MachineEpsilon,1 - Sqrt@$MachineEpsilon}], {x, 0, 1}, {y, 0, 1 - x}]? $\endgroup$ – Michael E2 Jul 3 '17 at 0:23
  • $\begingroup$ @MichaelE2 that's a good way to go. I also found another possibility by using Exclusions (which works only if the boundary is known): Plot3D[f[x, y], {x, 0, 1}, {y, 0, 1 - x}, Exclusions -> x == 0] $\endgroup$ – QuantumDot Jul 3 '17 at 1:21
  • $\begingroup$ When I use Exclusions, I get an empty plot still. (I tried that first, in fact.) V10.4.1 (Mac). $\endgroup$ – Michael E2 Jul 3 '17 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.