Message Boards Message Boards


Problem using Piecewise with NDSolve

Posted 5 years ago
6 Replies
0 Total Likes

I'm using an interpolation function defined over a limited range & using "Piecewise" to extend the range. This "Piecewise" function defines the derivative in "NDSolve." The result seems to be correct, but an error message results indicating that the interpolation function is being asked to extrapolate out of its defined range. I have used "Piecewise" in the past with "NDSolve" without this error. Using "Integration" to integrate the "Piecewise" function doesn't generate the error message. Here is a simplified version of the code:

hf=  Interpolation[{{0, 0}, {1, 0.4}, {2, 1.2}, {4.7, 3.4}, {5,  3.4}, {5.4, 3.3}, {9, 1.8}, {12.5, 1}, {16, 0.1}, {17, 0}}, InterpolationOrder -> 1];

h[t_] := Piecewise[{{hf[t], 0 <= t <= 17}}];

ih = NDSolveValue[{ih'[t] == h[t], ih[0] == 0}, ih, {t, 0, 30}]

{Integrate[h[t], {t, 0, 30}], ih[30]}

Am I doing something wrong? Comments please. Thank you.

6 Replies

It's not an error message; it's a warning that extrapolation is being used.

Posted 5 years ago

Thanks. Sorry, i shouldn't have used the word "error." I know it's just warning that it is extrapolating beyond the interpolation function's defined range.

The issue is that because of the "Piecewise" function, h[t], "NDSolve" shouldn't be calling the interpolation function outside it's defined range. The attachment shows what my system looks like when I execute the example.

Note than when I plot the function,h[t], & use "NIntegrate," there are no warning messages.

Further comments? Thanks.


I am having the same problem. The attached notebook gives a minimal realization and a workaround, based on this discussion. To me, this seems a bug: NDSolveValue (and its cousins) misinterpret functions defined using Piecewise, generating an event and then somehow turning what should be a call within the defined range into an extrapolation.

If anyone can explain why this shouldn't be considered a bug, I'd be very interested to understand why! And if it is a bug, hopefully, it can get corrected soon....

Posted 16 days ago

Thanks John. I'd forgotten about this. I guess nothing has changed in the last 5 years if you are still seeing this behavior.

I contacted Wolfram Support. Apparently, the message is deliberate and reflects an internal evaluation that gets triggered whenever there is a discontinuity in the function.

In any case, an easier way to solve the issue is to use a built-in option for NDSolveValue:

ysol2 = NDSolveValue[{y'[t] == utest2[t], y[0] == 0}, y, {t, 0, 5}, 
  Method -> {"DiscontinuityProcessing" -> None} ]

Using this option seems to roughly double the number of function calls -- but you do suppress the warning.

Posted 6 days ago

Thanks, John. Yes, the "Method" option fixes the warning msg in my example.

Maybe Mathematica is evaluating the function in the Piecewise function before it evaluates the test to see whether it needs to be evaluated?

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract