# Handling events in DifferentialEquations.jl that take place over a time interval

**URL:** https://discourse.julialang.org/t/handling-events-in-differentialequations-jl-that-take-place-over-a-time-interval/64262
**Category:** Numerics
**Tags:** question, diffeq
**Created:** [July 8, 2021, 8:56am UTC](https://discourse.julialang.org/t/handling-events-in-differentialequations-jl-that-take-place-over-a-time-interval/64262 "2021-07-08T08:56:57Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![yuvalw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuvalw/32/19224_2.png) [@yuvalw](https://discourse.julialang.org/u/yuvalw)
#### Post date: [July 8, 2021, 8:56am UTC](https://discourse.julialang.org/t/handling-events-in-differentialequations-jl-that-take-place-over-a-time-interval/64262/1 "2021-07-08T08:56:57Z")

</div>

Hello,  
I am wondering what is the best way to implement events that happen during a time interval that affect an element of the derivative. Currently my code is of the form:

```julia
intervals = [1.0 2.0; 5.0 6.0; 9.0 10.0] # every row is a 1-sec interval in which the event takes place

function dydt!(dy,y,p,t)
    @. dy = .....
    # event handling:
    idx = findfirst([intervals[i,1] <= t < intervals[i,2] for i in 1:size(intervals,1)])
    if idx != nothing
        dy[idx] += ..... #add something to that component of the derivative for 1 sec
    end

end

```

What I was worried about is that the solver won’t actually start and end the effect of the events at the correct times, but rather depending on the time steps. One option is making the solver stop at the start and end points with `tstops=intervals[:]`, but I wasn’t sure if that would be the correct way to handle that.  
I thought I should use `PresetTimeCallback` to handle these events, however it seems mostly appropriate for instantaneous events.  
Thanks!

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [July 8, 2021, 10:06am UTC](https://discourse.julialang.org/t/handling-events-in-differentialequations-jl-that-take-place-over-a-time-interval/64262/2 "2021-07-08T10:06:16Z")

</div>

> [@yuvalw](#):
>
> One option is making the solver stop at the start and end points with `tstops=intervals[:]` , but I wasn’t sure if that would be the correct way to handle that.

That would be the correct way.
