# Nonlinear Least Squares Problems with ModelingToolkit

I’m trying set up a nonlinear least squares problem with variables and parameters defined by ModelingToolkit.jl. However, I’m getting an error because I have more equations than variables. Here is an example:

``````using NonlinearSolve
using ModelingToolkit

@variables begin
cₜ
k
ϕₘ, [bounds = (1, Inf)]
η, [bounds = (0, Inf)]
b, [bounds = (0, Inf)]
end

@parameters begin
t[1:100]
ϕ[1:100]
end

krieger = @. (1 - ϕ/ϕₘ)^(-η * ϕₘ)
eqs = @. 0 ~ b + cₜ * t + k * krieger

@mtkbuild ns = NonlinearSystem(eqs, [cₜ, k, ϕₘ, η, b], [t, ϕ])
``````

With this, I get the following error:

``````ERROR: ExtraEquationsSystemException: The system is unbalanced. There are 5 highest order derivative variables and 100 equations.
More equations than variables, here are the potential extra equation(s):
``````

Is there a way to specify that having more equations than variables is acceptable or am I going about this the wrong way?

That should be fine. It’s just a warning to ensure that you meant for unbalanced. You can turn it off.

Thank you for the reply. Pardon my ignorance, but how do you turn off the warning? I don’t see anything obvious in the docs for `NonLinearSystem`.

We need to better document it. This works:

``````using NonlinearSolve
using ModelingToolkit

@variables begin
cₜ
k
ϕₘ, [bounds = (1, Inf)]
η, [bounds = (0, Inf)]
b, [bounds = (0, Inf)]
end

@parameters begin
t[1:100]
ϕ[1:100]
end

krieger = @. (1 - ϕ/ϕₘ)^(-η * ϕₘ)
eqs = @. 0 ~ b + cₜ * t + k * krieger

@named ns = NonlinearSystem(eqs, [cₜ, k, ϕₘ, η, b], [t, ϕ])
ns = ModelingToolkit.complete(structural_simplify(ns, fully_determined = false))

prob = NonlinearLeastSquaresProblem(ns, [k => 0.0
ϕₘ => 0.0
η => 0.0
b => 0.0],
[
t => rand(100)
ϕ => rand(100)
])
sol = solve(prob)
``````

We probably want to flip that default on NonlinearSystem to allow for unbalanced. That is only supposed to be a check on ODESystem.

That works! Thank you for your help.