# LinFit

Advanced Analysis Library Only

AnalysisLibErrType LinFit (double arrayX[], double arrayY[], int numberOfElements, double outputArray[], double *slope, double *intercept, double *meanSquaredError);

## Purpose

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

Finds the slope and intercept values that best represent the linear fit of the data points (X, Y) using the least squares method. LinFit obtains the element of the output array, using the following formula:

Zi = slope × Xi + intercept

LinFit obtains the mean squared error using the following formula:

where n is the number of sample points.

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

### Example

/* Generate a ramp pattern and find the best linear fit. */
double x[200], y[200], z[200];
double start, end, a, b, slope, intercept, mse;
int n;
n = 200;
start = 0.0;
end = 1.99E2;
Ramp (n, start, end, x); /* x[i] = i */
a = 3.5;
b = -2.75;
LinEv1D (x, n, a, b, y); /* y[i] = a*x[i] + b */
/* Find the best linear fit in z. */
LinFit (x, y, n, z, &slope, &intercept, &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 linear fit operation.
Output
Name | Type | Description
outputArray | double-precision array | The linear values that best represent the data.
slope | double-precision | Slope of line that best describes the data set.
intercept | double-precision | Y-intercept value that best represents the data.
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.