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?

  • 3
    $\begingroup$ Try to restrict the call pattern by defining myfcn[x_/;0<=x<=1.5] := ... $\endgroup$
    – halirutan
    Oct 14 '14 at 2:33
  • $\begingroup$ @halirutan That worked great. Thanks! $\endgroup$ Oct 14 '14 at 2:45
  • 1
    $\begingroup$ @halirutan I think this merits an answer. $\endgroup$
    – Yves Klett
    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}]

Mathematica graphics

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]

Mathematica graphics

The option also works with NDSolve, if that is the way the InterpolatingFunction has been generated.

myfcn = NDSolveValue[{y'[x] == Sin[y[x]], y[0] == 1/2}, 
   y, {x, 0, 1.5}, 
   "ExtrapolationHandler" -> {Indeterminate &, "WarningMessage" -> False}];
ContourPlot[myfcn[x t] == t, {t, 0, 5}, {x, 0, 1}]
(* plot omitted *)
  • 3
    $\begingroup$ 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. $\endgroup$ Feb 15 '16 at 12:29
  • 2
    $\begingroup$ @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. $\endgroup$
    – Michael E2
    Feb 15 '16 at 13:40
  • $\begingroup$ @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. $\endgroup$
    – Michael E2
    Feb 15 '16 at 13:50
  • $\begingroup$ 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. $\endgroup$
    – Ruslan
    Jun 27 at 21:58
  • $\begingroup$ @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 $\endgroup$
    – Michael E2
    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]

enter image description here

(I copied the output from MichaelE2's answer as there is no visible difference).

  • 2
    $\begingroup$ ...or you can do myfcn["Domain"]. $\endgroup$
    – J. M.'s torpor
    Nov 23 '15 at 1:07

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.