How to use PeriodicCallbacks from DiffEqs

Hey guys I’m having trouble understanding the documentation of periodic callbacks.

Say I have a ODE and want periodically input some number to a value. From reading the documentation I thought periodic callback would do this for me but I can’t get it to work.

as a minimal working example I have the lorenz system with adding 10 to the y variable every 2 units of time.

println("loading packages")
using DifferentialEquations
using Plots
gr()

g = @ode_def LorenzExample begin
  dx = σ*(y-x)
  dy = x*(ρ-z) - y
  dz = x*y - β*z
end σ ρ β

u0 = [1.0,0.0,0.0]
tspan = (0.0,10.0)
p = [10.0,28.0,8/3]
prob = ODEProblem(g,u0,tspan,p)

function periodic(integrator)
    integrator.u[2] += 10
end

cb = PeriodicCallback(periodic, 2::Number)

sol = solve(prob, callback = cb)
plot(sol)
1 Like

The solution is to change this to a float:

cb = PeriodicCallback(periodic, 2::Number)

We should auto-upconvert here.

3 Likes