# Time-dependent events in ODE

**URL:** https://discourse.julialang.org/t/time-dependent-events-in-ode/14951
**Category:** Modelling & Simulations
**Tags:** question
**Created:** [September 14, 2018, 11:32am UTC](https://discourse.julialang.org/t/time-dependent-events-in-ode/14951 "2018-09-14T11:32:17Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [September 14, 2018, 2:21pm UTC](https://discourse.julialang.org/t/time-dependent-events-in-ode/14951/4 "2018-09-14T14:21:59Z")

</div>

You need to split into two callbacks (as your code errors on this), see [http://docs.juliadiffeq.org/latest/features/callback\_functions.html#CallbackSet-1](http://docs.juliadiffeq.org/latest/features/callback_functions.html#CallbackSet-1) on how to combine them. Then the other error is

```julia
function affect!(integrator)
    integrator.u[1] = stim
end

```

as stated in the docs: [http://docs.juliadiffeq.org/latest/features/callback\_functions.html#Example-1:-Bouncing-Ball-1](http://docs.juliadiffeq.org/latest/features/callback_functions.html#Example-1:-Bouncing-Ball-1)

So all in all, slightly modified:

```julia
using Plots
using DifferentialEquations

# Parameters
const k21 = 0.14*24
const k12 = 0.06*24
const ke = 1.14*24
const α = 0.5
const β = 0.05
const η = 0.477
const μ = 0.218
const k1 = 0.5
const V1 = 6

# Time
maxtime = 0.1
tspan = (0.0, maxtime)

# Dose
const stim = 100

# Initial conditions
x0 = [0 0 2e11 8e11]

# Model equations
function system(dy, y, p, t)
  dy[1] = k21*y[2] - (k12 + ke)*y[1]
  dy[2] = k12*y[1] - k21*y[2]
  dy[3] = (α - μ - η)*y[3] + β*y[4] - k1/V1*y[1]*y[3]
  dy[4] = μ*y[3] - β*y[4]
end

# Events
function condition(y, t, integrator)
    t - 0.05
end
function affect!(integrator)
    integrator.u[1] = stim
end
cb = ContinuousCallback(condition, affect!)

# Solve
prob = ODEProblem(system, x0, tspan)
sol = solve(prob, Rodas4(), callback = cb)

# Plotting
plot(sol, layout = (2, 2))

```

(But please, next time provide an example which is more minimal and runs faster.)

---

_[View the full topic](https://discourse.julialang.org/t/time-dependent-events-in-ode/14951)._
