Skip to main content

curve_fit

curve_fit(const List x, const List y, const Function<real(List,List)> f, Map options) -> Map

Fits the input data xx,yy to a user specified function f(x,p)f(x,p) with parameters pp.

Performs least squares fitting on an input funtion to input data. The funciton must be of a single variable xx, this function does not currently support higher dimensional curve fitting. The default algorithm used to minimize the erorr is Nelder-Mead.

Parameters

  • x: The input data corresponding to the x data for the fit.
  • y: The input data corresponding to the y data for the fit.
  • f: The objective function to minimize. Must be of the form f(x,p)f(x,p) where xx is a real number and pp is a list of real parameters.
  • options: Map containing optimization settings.
    • p_init (List<real>): initial values of parameters.
    • lb (List<real>): lower bounds of parameters.
    • ub (List<real>): upper bounds of parameters.
    • xtol (real): numerical error tolerance of the solution (default 1e-6).

Returns

Map containing optimization results.

Example

// Curve fit to -2.32x + 0.23
def myfunc(List x, List p)
{
return real(p[0]*x[0] + p[1])
}

var options = ["lb": [-10.0,-10.0],
"ub": [5.0,5.0],
"p_init": [0.2,0.1],
"xtol" : 1e-4]

var x = [1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0]
var y = [2.09,4.41,6.73,9.05,11.37,13.69,16.01,18.33,20.65,22.97]

var result = curve_fit(x,y,myfunc,options)

print(result)