I have changed the function dA(x,g)
to compute a vector of 5 elements, one entry for each element of R
. The integration is done element-wise, each element of the vector g->dA(x,g)
is integrated in the range [0,10]
. Optim
then minimizes the norm 2 squared of the vector difference A(x) - A_exp
.
As for your point of a tiny change of starting points giving you different values of x
, the problem is that your integrand depends on the product x[1]*x[2]
: the optimal value remains constant throughout the different runs as you can check. Indeed your problem is univariate.
using QuadGK
using Optim
using PyPlot
A_exp = [1.0, 2, 3, 4, 5]
R = [1.0, 0, 5, 7, 8]
dA(x,g) = exp.(x[1]*R.^2*x[2]*g)
A(x) = quadgk(g -> dA(x,g), 0 , 10)[1]
X2(x) = sum(abs2, A(x) - A_exp)
opt = optimize(X2, [0.01, 0.01])
println(Optim.minimizer(opt))
A_opt = Optim.minimum(opt)