# Updating the solution of ODE at every time step

**URL:** <https://discourse.julialang.org/t/updating-the-solution-of-ode-at-every-time-step/43096>\
**Category:** General Usage\
**Created:** [July 15, 2020, 1:57am UTC](https://discourse.julialang.org/t/updating-the-solution-of-ode-at-every-time-step/43096 "2020-07-15T01:57:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [July 15, 2020, 1:57am UTC](https://discourse.julialang.org/t/updating-the-solution-of-ode-at-every-time-step/43096/1 "2020-07-15T01:57:32Z")

</div>

I have a system of differential equations solved as following:

```julia
## Lets solve the ODE first without using Kalman Filters
function theo_ode!(du,u,p,t)
    du[1] = u[2]
    du[2] = -0.01*u[2] - u[1] + sin(2*t)
end

u₀ = [0.1, 0.5] # initial state vector
tspan = (0.0,50.0) # time interval

prob = ODEProblem(theo_ode!,u₀,tspan)
sol = solve(prob)

```

At every time step `t` I would like to get the current solved value of `u`, update it (a slight pertubation), and then use it as initial conditions for the next time step.

Would I be able to do that directly in `theo_ode` function or do I need some sort of callback?

---

<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:** [July 15, 2020, 2:03am UTC](https://discourse.julialang.org/t/updating-the-solution-of-ode-at-every-time-step/43096/2 "2020-07-15T02:03:56Z")

</div>

> [@affans](#):
>
> Would I be able to do that directly in `theo_ode` function or do I need some sort of callback?

You’ll need to use a callback, otherwise it can’t account for rejections. Discrete callback where the condition is just `true`, and then you apply the affect.

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [July 15, 2020, 2:23am UTC](https://discourse.julialang.org/t/updating-the-solution-of-ode-at-every-time-step/43096/3 "2020-07-15T02:23:33Z")

</div>

Thanks, figured it out based on documentation example.

```julia
condition(u,t,integrator) = t==4
affect!(integrator) = integrator.u[1] += 10
cb = DiscreteCallback(condition,affect!)

u₀ = [0.1, 0.5] # initial state vector
tspan = (0.0,50.0) # time interval

prob = ODEProblem(cb_ode!,u₀,tspan)
sol = solve(prob, callback=cb, tstops=[4.0])

```

But is there a way to pass in an outside value to the `affect` function? I tried replacing `affect!(integrator)` with `affect!(integrator, value)` but that defines a new method that is not used.

---

<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:** [July 15, 2020, 2:32am UTC](https://discourse.julialang.org/t/updating-the-solution-of-ode-at-every-time-step/43096/4 "2020-07-15T02:32:58Z")

</div>

> [@affans](#):
>
> But is there a way to pass in an outside value to the `affect` function? I tried replacing `affect!(integrator)` with `affect!(integrator, value)` but that defines a new method that is not used.

Use a closure

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [July 15, 2020, 3:13pm UTC](https://discourse.julialang.org/t/updating-the-solution-of-ode-at-every-time-step/43096/5 "2020-07-15T15:13:58Z")

</div>

Is there an example of this? Here is my version of this, and just want to confirm this is the correct way to go:

```julia
function out_affect!(integrator, value)
    integrator.u[1] += value
end

outsidevalue = 109
cb = DiscreteCallback(condition, int -> out_affect!(int, outsidevalue))

```

---

<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:** [July 15, 2020, 3:14pm UTC](https://discourse.julialang.org/t/updating-the-solution-of-ode-at-every-time-step/43096/6 "2020-07-15T15:14:45Z")

</div>

Yup that’s it.
