Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I just ran into this and it had me scratching my head for a good while. Say I have a function which may produce error messages for certain inputs, like e.g.

f[x_, y_] := (Sin[x] - Sin[y])/(x - y)

which will return Power::infy and Infinity::indet when called on the diagonal x==y. If I want to avoid that singular input in a plot, the standard way of telling Mathematica to not evaluate it there is to use a RegionFunction command, like

Plot3D[f[x, y]
 , {x, -10, 10}, {y, -10, 10}
 , RegionFunction -> Function[{x, y, f}, Abs[x - y] > 5]

This will successfully generate a plot with the diagonal taken off, but it will still produce the same error messages as f[1,1]. This behaviour remains even if I add the qualifiers f[x_: NumericQ, y_: NumericQ] to the definition, so some singular inputs are definitely passed to the function even though they are away from where I'm telling it to plot.

So: why is this? Is this actually the correct way to specify regions to stay away from? If not, what's the best practice for that?

share|improve this question
If you restrict your function with a condition the error will not occure f[x_, y_] /; Abs[x - y] > 5 := (Sin[x] - Sin[y])/(x - y). Maybe the points are sampled, but just not drawn if one uses RegionFunction – Max1 Apr 14 '14 at 15:25
up vote 7 down vote accepted

One option would be to restrict the function from funky regions with a Condition liks this

f[x_, y_] /; Abs[x - y] > 5 := (Sin[x] - Sin[y])/(x - y);
Plot3D[f[x, y], {x, -10, 10}, {y, -10, 10}]


Restricted Region

One can easily see that Plot3D will also sample points in the region which is "forbidden", the points are just not drawn due to the RegionFunction.

f[x_, y_] /; Abs[x - y] > 5 := (Sin[x] - Sin[y])/(x - y);
xyValues = {};
Plot3D[xyValues = Append[xyValues, {x, y}];
  f[x, y], {x, -10, 10}, {y, -10, 10}, Evaluated -> False, 
  RegionFunction -> Function[{x, y, f}, Abs[x - y] > 5]];



thus mathematica will print some errors once the points close to zero are evaluated, if the function is not restricted. No errors will occure, if the function is restricted, because in that case just the Input will be returned:

f[x_, y_] /; Abs[x - y] > 5 := (Sin[x] - Sin[y])/(x - y);
f[0, 0]


f[0, 0]

Another possible solution would be to solve the 0/0 Problem analyticaly like this:

Limit[(Sin[x] - Sin[y])/(x - y), x -> y]



and thus redefine our function to account for the x==y situation.

f[x_, y_] := (Sin[x] - Sin[y])/(x - y)
f[x_, y_] /; x == y := Cos[y]
Plot3D[f[x, y], {x, -10, 10}, {y, -10, 10}]


which gives the function over the whole area.

edit: added some pictures and a little more Info.

share|improve this answer
This is of course always possible. However, I would mostly be interested in understanding the behaviour of RegionFunction and in solutions which act within the plot. One does not always have the freedom to redefine the functions one is interested in, and one may want or even need the errors to appear for certain inputs. – Emilio Pisanty Apr 14 '14 at 16:51
ok in that case I misinterpreted the question a bit. Maybe someone else can give a more complete answer regarding "best practice" and the region function in this case. – Max1 Apr 14 '14 at 17:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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