class="resources"
Finds the k-dimension linear curve values and the set of k-dimension linear fit coefficients, which describe the k-dimension linear curve that best represents the input data set using the least-squares solution. Details Examples

![]() |
Standard Deviation is the standard deviation, sigma[i] for data point (x[i],y[i]). If they are equal or you do not know, leave this array empty. Internally, LabVIEW sets all to 1.0. | ||||||||||||
![]() |
H is the matrix that represents the formula you use to fit the data set {X,Y}. H[i][j] are the function values of X[i]. | ||||||||||||
![]() |
Y is the observed data set Y. The number of elements in Y should be equal to the number of rows in H. | ||||||||||||
![]() |
covariance selector indicates whether to compute the Covariance matrix. | ||||||||||||
![]() |
algorithm specifies the algorithm the VI uses to compute Best Fit.
|
||||||||||||
![]() |
Coefficients is the set of coefficients that minimize chi-square.
The following equation defines 2.
|
||||||||||||
![]() |
Best Fit is the fitted data computed by using Coefficients. | ||||||||||||
![]() |
mse is the mean squared error. | ||||||||||||
![]() |
error returns any error or warning from the VI. You can wire error to the Error Cluster From Error Code VI to convert the error code or warning into an error cluster. | ||||||||||||
![]() |
Covariance is the matrix of covariances C with k-by-k elements. cjk is the covariance between ai and ak. cjj is the variance of aj. |
The following example demonstrates not only how to use the General LS Linear Fit VI to obtain the set of least square coefficients a and the fitted values, but also how to set up the input parameters.
The purpose is to find the set of least square coefficients a that best represents the set of data points (x,y). The relationship between x and y is of the form

where
a = {a0, a1, a2, , an 1}
and n is the total number of functions.
Assume the data is generated using the relationship
y = 2h0(x) + 3h1(x) + 4h2(x) + noise
where

noise is a random value.
Also, assume that you think the relationship between x and y is of the form
y = a0f0(x) + a1f1(x) + a2f2(x) + a3f3(x) + a4f4(x)
where

To obtain the coefficients a, you must supply the set of (x,y) points in the arrays X and Y and you must also supply the basis function H(X,i), which is a 2D array, to the General LS Linear Fit VI.
The arrays X and Y are the values observed in your experiment. A way to provide the basis function H(X,i) is shown in the following block diagram.

You can easily edit the Formula Node to change, add, or delete functions. At this point, we have all the necessary inputs to use the General LS Linear Fit VI to solve for a. The expected set of coefficients are
a={0.0, 2.0, 1.0, 4.0, 0.0}
Executing the General LS Linear Fit VI with the values of X, Y, and H(X,i) returns the following set of coefficients.

The following block diagram demonstrates how to set up the General LS Linear Fit VI application to obtain the coefficients and a new set of y values.

The icon labeled Data Create generates the X and Y arrays. You can replace this icon with one that actually collects the data in your experiments. The icon labeled H(X,i) generates the 2D H(X,i) basis function.
The last portion of the block diagram overlays the original and the estimated data points and produces a visual record of the General LS Linear Fit VI. The following graph shows the results obtained with the General LS Linear Fit VI.

The General LS Linear Fit VI has six different algorithms to obtain the set of coefficients and the fitted values. In this example, there is no significant difference among different algorithms. You can select different algorithms on the front panel to see the results. In some cases, different algorithms may have significant differences, depending on your observed data set.
Refer to the following VIs for examples of using the General LS Linear Fit VI: