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.

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