I am trying to solve a fox-rabbit pursuit problem with ModelToolkit.jl
.
The fox’s path satisfies
x'(t) = R(t) (r(t) - x(t))
y'(t) = R(t) (s(t) - y(t))
where
R(t) = k \dfrac{\sqrt{r'(t)^2 + s'(t)^2}}{\sqrt{(r(t) - x(t))^2+(s(t) - y(t))^2}}
and (r(t), s(t)) is the rabbit’s path.
Citation: Griffiths, D.F. and Higham, D.J., 2010. Numerical methods for ordinary differential equations: initial value problems. Springer Science & Business Media.
I’ve solved this by manually calculating (r'(t), s'(t)), but I’d rather not need to do this. Below is my first and most naive attempt. I have also tried registering the functions r_(t)
and s_(t)
and also extracting the derivatives D(ra)
and D(sa)
in the ODESystem
by introducing new variables.
using ModelingToolkit
using DifferentialEquations
## Fox-Rabbit Pursuit
@variables t x(t) y(t) R(t) ra(t) sa(t)
@parameters k
D = Differential(t)
# rabbit's path, an outward spiral
r_(t) = sqrt(1+t) * cos(t)
s_(t) = sqrt(1+t) * sin(t)
@named rabbitfox_model = ODESystem([
ra ~ r_(t),
sa ~ s_(t),
D(x) ~ R * (ra - x),
D(y) ~ R * (sa - y),
R ~ k * sqrt(D(ra)^2 + D(sa)^2)/
sqrt((ra - x)^2 + (sa - y)^2)
])
prob = ODEProblem(structural_simplify(rabbitfox_model),
[x=>3, y=>0],
(0, 4),
[k=>1.1]
)
sol = solve(prob)
The error I get before even calling the last line is ArgumentError: Any[ra(t), sa(t), r_(t), s_(t), R(t)] are missing from the variable map.
I’d prefer a solution to be as close to my naive attempt as possible because it most closely matches how the problem was presented.
What do I need to change to get this to work and what am I missing?