Nonlinear Fit (Matrix Form) - Maple Help

fit a nonlinear model function to data

 Calling Sequence NonlinearFit(m, f, XY, options)

Parameters

 m - posint; number of model parameters f - procedure; model function in Matrix form XY - Matrix; values of independent and dependent variables options - (optional) equation(s) of the form option=value where option is one of initialvalues, modelfunctiongradient, output and parameterranges; specify options for the NonlinearFit command

Description

 • 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\left({x}_{1},{x}_{2},\mathrm{...},{x}_{n};{a}_{1},{a}_{2},\mathrm{...},{a}_{m}\right)$, where y is the dependent variable and f is the model function of n independent variables ${x}_{1},{x}_{2},\mathrm{...},{x}_{n}$, and m model parameters ${a}_{1},{a}_{2},\mathrm{...},{a}_{m}$. Given k data points, where each data point is an (n+1)-tuple of numerical values for $\left({x}_{1},{x}_{2},\mathrm{...},{x}_{n},y\right)$, 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\left({x}_{1},{x}_{2},\mathrm{...},{x}_{n};{a}_{1},{a}_{1},\mathrm{...},{a}_{m}\right)$ 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.

Options

 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.

Notes

 • 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 ${\mathrm{infolevel}}_{\mathrm{Optimization}}$ to 1 or higher.

Examples

 > $\mathrm{with}\left(\mathrm{Statistics}\right):$

Below, the model function is $a+bx+{ⅇ}^{cx}$ where $x$ is the independent variable and $a$, $b$ and $c$ are the model parameters.

 > $\mathrm{XY}≔\mathrm{Matrix}\left(\left[\left[1.0,2.2\right],\left[2.0,3.0\right],\left[3.0,4.8\right],\left[4.0,10.2\right],\left[5.0,24.5\right],\left[6.0,75.0\right]\right],\mathrm{datatype}=\mathrm{float}\right):$
 > f := proc(p, v, i) p[1]+p[2]*v[i,1]+exp(p[3]*v[i,1])-v[i,2] end proc:
 > $\mathrm{NonlinearFit}\left(3,f,\mathrm{XY}\right)$
 $\left[\begin{array}{c}{6.49670407384335}\\ {-4.54643947753519}\\ {0.758384141232909}\end{array}\right]$ (1)

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.

 > $\mathrm{XYweighted}≔\mathrm{Matrix}\left(\left[\left[1.0,2.2,1.0\right],\left[2.0,3.0,1.0\right],\left[3.0,4.8,1.0\right],\left[4.0,10.2,2.0\right],\left[5.0,24.5,2.0\right],\left[6.0,75.0,3.0\right]\right],\mathrm{datatype}=\mathrm{float}\right):$
 > fweighted := proc(p, v, i) v[i,3]*(p[1]+p[2]*v[i,1]+exp(p[3]*v[i,1])-v[i,2])   end proc:
 > $\mathrm{NonlinearFit}\left(3,\mathrm{fweighted},\mathrm{XYweighted}\right)$
 $\left[\begin{array}{c}{8.49085778809462}\\ {-5.40574803336152}\\ {0.765173952062712}\end{array}\right]$ (2)

Consider now an experiment where quantities $x$, $y$, and $z$ are quantities influencing a quantity $w$ according to an approximate relationship

$w={x}^{a}+\frac{b{x}^{2}}{y}+cyz$

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$.

 > $\mathrm{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⟩⟩$
 ${\mathrm{ExperimentalData}}{≔}\left[\begin{array}{cccc}{1}& {1}& {1}& {0.531}\\ {1}& {2}& {2}& {0.341}\\ {1}& {3}& {3}& {0.163}\\ {2}& {1}& {4}& {0.641}\\ {2}& {2}& {5}& {0.713}\\ {2}& {3}& {6}& {-0.040}\end{array}\right]$ (3)

The residual for the $i$th 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[1] + p[2] * v[i, 1]^2 / v[i, 2] + p[3] * v[i, 2] * v[i, 3] - v[i, 4]; end proc;
 ${\mathrm{residual}}{≔}{\mathbf{proc}}\left({p}{,}{v}{,}{i}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{v}{[}{i}{,}{1}{]}{^}{p}{[}{1}{]}{+}{p}{[}{2}{]}{*}{v}{[}{i}{,}{1}{]}{^}{2}{/}{v}{[}{i}{,}{2}{]}{+}{p}{[}{3}{]}{*}{v}{[}{i}{,}{2}{]}{*}{v}{[}{i}{,}{3}{]}{-}{v}{[}{i}{,}{4}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (4)

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.

 > $\mathrm{NonlinearFit}\left(3,\mathrm{residual},\mathrm{ExperimentalData},\mathrm{initialvalues}=⟨2,1,0⟩,\mathrm{output}=\left[\mathrm{parametervalues},\mathrm{residuals}\right]\right)$
 $\left[\left[\begin{array}{c}{1.14701973996968}\\ {-0.298041864889394}\\ {-0.0982511893429762}\end{array}\right]{,}\left[\begin{array}{cccccc}{0.0727069457676300}& {0.116974310183398}& {-0.146607992383251}& {-0.0116127470057686}& {-0.0770361532848388}& {0.0886489085642805}\end{array}\right]\right]$ (5)

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.