# Bounding variables in a NonlinearSystem using MTK

**URL:** https://discourse.julialang.org/t/bounding-variables-in-a-nonlinearsystem-using-mtk/101582
**Category:** Modelling & Simulations
**Tags:** modelingtoolkit
**Created:** [July 13, 2023, 4:27pm UTC](https://discourse.julialang.org/t/bounding-variables-in-a-nonlinearsystem-using-mtk/101582 "2023-07-13T16:27:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![chris-hampel-CA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris-hampel-ca/32/51508_2.png) [@chris-hampel-CA](https://discourse.julialang.org/u/chris-hampel-CA)
#### Post date: [July 13, 2023, 4:27pm UTC](https://discourse.julialang.org/t/bounding-variables-in-a-nonlinearsystem-using-mtk/101582/1 "2023-07-13T16:27:53Z")

</div>

Hello,

I have a general question about using ModelingToolkit when solving a `NonlinearSystem`.

I noticed when building an `OptimizationSystem` you can bound your variables. [Modeling Optimization Problems · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/tutorials/optimization/)  
I was wondering if you could do the same when solving a `NonlinearSystem`. Does anyone know? I tried replicating the syntax shown in the link above in my NL system but it did not appear to work.

My motivation stems from the thought that placing bounds may be useful because my model will fail if certain variables dip below known physical bounds (e.g. temperature cannot be less than zero).

Thanks, Chris

---

<div class="post-metadata">

### Author: ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)
#### Post date: [July 13, 2023, 6:54pm UTC](https://discourse.julialang.org/t/bounding-variables-in-a-nonlinearsystem-using-mtk/101582/2 "2023-07-13T18:54:12Z")

</div>

I think this is possible, but not via the `bounds` syntax. You can replace `x <= 0` with `max(x,0) ~ 0`. However, this is now highly non-linear and difficult to solve. Can you not just use an `OptimizationSystem` directly?

---

<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: [July 13, 2023, 8:05pm UTC](https://discourse.julialang.org/t/bounding-variables-in-a-nonlinearsystem-using-mtk/101582/3 "2023-07-13T20:05:04Z")

</div>

It was already discussed in [How does ModelingToolkit use bounds metadata? - #6 by ChrisRackauckas](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/6)  
And I opened an issue based on @ChrisRackauckas reply here [Consider supporting bounds for `NonlinearProblem` resolution · Issue #183 · SciML/NonlinearSolve.jl · GitHub](https://github.com/SciML/NonlinearSolve.jl/issues/183)

---

<div class="post-metadata">

### Author: ![chris-hampel-CA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris-hampel-ca/32/51508_2.png) [@chris-hampel-CA](https://discourse.julialang.org/u/chris-hampel-CA)
#### Post date: [July 13, 2023, 9:14pm UTC](https://discourse.julialang.org/t/bounding-variables-in-a-nonlinearsystem-using-mtk/101582/4 "2023-07-13T21:14:02Z")

</div>

Sorry I missed that thread that was previously discussed.

Using the `max` method doesn’t sound worthwhile as you pointed out. However, converting the system to an `OptimizationSystem` sounds reasonable. I believe the equations `eqs` in the `NonlinearSystem` would simply become the constraints `cons` in the optimization sys, and the objective function would be left blank, if possible, right? If not, how would you suggest writing the obj func in order to trick the solver to not try to minimize the obj func, and simply solve the system of equations at hand.
