Admin Production ni-theme
Current Publication

NonLinearFitWithWeight

LabWindows/CVI

NonLinearFitWithWeight

Advanced Analysis Library Only

AnalysisLibErrType NonLinearFitWithWeight (double arrayX[], double arrayY[], double weight[], double fittedArray[], ssize_t numberOfElements, int maximumIterations, ModelFun *modelFunction, double coefficientArray[], int numberOfCoefficients, double *meanSquaredError, int *actualIterations);

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

NonLinearFitWithWeight calculates the output weighted mean square error using the following formula:

Note    If NonLinearFitWithWeight reaches the maximum number of iterations without reaching a solution, it returns an error. The outputs fittedArray, coefficientArray, and residue contain the best filtered array, best fit coefficients, and the weighted 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.
weight double [] The weight of each data point. If weight is NULL, the function assumes all the weights are 1.
numberOfElements ssize_t Number of elements in both arrayX and arrayY.
maximumIterations int Maximum number of iterations allowed.

If NonLinearFitWithWeight 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 weighted 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 the 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 weighted mean square error generated by the difference between the fitted curve and the raw data.
actualIterations int The number of iterations to reach the final results.

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 2009 and later