# How can I use optim.jl to find some values

**URL:** <https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317>\
**Category:** Optimization (Mathematical)\
**Tags:** optim\
**Created:** [December 5, 2020, 7:33pm UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317 "2020-12-05T19:33:42Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucas\_Pollito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_pollito/32/14518_2.png) [@Lucas\_Pollito](https://discourse.julialang.org/u/Lucas_Pollito)\
**Post date:** [December 5, 2020, 7:33pm UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/1 "2020-12-05T19:33:42Z")

</div>

I have some working code that i cant optmization to find some x[1] x[2] x[3] x[4] constants.  
A link for all the code is:

[https://drive.google.com/drive/folders/11cqtyUnWAoAYWdWyWOd6fbu4TVejbBlM?usp=sharing](https://drive.google.com/drive/folders/11cqtyUnWAoAYWdWyWOd6fbu4TVejbBlM?usp=sharing)

> > aRe = [1.28; 1.47; 1.67; 1.95; 2.26; 2.38; 2.68; 2.92; 3.16; 3.77; 4.47]  
> > aΩ11e = [2.52; 2.26; 2.07; 1.92; 1.90; 2.08; 2.31; 2.53; 2.68; 2.51; 2.27]
> 
> function dΩ11(x,g,sigma)  
> R = 0.388/sigma  
> T=0.119  
> α = x[3]  
> ϕ = 0.9\*(1-1/(1+(R/x[1])^x[2]))
> 
> ```
> θ(g) = ϕ*exp(-x[3]*g^2/T)
> γ = x[4]
> 
> g^5*θ(g)*Qb(g,sigma,α)*exp(-g^2/T) + g^5*(1-θ(g))*γ*Qa(g,sigma)*exp(-g^2/T) +
> g^5*(1-θ(g))*(1-γ)*Qc(g,sigma)*exp(-g^2/T)
> 
> ```
> 
> end
> 
> function Ω11(x,sigma)
> 
> ```
> 1/T^3*quadgk(g -> dΩ11(x,g,sigma) ,0.01 , 1.51, atol=1e-3)[1]
> 
> ```
> 
> end

> function X2(x)  
> soma = 0  
> for (i,sigma) in enumerate(aσ)
> 
> ```
> soma = soma + (Ω11(x,sigma)-aΩ11e[i])^2
> end
> soma
> 
> ```
> 
> end
> 
> lower = [1.0; 0 ; 0.0 ; 0.7]  
> upper = [5.0; 30.0; 1.0 ; 1.0]  
> initial\_x = [3; 20; 0.5 ; 1.0]  
> opt = optimize(x-\>X2(x), initial\_x, lower, upper,NelderMead())  
> x = Optim.minimizer( opt )  
> Optim.minimum( opt )

Is there any error in the code ?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 5, 2020, 9:45pm UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/2 "2020-12-05T21:45:50Z")

</div>

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

Please read this post. That will facilitate everyone to help you.

---

<div class="post-metadata">

**Author:** ![thisrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisrod/32/10642_2.png) [@thisrod](https://discourse.julialang.org/u/thisrod)\
**Post date:** [December 7, 2020, 3:25am UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/3 "2020-12-07T03:25:30Z")

</div>

The template that I use for this kind of request, and for bug reports, is:

1. What I did

2. What I expected to happen when I did that

3. What actually happened

You’ve told us 1. To help you, we need to know 2 and 3 as well. Did Optim throw an error? Does this function have a known minimum that Optim didn’t find? Or is there some other reason that you suspect there is an error in the code?

---

<div class="post-metadata">

**Author:** ![Lucas\_Pollito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_pollito/32/14518_2.png) [@Lucas\_Pollito](https://discourse.julialang.org/u/Lucas_Pollito)\
**Post date:** [December 7, 2020, 5:34pm UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/4 "2020-12-07T17:34:51Z")

</div>

I have a range for alpha, but i use lower and upper values in that range, but optim do not respect it …  
i will upgrade the code

---

<div class="post-metadata">

**Author:** ![thisrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisrod/32/10642_2.png) [@thisrod](https://discourse.julialang.org/u/thisrod)\
**Post date:** [December 7, 2020, 11:28pm UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/5 "2020-12-07T23:28:25Z")

</div>

Oh right. I think you’re trying to optimise a multivariate function, but using the syntax for a univariate one. Here’s the relevant part of [the manual](https://julianlsolvers.github.io/Optim.jl/stable/#user/minimization/#box-constrained-optimization). I haven’t used `Fminbox` myself, and I don’t know if it works with Nelder-Mead.

---

<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:** [December 9, 2020, 2:10am UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/6 "2020-12-09T02:10:16Z")

</div>

yes, it works … but i do not know what happening because the lower and upper limits does not be respect by the program

---

<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:** [December 9, 2020, 2:10am UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/7 "2020-12-09T02:10:49Z")

</div>

try to run and see it

---

<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:** [December 9, 2020, 2:17am UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/8 "2020-12-09T02:17:00Z")

</div>

![1](https://global.discourse-cdn.com/julialang/original/3X/8/f/8f7062fab63ed85adfbe12c6d0dbb9f6b04a8f18.jpeg)

---

<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:** [December 9, 2020, 4:13am UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/9 "2020-12-09T04:13:39Z")

</div>

Please post code (complete and working that people can copy and paste), not screenshots.

---

<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:** [December 9, 2020, 1:05pm UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/10 "2020-12-09T13:05:26Z")

</div>

Hello man … i cannot post my entire code because needs a .dat files …  
a link from google drive in the beggining have my entire code.  
i use interpolate, so i have .dat files

can you know how to solve it ?

---

<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:** [December 9, 2020, 2:42pm UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/11 "2020-12-09T14:42:59Z")

</div>

I am just guessing but it seems that you did not use the correct syntax for `Fminbox`, which should be  
`Fminbox(GradientDescent())`, not `Fminbox{GradientDescent}()`

---

<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:** [December 10, 2020, 2:46pm UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/12 "2020-12-10T14:46:52Z")

</div>

even if i take off the FminBox, leave only the lower and upper, the algoritm fails to respect the limits

---

<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:** [December 10, 2020, 2:50pm UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/13 "2020-12-10T14:50:40Z")

</div>

what are this error ?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/b/eb6229da26d55002df21033c5f294b3565a15ed7.png)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 10, 2020, 3:22pm UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/14 "2020-12-10T15:22:32Z")

</div>

It looks like you are confusing the argument order in your `optimize` call - the example given in the docs is

```julia
inner_optimizer = GradientDescent()
results = optimize(f, g!, lower, upper, initial_x, Fminbox(inner_optimizer))

```

(note the order of lower, upper, and initial in comparison to your call)

---

<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:** [December 10, 2020, 9:41pm UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/15 "2020-12-10T21:41:45Z")

</div>

two questions :

1. why cannot use NelderMead method ?

2. in my x[3] value i cannot put 0.0 , but my range include the 0.0 value … why the optim do not respect that?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/e/5e11ba08e5055f660672ac9af06953089a878498.png)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 11, 2020, 5:33am UTC](https://discourse.julialang.org/t/how-can-i-use-optim-jl-to-find-some-values/51317/16 "2020-12-11T05:33:31Z")

</div>

As mentioned by David above it’s very hard to help you without an MWE, please consider the advice in [Please read: make it easier to help you - #81](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/81)
