API References

API

LsqFit.curve_fitFunction.
curve_fit(model, xdata, ydata, p0) -> fit

Fit data to a non-linear model. p0 is an initial model parameter guess (see Example). The return object is a composite type (LsqFitResult), with some interesting values:

  • fit.dof : degrees of freedom

  • fit.param : best fit parameters

  • fit.resid : residuals = vector of residuals

  • fit.jacobian : estimated Jacobian at solution

Example

# 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 = range(0, stop=10, length=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)
source