# Add a constraint to a modeling toolkit model

**URL:** <https://discourse.julialang.org/t/add-a-constraint-to-a-modeling-toolkit-model/132553>\
**Category:** General Usage\
**Tags:** modelingtoolkit\
**Created:** [September 22, 2025, 7:30am UTC](https://discourse.julialang.org/t/add-a-constraint-to-a-modeling-toolkit-model/132553 "2025-09-22T07:30:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [September 22, 2025, 7:30am UTC](https://discourse.julialang.org/t/add-a-constraint-to-a-modeling-toolkit-model/132553/1 "2025-09-22T07:30:04Z")

</div>

I have a ModelingToolkit model that is a nonlinear ODE. I have made steady-state, transient and optimization analyses on this model.  
Now I would like to impose an additional constraint on one of the variables of the model, and find a steady-state solution that respects this additional constraint.  
I do not want to rebuild another model from scratch, only to add this constraint for specific solutions.

@ChrisRackauckas seems to provide a solution for that in [SteadyStateProblem solved result problem - #21 by ChrisRackauckas](https://discourse.julialang.org/t/steadystateproblem-solved-result-problem/88511/21), but that forbids using standard ModelingToolkit indexing of solutions and other convenient tools.

Here is an example based on ModelingToolkit’s documentation (maybe not a good one).

```julia-auto
using ModelingToolkit, OrdinaryDiffEq
using ModelingToolkit: t_nounits as t, D_nounits as D

@parameters g
@variables x(t) y(t) [state_priority = 10] λ(t)
eqs = [D(D(x)) ~ λ * x
       D(D(y)) ~ λ * y - g
       x^2 + y^2 ~ 1]
@mtkbuild pend = ODESystem(eqs, t)

```

What I would like to do is to modify `pend` in some fashion to enforce e.g. `lambda = 1` at all times

---

<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:** [October 24, 2025, 12:03pm UTC](https://discourse.julialang.org/t/add-a-constraint-to-a-modeling-toolkit-model/132553/2 "2025-10-24T12:03:20Z")

</div>

> [@BambOoxX](#):
>
> What I would like to do is to modify `pend` in some fashion to enforce e.g. `lambda = 1` at all times

For “at all times” you need to use the dynamic optimization interface. See:

> **[Solving Dynamic Optimization Problems · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/tutorials/dynamic_optimization/)**
>
> Documentation for ModelingToolkit.jl.

On your problem that would be:

```julia-auto
using ModelingToolkit, OrdinaryDiffEq
using ModelingToolkit: t_nounits as t, D_nounits as D

@parameters g
@variables x(t) y(t) [state_priority = 10] λ(t)
eqs = [D(D(x)) ~ λ * x
       D(D(y)) ~ λ * y - g
       x^2 + y^2 ~ 1]
cons = [λ ~ 1]
@mtkbuild pend = ODESystem(eqs, t)

```

Of course in this case you have a trivial equality, in which case you could just make it be an algebraic equation. But if you have too many algebraic equations you’d need to delete a differential equation. For example:

```julia-auto
eqs = [D(D(x)) ~ λ * x
       λ ~ 1
       x^2 + y^2 ~ 1]
cons = [λ ~ 1]
@mtkbuild pend = ODESystem(eqs, t)

```

etc.
