Message Boards Message Boards


How can I end NDsolve when the function reaches a threshold value ?

Posted 9 years ago
2 Replies
2 Total Likes

I am trying to use NDsolve to find solution of coupled differential equation. But what i want to achieve is that i want to terminate NDsolve when a particular value of the solution is reached and i want to perform this operation multiple times.

POSTED BY: Angad Yuvraj
2 Replies
Posted 9 years ago

Rather than using Needs["DifferentialEquationsInterpolatingFunctionAnatomy"]; you can just simply use

domain = sol[[1,1,2,1]]


POSTED BY: Kevin McCann


not sure whether this helps; it is certainly not the most elegant solution, but it can be generalised. I use a univariate ODE as a test case and I only perform the operation once. This integrates a simple exponential decay until we reach the value 0.1:

sol = NDSolve[{x'[t] == -2 x[t], x[0] == 1, WhenEvent[x[t] == 0.1, "StopIntegration"]}, x, {t, 0, 5}]

We then determine the domain of the interpolating function - otherwise it extrapolates when we plot it.

domain = InterpolatingFunctionDomain[First[x /. sol]]

We can then plot the result and the threshold value:

Plot[{x[t] /. sol, 0.1}, {t, 0, domain[[1, 2]]}, PlotRange -> All]

which gives:

enter image description here

I hope that this helps to achieve what you want.



POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract