NonLinearFit

Advanced Analysis Library Only

AnalysisLibErrType NonLinearFit (double XArray[], double YArray[], double fittedArray[], int numberOfElements, ModelFun *modelFunction, double coefficientArray[], int numberOfCoefficients, double *meanSquaredError);

Purpose

Uses the Levenberg-Marquardt algorithm to determine the least squares set of coefficients that best fit the set of input data points (X, Y) as expressed by a nonlinear function y = f(x, a) where a is the set of coefficients. NonLinearFit also gives the best fit curve y = f(x, a).

You must pass a pointer to the nonlinear function f(x, a) along with a set of initial guess coefficients a. NonLinearFit does not always give the correct answer. The correct output sometimes depends on the initial choice of a. It is very important to verify the final result.

NonLinearFit calculates the output mean squared error using the following formula:

Parameters

Input
Name Type Description
XArray double-precision array An array whose elements contain the X coordinates of the (X,Y) data sets
YArray double-precision array An array whose elements contain the Y coordinates of the (X,Y) data sets
numberOfElements integer Number of elements in both XArray and YArray.
modelFunction ModelFun Pointer to the model function, f(xi ,a), used in the nonlinear fitting algorithm. The model function must be defined as follows:

double ModelFunct (double x, double a[], int ncoef);

where a contains the function coefficients.
numberOfCoefficients integer The number of coefficients in coefficientArray. Notice that this is the number of coefficients that modelFunction actually uses and not necessarily the number of allocated elements in coefficientArray. For example, if coefficientArray is allocated as shown in the following code:

coef = malloc(10*sizeof(double))

and the model function myModel() looks like the following code:

double myModel(double x, double coef[], int ncoef)
{
return (coef[0]*exp(coef[1]*x););
}


then the correct number of coefficients for this model function is 2, even though the coefficient array is allocated with 10 elements. Specifying anything more than 2 for the number of coefficients results in an error.
Output
Name Type Description
fittedArray double-precision array The array of values, y = f(x,a), where f is the user-supplied nonlinear model function, and a is the set of best-fit coefficients.

The size of this array must be at least numberOfElements.
coefficientArray double-precision array The initial guess of the nonlinear fit coefficients.

If this initial set of coefficients is significantly different from the best fit set, the function might fail to return the appropriate coefficients and best fit curve.

On exit, this array contains the fitted coefficients that best describe the nonlinear curve fitting given the model user-supplied model function.
meanSquaredError double-precision The mean squared error generated by the difference between the fitted curve and the raw data.

Return Value

Name Type Description
status AnalysisLibErrType A value that specifies the type of error that occurred. Refer to analysis.h for definitions of these constants.