# lsqcurvefit

Solve nonlinear curve-fitting (data-fitting) problems in least-squares sense

## Description

Nonlinear least-squares solver

Find coefficients x that solve the problem

$\underset{x}{\mathrm{min}}{‖F\left(x,xdata\right)-ydata‖}_{2}^{2}=\underset{x}{\mathrm{min}}\sum _{i}{\left(F\left(x,xdat{a}_{i}\right)-ydat{a}_{i}\right)}^{2},$

given input data xdata, and the observed output ydata, where xdata and ydata are matrices or vectors, and F (x, xdata) is a matrix-valued or vector-valued function of the same size as ydata.

Optionally, the components of x can have lower and upper bounds lb, and ub. The arguments x, lb, and ub can be vectors or matrices; see Matrix Arguments.

The lsqcurvefit function uses the same algorithm as lsqnonlin. lsqcurvefit simply provides a convenient interface for data-fitting problems.

Rather than compute the sum of squares, lsqcurvefit requires the user-defined function to compute the vector-valued function

$F\left(x,xdata\right)=\left[\begin{array}{c}F\left(x,xdata\left(1\right)\right)\\ F\left(x,xdata\left(2\right)\right)\\ ⋮\\ F\left(x,xdata\left(k\right)\right)\end{array}\right].$

example

x = lsqcurvefit(fun,x0,xdata,ydata) starts at x0 and finds coefficients x to best fit the nonlinear function fun(x,xdata) to the data ydata (in the least-squares sense). ydata must be the same size as the vector (or matrix) F returned by fun.

Note

Passing Extra Parameters explains how to pass extra parameters to the vector function fun(x), if necessary.

example

x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub) defines a set of lower and upper bounds on the design variables in x, so that the solution is always in the range lb  x  ub. You can fix the solution component x(i) by specifying lb(i) = ub(i).

Note

If the specified input bounds for a problem are inconsistent, the output x is x0 and the outputs resnorm and residual are [].

Components of x0 that violate the bounds lb ≤ x ≤ ub are reset to the interior of the box defined by the bounds. Components that respect the bounds are not changed.

example

x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options) minimizes with the optimization options specified in options. Use optimoptions to set these options. Pass empty matrices for lb and ub if no bounds exist.

x = lsqcurvefit(problem) finds the minimum for problem, a structure described in problem.

[x,resnorm] = lsqcurvefit(___), for any input arguments, returns the value of the squared 2-norm of the residual at x: sum((fun(x,xdata)-ydata).^2).

example

[x,resnorm,residual,exitflag,output] = lsqcurvefit(___) additionally returns the value of the residual fun(x,xdata)-ydata at the solution x, a value exitflag that describes the exit condition, and a structure output that contains information about the optimization process.

[x,resnorm,residual,exitflag,output,lambda,jacobian] = lsqcurvefit(___) additionally returns a structure lambda whose fields contain the Lagrange multipliers at the solution x, and the Jacobian of fun at the solution x.

## Examples

collapse all

Suppose that you have observation time data xdata and observed response data ydata, and you want to find parameters $x\left(1\right)$ and $x\left(2\right)$ to fit a model of the form

$\text{ydata}=x\left(1\right)\mathrm{exp}\left(x\left(2\right)\text{xdata}\right).$

Input the observation times and responses.

