# How do I restrict the range of an interpolating function in NDSolve?

I have a numerical solution for a differential equation. I know that the gray dotted solution is unphysical. Is there a way I can tell mathematica to ignore the gray dotted solution (e.g. restrict the interpolating function to values between 0 and 1)? (Later on, I want to do operations on this solution, and I don't want the gray solution to contribute) I tried to use WhenEvent, but I get an error message.

sa = NDSolve[{3/2 (a'[t]/a[t])^2 +
a'[t]/a[t] vdot[t]/v[t] - ωa/8 vdot[t]^2 ==
1/4 ρ0/a[t]^3, a[todaya] == 1,
WhenEvent[a[t] > 1, "StopIntegration"]}, a, {t, EQfraca, todaya}]

aa = Plot[Evaluate[a[t] /. sa], {t, EQfraca, todaya},
PlotRange -> {{EQfraca, todaya}, {mina, maxa}}, Frame -> True,
PlotStyle -> {{Red, Thick}, {Gray, Dotted}},
FrameLabel -> {"t", "a (t)"}];

• Hi! You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this this meta Q&A helpful – Michael E2 Aug 18 '15 at 20:56
• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Aug 18 '15 at 20:57
• Many symbols are undefined in the code in your question. – bbgodfrey Aug 18 '15 at 21:33

In the absence of definitions for several functions and constants, I chose

v[t] = 1; vdot[t] = 1; todaya = .8; EQfraca = .5; ωa = 1; ρ0 = 1; mina = 0.9; maxa = 1.3;


NDSolve then yields two InterpolatingFunctions

(* {{a -> InterpolatingFunction[{{0.5, 0.8}}, <>]},
{a -> InterpolatingFunction[{{0.5, 0.8}}, <>]}} *)


which in turn yields two curves. (Note that I changed PlotStyle to {{Gray, Dotted}, {Red, Thick}} for consistency with the figure in the question.) Because the two InterpolatingFunctions are distinct, simply do not use the one you do not want.

By the way, WhenEvent[a[t] > 1, "StopIntegration"] has no effect, because the upper curve starts at a[t] ==1, so no crossing occurs. To trigger it, use a[t] > 1.000001, for instance. This does not eliminate Gray curve, but it does make it very short.

Addendum: In answer to a comment below, the InterpolatingFunction which is less than 1 can be picked out by, for instance,

If[First@(a[todaya - .01] /. sa) < 1, First@sa, Last@sa]

• Got it. Thanks. =) I wrote {say,sa2}= NDSolve[....] sa1 covers one solution, say2 covers the other solution. – Bob Aug 18 '15 at 22:16
• Yes, that works. If you wish to automate the process, try something like sa[[Which[First@(a[.7] /. sa) < 1, 1, Last@(a[.7] /. sa) < 1, 2]]] – bbgodfrey Aug 18 '15 at 22:22