Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
1  
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 at 15:25
add comment

1 Answer 1

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.

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. –  episanty Apr 14 at 16:51
1  
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 at 17:08
add comment

Your Answer

 
discard

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.