# Du[i] not the same as integrator(t,Val{1})? DifferentialEquations.jl

Hi All,
I am playing with a basic SIR disease model and would like to make the transmission
rate (Beta) depend on the rate at which the number of disease-related deaths changes.
(e.g., when few deaths are occurring transmission rate is high because there are no social distancing precautions in place. When deaths start to occur rapidly, transmission becomes low because greater social distancing. Then when deaths become less frequent transmission rate becomes high again due to relaxing of social distancing measures).

However, I can’t figure out how to make Beta depend on the rate of deaths.
Here’s an example (that doesn’t quite do what I want):

``````using DifferentialEquations, Plots
#The transmission rate Beta (du) is (supposed to be) a function of the rate of change of deaths (i.e., of dD/dt)

function SIR(du,u,p,t)
du = dS = p*u - p*u - u*u*u
du = dI = u*u*u - p*u - p*u - p*u
du = dR = p*u - p*u
du = dD = p*u
du = dβ = -du*p
end

const β = 1e-3
u0 = [100.0, 1.0, 0.0, 0.0, β]
tspan = (0.0, 365.0)
p = [1/(30*365), 1/(85*365), 1/100, 1/40, β]

prob = ODEProblem(SIR, u0, tspan, p)

saved_values = SavedValues(Float64, Array{Float64})
cb = SavingCallback((u,t,integrator)->integrator(t,Val{1}), saved_values, saveat = 0.0:1:365.0)
sol = solve(prob, Tsit5(), saveat=1.0,callback=cb)
``````

As you can see below, when I plot how Beta changes over time by plotting the solution
for du I get a monotonic curve rather than the unimodal curve
that I expected from including du in the equation:

``````# plot(sol,xlabel="t",ylabel="#")
plot(sol[5,:],xlabel="t",ylabel="#",label="Beta")
`````` I expected that du would give me the derivatives, similar to the callback below:

``````dDdt = [saved_values.saveval[i] for i in 2:length(sol)]
tD = saved_values.t[2:end]
plot(tD,-dDdt,xlabel="t",label="Beta")
`````` This is all a long-winded way of asking why du[i] doesn’t give me the same thing
as integrator(t,Val{1}).

I hope what I’m asking is clear. Any help would be appreciated!

This will give you the integral of the dD*p, not dD. If you want Beta to depend on the death rate and not the integral of the death rate, just directly use the values:

``````function SIR(du,u,p,t)
dD = p*u
du = dS = p*u - p*u - dD*u*u
du = dI = u*u*u - p*u - p*u - p*u
du = dR = p*u - p*u
du = dD
end
``````
1 Like

Ah of course. Thanks for your help!