JuliaNLSolvers / Optim.jl

Optimization functions for Julia
Other
1.11k stars 213 forks source link

Would it be possible to add an argument to the optimization function? #1022

Closed juagudeloo closed 1 year ago

juagudeloo commented 1 year ago

I want to define a residual function (resid) that includes the weights (sig) and the data (data), to minimize Chi square (chi_sqr). For example

function resid(x, data, sig, params)

a = params[1]
b = params[2]

model(z) = a*exp(b*z)
residual = data - model(x)
chi_sqr = sum((residual./sig).^2)

return chi_sqr

end

Is there a way to pass sig y data through an argument in the optim function?

longemen3000 commented 1 year ago

you could do:

f0 = x -> resid(x, data, sig, params)
optimize(f0, zeros(2), BFGS())
pkofod commented 1 year ago

Yes, the solution is to use a closure as @longemen3000 shows above. You can input parameters in NLSolvers.jl https://github.com/JuliaNLSolvers/NLSolvers.jl .