# Can I enable the ContinuousCallback only on certain intervals?

**URL:** https://discourse.julialang.org/t/can-i-enable-the-continuouscallback-only-on-certain-intervals/96788
**Category:** General Usage
**Tags:** diffeq
**Created:** [March 29, 2023, 4:27pm UTC](https://discourse.julialang.org/t/can-i-enable-the-continuouscallback-only-on-certain-intervals/96788 "2023-03-29T16:27:15Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![vtfanta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vtfanta/32/48144_2.png) [@vtfanta](https://discourse.julialang.org/u/vtfanta)
#### Post date: [March 29, 2023, 4:27pm UTC](https://discourse.julialang.org/t/can-i-enable-the-continuouscallback-only-on-certain-intervals/96788/1 "2023-03-29T16:27:15Z")

</div>

I understand the difference between the DiscreteCallback and the ContinuousCallback. My question is, how could I use the continuous one while being active only on a specified interval of the independent variable?

For example, let’s say I’m interested in finding value x, such that the solution y(x) to the differential equation y'' + y = 0 hits the value 0.5 **on the interval x\geq 4**.

At first, I tried cheesing my way around by including the interval requirement in the condition and returning some nonzero value, if I’m outside of the interval of interest.

```julia
using Plots, DifferentialEquations

function f!(du, u, p, t) 
	du[1] = u[2]
	du[2] = -u[1]
end

x0 = [1.0, 0.0]
span = (0.0, 10.0)

condition(u, t, int) = (t < 4 ? -1.0 : u[1] - 0.5)
affect!(int) = terminate!(int)
cb = ContinuousCallback(condition, affect!)
prob = ODEProblem(f, x0, span; callback = cb)
sol = solve(prob) 
plot(sol, idxs = (1))

```

This seems to work, but if I change the initial condition to `x0 = [-1.0, 0.0]`, this ‘solution’ fails miserably:

 ![Výstřižek](https://global.discourse-cdn.com/julialang/original/3X/9/1/916faf2444b790a7571a2f51fc1e7e580542a8a2.png)

The problem is, the condition switches sign at the interval boundary and the integration is stopped at x=4. What is the intended way to do this? I would like to solve similar problem, but with a more complicated system.

---

<div class="post-metadata">

### Author: ![vtfanta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vtfanta/32/48144_2.png) [@vtfanta](https://discourse.julialang.org/u/vtfanta)
#### Post date: [March 30, 2023, 8:10am UTC](https://discourse.julialang.org/t/can-i-enable-the-continuouscallback-only-on-certain-intervals/96788/2 "2023-03-30T08:10:36Z")

</div>

Okay, I slept on it and it occurred to me that I can simply put the interval condition inside of the `affect!` function. So this example problem would be solved by setting this:

```julia
condition(u, t, int) = u[1] - 0.5
affect!(int) = int.t < 4 ? nothing : terminate!(int)

```
