# Terminate condition in ContinuousCallback in DifferentialEquations.jl

**URL:** https://discourse.julialang.org/t/terminate-condition-in-continuouscallback-in-differentialequations-jl/69748
**Category:** General Usage
**Tags:** diffeq, scientific-computing, sciml, differentialequation
**Created:** [October 14, 2021, 12:16pm UTC](https://discourse.julialang.org/t/terminate-condition-in-continuouscallback-in-differentialequations-jl/69748 "2021-10-14T12:16:35Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)
#### Post date: [October 14, 2021, 2:17pm UTC](https://discourse.julialang.org/t/terminate-condition-in-continuouscallback-in-differentialequations-jl/69748/5 "2021-10-14T14:17:49Z")

</div>

A minor issue first: you can just easily write

```julia
terminate_affect!(integrator) = terminate!(integrator)

```

Now for the major issue: why aren’t you happy with the result you get? If I run the code with the `terminate_condition` relaxed, that is, I set

```julia
function terminate_condition(u,t,integrator)
           u[2] < -10e6
end

```

and I plot the results (but just the points that were obtained by the solver, nothing interpolated afterwards), I get this:  
 ![discourse](https://global.discourse-cdn.com/julialang/original/3X/b/2/b2e44b10cdb9dea8b1e36e80446da1b6fcd76909.png)

Note that the ninth component of the simulated sequence of `u[2]`s (in the figure labelled as `x`) is already below 0.5. Hence the eighth `u[2]` was the last one that satisfied the condition.

For convenience the whole code here:

```julia
using DifferentialEquations
u0 = [1.,0.]
harmonic! = @ode_def HarmonicOscillator begin
          dv = -x
          dx = v
      end
tspan = (0.0,10.0)
prob = ODEProblem(harmonic!,u0,tspan)
function terminate_condition(u,t,integrator)
           u[2] < -10e6
end
terminate_affect!(integrator) = terminate!(integrator)
terminate_cb = ContinuousCallback(terminate_condition,terminate_affect!)
sol = solve(prob,callback=terminate_cb,dense=true)

using Plots
scatter(sol,denseplot=false)

savefig("discourse.png")

```

---

_[View the full topic](https://discourse.julialang.org/t/terminate-condition-in-continuouscallback-in-differentialequations-jl/69748)._