xdata = ...
[0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
ydata = ...
[455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];

Create a simple exponential decay model.

fun = @(x,xdata)x(1)*exp(x(2)*xdata);

Fit the model using the starting point x0 = [100,-1].

x0 = [100,-1];
x = lsqcurvefit(fun,x0,xdata,ydata)
Local minimum possible.

lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the value of the function tolerance.
x = 1×2

498.8309   -0.1013

Plot the data and the fitted curve.

times = linspace(xdata(1),xdata(end));
plot(xdata,ydata,'ko',times,fun(x,times),'b-')
legend('Data','Fitted exponential')
title('Data and Fitted Curve')

Find the best exponential fit to data where the fitting parameters are constrained.

Generate data from an exponential decay model plus noise. The model is

$y=\mathrm{exp}\left(-1.3t\right)+\epsilon ,$

with $t$ ranging from 0 through 3, and $\epsilon$ normally distributed noise with mean 0 and standard deviation 0.05.

rng default % for reproducibility
xdata = linspace(0,3);
ydata = exp(-1.3*xdata) + 0.05*randn(size(xdata));

The problem is: given the data (xdata, ydata), find the exponential decay model $y=x\left(1\right)\mathrm{exp}\left(x\left(2\right)\text{xdata}\right)$ that best fits the data, with the parameters bounded as follows:

$0\le x\left(1\right)\le 3/4$

$-2\le x\left(2\right)\le -1.$

lb = [0,-2];
ub = [3/4,-1];

Create the model.

fun = @(x,xdata)x(1)*exp(x(2)*xdata);

Create an initial guess.

x0 = [1/2,-2];

Solve the bounded fitting problem.

x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
Local minimum found.

Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
x = 1×2

0.7500   -1.0000

Examine how well the resulting curve fits the data. Because the bounds keep the solution away from the true values, the fit is mediocre.

plot(xdata,ydata,'ko',xdata,fun(x,xdata),'b-')
legend('Data','Fitted exponential')
title('Data and Fitted Curve')

Compare the results of fitting with the default 'trust-region-reflective' algorithm and the 'levenberg-marquardt' algorithm.

Suppose that you have observation time data xdata and observed response data ydata, and you want to find parameters $x\left(1\right)$ and $x\left(2\right)$ to fit a model of the form

$\text{ydata}=x\left(1\right)\mathrm{exp}\left(x\left(2\right)\text{xdata}\right).$

Input the observation times and responses.

xdata = ...
[0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
ydata = ...
[455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];

Create a simple exponential decay model.

fun = @(x,xdata)x(1)*exp(x(2)*xdata);

Fit the model using the starting point x0 = [100,-1].

x0 = [100,-1];
x = lsqcurvefit(fun,x0,xdata,ydata)
Local minimum possible.

lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the value of the function tolerance.
x = 1×2

498.8309   -0.1013

Compare the solution with that of a 'levenberg-marquardt' fit.

options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt');
lb = [];
ub = [];
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options)
Local minimum possible.
lsqcurvefit stopped because the relative size of the current step is less than
the value of the step size tolerance.
x = 1×2

498.8309   -0.1013

The two algorithms converged to the same solution. Plot the data and the fitted exponential model.

times = linspace(xdata(1),xdata(end));
plot(xdata,ydata,'ko',times,fun(x,times),'b-')
legend('Data','Fitted exponential')
title('Data and Fitted Curve')

Compare the results of fitting with the default 'trust-region-reflective' algorithm and the 'levenberg-marquardt' algorithm. Examine the solution process to see which is more efficient in this case.

Suppose that you have observation time data xdata and observed response data ydata, and you want to find parameters $x\left(1\right)$ and $x\left(2\right)$ to fit a model of the form

$\text{ydata}=x\left(1\right)\mathrm{exp}\left(x\left(2\right)\text{xdata}\right).$

Input the observation times and responses.

xdata = ...
[0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
ydata = ...
[455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];

Create a simple exponential decay model.

fun = @(x,xdata)x(1)*exp(x(2)*xdata);

Fit the model using the starting point x0 = [100,-1].

x0 = [100,-1];
[x,resnorm,residual,exitflag,output] = lsqcurvefit(fun,x0,xdata,ydata);
Local minimum possible.

lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the value of the function tolerance.

Compare the solution with that of a 'levenberg-marquardt' fit.

options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt');
lb = [];
ub = [];
[x2,resnorm2,residual2,exitflag2,output2] = lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options);
Local minimum possible.
lsqcurvefit stopped because the relative size of the current step is less than
the value of the step size tolerance.

Are the solutions equivalent?

norm(x-x2)
ans = 2.0626e-06

Yes, the solutions are equivalent.

Which algorithm took fewer function evaluations to arrive at the solution?

fprintf(['The ''trust-region-reflective'' algorithm took %d function evaluations,\n',...
'and the ''levenberg-marquardt'' algorithm took %d function evaluations.\n'],...
output.funcCount,output2.funcCount)
The 'trust-region-reflective' algorithm took 87 function evaluations,
and the 'levenberg-marquardt' algorithm took 72 function evaluations.

Plot the data and the fitted exponential model.

times = linspace(xdata(1),xdata(end));
plot(xdata,ydata,'ko',times,fun(x,times),'b-')
legend('Data','Fitted exponential')
title('Data and Fitted Curve')

The fit looks good. How large are the residuals?

fprintf(['The ''trust-region-reflective'' algorithm has residual norm %f,\n',...
'and the ''levenberg-marquardt'' algorithm has residual norm %f.\n'],...
resnorm,resnorm2)
The 'trust-region-reflective' algorithm has residual norm 9.504887,
and the 'levenberg-marquardt' algorithm has residual norm 9.504887.

## Input Arguments

collapse all

Function you want to fit, specified as a function handle or the name of a function. fun is a function that takes two inputs: a vector or matrix x, and a vector or matrix xdata. fun returns a vector or matrix F, the objective function evaluated at x and xdata. The function fun can be specified as a function handle for a function file:

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

where myfun is a MATLAB® function such as

function F = myfun(x,xdata)
F = ...     % Compute function values at x, xdata

fun can also be a function handle for an anonymous function.

f = @(x,xdata)x(1)*xdata.^2+x(2)*sin(xdata);
x = lsqcurvefit(f,x0,xdata,ydata);

If the user-defined values for x and F are arrays, they are converted to vectors using linear indexing (see Array Indexing).

Note

fun should return fun(x,xdata), and not the sum-of-squares sum((fun(x,xdata)-ydata).^2). lsqcurvefit implicitly computes the sum of squares of the components of fun(x,xdata)-ydata. See Examples.

If the Jacobian can also be computed and the 'SpecifyObjectiveGradient' option is true, set by

then the function fun must return a second output argument with the Jacobian value J (a matrix) at x. By checking the value of nargout, the function can avoid computing J when fun is called with only one output argument (in the case where the optimization algorithm only needs the value of F but not J).

function [F,J] = myfun(x,xdata)
F = ...          % objective function values at x
if nargout > 1   % two output arguments
J = ...   % Jacobian of the function evaluated at x
end

If fun returns a vector (matrix) of m components and x has n elements, where n is the number of elements of x0, the Jacobian J is an m-by-n matrix where J(i,j) is the partial derivative of F(i) with respect to x(j). (The Jacobian J is the transpose of the gradient of F.) For more information, see Writing Vector and Matrix Objective Functions.

Example: @(x,xdata)x(1)*exp(-x(2)*xdata)

Data Types: char | function_handle | string

Initial point, specified as a real vector or real array. Solvers use the number of elements in x0 and the size of x0 to determine the number and size of variables that fun accepts.

Example: x0 = [1,2,3,4]

Data Types: double

Input data for model, specified as a real vector or real array. The model is

ydata = fun(x,xdata),

where xdata and ydata are fixed arrays, and x is the array of parameters that lsqcurvefit changes to search for a minimum sum of squares.

Example: xdata = [1,2,3,4]

Data Types: double

Response data for model, specified as a real vector or real array. The model is

ydata = fun(x,xdata),

where xdata and ydata are fixed arrays, and x is the array of parameters that lsqcurvefit changes to search for a minimum sum of squares.

The ydata array must be the same size and shape as the array fun(x0,xdata).

Example: ydata = [1,2,3,4]

Data Types: double

Lower bounds, specified as a real vector or real array. If the number of elements in x0 is equal to the number of elements in lb, then lb specifies that

x(i) >= lb(i) for all i.

If numel(lb) < numel(x0), then lb specifies that

x(i) >= lb(i) for 1 <= i <= numel(lb).

If there are fewer elements in lb than in x0, solvers issue a warning.

Example: To specify that all x components are positive, use lb = zeros(size(x0)).

Data Types: double

Upper bounds, specified as a real vector or real array. If the number of elements in x0 is equal to the number of elements in ub, then ub specifies that

x(i) <= ub(i) for all i.

If numel(ub) < numel(x0), then ub specifies that

x(i) <= ub(i) for 1 <= i <= numel(ub).

If there are fewer elements in ub than in x0, solvers issue a warning.

Example: To specify that all x components are less than 1, use ub = ones(size(x0)).

Data Types: double

Optimization options, specified as the output of optimoptions or a structure such as optimset returns.

Some options apply to all algorithms, and others are relevant for particular algorithms. See Optimization Options Reference for detailed information.

Some options are absent from the optimoptions display. These options appear in italics in the following table. For details, see View Options.

Example: options = optimoptions('lsqcurvefit','FiniteDifferenceType','central')

Problem structure, specified as a structure with the following fields:

Field NameEntry

objective

Objective function of x and xdata

x0

Initial point for x, active set algorithm only

xdata

Input data for objective function

ydata

Output data to be matched by objective function
lbVector of lower bounds
ubVector of upper bounds

solver

'lsqcurvefit'

options

Options created with optimoptions

You must supply at least the objective, x0, solver, xdata, ydata, and options fields in the problem structure.

Data Types: struct

## Output Arguments

collapse all

Solution, returned as a real vector or real array. The size of x is the same as the size of x0. Typically, x is a local solution to the problem when exitflag is positive. For information on the quality of the solution, see When the Solver Succeeds.

Squared norm of the residual, returned as a nonnegative real. resnorm is the squared 2-norm of the residual at x: sum((fun(x,xdata)-ydata).^2).

Value of objective function at solution, returned as an array. In general, residual = fun(x,xdata)-ydata.

Reason the solver stopped, returned as an integer.

 1 Function converged to a solution x. 2 Change in x is less than the specified tolerance, or Jacobian at x is undefined. 3 Change in the residual is less than the specified tolerance. 4 Relative magnitude of search direction is smaller than the step tolerance. 0 Number of iterations exceeds options.MaxIterations or number of function evaluations exceeded options.MaxFunctionEvaluations. -1 A plot function or output function stopped the solver. -2 Problem is infeasible: the bounds lb and ub are inconsistent.

Information about the optimization process, returned as a structure with fields:

 firstorderopt Measure of first-order optimality iterations Number of iterations taken funcCount The number of function evaluations cgiterations Total number of PCG iterations (trust-region-reflective algorithm only) stepsize Final displacement in x algorithm Optimization algorithm used message Exit message

Lagrange multipliers at the solution, returned as a structure with fields:

 lower Lower bounds lb upper Upper bounds ub

Jacobian at the solution, returned as a real matrix. jacobian(i,j) is the partial derivative of fun(i) with respect to x(j) at the solution x.

## Limitations

• The trust-region-reflective algorithm does not solve underdetermined systems; it requires that the number of equations, i.e., the row dimension of F, be at least as great as the number of variables. In the underdetermined case, lsqcurvefit uses the Levenberg-Marquardt algorithm.

• lsqcurvefit can solve complex-valued problems directly. Noe that bound constraints do not make sense for complex values. For a complex problem with bound constraints, split the variables into real and imaginary parts. See Fit a Model to Complex-Valued Data.

• The preconditioner computation used in the preconditioned conjugate gradient part of the trust-region-reflective method forms JTJ (where J is the Jacobian matrix) before computing the preconditioner. Therefore, a row of J with many nonzeros, which results in a nearly dense product JTJ, can lead to a costly solution process for large problems.

• If components of x have no upper (or lower) bounds, lsqcurvefit prefers that the corresponding components of ub (or lb) be set to inf (or -inf for lower bounds) as opposed to an arbitrary but very large positive (or negative for lower bounds) number.

You can use the trust-region reflective algorithm in lsqnonlin, lsqcurvefit, and fsolve with small- to medium-scale problems without computing the Jacobian in fun or providing the Jacobian sparsity pattern. (This also applies to using fmincon or fminunc without computing the Hessian or supplying the Hessian sparsity pattern.) How small is small- to medium-scale? No absolute answer is available, as it depends on the amount of virtual memory in your computer system configuration.

Suppose your problem has m equations and n unknowns. If the command J = sparse(ones(m,n)) causes an Out of memory error on your machine, then this is certainly too large a problem. If it does not result in an error, the problem might still be too large. You can find out only by running it and seeing if MATLAB runs within the amount of virtual memory available on your system.

## Algorithms

The Levenberg-Marquardt and trust-region-reflective methods are based on the nonlinear least-squares algorithms also used in fsolve.

• The default trust-region-reflective algorithm is a subspace trust-region method and is based on the interior-reflective Newton method described in [1] and [2]. Each iteration involves the approximate solution of a large linear system using the method of preconditioned conjugate gradients (PCG). See Trust-Region-Reflective Least Squares.

• The Levenberg-Marquardt method is described in references [4], [5], and [6]. See Levenberg-Marquardt Method.

## Alternative Functionality

### App

The Optimize Live Editor task provides a visual interface for lsqcurvefit.

## References

[1] Coleman, T.F. and Y. Li. “An Interior, Trust Region Approach for Nonlinear Minimization Subject to Bounds.” SIAM Journal on Optimization, Vol. 6, 1996, pp. 418–445.

[2] Coleman, T.F. and Y. Li. “On the Convergence of Reflective Newton Methods for Large-Scale Nonlinear Minimization Subject to Bounds.” Mathematical Programming, Vol. 67, Number 2, 1994, pp. 189–224.

[3] Dennis, J. E. Jr. “Nonlinear Least-Squares.” State of the Art in Numerical Analysis, ed. D. Jacobs, Academic Press, pp. 269–312.

[4] Levenberg, K. “A Method for the Solution of Certain Problems in Least-Squares.” Quarterly Applied Mathematics 2, 1944, pp. 164–168.

[5] Marquardt, D. “An Algorithm for Least-squares Estimation of Nonlinear Parameters.” SIAM Journal Applied Mathematics, Vol. 11, 1963, pp. 431–441.

[6] Moré, J. J. “The Levenberg-Marquardt Algorithm: Implementation and Theory.” Numerical Analysis, ed. G. A. Watson, Lecture Notes in Mathematics 630, Springer Verlag, 1977, pp. 105–116.

[7] Moré, J. J., B. S. Garbow, and K. E. Hillstrom. User Guide for MINPACK 1. Argonne National Laboratory, Rept. ANL–80–74, 1980.

[8] Powell, M. J. D. “A Fortran Subroutine for Solving Systems of Nonlinear Algebraic Equations.” Numerical Methods for Nonlinear Algebraic Equations, P. Rabinowitz, ed., Ch.7, 1970.

## Extended Capabilities

Introduced before R2006a