Yesterday I posted about getting control inputs into an ODE and I got great advice. However, I’m running into an issue where the exogenous functions used don’t seem to work if they’re even moderately complicated.
Below is a minimal example. I have an ODE defined that pulls in a constant c
and exogenous function D_(t)
. If D(t)
uses a simple condition, e.g., time > 5
, it works as intended. If I add anything slightly more complicated, like another condition, e.g., (time > 5) && (time < 6)
, it doesn’t appear to have any effect anymore. In my real project I’m trying to pull in values in D from data, but even just hard-coding a control signal does not seem to work. I’m not sure where I’m going wrong in this.
Minimal working example:
using DifferentialEquations
using ParameterizedFunctions
using Optim
using Plots
gr()
function D1(t)
if (t > 5)
return 0.1
else
return 0.0
end
end
function D2(t)
if (t > 5) && (t < 6)
return 0.1
else
return 0.0
end
end
p = [
-0.1089 ;
75.2304 ;
2.3965
]
c = p[end]
sma_ode1 = @ode_def begin
dT = a*T + b*D1(t) + c
end a b
sma_ode2 = @ode_def begin
dT = a*T + b*D2(t) + c
end a b
u0 = [22.0]
p0 = p[1:2]
tspan = (0.0, 10.0)
prob1 = ODEProblem(sma_ode1, u0, tspan, p0)
prob2 = ODEProblem(sma_ode2, u0, tspan, p0)
sol1 = solve(prob1, Tsit5(), saveat=0.1)
sol2 = solve(prob2, Tsit5(), saveat=0.1)
p11 = plot(sol1, ylims=(0,50), title="Single condition")
p12 = plot(sol1.t, D1.(sol1.t))
p21 = plot(sol2, ylims=(0,50), title="Two conditions")
p22 = plot(sol2.t, D2.(sol2.t))
l = @layout [a b ; c d]
plot(p11, p21, p12, p22, layout = l)
Output: