Guys, I am really new to Julia. I came to solve a system of ODE and want to stay. For sure my question is trivial, but I looked for similar solutions and could not find for 3 days.
Suppose I am solving Lorentz equations. But at a given time I need to change a parameter:
say a = 10 to a = 3 at time t = 50.
How can I do that? I know I can use callbacks, but could not find minimal working examples.
using Plots
using DifferentialEquations
# parameters
a = 10.0
b = 28.0
c = 8/3
function lorenz!(du,u,t)
du[1] = a*(u[2]-u[1])
du[2] = u[1]*(b-u[3]) - u[2]
du[3] = u[1]*u[2] - c*u[3]
end
u0 = [1.0;0.0;0.0]
tspan = (0.0,100.0)
prob = ODEProblem(lorenz!,u0,tspan)
@time sol = solve(prob)
plot(sol,vars=(0,1))
Once again, I need to change a=10 to a=2, for example, in a given time t = 50.
Tried:
This was close to solve, where one can change the variables but not the parameters.
Inspired by the solution above I tried:
using Plots
using DifferentialEquations
# parameters
a = 10.0
b = 28.0
c = 8/3
function lorenz!(du,u,t)
du[1] = a*(u[2]-u[1])
du[2] = u[1]*(b-u[3]) - u[2]
du[3] = u[1]*u[2] - c*u[3]
end
u0 = [1.0;0.0;0.0]
tspan = (0.0,100.0)
prob = ODEProblem(lorenz!,u0,tspan)
# Events
function condition(y, t, integrator)
t - 50
end
function affect!(integrator)
a = 2
end
cb = ContinuousCallback(condition, affect!)
#cb = DiscreteCallback(condition, affect!)
# Solve
@time sol = solve(prob, Rodas4(), callback = cb)
I expected to see a = 2, but it does not change, it keeps a = 10.
Could someone help me?
Thanks in advance.