# Updating the solution of ODE at every time step

I have a system of differential equations solved as following:

## 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?

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.

Thanks, figured it out based on documentation example.

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.

Use a closure

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

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

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


Yup that’s it.