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

**URL:** <https://discourse.julialang.org/t/du-i-not-the-same-as-integrator-t-val-1-differentialequations-jl/39616>\
**Category:** Modelling & Simulations\
**Created:** [May 17, 2020, 4:17am UTC](https://discourse.julialang.org/t/du-i-not-the-same-as-integrator-t-val-1-differentialequations-jl/39616 "2020-05-17T04:17:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANasc](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@ANasc](https://discourse.julialang.org/u/ANasc)\
**Post date:** [May 17, 2020, 4:17am UTC](https://discourse.julialang.org/t/du-i-not-the-same-as-integrator-t-val-1-differentialequations-jl/39616/1 "2020-05-17T04:17:00Z")

</div>

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[5]) 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[1] = dS = p[1]*u[1] - p[2]*u[1] - u[5]*u[1]*u[2]
    du[2] = dI = u[5]*u[1]*u[2] - p[2]*u[2] - p[3]*u[2] - p[4]*u[2]
    du[3] = dR = p[4]*u[2] - p[2]*u[3]
    du[4] = dD = p[3]*u[2]
    du[5] = dβ = -du[4]*p[5]
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[5] I get a monotonic curve rather than the unimodal curve  
that I expected from including du[4] in the equation:

```
# plot(sol,xlabel="t",ylabel="#")
plot(sol[5,:],xlabel="t",ylabel="#",label="Beta")

```

![du5](https://global.discourse-cdn.com/julialang/original/3X/9/e/9e120375b464a4a1be1e5d066f084f1d2ca6f002.png)

I expected that du[4] would give me the derivatives, similar to the callback below:

```
dDdt = [saved_values.saveval[i][4] for i in 2:length(sol)]
tD = saved_values.t[2:end]
plot(tD,-dDdt,xlabel="t",label="Beta")

```

![dDdt](https://global.discourse-cdn.com/julialang/original/3X/7/8/78de7c2b7a18d51fea0196a4789bf59c2de1a9b3.png)

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!

---

<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:** [May 17, 2020, 4:39am UTC](https://discourse.julialang.org/t/du-i-not-the-same-as-integrator-t-val-1-differentialequations-jl/39616/2 "2020-05-17T04:39:43Z")

</div>

> [@ANasc](#):
>
> du[5] = dβ = -du[4]\*p[5]

This will give you the integral of the dD\*p[5], 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:

```julia
function SIR(du,u,p,t)
    dD = p[3]*u[2]
    du[1] = dS = p[1]*u[1] - p[2]*u[1] - dD*u[1]*u[2]
    du[2] = dI = u[5]*u[1]*u[2] - p[2]*u[2] - p[3]*u[2] - p[4]*u[2]
    du[3] = dR = p[4]*u[2] - p[2]*u[3]
    du[4] = dD 
end

```

---

<div class="post-metadata">

**Author:** ![ANasc](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@ANasc](https://discourse.julialang.org/u/ANasc)\
**Post date:** [May 17, 2020, 12:49pm UTC](https://discourse.julialang.org/t/du-i-not-the-same-as-integrator-t-val-1-differentialequations-jl/39616/3 "2020-05-17T12:49:59Z")

</div>

Ah of course. Thanks for your help!
