Hello everyone,

I am a PostDoc in Department of Radiology, University of Minnesota. I want to use Julia to solve an ODE problem, where the user-provided parameter `p`

is changed dynamically. I refered to several topics in this forum(https://discourse.julialang.org/t/diffeqs-hybrid-continuous-discrete-system-periodic-callback/23791/2), and I tried to use the function `DiscreteCallback`

to dynamically update `integrator.p`

. However, in my code the parameter p is not updated successfully. It is always equal to the initial value. I attached my code below. Is there something wrong in it?

Thanks a lot for your kind help!

Xiaodong

```
using DifferentialEquations
using MATLAB
using Plots
# load variables
mf = MatFile("C:/Users/maxiao/.julia/dev/BlochSimWithDiffEq/test/testdata_blochsimu.mat")
Brt = get_variable(mf, "Brt")
Δt = get_variable(mf, "dt")
Brt = Brt[:,1000,:]
B = [0.0, 0.0, 0.0]
# Define the Bloch equation
function BlochEq!(du, u, p, t)
du[1] = γ/(2*π) * u[2] * p[3] - γ/(2*π) * u[3] * p[2]
du[2] = γ/(2*π) * u[3] * p[1] - γ/(2*π) * u[1] * p[3]
du[3] = γ/(2*π) * u[1] * p[2] - γ/(2*π) * u[2] * p[1]
end
# Constant parameters
γ = 2.675e8
# Define callback function for discrete control
Nt = 736
tstops = collect(0:Δt:(Nt-1)*Δt)
function condition_control_loop(t,u,integrator)
(t in tstops)
end
function control_loop!(integrator)
nt = round(integrator.t/Δt+1)
integrator.p = Brt[:,nt]
end
cb = DiscreteCallback(condition_control_loop, control_loop!)
# Initial conditions
u0 = [0.0, 0.0, 1.0]
prob = ODEProblem(BlochEq!, u0, (0.0, (Nt-1)*Δt), B)
sol = solve(prob, Tsit5(), callback = cb, tstops=tstops)
```