Skip to content

Constrained Parameters on Nonlinear Least Squares fitting #575

Open
@ChrisRackauckas

Description

@ChrisRackauckas

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.

$$ \langle n(r) n(0)\rangle = \frac{K_\rho}{(\pi r)^2} + A_1 \frac{\cos \left(2 k_F r\right)}{r^{1+K_\rho}} \ln ^{-3 / 2}(r) + A_2 \cos \left(4 k_F r\right) r^{-4 K_\rho} $$

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions