I am working with NDSolve to plot an ODE as a function of time. I need to find a way to figure out the exact value X of the solution for some specific time, say t*. Even better, a way to figure out the time t* at which the F[t*]=X. It's probably straightforward but don't have much experience with NDSolve! Thanks in advance!


2 Answers 2


You can use WhenEvent. Let's say we want the points that the solution takes the value of 2.5:

{sol,points}=Reap@NDSolve[{D[y[x],x]==Sin[x], y[0]==1, WhenEvent[y[x]==2.5,Sow[{x,y[x]}]]}, {y[x]}, {x, 0, 10}];

pointsis our desired set of points:


You can see them on a plot:

Show[Plot[y[x] /. sol, {x, 0, 10}], 
 ListPlot[points, PlotStyle -> Orange]] 

enter image description here

Also, the exact value at any point, e.g. $x=1$ can be retrieved by y[1]/.sol

Sometimes I find it easier to use NDSolveValue which is exactly the same, but gives the values instead of rules. Look it up and you might find it useful.


It would be better if you had supplied an example problem. It is possible that the answer below will not work in general.

Let's use a simple gravity example where we start with an object 10 feet above the ground and let it free fall.

sol = NDSolve[{h''[t] == -98/10, h'[0] == 0, h[0] == 10}, 
  h, {t, 0, 10/7}]

{{h -> InterpolatingFunction[{{0., 1.42857}}, <>]}}

The method I use to define a function of time from this output (there is more than one method) is:

f[t_] := sol[[1, 1, 2]][t]

Now one can plot it:

Plot[f[t], {t, 0, 10/7}]

Mathematica graphics

If we wanted to know at what time the object would be 4 feet above the ground one could use:

solN = NSolve[f[t] == 4, t]
(* {{t -> 1.10657}} *)

This will give you a warning message about Inverse functions but that is OK.

  • $\begingroup$ That's great! Thanks. Will get right back to trying it out :-) $\endgroup$
    – Tox
    Apr 9, 2016 at 15:54

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.