WOLFRAM NOTEBOOK

In[]:=
Clear["Global`*"];
In[]:=
utest=ListInterpolation[Table[
2
i
,{i,0,3}],{0,3},InterpolationOrder1];utest1[t_?NumericQ/;0<=t<=3]:=utest[t];utest1[_?NumericQ]=0;Plot[utest1[t],{t,0,5}]
Out[]=
1
2
3
4
5
2
4
6
8
In[]:=
ysol1=NDSolveValue[{y'[t]==utest1[t],y[0]==0},y,{t,0,5}]
Out[]=
InterpolatingFunction
Domain: {{0.,5.}}
Output: scalar
This method was taken from the following web posting:

https://mathematica.stackexchange.com/questions/164257/interpolatingfunctiondmval-error-in-the-piecewise-function-with-an-interpolati

In contrast, the “obvious” way of proceeding, using Piecewise[...] gives an extrapolation bug, because the discontinuity in the function erroneously generates an “Event” in the code:
In[]:=
utest=ListInterpolation[Table[
2
i
,{i,0,3}],{0,3},InterpolationOrder1];utest2[t_]:=Piecewise[{{utest[t],0<=t<=3}},0]Plot[utest2[t],{t,0,5}]
Out[]=
1
2
3
4
5
2
4
6
8
In[]:=
ysol2=NDSolveValue[{y'[t]==utest2[t],y[0]==0},y,{t,0,5}]
InterpolatingFunction
:Input value {3.42007} lies outside the range of data in the interpolating function. Extrapolation will be used.
Out[]=
InterpolatingFunction
Domain: {{0.,5.}}
Output: scalar
The two methods give nearly the same result, with a slight difference occurring at the discontinuity.
In[]:=
Plot[ysol1[t]-ysol2[t],{t,0,5}]
Out[]=
1
2
3
4
5
-2e-7
-1e-7
1e-7
2e-7
Wolfram Cloud

You are using a browser not supported by the Wolfram Cloud

Supported browsers include recent versions of Chrome, Edge, Firefox and Safari.


I understand and wish to continue anyway »

You are using a browser not supported by the Wolfram Cloud. Supported browsers include recent versions of Chrome, Edge, Firefox and Safari.