lsqcurvefit

Find the equation parameters that produce the least squares best fit to a data set.

Syntax

x = lsqcurvefit(@func,x0,xdata,ydata)

x = lsqcurvefit(@func,x0,xdata,ydata,lb,ub)

x = lsqcurvefit(@func,x0,xdata,ydata,lb,ub,options)

[x,resnorm,residual,exitflag,output] = lsqcurvefit(...)

Inputs

func
The function of system residuals. See the optimset option Jacobian for details.
x0
An estimate of the best fit parameters.
xdata
The domain values for which the best fit is performed. If the domain is multivariate then each variable is stored in xdata by column.
ydata
The range values for which the best fit is performed.
lb
The fitting parameter lower bounds.
Use [ ] or -inf values if needed to specify unbounded conditions.
ub
The fitting parameter upper bounds.
Use [ ] or inf values if needed to specify unbounded conditions.
options
A struct containing option settings.
See optimset for details.

Outputs

x
The best fit parameters.
resnorm
The squared length of the residuals vector.
residual
The residuals vector.
info
The convergence status flag.
info = 4
Relative step size converged to within tolX.
info = 3
Relative function value converged to within tolFun.
info = 2
Step size converged to within tolX.
info = 1
Function value converged to within tolFun.
info = 0
Reached maximum number of iterations or function calls, or the algorithm aborted because it was not converging.
info = -3
Trust region became too small to continue.
output
A struct containing iteration details. The members are as follows:
iterations
The number of iterations.
nfev
The number of function evaluations.
xiter
The candidate solution at each iteration.
resnormiter
The objective function value at each iteration.

Examples

Fit an exponential curve to the data provided.
function y = FittingFunc(p, x)
    y = p(1) * exp(-p(2)*x);
end

x = [1; 2; 3; 4];
y = [8.025, 3.975, 2.025, 0.975];
p0 = [15; 1];
[p,res] = lsqcurvefit(@FittingFunc,p0,x,y)
p = [Matrix] 2 x 1
16.09850
 0.69669
res = 0.00190995097
Modify the first example to pass an extra parameter to the system function using a function handle.
function y = FittingFunc(p, x, offset)
    y = p(1) * exp(-p(2)*x) + offset;
end

handle = @(x, p) FittingFunc(x, p, 2);
[p,res] = lsqcurvefit(handle,p0,x,y+2)
p = [Matrix] 2 x 1
16.09850
 0.69669
res = 0.00190995097
Modify the first example to include bounds constraints.
[p,res] = lsqcurvefit(@FittingFunc,p0,x,y,[10,0.7],[16.2,2])
p = [Matrix] 2 x 1
16.16859
 0.70000
resnorm = 0.00226086103

Comments

lsqcurvefit uses a modified Gauss-Netwon algorithm with a trust region method. Bounds are supported with the addition of an affine scaling method drawn from the following sources.

Options for convergence tolerance controls and analytical derivatives are specified with optimset.

To pass additional parameters to a function argument, use an anonymous function.

When using lb or ub with unbounded variables, use or +/-inf rather than arbitrarily large +/- numeric values. Large numeric bound ranges will degrade the performance of the function.

The optimset options and defaults are as follows:
  • MaxIter: 400
  • MaxFunEvals: 1,000,000
  • TolFun: 1.0e-7
  • TolX: 1.0e-7
  • Jacobian: 'off'
  • Display: 'off'