# How to implement adaptive parameters for ODE solvers

**URL:** <https://discourse.julialang.org/t/how-to-implement-adaptive-parameters-for-ode-solvers/84129>\
**Category:** General Usage\
**Tags:** ode, turing\
**Created:** [July 12, 2022, 10:20pm UTC](https://discourse.julialang.org/t/how-to-implement-adaptive-parameters-for-ode-solvers/84129 "2022-07-12T22:20:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timb](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@timb](https://discourse.julialang.org/u/timb)\
**Post date:** [July 12, 2022, 10:20pm UTC](https://discourse.julialang.org/t/how-to-implement-adaptive-parameters-for-ode-solvers/84129/1 "2022-07-12T22:20:30Z")

</div>

I would like to create parameters that change with time t.  
In the classic SIR model below for example, I would like to have a beta that changes: say there are two betas and a dummy variable that indicates when to use which beta.

```julia
function sir_ode!(du,u,p,t)
    (S,I,R,C) = u
    (β,c,γ) = p
    N = S+I+R
    infection = β*c*I/N*S
    recovery = γ*I
    @inbounds begin
        du[1] = -infection
        du[2] = infection - recovery
        du[3] = recovery
        du[4] = infection
    end
    nothing
end;

```

I could do this by not using an ODE solver and simply looping over a discretised code above. But for later I would like to do this with the model description above, so that I can use the turing packages.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [July 12, 2022, 10:46pm UTC](https://discourse.julialang.org/t/how-to-implement-adaptive-parameters-for-ode-solvers/84129/2 "2022-07-12T22:46:54Z")

</div>

If you have a simple function of t, you can just add the time dependence right there:

```julia
function sir_ode!(du,u,p,t)
    (S,I,R,C) = u
    (a,ω,c,γ) = p
    β = a * (1+sin(ω*t))
    N = S+I+R
    infection = β*c*I/N*S
    recovery = γ*I
    @inbounds begin
        du[1] = -infection
        du[2] = infection - recovery
        du[3] = recovery
        du[4] = infection
    end
    nothing
end

```

In a more complicated scenario, you might use [events](https://diffeq.sciml.ai/stable/features/callback_functions/).

---

<div class="post-metadata">

**Author:** ![timb](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@timb](https://discourse.julialang.org/u/timb)\
**Post date:** [July 12, 2022, 11:32pm UTC](https://discourse.julialang.org/t/how-to-implement-adaptive-parameters-for-ode-solvers/84129/3 "2022-07-12T23:32:54Z")

</div>

Oh, thank you!  
It seems to have worked, just that I had to change a lot of vector lengths.

Is there a nice beginners tutorial somewhere how to create more complex ODEs?
