How can I use optim.jl to find some values

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:

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
α = 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) +


function Ω11(x,sigma)

1/T^3*quadgk(g -> dΩ11(x,g,sigma) ,0.01 , 1.51, atol=1e-3)[1]


function X2(x)
soma = 0
for (i,sigma) in enumerate(aσ)

    soma = soma + (Ω11(x,sigma)-aΩ11e[i])^2


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 ?

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


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?


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

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. I haven’t used Fminbox myself, and I don’t know if it works with Nelder-Mead.

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

try to run and see it

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

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 ?

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}()

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

what are this error ?

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

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)

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?

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

1 Like