Solving overdetermined systems in ModelingToolkit.jl

Hi,
I’m getting to know ModelingToolkit.jl and I’d like to know if it’s possible to solve a system where there are more equations than variables.

For example:

@variables x, y
julia> eqs = [Equation(x + y, 2), Equation(y, 1), Equation(x, y)]
3-element Array{Equation,1}:
 x + y ~ 2
 y ~ 1
 x ~ y

julia> ModelingToolkit.solve_for(eqs, [x, y])
ERROR: DimensionMismatch("matrix is not square: dimensions are (3, 2)")

If you build it as a nonlinear system and then alias eliminate I think it will actually just give the answer. This is something that will be documented soon.

1 Like

Thanks for the hint. I still get 3 equations though.

sys = NonlinearSystem(eqs, [x, y], [])
julia> ModelingToolkit.alias_elimination(sys).eqs
3-element Array{Equation,1}:
 0 ~ 2 - x - y
 0 ~ 1 - y
 0 ~ y - x

With master?

Yes I saw that alias_elimination for nonlinear systems was implemented in Make alias elimination work with NonlinearSystem by YingboMa · Pull Request #758 · SciML/ModelingToolkit.jl · GitHub

Hmm I would’ve expected that to work. That’s a question for @YingboMa then since he was the one who just did it.

Writing Equation(x + y, 2) is not quite right. Users should use ~.

Also, for NonlinearSystem to work, you need to have the same number equations and states. So, it should be

julia> using ModelingToolkit

julia> @variables x, y
(x, y)

julia> sys = NonlinearSystem([x + y ~ 2, x ~ y], [x, y], [])
Equations (2):
 x + y ~ 2
 x ~ y
States (2):
  x
  y
Parameters (0):

julia> equations(alias_elimination(sys))
1-element Vector{Equation}:
 0 ~ 2 - (2y)

julia> observed(alias_elimination(sys))
1-element Vector{Equation}:
 x ~ y

Or to use solve_for

julia> ModelingToolkit.solve_for([x + y ~ 2, x ~ y], [x, y])
2-element Vector{Float64}:
 1.0
 1.0
1 Like

Thanks for clarifying!