Nonlinear Fit (Matrix Form) - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


Statistics[NonlinearFit](Advanced Matrix Form)

fit a nonlinear model function to data

 

Calling Sequence

Parameters

Description

Options

Notes

Examples

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=fx1,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 yfx1,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.

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 infolevelOptimization to 1 or higher.

Examples

withStatistics:

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

XYMatrix1.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[1]+p[2]*v[i,1]+exp(p[3]*v[i,1])-v[i,2] end proc:

NonlinearFit3,f,XY

6.49670407384335−4.546439477535190.758384141232909

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

XYweightedMatrix1.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[1]+p[2]*v[i,1]+exp(p[3]*v[i,1])-v[i,2])
  end proc:

NonlinearFit3,fweighted,XYweighted

8.49085778809462−5.405748033361520.765173952062712

(2)

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

w=xa+bx2y+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.

ExperimentalData1,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

ExperimentalData1110.5311220.3411330.1632140.6412250.713236−0.040

(3)

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[1] + p[2] * v[i, 1]^2 / v[i, 2] + p[3] * v[i, 2] * v[i, 3] - v[i, 4];
end proc;

residualprocp,v,ireturnv[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

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

NonlinearFit3,residual,ExperimentalData,initialvalues=2,1,0,output=parametervalues,residuals

1.14701973996968−0.298041864889394−0.0982511893429762,0.07270694576763000.116974310183398−0.146607992383251−0.0116127470057686−0.07703615328483880.0886489085642805

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

See Also

CurveFitting

Optimization[LSSolveMatrixForm]

Statistics

Statistics/Computation

Statistics/Regression

Statistics/Regression/InputForms

Statistics/Regression/Options

Statistics/Regression/Solution

Statistics[Fit]

Statistics[LinearFit]

Statistics[NonlinearFit]