Modeling Step function in ODEs

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.

``````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?

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

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?

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`.

I tried something like this:

``````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!