Hi everyone,
I’m trying to use a callback function. I’m not sure how the callback function works and am trying out this very simple script, but it has not been working. Here is the script:
### Installs all relevant packages if you don't have them already
using Pkg
function install_package(pkg_name::String)
if !(pkg_name in keys(Pkg.installed()))
Pkg.add(pkg_name)
end
end
packages = [
"DifferentialEquations",
"Plots",
"ModelingToolkit",
"OrdinaryDiffEq"
]
for pkg in packages
install_package(pkg)
end
using DifferentialEquations
using Plots
using ModelingToolkit
using OrdinaryDiffEq
###
@variables t x(t)
@independent_variables t
dt = Differential(t)
sys = ODESystem(
[
dt(x) ~ -x
],
t,
name=:simple) |> structural_simplify
u₀ = Dict(x => 1.0)
tspan = (0.0, 5.0)
callback = DiscreteCallback(
(t, u, integrator) -> t >= 1.0,
(t, u, integrator) -> begin
display("The time is over 1 second")
return true
end
)
# Define and solve the ODE problem
prob = ODEProblem(sys, u₀, tspan)
sol = solve(prob, Tsit5(), callback=callback)
plot(sol)
However, I am getting the following error:
ERROR: MethodError: no method matching isless(::Float64, ::Vector{Float64})
The function `isless` exists, but no method is defined for this combination of argument types.
…Which references the “sol” line.
How can I fix this?