# NonLinearFitWithMaxIters

Advanced Analysis Library Only

AnalysisLibErrType NonLinearFitWithMaxIters (const double XArray[], const double YArray[], const double fittedArray[], int numberOfElements, int maximumIterations, 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. NonLinearFitWithMaxIters 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. NonLinearFitWithMaxIters 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.

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

| Note If NonLinearFitWithMaxIters reaches the maximum number of iterations without reaching a solution, it returns an error. The outputs fittedArray, coefficientArray, and meanSquaredError contain the best filtered array, best fit coefficients, and the mean square error at the end of maximum iterations, respectively.

## Parameters

Input
Name | Type | Description
XArray | const double [] | An array whose elements contain the X coordinates of the (X,Y) data sets.
YArray | const double [] | An array whose elements contain the Y coordinates of the (X,Y) data sets.
numberOfElements | integer | Number of elements in both XArray and YArray.
maximumIterations | integer | Maximum number of iterations allowed. If NonLinearFitWithMaxIters reaches the maximum number of iterations without reaching a solution, it returns an error. The outputs fittedArray, coefficientArray, and meanSquaredError contain the best filtered array, best fit coefficients, and the mean square error at the end of maximum iterations, respectively.
modelFunction | ModelFun pointer | 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 | const double [] | 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 is initial set of coefficients is significantly different from the best fit set, the function might fail to return the appropriate coeficients 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.