Statistics[NonlinearFit](Advanced Matrix Form)
fit a nonlinear model function to data
NonlinearFit(m, f, XY, options)
posint; number of model parameters
procedure; model function in Matrix form
Matrix; values of independent and dependent variables
(optional) equation(s) of the form option=value where option is one of initialvalues, modelfunctiongradient, output and parameterranges; specify options for the NonlinearFit command
The NonlinearFit command fits a model function that is nonlinear in the model parameters to data by minimizing the least-squares error. If your model function is linear, it is recommended that you use the Statistics[LinearFit] command. The error is minimized in a local sense; see the Notes section below.
This help page describes how to specify the problem in Matrix form. For more information about the input forms, see the Input Forms help page. The algebraic and operator forms for specifying the problem are described in the Statistics[NonlinearFit] help page. The Matrix form is more complex but leads to more efficient computation. Unlike for the algebraic and operator forms, Arrays and lists are not accepted in the place of Matrices and Vectors. The input parameters must have the exact types specified on this help page.
Consider the model y=f⁡x1,x2,...,xn⁢; ⁢a1,a2,...,am, where y is the dependent variable and f is the model function of n independent variables x1,x2,...,xn, and m model parameters a1,a2,...,am. Given k data points, where each data point is an (n+1)-tuple of numerical values for x1,x2,...,xn,y, the NonlinearFit command finds values of the model parameters such that the sum of the k residuals squared is minimized. The ith residual is the value of y−f⁡x1,x2,...,xn⁢; ⁢a1,a1,...,am evaluated at the ith data point.
The first parameter m is the number of model parameters. The second parameter f is a procedure representing the model function. This procedure has the form proc(p, v, i) where p is a Vector input parameter of dimension m holding values of the model parameters, v is a Matrix having the same form as the XY parameter described below, and i is an integer from 1 to k. The procedure should return a single floating-point value equal to the residual for data point i.
The third parameter XY is a Matrix containing the values of the independent variables in the first n columns followed by the values of the dependent variable in the last column. Row i in the Matrix contains the n independent values and the dependent value for the ith data point.
By default, a Vector containing the parameter values is returned. Additional results or a solution module that allows you to query for various settings and results can be obtained with the output option. For more information, see the Statistics/Regression/Solution help page.
Unlike the regular calling sequence for NonlinearFit, the Matrix-form calling sequence does not accept the weights option. Instead, a column of weights can be added to the XY data Matrix and the procedure f adjusted to compute the residuals with the provided weights.
The options argument can contain one or more of the options shown below. These options are described in more detail on the Statistics/Regression/Options help page.
initialvalues = Vector(realcons) -- Provide initial values for the parameters.
modelfunctiongradient = list(procedure) -- Specify the gradient of the model function as a list of procedures. The jth procedure computes the derivative of the model function with respect to the jth parameter, at a given data point. The form of each procedure is the same as that required for the residual, described above.
output = name or string -- Specify the form of the solution. The output option can take as a value the name solutionmodule, or one of the following names (or a list of these names): degreesoffreedom, parametervalues, parametervector, residuals, residualmeansquare, residualstandarddeviation, residualsumofsquares. For more information, see the Statistics/Regression/Solution help page.
parameterranges = [Vector, Vector] -- Specify the allowable range for each parameter as two Vectors of dimension m. The first contains lower bounds on the parameters and the second contains upper bounds.
The underlying computation is done in floating-point; therefore, all data points must have type realcons and all returned solutions are floating-point, even if the problem is specified with exact values. For more information about numeric computation in the Statistics package, see the Statistics/Computation help page.
The NonlinearFit command relies on the Matrix-form version of the Optimization[LSSolve] command, which in turns uses various methods implemented in a built-in library provided by the Numerical Algorithms Group (NAG). More information is available on the Optimization[LSSolveMatrixForm] help page. Additional options listed on that page may be provided to the NonlinearFit command and are passed directly to the LSSolve command.
The Optimization[LSSolve] command computes only local solutions to nonlinear least-squares problems. The parameter values returned by NonlinearFit minimize the sum of the residuals squared in a local sense. It is highly recommended that you provide initial values for the parameters using the initialvalues option.
If you provide bounds on the parameters using the parameterranges option, then an optimization solver for constrained nonlinear least-squares problems is called. For this solver, it is highly recommended that you provide procedures for computing the gradient using the modelfunctiongradient option. (Note that, with algebraic and operator forms of input, the gradient is automatically computed by Maple. With Matrix form, however, the procedures must be explicitly provided.)
To obtain more details as the least-squares problem is being solved, set infolevel[Statistics] to 2 or higher. To obtain details about the progress of the Optimization solver, set infolevelOptimization to 1 or higher.
Below, the model function is a+b⁢x+ⅇc⁢x where x is the independent variable and a, b and c are the model parameters.
XY ≔ Matrix⁡1.0,2.2,2.0,3.0,3.0,4.8,4.0,10.2,5.0,24.5,6.0,75.0,datatype=float:
f := proc(p, v, i) p+p*v[i,1]+exp(p*v[i,1])-v[i,2] end proc:
To solve a weighted problem, add a column of weights to the data Matrix and apply these weights in the procedure for computing the residuals.
XYweighted ≔ Matrix⁡1.0,2.2,1.0,2.0,3.0,1.0,3.0,4.8,1.0,4.0,10.2,2.0,5.0,24.5,2.0,6.0,75.0,3.0,datatype=float:
fweighted := proc(p, v, i) v[i,3]*(p+p*v[i,1]+exp(p*v[i,1])-v[i,2])
Consider now an experiment where quantities x, y, and z are quantities influencing a quantity w according to an approximate relationship
with unknown parameters a, b, and c. Six data points are given by the following matrix, with respective columns for x, y, z, and w.
ExperimentalData ≔ 1,1,1,2,2,2|1,2,3,1,2,3|1,2,3,4,5,6|0.531,0.341,0.163,0.641,0.713,−0.040
The residual for the ith data point at a given value of x, y, and z is given as follows:
residual := proc(p, v, i)
return v[i, 1]^p + p * v[i, 1]^2 / v[i, 2] + p * v[i, 2] * v[i, 3] - v[i, 4];
residual ≔ procp,v,ireturnv[i,1]^p+p*v[i,1]^2/v[i,2]+p*v[i,2]*v[i,3] − v[i,4]end proc
We take an initial guess that the first term will be approximately quadratic in x, that b will be approximately 1, and for c we don't even know whether it's going to be positive or negative, so we guess c=0. We compute both the parameter values and the residuals.
We see that the exponent on x is only about 1.14, and the other guesses were not very good either. However, this problem is conditioned well enough that Maple finds a good fit anyway.
Download Help Document
What kind of issue would you like to report? (Optional)