# Using optim.jl - how can i set an interval for x\[1\] , for example

**URL:** <https://discourse.julialang.org/t/using-optim-jl-how-can-i-set-an-interval-for-x-1-for-example/6949>\
**Category:** New to Julia\
**Created:** [November 8, 2017, 2:43am UTC](https://discourse.julialang.org/t/using-optim-jl-how-can-i-set-an-interval-for-x-1-for-example/6949 "2017-11-08T02:43:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![leaoshark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leaoshark/32/11462_2.png) [@leaoshark](https://discourse.julialang.org/u/leaoshark)\
**Post date:** [November 8, 2017, 2:43am UTC](https://discourse.julialang.org/t/using-optim-jl-how-can-i-set-an-interval-for-x-1-for-example/6949/1 "2017-11-08T02:43:10Z")

</div>

Hello.

using optim.jl in julia ( using ATOM ide )

opt = optimize(function, [starting\_guess] )

suposse my starting guess is [1 ; 2] for x[1] and x[2] … the program will find the best x[1] and x[2] to fit my data …

but if i want to tell julia to find x[1] in a range of ( 0 , 2 ) and x[2] in a range of ( 1 , 5 ) which best fit my data ?

is it possble using optim ? or Nlopt ?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [November 8, 2017, 3:24am UTC](https://discourse.julialang.org/t/using-optim-jl-how-can-i-set-an-interval-for-x-1-for-example/6949/2 "2017-11-08T03:24:28Z")

</div>

Using [Optim](http://julianlsolvers.github.io/Optim.jl/stable/user/minimization/#box-minimization) and [NLOpt](https://github.com/JuliaOpt/NLopt.jl#bound-constraints).

Questions like these can be answered with 30 seconds of Googling–it is often best to save the community’s goodwill for when you’re truly stuck.

---

<div class="post-metadata">

**Author:** ![leaoshark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leaoshark/32/11462_2.png) [@leaoshark](https://discourse.julialang.org/u/leaoshark)\
**Post date:** [November 8, 2017, 7:17am UTC](https://discourse.julialang.org/t/using-optim-jl-how-can-i-set-an-interval-for-x-1-for-example/6949/3 "2017-11-08T07:17:30Z")

</div>

none of this documentations has an example using a range of starting guess instead of a unique point

but thanks …

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [November 8, 2017, 7:54am UTC](https://discourse.julialang.org/t/using-optim-jl-how-can-i-set-an-interval-for-x-1-for-example/6949/4 "2017-11-08T07:54:09Z")

</div>

Have you actually read [Optim](http://julianlsolvers.github.io/Optim.jl/stable/user/minimization/#box-minimization)’s documentation? The first line says: “…for simple “box” constraints (lower and upper bounds)…”.  
Box constrains means setting an interval on each variable independently of the others.

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [November 8, 2017, 9:14am UTC](https://discourse.julialang.org/t/using-optim-jl-how-can-i-set-an-interval-for-x-1-for-example/6949/5 "2017-11-08T09:14:49Z")

</div>

People have been interpreting your question as optimization over a bounded parameter space. If you are asking how to use multiple start values, then just embed the call to optimize in a loop, and generate the trial start values in the loop.

---

<div class="post-metadata">

**Author:** ![leaoshark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leaoshark/32/11462_2.png) [@leaoshark](https://discourse.julialang.org/u/leaoshark)\
**Post date:** [November 8, 2017, 10:29pm UTC](https://discourse.julialang.org/t/using-optim-jl-how-can-i-set-an-interval-for-x-1-for-example/6949/6 "2017-11-08T22:29:59Z")

</div>

look what i did 🙂  
 ![optimjulia](https://global.discourse-cdn.com/julialang/original/3X/0/2/02c8445a64d2471dcb8a3cdcc3a64d04faa8c351.jpeg)

and look the results …

do you see the negative value ? why it happens ?

my lower and upper value do not be negative, in the case of x[3]

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [November 9, 2017, 12:58am UTC](https://discourse.julialang.org/t/using-optim-jl-how-can-i-set-an-interval-for-x-1-for-example/6949/7 "2017-11-09T00:58:39Z")

</div>

Please post a minimal working example, i.e. Complete code that people can copy and paste to run it, instead of screenshots.

In this case, for example, you also need to post the definition of the function X2.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [November 9, 2017, 11:39am UTC](https://discourse.julialang.org/t/using-optim-jl-how-can-i-set-an-interval-for-x-1-for-example/6949/8 "2017-11-09T11:39:42Z")

</div>

I am not sure about the details but I think `GradientDescent` needs the objective function gradient which will be computed numerically [1] if you don’t provide it. If your `X2` still contains a numerical integration routine, it may compute a wrong gradient.

But please post a **minimal** (20 lines at most) **working** example if you want help.

[1] From [the manual](http://julianlsolvers.github.io/Optim.jl/stable/user/minimization/#minimizing-a-multivariate-function):

> If we pass `f` alone, Optim will construct an approximate gradient for us using central finite differencing

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [October 1, 2020, 2:02pm UTC](https://discourse.julialang.org/t/using-optim-jl-how-can-i-set-an-interval-for-x-1-for-example/6949/9 "2020-10-01T14:02:02Z")

</div>

Is this problem solved? I am having similar issues

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [October 2, 2020, 7:33am UTC](https://discourse.julialang.org/t/using-optim-jl-how-can-i-set-an-interval-for-x-1-for-example/6949/10 "2020-10-02T07:33:38Z")

</div>

Are you talking about using box constraints or these constraints not being fulfilled? If the former then check the links in the top, if the latter I think we might need to see a MWE (minimal working example) to check out what’s going on.

Edit: nevermind, I just saw you already got some help in another thread.
