Admin Production ni-theme
Current Publication

NonLinearFitWithMaxIters

LabWindows/CVI

NonLinearFitWithMaxIters

Advanced Analysis Library Only

AnalysisLibErrType NonLinearFitWithMaxIters (double arrayX[], double arrayY[], double fittedArray[], ssize_t 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
arrayX double [] An array whose elements contain the X coordinates of the (X,Y) data sets.
arrayY double [] An array whose elements contain the Y coordinates of the (X,Y) data sets.
numberOfElements ssize_t Number of elements in both XArray and YArray.
maximumIterations int 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 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 int 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 [] 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 [] 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 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.

Additional Information

Library: Advanced Analysis Library

Include file: analysis.h

LabWindows/CVI compatibility: LabWindows/CVI 5.0 and later

Examples

Refer to the following examples that use the NonLinearFitWithMaxIters function:

  • analysis\nlinfit.cws

    Open example
  • analysis\nonlnfit.cws

    Open example