# Suppress extrapolation of interpolating function in a ContourPlot

I have defined an interpolating function, myfcn[x], valid on the domain x = 0 to 1.5. I am then using ContourPlot to create an implicit plot. Something like:

ContourPlot[myfcn[x t] == t, {t,0,5}, {x,0,1}]


I know a priori that the points on the contour are such that x t will always be in the domain x = 0 to 1.5. However, when I run this, Mathematica outputs the warning:

Input value lies outside the range of data in the interpolating function. Extrapolation will be used.

Is there a way I can tell Mathematica not to extrapolate, and not to try to evaluate the interpolating function outside x= 0 to 1.5?

• Try to restrict the call pattern by defining myfcn[x_/;0<=x<=1.5] := ... Oct 14 '14 at 2:33
• @halirutan That worked great. Thanks! Oct 14 '14 at 2:45
• @halirutan I think this merits an answer. Oct 14 '14 at 4:57

As already said in my comment, in newer version of Mathematica you can simply restrict your interpolating function. If ContourPlot gets a non-numeric result, it will ignore it. A simple example is

With[{ip = Interpolation[{1, 4, 5, 7, 9}]},
func[x_ /; 1 <= x <= 5] := ip[x]
]

ContourPlot[func[x t] == x, {t, 0, 2}, {x, 0, 8}] I'm not sure whether this works in all version (especially < V7), but if it doesn't, you still have the chance to let func return a default value instead: func[_]:=0 if it is not in the interpolated region.

The option "ExtrapolationHandler" -> function can be used to control extrapolation. The setting "ExtrapolationHandler" -> {Indeterminate &} causes Indeterminate to be the value of the interpolation for inputs outside the domain. One can turn off the warning message, too, as done below.

myfcn = Interpolation[
Table[{x, 2 x^3 - 5 x^4/3 + 5 x/2}, {x, 1, 1.5, 0.1}],
"ExtrapolationHandler" -> {Indeterminate &, "WarningMessage" -> False}];
ContourPlot[myfcn[x t] == t, {t, 0, 5}, {x, 0, 1}, MaxRecursion -> 3] The option also works with NDSolve, if that is the way the InterpolatingFunction has been generated.

myfcn = NDSolveValue[{y'[x] == Sin[y[x]], y == 1/2},
y, {x, 0, 1.5},
"ExtrapolationHandler" -> {Indeterminate &, "WarningMessage" -> False}];
ContourPlot[myfcn[x t] == t, {t, 0, 5}, {x, 0, 1}]
(* plot omitted *)

• Where do you find these undocumented function options (like "ExtrapolationHandler")? I keep seeing answers like this on stack exchange, which are incredibly helpful but use features that don't seem documented at all by Wolfram. Feb 15 '16 at 12:29
• @NealPisenti I found that particular one here. Many others are catalogued in this Q&A. There are several Q&A on Method options for various functions. -- Not sure why they aren't documented; it could be that in WRI there is no consensus they will be permanent. Feb 15 '16 at 13:40
• @NealPisenti As for discovering them, sometimes they are revealed when a WRI developer posts an answer here or on another forum. Sometimes someone else finds out about one by getting help from WRI support (that has happened to me). Sometimes I've accidentally found them while using Trace. Feb 15 '16 at 13:50
• I suppose this option is undocumented because it's not quite complete, at least in the Notebook UI: syntax highlighter thinks they are errors, highlighting the strings in red. Jun 27 at 21:58
• @Ruslan Yes, it's not recognized in the syntax coloring, even though I believe "ExtrapolationHandler" -> {Indeterminate &, "WarningMessage" -> True} the default setting for the finite element method. I first learned about this option from the FEM developer here: mathematica.stackexchange.com/a/59289/4999 Jun 28 at 0:22

Another possibility, probably not as good as the ones proposed here but which can be helpful in some other cases, is to extract the boundaries of the interval on which the InterpolatingFunction is defined. This is done below in the line {{xmin, xmax}} = myfcn["Domain"]; (thank you @J.M. for this info). Then, a piecewise function is built, defined only on the appropriate interval.

myfcn = Interpolation[Table[{x, 2 x^3 - 5 x^4/3 + 5 x/2}, {x, 1, 1.5, 0.1}]];
{{xmin, xmax}} = myfcn["Domain"];
myfcn2[x_] = Piecewise[{{myfcn[x], xmin <= x <= xmax}}, Indeterminate];
ContourPlot[myfcn2[x t] == t, {t, 0, 5}, {x, 0, 1}, MaxRecursion -> 3] (I copied the output from MichaelE2's answer as there is no visible difference).

• ...or you can do myfcn["Domain"]. Nov 23 '15 at 1:07