5
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ 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 $\endgroup$ – Max1 Apr 14 '14 at 15:25
7
$\begingroup$

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

Out:

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.

ClearAll[f];
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]];
ListPlot[xyValues]

Out:

Sample_region

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:

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

Out:

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]

Out:

Cos[y]

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

ClearAll[f];
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}]

Analytic_solution

which gives the function over the whole area.

edit: added some pictures and a little more Info.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Emilio Pisanty Apr 14 '14 at 16:51
  • 1
    $\begingroup$ 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. $\endgroup$ – Max1 Apr 14 '14 at 17:08

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.