# Modeling Step function in ODEs

**URL:** <https://discourse.julialang.org/t/modeling-step-function-in-odes/110313>\
**Category:** Modelling & Simulations\
**Created:** [February 16, 2024, 6:53pm UTC](https://discourse.julialang.org/t/modeling-step-function-in-odes/110313 "2024-02-16T18:53:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Nick1](https://discourse.julialang.org/u/Nick1)\
**Post date:** [February 16, 2024, 6:53pm UTC](https://discourse.julialang.org/t/modeling-step-function-in-odes/110313/1 "2024-02-16T18:53:38Z")

</div>

Hi,

I am modeling a stimulus that lasts for a certain amount of time. The way I model it is with a set of callback functions, e.g.

```julia
ton = 0.0;
toff = 2.0;
stim = 10.0;

function condition1(u, t, integrator)
    t>=ton && t < toff
end

function condition2(u, t, integrator)
    t>= toff
end

function affect1!(integrator)
    integrator.u[1] = stim
end

function affect2!(integrator)
    integrator.u[1] = 0.0
end

cb1 = DiscreteCallback(condition1, affect1!);
cb2 = DiscreteCallback(condition2, affect2!);
cbs = CallbackSet(cb1, cb2)

T = 60.0;
tspan = (0.0, T); 
prob = ODEProblem(odesys!, collect(init), tspan, collect(param), callback=cbs);

```

Is there any better way to do it, especially in cases where I want to apply it multiple times during the simulation?

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [February 16, 2024, 7:17pm UTC](https://discourse.julialang.org/t/modeling-step-function-in-odes/110313/2 "2024-02-16T19:17:25Z")

</div>

You could use a `VectorCallback` if you have many stimuli. Also check out the DiffEqCallbacks.jl package.

---

<div class="post-metadata">

**Author:** ![Nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Nick1](https://discourse.julialang.org/u/Nick1)\
**Post date:** [February 23, 2024, 11:37pm UTC](https://discourse.julialang.org/t/modeling-step-function-in-odes/110313/3 "2024-02-23T23:37:13Z")

</div>

Thanks for your response! In the case of VectorContinuousCallback, the event is applied when a condition hits zero. How can it be used in the case of time intervals?

---

<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:** [February 24, 2024, 7:31am UTC](https://discourse.julialang.org/t/modeling-step-function-in-odes/110313/4 "2024-02-24T07:31:39Z")

</div>

For this I would just use PresetTimeCallback and set the on time and off time and have one effect that runtime branches based off of `integrator.t`.

---

<div class="post-metadata">

**Author:** ![Nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Nick1](https://discourse.julialang.org/u/Nick1)\
**Post date:** [February 29, 2024, 12:07am UTC](https://discourse.julialang.org/t/modeling-step-function-in-odes/110313/5 "2024-02-29T00:07:32Z")

</div>

I tried something like this:

```julia
times = [ton, toff];
function affect!(integrator)
    if integrator.t == ton
        integrator.u[1] = stim;
    elseif integrator.t == toff
        integrator.u[1] = 0.0;
    end
end

cb = PresetTimeCallback(times, affect!);

```

But it does not change the value. I think I misunderstood your answer. Thanks!
