# PolyFit

Advanced Analysis Library Only

AnalysisLibErrType PolyFit (double arrayX[], double arrayY[], int numberOfElements, int order, double outputArray[], double coefficients[], double *meanSquaredError);

## Purpose

| Note This function is obsolete. National Instruments recommends that you use PolyFitEx instead.

Finds the coefficients that best represent the polynomial fit of the data points (X, Y) using the least squares method. PolyFit obtains the element of the output array using the following formula:

PolyFit obtains the mean squared error (mse) using the following formula:

where order is the polynomial order, and n is the number of sample points. The size of coefficients must be greater than or equal to order + 1.

PolyFit can perform this operation in place; that is, the input and output arrays can be the same.

If the elements in arrayX are large and order is also large, you might see unstable results. One solution is to scale the input data elements to the range [—1:1]. To do this, perform the following steps:

1. Find the number, for example, k, in arrayX that has the largest magnitude, or absolute value.
2. Divide all elements in the array by the absolute value of k.
3. Apply Polyfit and rescale the results in the output array by multiplying all elements in the output array by the absolute value of k.

### Example

/* Generate a 10th-order polynomial pattern with random coefficients and find the polynomial fit. */
double x[200], y[200], z[200], a[11], coef[11];
double first, last, mse;
n, k, order;
n = 200;
first = 0.0;
last = 1.99E2;
Ramp (n, first, last, x) /* x[i] = i */
k = 11;
Uniform (k, 17, a);
PolyEv1D (x, n, a, k, y); /* polynomial pattern */
/* Find the best polynomial fit. */
order = 10;
PolyFit (x, y, n, order, z, coef, &mse);

## Parameters

Input
Name | Type | Description
arrayX | double-precision array | An array whose elements contain the X coordinates of the (X,Y) data sets.
arrayY | double-precision array | An array whose elements contain the Y coordinates of the (X,Y) data sets
numberOfElements | integer | Number of sample points used in the polynomial fit operation.
order | integer | Order of the polynomial used in the polynomial fit operation. This value must be greater than 0 and less than 90. Default Value: 1.
Output
Name | Type | Description
outputArray | double-precision array | The polynomial values that best represent the data.
coefficients | double-precision array | Polynomial coefficients that best describe the polynomial curve fit. The size of coefficients must be greater than or equal to order + 1.
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.