# MTK : equations, unkowns and initial cond different lengths

**URL:** <https://discourse.julialang.org/t/mtk-equations-unkowns-and-initial-cond-different-lengths/129642>\
**Category:** Modelling & Simulations\
**Created:** [June 4, 2025, 1:42pm UTC](https://discourse.julialang.org/t/mtk-equations-unkowns-and-initial-cond-different-lengths/129642 "2025-06-04T13:42:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)\
**Post date:** [June 4, 2025, 1:42pm UTC](https://discourse.julialang.org/t/mtk-equations-unkowns-and-initial-cond-different-lengths/129642/1 "2025-06-04T13:42:37Z")

</div>

Hello,

I am playing with a toy problem and I have a query with internal equation generation.

The following will fail

```julia

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

@component function my_func(;name)
    para = @parameters begin
        p(t)
    end
    vars = @variables begin
        x(t),
        y(t)
    end
    eqs = [
        x^2 ~ y^2 - p^2,
    ]
    return System(eqs,t,vars,para,name=name)
end

@named sys = my_func()
sys_ = mtkcompile(sys,fully_determined = false,simplify = true)

u0 = [sys_.y => 1.0,sys_.x => 0]
para = [sys_.p => 0]

prob = NonlinearProblem(sys_,merge(Dict(u0),Dict(para)),guesses = [sys_.y => 1.0,sys_.x => 0])

```

This is because

```julia
ERROR: LoadError: ArgumentError: Equations (1), unknowns (2), and initial conditions (2) are of different lengths.

```

Now to solve this I provide the same equation again to the system and it works. As follows:

```julia
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

@component function my_func(;name)
    para = @parameters begin
        p(t)
    end
    vars = @variables begin
        x(t),
        y(t)
    end
    eqs = [
        x^2 ~ y^2 - p^2,
        x^2 - y^2 ~ -p^2
    ]
    return System(eqs,t,vars,para,name=name)
end

@named sys = my_func()
sys_ = mtkcompile(sys,fully_determined = false,simplify = true)

u0 = [sys_.y => 1.0,sys_.x => 0]
para = [sys_.p => 0]

prob = NonlinearProblem(sys_,merge(Dict(u0),Dict(para)),guesses = [sys_.y => 1.0,sys_.x => 0])
using NonlinearSolve
sol = solve(prob,NewtonRaphson())

```

This works returning the expected solution

```julia
retcode: Success
u: 2-element Vector{Float64}:
 0.0
 4.76837158203125e-7

```

Is there a way without giving the second equation? I know there are infinite solutions to this problem but for a fix `y` there are at most 2 solutions for `x`.

There will be cases in many applications where there will be way more than 2 unknows so providing the remaining trivial equations seems a bit inefficient.

Thank you

---

<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:** [June 4, 2025, 2:47pm UTC](https://discourse.julialang.org/t/mtk-equations-unkowns-and-initial-cond-different-lengths/129642/2 "2025-06-04T14:47:00Z")

</div>

A NonlinearProblem requires a consistent system. You can use NonlinearLeastSquaresProblem instead for over and underdetermined systems, i.e.

```julia
prob = NonlinearLeastSquaresProblem(sys_,merge(Dict(u0),Dict(para)),guesses = [sys_.y => 1.0,sys_.x => 0])
using NonlinearSolve
sol = solve(prob)

```

should work fine.

---

<div class="post-metadata">

**Author:** ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)\
**Post date:** [June 5, 2025, 8:39am UTC](https://discourse.julialang.org/t/mtk-equations-unkowns-and-initial-cond-different-lengths/129642/3 "2025-06-05T08:39:32Z")

</div>

Upon following the suggestion I get the following error:

```julia
ERROR: `NonlinearFunction` requires a time-independent system.

To disable this check, pass `check_compatibility = false`.

```

It persists even when I pass `check_compatibility = false`.

I am using the following versions:

```julia
  [961ee093] ModelingToolkit v10.1.0
  [8913a72c] NonlinearSolve v4.9.0

```

Here is the code block for reference:

```julia
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

@component function my_func(;name)
    para = @parameters begin
        p(t)
    end
    vars = @variables begin
        x(t),
        y(t)
    end
    eqs = [
        x^2 ~ y^2 - p^2,

    ]
    return System(eqs,t,vars,para,name=name)
end

@named sys = my_func()
sys_ = mtkcompile(sys,fully_determined = false)

u0 = []
para = [sys_.p => 2]

using NonlinearSolve
prob = NonlinearLeastSquaresProblem(sys_,merge(Dict(u0),Dict(para)),guesses = [sys_.y => 1.0,sys_.x => 0],check_compatibility = false)
sol = solve(prob)

```

---

<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:** [June 5, 2025, 9:54am UTC](https://discourse.julialang.org/t/mtk-equations-unkowns-and-initial-cond-different-lengths/129642/4 "2025-06-05T09:54:24Z")

</div>

@cryptic.ax issue with the new MTK v10? We should allow this as an alternative steady state definition.

But for now, removing the t-dependence is fine.

```julia
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

@component function my_func(;name)
    para = @parameters begin
        p
    end
    vars = @variables begin
        x,
        y
    end
    eqs = [
        x^2 ~ y^2 - p^2,
    ]
    return System(eqs,vars,para,name=name)
end

@named sys = my_func()
sys_ = mtkcompile(sys,fully_determined = false)

using NonlinearSolve
prob = NonlinearLeastSquaresProblem(sys_,[sys_.y => 1.0,sys_.x => 0, sys_.p => 2])
sol = solve(prob)

```

---

<div class="post-metadata">

**Author:** ![cryptic.ax](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cryptic.ax/32/220540_2.png) [@cryptic.ax](https://discourse.julialang.org/u/cryptic.ax)\
**Post date:** [June 5, 2025, 10:14am UTC](https://discourse.julialang.org/t/mtk-equations-unkowns-and-initial-cond-different-lengths/129642/5 "2025-06-05T10:14:54Z")

</div>

We have `NonlinearSystem(sys::System)` for this reason - it converts the time-dependent `sys` into the time-independent steady-state version.

---

<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:** [June 5, 2025, 10:24am UTC](https://discourse.julialang.org/t/mtk-equations-unkowns-and-initial-cond-different-lengths/129642/6 "2025-06-05T10:24:13Z")

</div>

Okay that works too

---

<div class="post-metadata">

**Author:** ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)\
**Post date:** [June 5, 2025, 11:29am UTC](https://discourse.julialang.org/t/mtk-equations-unkowns-and-initial-cond-different-lengths/129642/7 "2025-06-05T11:29:39Z")

</div>

If I understand this correctly, if I have a time dependent system after structural simplification I need to do one more step of using `NonlinearSystem` and pass that to `NonlinearLeastSquaresProblem`?

---

<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:** [June 6, 2025, 6:53am UTC](https://discourse.julialang.org/t/mtk-equations-unkowns-and-initial-cond-different-lengths/129642/8 "2025-06-06T06:53:13Z")

</div>

Yes we should document this.
