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) enter image description here

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)"}];
  • 1
    $\begingroup$ 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 $\endgroup$
    – Michael E2
    Aug 18, 2015 at 20:56
  • 1
    $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    Aug 18, 2015 at 20:57
  • 1
    $\begingroup$ Many symbols are undefined in the code in your question. $\endgroup$
    – bbgodfrey
    Aug 18, 2015 at 21:33

1 Answer 1


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.

enter image description here

(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]
  • $\begingroup$ Got it. Thanks. =) I wrote {say,sa2}= NDSolve[....] sa1 covers one solution, say2 covers the other solution. $\endgroup$
    – Bob
    Aug 18, 2015 at 22:16
  • 1
    $\begingroup$ 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]]] $\endgroup$
    – bbgodfrey
    Aug 18, 2015 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.