Function File: nlinfit (X, Y, modelfun, beta0)
Function File: nlinfit (X, Y, modelfun, beta0, options)
Function File: nlinfit (…, Name, Value)
Function File: [beta, R, J, CovB, MSE] = nlinfit (…)

Nonlinear Regression.

min [EuclidianNorm (Y - modelfun (beta, X))] ^ 2
beta

X is a matrix of independents, Y is the observed output and modelfun is the nonlinear regression model function. modelfun should be specified as a function handle, which accepts two inputs: an array of coefficients and an array of independents – in that order. The first four input arguments must be provided with non-empty initial guess of the coefficients beta0. Y and X must be the same size as the vector (or matrix) returned by fun. options is a structure containing estimation algorithm options. It can be set using statset. Follwing Matlab compatible options are recognized:

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

MaxIter Maximum number of iterations allowed. Default: 400.

DerivStep Step size factor. The default is eps^(1/3) for finite differences gradient calculation.

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

Optional Name, Value pairs can be provided to set additional options. Currently the only applicable name-value pair is ’Weights’, w, where w is the array of real positive weight factors for the squared residuals.

Returned values:

beta

Coefficients to best fit the nonlinear function modelfun (beta, X) to the observed values Y.

R

Value of solution residuals: modelfun (beta, X) - Y. If observation weights are specified then R is the array of weighted residuals: sqrt (weights) .* modelfun (beta, X) - Y.

J

A matrix where J(i,j) is the partial derivative of modelfun(i) with respect to beta(j). If observation weights are specified, then J is the weighted model function Jacobian: diag (sqrt (weights)) * J.

CovB

Estimated covariance matrix of the fitted coefficients.

MSE

Scalar valued estimate of the variance of error term. If the model Jacobian is full rank, then MSE = (R’ * R)/(N-p), where N is the number of observations and p is the number of estimated coefficients.

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

See also: nonlin_residmin, nonlin_curvefit, residmin_stat, curvefit_stat.

Demonstration 1

The following code

 modelfun = @(b, x) (b(1) + b(2) * exp (- b(3) * x));
 %% actual value
 beta_without_noise = [1; 3; 2];
 x = [3.49622; 0.33751; 1.25675; 3.66981; 0.26237; 5.51095; ...
      2.11407; 1.48774; 6.22436; 2.04519];
 y_actual = modelfun (beta_without_noise, x);
 noise = [0.176110; -0.066850; 0.231000; -0.047570; -0.108230; ...
          0.122790; 0.062940; 0.151510; 0.116010; -0.097460];
 y_noisy = y_actual + noise;
 %% initial guess
 beta0 = [2; 2; 2];
 %% weights vector
 weights = [5; 16; 1; 20; 12; 11; 17; 8; 11; 13];
 [beta, R, J, covb, mse] = nlinfit (x, y_noisy, modelfun, beta0)
 [beta_w, R_w, J_w, covb_w, mse_w] = nlinfit (x, y_noisy, modelfun, beta0, [], "weights", weights)

Produces the following output

beta =

   1.0593
   2.4516
   1.6207

R =

   1.1110e-01
  -1.7384e-02
   9.4882e-02
  -1.1131e-01
   5.1953e-03
   6.3231e-02
  -3.2298e-02
   2.5370e-02
   5.6636e-02
  -1.9565e-01

J =

   1.0000e+00   3.4603e-03  -2.9659e-02
   1.0000e+00   5.7868e-01  -4.7882e-01
   1.0000e+00   1.3044e-01  -4.0189e-01
   1.0000e+00   2.6117e-03  -2.3498e-02
   1.0000e+00   6.5362e-01  -4.2043e-01
   1.0000e+00   1.3213e-04  -1.7852e-03
   1.0000e+00   3.2506e-02  -1.6848e-01
   1.0000e+00   8.9706e-02  -3.2719e-01
   1.0000e+00   4.1577e-05  -6.3446e-04
   1.0000e+00   3.6345e-02  -1.8224e-01

covb =

   2.8309e-03   3.2125e-03   1.0622e-02
   3.2125e-03   5.5470e-02   5.7435e-02
   1.0622e-02   5.7435e-02   9.5325e-02

mse = 0.011605
beta_w =

   1.0361
   2.6290
   1.8142

R_w =

   3.0895e-01
  -2.5185e-03
   1.6898e-01
  -3.8045e-01
  -8.5216e-03
   2.8737e-01
   5.7045e-02
   2.5925e-01
   2.6505e-01
  -5.3240e-01

J_w =

   2.2361e+00   3.9338e-03  -3.6158e-02
   4.0000e+00   2.1684e+00  -1.9240e+00
   1.0000e+00   1.0228e-01  -3.3794e-01
   4.4721e+00   5.7422e-03  -5.5399e-02
   3.4641e+00   2.1521e+00  -1.4845e+00
   3.3166e+00   1.5088e-04  -2.1859e-03
   4.1231e+00   8.9033e-02  -4.9483e-01
   2.8284e+00   1.9026e-01  -7.4416e-01
   3.3166e+00   4.1355e-05  -6.7673e-04
   3.6056e+00   8.8220e-02  -4.7434e-01

covb_w =

   2.2105e-03   6.7625e-03   1.3396e-02
   6.7625e-03   1.1812e-01   1.4646e-01
   1.3396e-02   1.4646e-01   2.1109e-01

mse_w = 0.1108

Package: optim