LinFit
- Updated2023-02-21
- 2 minute(s) read
Advanced Analysis Library Only
AnalysisLibErrType LinFit (double arrayX[], double arrayY[], ssize_t numberOfElements, double outputArray[], double *slope, double *intercept, double *meanSquaredError);
Purpose
![]() |
Note This function has been superseded by LinearFitEx. |
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 ith element of the output array, using the following formula:
Zi = slope × Xi + intercept
LinFit obtains the mean squared error using the following formula:

where numberOfElements 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 Code
/* 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 [] | 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 sample points used in the linear fit operation. |
Output | ||
Name | Type | Description |
outputArray | double [] | The linear values that best represent the data. |
slope | double | Slope of line that best describes the data set. |
intercept | double | Y-intercept value that best represents the data. |
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 3.1 and later