I am using MTK and noticed that when I construct an algebraic variable defined as the max of the matrix-vector product of the state vector, the results are accurate for small matrices, but go inaccurate rapidly as the matrix size increases.
Specifically, I get accurate results up 100x10. For 500x10, it’s always inaccurate.
Copy-paste example (you can simply uncomment the definition of C):
using LinearAlgebra
using ModelingToolkit, Plots, DifferentialEquations
using Symbolics: scalarize
@variables t
D = Differential(t)
function F(;name, α0 = fill(0.0, 10), C = fill(1.0, 10, 10))
sts = @variables T(t)=0.0 α(t)[1:length(α0)]=α0
eqs = [ scalarize(D.(α) .~ 0.0)
T ~ maximum(C*α)
]
ODESystem(eqs, t, [α..., T], []; name=name)
end
α0 = rand(10)
#C = rand(100, 10)
C = rand(500, 10)
@named mySys = F(α0=α0, C=C)
sys = structural_simplify(mySys)
prob = ODEProblem(sys, [], (0.0, 1.0))
sol = solve(prob)
println(maximum(C*α0))
println(sol[mySys.T][end])
Any clues how to get around that using MTK? If i build the same system directly in DifferentialEquations with an algebraic state defined the same manner, I don’t run into the same problem.