Search code examples
julialeast-squares

Solver for non-linear least squares with boundary constraints


I'm looking for an analog to Matlab's lsqnonlin function in Julia.

LsqFit.jl looks great, but doesn't accept the same arguments Matlab's implementation does; specifically:

  • Lower bounds
  • Upper bounds
  • Initial conditions

where initial conditions, lower, and upper bounds are vectors of length 6.

Any advice would be awesome. Thanks!


Solution

  • Actually, it does, it's just not explained in the readme (for good measure, here is a stable link README.md).

    It is unclear what you mean by initial conditions. If you mean initial parameters, this is very much possible.

    using LsqFit
    
    # a two-parameter exponential model
    # x: array of independent variables
    # p: array of model parameters
    model(x, p) = p[1]*exp.(-x.*p[2])
    
    # some example data
    # xdata: independent variables
    # ydata: dependent variable
    xdata = linspace(0,10,20)
    ydata = model(xdata, [1.0 2.0]) + 0.01*randn(length(xdata))
    p0 = [0.5, 0.5]
    
    fit = curve_fit(model, xdata, ydata, p0)
    

    (taken from the manual). Here p0 is the initial parameter vector.

    This will give you something very close to [1.0, 2.0]. But what if we want to constrain the parameter to be in [0,1]x[0,1]? Then we simply set the keyword arguments lower and upper to be vectors of lower and upper bounds

    fit = curve_fit(model, xdata, ydata, p0; lower = zeros(2), upper = ones(2))
    

    That should give something like [1.0, 1.0] depending on your exact data.