Function File: lsqcurvefit (fun, x0, xdata, ydata)
Function File: lsqcurvefit (fun, x0, xdata, ydata, lb, ub)
Function File: lsqcurvefit (fun, x0, xdata, ydata, lb, ub, options)
Function File: lsqcurvefit (problem)
Function File: [x, resnorm, residual, exitflag, output, lambda, jacobian] = lsqcurvefit (…)

Solve nonlinear least-squares (nonlinear data-fitting) problems

min [EuclidianNorm (f(x, xdata) - ydata)] .^ 2
 x

The first four input arguments must be provided with non-empty initial guess x0. For a given input xdata, ydata is the observed output. ydata must be the same size as the vector (or matrix) returned by fun. The optional bounds lb and ub should be the same size as x0.

lsqcurvefit may also be called with a single structure argument with the fields fun, x0, xdata, ydata, lb, ub, and options, resembling the separate input arguments above. For compatibility reasons, field fun may also be called objective. Additionally, the structure must have the field solver, set to "lsqcurvefit".

options can be set with optimset. Follwing Matlab compatible options are recognized:

Algorithm String specifying backend algorithm. Currently available "lm_svd_feasible" only.

TolFun Minimum fractional improvement in objective function in an iteration (termination criterium). Default: 1e-6.

TypicalX Typical values of x. Default: 1.

MaxIter Maximum number of iterations allowed. Default: 20.

Jacobian If set to "on", the function fun must return a second output containing a user-specified Jacobian. The Jacobian is computed using finite differences otherwise. Default: "off"

FinDiffType "centered" or "forward" (Default) type finite differences estimation.

FinDiffRelStep Step size factor. The default is sqrt(eps) for forward finite differences, and eps^(1/3) for central finite differences

OutputFcn One or more user-defined functions, either as a function handle or as a cell array of function handles that an optimization function calls at each iteration. The function definition has the following form:

stop = outfun(x, optimValues, state)

x is the point computed at the current iteration. optimValues is a structure containing data from the current iteration in the following fields: "iteration"- number of current iteration. "residual"- residuals. state is the state of the algorithm: "init" at start, "iter" after each iteration and "done" at the end.

Display String indicating the degree of verbosity. Default: "off". Currently only supported values are "off" (no messages) and "iter" (some messages after each iteration).

Returned values:

x

Coefficients to best fit the nonlinear function fun(x,xdata) to the observed values ydata.

resnorm

Scalar value of objective as squared EuclidianNorm(f(x)).

residual

Value of solution residuals f(x).

exitflag

Status of solution:

0

Maximum number of iterations reached.

2

Change in x was less than the specified tolerance.

3

Change in the residual was less than the specified tolerance.

-1

Output function terminated the algorithm.

output

Structure with additional information, currently the only field is iterations, the number of used iterations.

lambda

Structure containing Lagrange multipliers at the solution x sepatared by constraint type (lb and ub).

jacobian

m-by-n matrix, where jacobian(i,j) is the partial derivative of fun(i) with respect to x(j) If Jacobian is set to "on" in options then fun must return a second argument providing a user-sepcified Jacobian. Otherwise, lsqnonlin approximates the Jacobian using finite differences.

This function is a compatibility wrapper. It calls the more general nonlin_curvefit function internally.

See also: lsqnonlin, nonlin_residmin, nonlin_curvefit.

Demonstration 1

The following code

  %% Example for user specified Jacobian.

  %% independents
  x = [1:10:100]';
  %% observed data
  y =[9.2160e-001, 3.3170e-001, 8.9789e-002, 2.8480e-002, 2.6055e-002,...
     8.3641e-003,  4.2362e-003,  3.1693e-003,  1.4739e-004,  2.9406e-004]';
  %% initial values:
  p0=[0.8; 0.05];
  %% bounds
  lb=[0; 0]; ub=[];
  %% Jacobian setting
  opts = optimset ("Jacobian", "on")

  %% model function:
  function [F,J] = myfun (p, x)
    F = p(1) * exp (-p(2) * x);
    if nargout > 1
      J = [exp(- p(2) * x), - p(1) * x .* exp(- p(2) * x)];
    endif
  endfunction

  [c, resnorm, residual, flag, output, lambda, jacob] = ...
      lsqcurvefit (@ (varargin) myfun(varargin{:}), p0, x, y, lb,  ub, opts)

Produces the following output

opts =

  scalar structure containing the fields:

    Jacobian = on

c =

   1.0281
   0.1068

resnorm = 8.6481e-04
residual =

   2.3738e-03
  -1.4045e-02
   1.9418e-02
   9.0645e-03
  -1.3148e-02
  -3.9266e-03
  -2.7106e-03
  -2.6448e-03
   3.2921e-05
  -2.3207e-04

flag = 3
output =

  scalar structure containing the fields:

    iterations = 5

lambda = [](0x0)
jacob =

   8.9873e-01  -9.2397e-01
   3.0898e-01  -3.4942e+00
   1.0622e-01  -2.2933e+00
   3.6519e-02  -1.1639e+00
   1.2555e-02  -5.2921e-01
   4.3163e-03  -2.2631e-01
   1.4839e-03  -9.3060e-02
   5.1015e-04  -3.7238e-02
   1.7539e-04  -1.4605e-02
   6.0296e-05  -5.6411e-03

Package: optim