One of the most annoying "features" of Mathematica is that the Plot family does extrapolation on InterpolatingFunctions without any warning. I'm sure it was discussed to hell previously, but I cannot seem to find any reference here in Mathematica.SE. While I know how to simply overcome the problem by defining a global variable for the domain of the interpolation, from time to time I forget to do this and then I spend days figuring out where does the numerical error originate. This can be avoided if Plot was to give a warning.
Consider the following example. An ODE system is defined and integrated for two different time ranges:
odes = {
a'[t] == -a[t] - .2 a[t]^2 + 2. b[t],
b'[t] == a[t] + .1 a[t]^2 - 1.1 b[t], a[0] == 1, b[0] == 1
};
sol100 = First@NDSolve[odes, {a, b}, {t, 0, 100}];
sol500 = First@NDSolve[odes, {a, b}, {t, 0, 500}];
Now querying the function value for a point outside of the range gives a correct warning:
(a /. sol100)[500]
InterpolatingFunction::dmval: Input value {500} lies outside the range of data in the interpolating function. Extrapolation will be used. >> 651.034
The same is not done when we use the function in Plot:
Show[
Plot[{a[t], b[t]} /. sol100, {t, 0, 400}, PlotStyle -> {Thick, Red}],
Plot[{a[t], b[t]} /. sol500, {t, 0, 500}, PlotStyle -> {Thick, Blue}]
]

I've tried to force a warning, with no avail. The following example still does not drop a warning, and returns the same plot and not "Error".
On[InterpolatingFunction::dmval]
Check[Plot[{a[t], b[t]} /. sol100, {t, 0, 500}], "Error",
InterpolatingFunction::dmval]
Interestingly, one can be sure that InterpolatingFunction::dmval is NOT turned off at all inside the Plot family. In the following example, LogLinearPlot is able to drop a warning about sampling from below the domain (that can be ignored being unrelated, see this post, also it seems to be fixed in v9), but it does not drop the same warning when sampling from above (> 100)!
LogLinearPlot[{a[t], b[t]} /. sol100, {t, 0.1, 500}]
InterpolatingFunction::dmval: Input value {-2.30241} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
It is even more disturbing to see that Plot checks the lower boundary but not the upper (thanks to J.M. for the comment):
Plot[{a[t], b[t]} /. sol100, {t, -1, 500}]
InterpolatingFunction::dmval: Input value {-0.989765} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
As Oleksandr has pointed out, it is not about lower vs. upper bound but first point vs. the rest.
Plot[{a[t], b[t]} /. sol100, {t, 101, 500}]
InterpolatingFunction::dmval: Input value {101.008} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
Questions
- Why
Plotdoes not give a warning when extrapolating anInterpolatingFunction? Is there some higher-level consideration that justifies this behaviour, or is it a higly annoying feature that is surely unexpected in most cases (maybe a bug)? - How can one force
Plotto give a warning? Is there any workaround that forces InterpolatingFunction::dmval not to be attenuated insidePlot?
Please note that I am not interested in solutions that make sure that I use the same value for integration and for plotting (i.e. defining a global time variable). I want to make sure that Plot NEVER extrapolates without giving a warning.




Plotalso ignores say complex values which is sometimes useful. – chris Nov 5 '12 at 12:20Plot::accbendand not because of extrapolating outside of the range (InterpolatingFunction::dmval). It is not informative for the user, and I'm not sure they are related. – István Zachar Nov 5 '12 at 13:14Plot[{a[t], b[t]} /. sol100, {t, -1, 500}]. – J. M.♦ Nov 5 '12 at 13:35Plot[{a[t], b[t]} /. sol100, {t, 400, 500}]. It seems to be determined not by lower vs. upper bounds but rather somehow depends on the range chosen (first point vs. the rest?); apparentlyInterpolatingFunction::dmvalis turned off, but only later in the plotting process. The strange thing is that hookingQuietandOffshow no relevant usage of the former inPlotand no instances at all of the latter. – Oleksandr R. Nov 5 '12 at 13:56