# How to pass constraints in NLsolve?

**URL:** <https://discourse.julialang.org/t/how-to-pass-constraints-in-nlsolve/60219>\
**Category:** Numerics\
**Created:** [April 29, 2021, 5:11am UTC](https://discourse.julialang.org/t/how-to-pass-constraints-in-nlsolve/60219 "2021-04-29T05:11:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [April 29, 2021, 5:11am UTC](https://discourse.julialang.org/t/how-to-pass-constraints-in-nlsolve/60219/1 "2021-04-29T05:11:34Z")

</div>

I am solving 2-variable system of first order conditions for an optimization problem with `NLsolve`. I know that the optimal values of the optimization problem satisfy some conditions. For example, the optimal values should satisfy T\_1 = H\_1^{-1}(K\_1, x\_1) \< T\_2 \leq H\_2^{-1}(K\_2/\delta, x\_2). How can I supply that information on the inequalities to `nlsolve`? I want to enforce that T\_1 \< T\_2 and also ideally speed up the computation of T\_2 by providing bounds on it.

To provide a starting point, consider:

```julia
function foc!(res, t; x=x, K1=K1, K2=K2, θ=θ, δ=δ)
    x1 = x[1]
    x2 = x[2]
    t1 = t[1]
    t2 = t[2]
   
    res[1] = K1 - Hi(1, t1, x1; θ=θ) # foc wrt t1
    res[2] = Hi(1, t2, x1; θ=θ)*(1 - δ)*termII(t, x2; θ=θ, K2=K2) + # foc wrt t2
            termI(t, x1; θ=θ, K1=K1) * (K2- Hi(2, t2, x2; θ=θ)*δ)
end

```

I would then call `nlsolve(foc!, t0)`, so I suppose I’d have to somehow include those constraints in the definition of `foc!`, how?

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [April 30, 2021, 7:03am UTC](https://discourse.julialang.org/t/how-to-pass-constraints-in-nlsolve/60219/2 "2021-04-30T07:03:33Z")

</div>

I’m not an expert on this!  
As far as I know, NLsolve doesn’t seem to support nonlinear constraints. (Ok, as a hack, you could define your own Jacobian and maybe the solvers will then never go into the infeasible regions. But, I think that comes with the risk of messing up the methods as they are not supposed to be used in that way.)

Have you considered trying another package?

(I’m not sure which one is best for you, maybe JuMP? [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/v0.21.7/manual/nlp/) )

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [April 30, 2021, 7:06am UTC](https://discourse.julialang.org/t/how-to-pass-constraints-in-nlsolve/60219/3 "2021-04-30T07:06:47Z")

</div>

> [@SteffenPL](#):
>
> Have you considered trying another package?
> 
> (I’m not sure which one is best for you, maybe JuMP? [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/v0.21.7/manual/nlp/) )

I am afraid that solving it through `JuMP` may be slower. Remember that I will have to solve the same problem many times. I guess I can try it at the worst.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [April 30, 2021, 7:13am UTC](https://discourse.julialang.org/t/how-to-pass-constraints-in-nlsolve/60219/4 "2021-04-30T07:13:48Z")

</div>

How about [GitHub - JuliaOpt/NLopt.jl: Package to call the NLopt nonlinear-optimization library from the Julia language](https://github.com/JuliaOpt/NLopt.jl) or [https://julianlsolvers.github.io/Optim.jl/stable/#examples/generated/ipnewton\_basics/#\_top](https://julianlsolvers.github.io/Optim.jl/stable/#examples/generated/ipnewton_basics/#_top)

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [April 30, 2021, 7:15am UTC](https://discourse.julialang.org/t/how-to-pass-constraints-in-nlsolve/60219/5 "2021-04-30T07:15:25Z")

</div>

Yes, I’m trying `NLopt` now.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [April 30, 2021, 10:49am UTC](https://discourse.julialang.org/t/how-to-pass-constraints-in-nlsolve/60219/6 "2021-04-30T10:49:50Z")

</div>

Actually, the solution for `t1` is independent of the solution for `t2`, so I can use the (faster?) univariate methods in `Roots.jl`. Then I can find `t2` with the same univariate approach.
