InterpolatingFunction within Piecewise gives range error?

So I have a function myfun with positive domain that I got from NDSolve and a bunch of transformations afterwards. I can plot it on its own like

Plot[myfun[t], {t, 0, 6}]


but when I try to insert it into a Piecewise function like

Plot[Piecewise[{{myfun[t], t > 0 && t < 6}}], {t, -6, 6}]


or

Plot[Piecewise[{{myfun[t], t > 0 && t < 6}, {1, t<=0}}], {t, -6, 6}]


it gives me warnings

InterpolatingFunction::dmval: Input value {-5.99975} lies outside the range of data in the interpolating function. Extrapolation will be used. >>


and doesn't plot the function myfun.

What is even more weird, I was unable to create a minimal working example, because something like

Plot[Piecewise[{{Interpolation[{1, 2, 3, 5, 8, 5}][t],
t > 0 && t < 6}}], {t, -6, 6}]


works just fine!

What could be the problem here?

EDIT:

Plot[Piecewise[{{Sqrt[Interpolation[{1, 2, 3, 5, 8, 5}][t]], t > 1 && t < 6}}], {t, -6, 6}]


generates the same warnings, but does draw the plot. (Note that domain and range of the function under square root are positive.)

EDIT 2: Here's reduced code that has the problem

soln = NDSolve[{y'[t] == 1/(1/(1 + Cos[y[t]]))^2, y[0] == 0}, y, {t, 0, 10}]

pw = Piecewise[{{1, t < 0}, {y[t] /. soln, t > 0}}]


Now

Plot[pw /. x -> t, {t, 0, 10}, PlotRange -> All]


works but

Plot[pw /. x -> t, {t, -1, 10}, PlotRange -> All]


doesn't.

• Can you find a (hopefully simpler) NDSolve[]-generated InterpolatingFunction[] that generates that error? Dec 15, 2016 at 1:46
• No warning in v8.0.4 and v9.0.1 with the code in EDIT. Dec 15, 2016 at 8:38

Edit to add fix for plot not appearing

I think the issue here is the way you are evaluating the Piecewise function. If you look at the Scope part of the Piecewise documentation it says that to evaluate a piecewise funtion you should use a ReplaceAll, like this:

pw = Piecewise[{{Sin[x]/x, x < 0}, {1, x == 0}}, -x^2/100 + 1];
pw /. {{x -> -5}, {x -> 0}, {x -> 5}}
(*{Sin[5]/5, 1, 3/4}*)


This way the piecewise is constructed, then the variable (in this case x) is input. The way you have written it substitutes the variable and then constructs the piecewise. So to make your plot work correctly, you could write this:

p = Piecewise[{{Sqrt[Interpolation[{1, 2, 3, 5, 8, 5}][t]], t > 1 && t < 6}}];
Plot[p /. {t -> x}, {x, -6, 6}]


To fix the issue with incorrect evaluation, again I think you need to define the piecewise function in the correct order. In the original code (apologies for screenshot, formatting the symbol is hard):

Notice the curly brackets (List) around the interpolating function? If you use First to get rid of this the plot works. Also note that in the ReplaceAll you should replace the variable in the piecewise function (i.e. t) with the variable you are plotting over (i.e. x) rather than the other way round. So the result:

soln = NDSolve[{y'[t] == 1/(1/(1 + Cos[y[t]]))^2, y[0] == 0}, y, {t, 0, 10}];
pw = Piecewise[{{1, t < 0}, {First[y[t] /. soln], t > 0}}];
Plot[pw /. t -> x, {x, -1, 10}, PlotRange -> {{-1, 10}, All}]


• Thanks, this does make the warnings go away, but my original problem persists (see EDIT 2). Dec 15, 2016 at 15:37
• Edited to add detail Dec 15, 2016 at 16:00

Only simple fixes are needed.

Regarding the first edit - add Evaluate:

Plot[Evaluate@
Piecewise[{{Sqrt[Interpolation[{1, 2, 3, 5, 8, 5}][t]],
t > 1 && t < 6}}], {t, -6, 6}]


Regarding the second edit - add First; no need for replacements in either case:

soln = First@
NDSolve[{y'[t] == 1/(1/(1 + Cos[y[t]]))^2, y[0] == 0},
y, {t, 0, 10}];
pw = Piecewise[{{1, t < 0}, {y[t] /. soln, t > 0}}];
plot2 = Plot[pw, {t, -1, 10}, PlotRange -> All]


Either one of the fixes, or both combined should work for your original problem as well.