ArgumentError in Fox-Rabbit Pursuit Problem with ModelingToolkit.jl

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?

I got it to work with the following

using ModelingToolkit
using DifferentialEquations

## Fox-Rabbit Pursuit
@variables  t x(t) y(t) R(t) ra(t) sa(t) dra(t) dsa(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(ra) ~ dra,
    D(sa) ~ dsa,
    D(x) ~ R * (ra - x),
    D(y) ~ R * (sa - y),
    R ~ k * sqrt(dra^2 + dsa^2)/
        sqrt((ra - x)^2 + (sa - y)^2),
])

prob = ODEProblem(structural_simplify(rabbitfox_model),
            [x=>3, y=>0, ra=>r_(0), sa=>s_(0)],
            (0, 5.071),
            [k=>1.1]
            )

sol = solve(prob)

Though, it seems strange to me that I need to give the initial values ra=>r_(0), sa=>s_(0) for the rabbit’s path. I initially thought that these were determined somehow. It also leads to a bit of an inconsistency if we give values other than those.

This might be a MTK bug. I will look into it next week. Could you file an issue as well?

Sure. Though, I am not quite sure how to title and phrase it as an issue. (And I’m not sure I understand what the bug is. )

I’ve posted it here: Possible Inconsistent Initial Values of Some State Variables · Issue #1015 · SciML/ModelingToolkit.jl · GitHub

Solved in Possible Inconsistent Initial Values of Some State Variables · Issue #1015 · SciML/ModelingToolkit.jl · GitHub