# Does LsqFit have constraint interval?

**URL:** <https://discourse.julialang.org/t/does-lsqfit-have-constraint-interval/12300>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 10, 2018, 5:25pm UTC](https://discourse.julialang.org/t/does-lsqfit-have-constraint-interval/12300 "2018-07-10T17:25:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [July 10, 2018, 5:25pm UTC](https://discourse.julialang.org/t/does-lsqfit-have-constraint-interval/12300/1 "2018-07-10T17:25:27Z")

</div>

Hello everyone

I would like to know if I could specify a range for parameters when I perform a non-linear fit ?

For example, how to run something similar to

`curve_fit(model, x, y, p, ?)`

in such way that parameter p[1] must be between [1,2] and p[2] between [0,0.5]

or is there any package that compute non-linear fit and also does this for me ?

Thank you

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [July 10, 2018, 5:43pm UTC](https://discourse.julialang.org/t/does-lsqfit-have-constraint-interval/12300/2 "2018-07-10T17:43:47Z")

</div>

What you can always do is use Optim.jl (or BlackBoxOptim.jl if your problem is hard) and add bounds in your error function, I usually define my penalties like this:

```
lower_bound(p,lb,α) = p < lb ? exp(α*(lb-p))-1 : 0.0

```

It’s not the most elegant or efficient solution, but usually it works well enough.

---

<div class="post-metadata">

**Author:** ![platawiec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/platawiec/32/31914_2.png) [@platawiec](https://discourse.julialang.org/u/platawiec)\
**Post date:** [July 10, 2018, 5:48pm UTC](https://discourse.julialang.org/t/does-lsqfit-have-constraint-interval/12300/3 "2018-07-10T17:48:21Z")

</div>

You can use the `lower` and `upper` keywords. In your example:

```julia
lower_p = [1.0; 0.0]
upper_p = [2.0; 0.5]
curve_fit(model, x, y, p; lower = lower_p, upper = upper_p)

```

I’m on `julia v0.6.2` and `LsqFit 0.3.0`.
