Description
Discussed in https://github.com/orgs/SciML/discussions/21
Originally posted by andrewkhardy March 10, 2025
I am attempting to fit a correlation function. I found a great example of using NonLinearLeastSquaresProblem
, which I'll repeat here
using NonlinearSolve, Plots
function curve_fit(model, u0, xdata, ydata, p)
data = (xdata, ydata, p)
function lossfn!(du, u, data)
(xs, ys, p) = data
du .= model.(xs, Ref(u), Ref(p)) .- ys
return nothing
end
prob = NonlinearLeastSquaresProblem(
NonlinearFunction(lossfn!, resid_prototype = similar(ydata)), u0, data)
sol = solve(prob)
u = sol.u
fit = model.(xdata, Ref(u), Ref(p))
return (;sol, fit)
end
The problem is that I do not know how to give constraints to the allowed parameters to search over ( or fix one of the parameters if I know if for example).
In LsqFit.jl
, this is accomplished by a upper
and lower
keyword. I'm wondering if there's anything similar?
BTW: The form hopefully doesn't matter, but including it for completion. The issue is that there is finite frequency data I am trying to capture and a least-squares fit erases that. If there's a suggestion for a better way to be searching, then I'd be very appreciative.