I am trying to solve a differential equation that contains some interpolating functions as input, and for reasons unknown to me I am encountering an error which makes Mathematica use extrapolation. I managed to create a toy problem which seems to reproduce my error below, and I hope someone can explain me why I am getting the error I am getting.
The following numerically solves a simple differential equation:
Asol = NDSolve[{A'[r] + A[r] == 0, A[1] == 1}, A, {r, 1, 0}, WorkingPrecision -> 30]
and gives me an interpolating function as output:
{{A -> InterpolatingFunction[{{0, 1.00000000000000000000000000000}}, <>]}}.
Now I would like to numerically solve a differential equation that contains this interpolating function:
NDSolve[{(B'[r] + Sqrt[A[r]] /. Asol) == 0, B[1] == 2}, B, {r, 0, 1}].
Although Mathemathica manages to solve it, it does so while giving the following error:
InterpolatingFunction::dmval: Input value {1.} lies outside the range
of data in the interpolating function. Extrapolation will be used. >>
{{B -> InterpolatingFunction[{{0., 1.}}, <>]}}
When I copy the input value in the error (i.e. the {1.}) and paste it, it reveals that the value is actually 1.0000000149011612`, which is indeed not in the range of the interpolating function.
My question is: why does Mathematica try to evaluate it at this point in the first place, as it is outside the range I am requesting in NDSolve? And how can I prevent it?
Some comments/things I noticed:
- The error does not seem to occur if I try to solve the equation B'[r]+A[r]==0. It only occurs when I the equation contains the square root, or any non-integer power of A[r].
- The error also does not seem to occur if I remove the WorkingPrecision -> 30 part in the command Asol. However, it does occur even if I also add WorkingPrecision -> 30 to the differential equation for B.
- Of course I can solve these equations exactly, but in my actual program I am using analogues of A and B which do not have exact solutions. In this program I would like a quite precise answer, therefore I do need the WorkingPrecision command.
NDSolve
is going beyond the upper bound of integration due to round-off issues of some sort. Of course, only individuals familiar with the guts ofNDSolve
would be able to answer with certainty. In any case, this should have no impact on the accuracy of the final answer. Turn off this message, if it bothers you. $\endgroup$r -> -u
, then the warning goes away:NDSolve[{(-BB'[u] + Sqrt[A[-u]] /. Asol) == 0, BB[-1] == 2}, BB, {u, -1, 0}]
-- BTW, the warning probably does not matter, since it the steps outside the interval are so small. Another BTW, you will get a more accurate interpolating function forA
, if you use the optionInterpolationOrder -> All
inNDSolve
. $\endgroup$