# Nonlinear Least Squares Problems with ModelingToolkit

**URL:** <https://discourse.julialang.org/t/nonlinear-least-squares-problems-with-modelingtoolkit/116680>\
**Category:** Modelling & Simulations\
**Created:** [July 5, 2024, 8:50pm UTC](https://discourse.julialang.org/t/nonlinear-least-squares-problems-with-modelingtoolkit/116680 "2024-07-05T20:50:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dreycen\_Foiles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dreycen_foiles/32/44744_2.png) [@Dreycen\_Foiles](https://discourse.julialang.org/u/Dreycen_Foiles)\
**Post date:** [July 5, 2024, 8:50pm UTC](https://discourse.julialang.org/t/nonlinear-least-squares-problems-with-modelingtoolkit/116680/1 "2024-07-05T20:50:57Z")

</div>

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:

```julia
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:

```julia
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?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 5, 2024, 10:53pm UTC](https://discourse.julialang.org/t/nonlinear-least-squares-problems-with-modelingtoolkit/116680/2 "2024-07-05T22:53:34Z")

</div>

> [@Dreycen\_Foiles](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Dreycen\_Foiles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dreycen_foiles/32/44744_2.png) [@Dreycen\_Foiles](https://discourse.julialang.org/u/Dreycen_Foiles)\
**Post date:** [July 5, 2024, 11:24pm UTC](https://discourse.julialang.org/t/nonlinear-least-squares-problems-with-modelingtoolkit/116680/3 "2024-07-05T23:24:44Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 6, 2024, 6:22pm UTC](https://discourse.julialang.org/t/nonlinear-least-squares-problems-with-modelingtoolkit/116680/4 "2024-07-06T18:22:59Z")

</div>

We need to better document it. This works:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Dreycen\_Foiles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dreycen_foiles/32/44744_2.png) [@Dreycen\_Foiles](https://discourse.julialang.org/u/Dreycen_Foiles)\
**Post date:** [July 6, 2024, 9:40pm UTC](https://discourse.julialang.org/t/nonlinear-least-squares-problems-with-modelingtoolkit/116680/5 "2024-07-06T21:40:25Z")

</div>

That works! Thank you for your help.
