# DifferentialEquations: Change parameter in ODE after some time

**URL:** <https://discourse.julialang.org/t/differentialequations-change-parameter-in-ode-after-some-time/30438>\
**Category:** General Usage\
**Created:** [October 29, 2019, 10:28am UTC](https://discourse.julialang.org/t/differentialequations-change-parameter-in-ode-after-some-time/30438 "2019-10-29T10:28:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rad1um](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rad1um/32/8483_2.png) [@rad1um](https://discourse.julialang.org/u/rad1um)\
**Post date:** [October 29, 2019, 10:28am UTC](https://discourse.julialang.org/t/differentialequations-change-parameter-in-ode-after-some-time/30438/1 "2019-10-29T10:28:44Z")

</div>

Hello everyone,

I hope that this question belongs here, since my problem is mostly about the DifferentialEquations package.

I have the following dynamical system:

```julia
using DifferentialEquations
function some_dynamics(u,p,t)
    du = zeros(length(u))
    for i in 1:length(u)
        for j in 1:length(u)
            du[i] += p[i,j]*u[j]*u[i]
        end
    end
    return du
end

```

with some initial conditions:

```julia
p = rand(4,4)
u0 = zeros(length(p[1,:])) .+ 0.1
tspan = (0.,1.)

```

I can solve the ODE using:

```julia
prob = ODEProblem(some_dynamics,u0,tspan,p)
sol = solve(prob)

```

Is it possible to change the parameter p at some point before `tspan[end]?`  
In pseudo code, I would like to be able to plug in something like  
`if t == 0.5 -> p = ones(4,4)`

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![korsbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/korsbo/32/15942_2.png) [@korsbo](https://discourse.julialang.org/u/korsbo)\
**Post date:** [October 29, 2019, 12:38pm UTC](https://discourse.julialang.org/t/differentialequations-change-parameter-in-ode-after-some-time/30438/2 "2019-10-29T12:38:19Z")

</div>

Hi,

you can use [callbacks](http://docs.juliadiffeq.org/latest/features/callback_functions.html) for this. That would entail adding something like

```julia
condition(u, t, integrator) = t - 0.5
affect!(integrator) = (integrator.p .= ones(4, 4))
callback = ContinuousCallback(condition, affect!)

...

solve(prob; callback = callback)

```

Happy coding!

---

<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:** [October 29, 2019, 3:42pm UTC](https://discourse.julialang.org/t/differentialequations-change-parameter-in-ode-after-some-time/30438/3 "2019-10-29T15:42:24Z")

</div>

You may want to try the new `PresetTimeCallback`, now added by popular request! Just add the time that you want the callback to deploy at and then the `affect!` on the integrator goes as expected. Use a vector of times if you want multiple call times.

```julia
using OrdinaryDiffEq, DiffEqCallbacks, Test

function some_dynamics(u,p,t)
    du = zeros(length(u))
    for i in 1:length(u)
        for j in 1:length(u)
            du[i] += p[i,j]*u[j]*u[i]
        end
    end
    return du
end

p = rand(4,4)
startp = copy(p)

u0 = zeros(length(p[1,:])) .+ 0.1
tspan = (0.,1.)
prob = ODEProblem(some_dynamics,u0,tspan,p)
cb = PresetTimeCallback(0.5,integrator -> integrator.p .= rand(4,4))
sol = solve(prob,Tsit5(),callback = cb)

prob = ODEProblem(some_dynamics,u0,tspan,p)
cb = PresetTimeCallback([0.3,0.6],integrator -> integrator.p .= rand(4,4))
sol = solve(prob,Tsit5(),callback = cb)

```

---

<div class="post-metadata">

**Author:** ![rad1um](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rad1um/32/8483_2.png) [@rad1um](https://discourse.julialang.org/u/rad1um)\
**Post date:** [October 29, 2019, 5:24pm UTC](https://discourse.julialang.org/t/differentialequations-change-parameter-in-ode-after-some-time/30438/4 "2019-10-29T17:24:38Z")

</div>

That is exactly what I needed. Thank you so much!
