# Curve Fitting in LabWindows/CVI

For the Curve Fitting functions, the input sequences arrayY and arrayX represent the data set y(x). A sample or point in the data set is (xi, yi), where xi is the ith element of the sequence arrayX and yi is the ith element of the sequence arrayY.

## Linear Fit

LinearFitEx fits experimental data to a straight line of the general form described by the following equation.

y = ax + b | (B)

LinearFitEx calculates the coefficients a0 and a1 that best fit the experimental data (x[i] and y[i]) to a straight line model described by the following equation.

y[i] = a0 + a1x[i] | (C)

where y[i] is a linear combination of the coefficients a0 and a1.

## Exponential Fit

ExpFitEx fits data to an exponential curve of the general form described by the following equation.

y = aebx | (D)

The following equation specifically describes the exponential curve resulting from the exponential fit algorithm.

| (E)

## Power Fit

PowerFit fits data to power function of the general form described by the following equation.

y = axb | (F)

The following equation specifically describes the power function resulting from the general power fit algorithm.

| (G)

## Logarithm Fit

LogFit fits data to a logarithmic function of the general form described by the following equation.

y = a logbase(bx) | (H)

## Gaussian Fit

GaussFit fits data to a Gaussian function of the general form described by the following equation:

## Cubic Spline Fit

CubicSplineFit fits the data by minimizing

## Goodness of Fit

GoodnessOfFit calculates three parameters, sse, rSquare, and rmse, which describe how well a fitted model matches the original data set. GoodnessOfFit uses the following calculations:

where is the mean value of the input observations.

## General Polynomial Fit

PolyFitEx fits data to a polynomial function of the general form described by the following equation.

y = a + bx + cx2 + … | (I)

The following equation specifically describes the polynomial function resulting from the general polynomial fit algorithm.

y[i] = a0 + a1x[i] + a2x[i]2 … | (J)

## General LS Linear Fit

GenLSFit fits data to a line described by the following equation.

y[i] = a0 + a1f1(x[i]) + a2f2(x[i]) + … | (K)

where y[i] is a linear combination of the parameters a0, a1, a2, ….

You can extend the concept of a linear combination of coefficients further so that the multiplier for a1 is some function of x, as shown in the following equations:

y[i] = a0 + a1sin(ωx[i]) | (L)

y[i] = a0 + a1(x[i])2 | (M)

y[i] = a0 + a1cos(ωx[i]2) | (N)

where ω is the angular frequency.

In each of the preceding equations, y[i] is a linear combination of the coefficients a0 and a1. In the case of GenLSFit, you can have y[i] that is a linear combination of several coefficients. Each coefficient can have a multiplier of some function of x[i]. Therefore, you can use GenLSFit to calculate coefficients of the functional models and represent the coefficients of the functional models as linear combinations of the coefficients, as shown in the following equations.

y = a0 + a1sin(ωx) | (O)

y = a0 + a1x2 + a2cos(ωx2) | (P)

y = a0 + a1(3sin(ωx)) + a2x3 + (a3/x) + … | (Q)

In each of the preceding equations, y is a linear function of the coefficients, although it might be a nonlinear function of x.

### Computing Covariance

GenLSFit returns a k &#times; k matrix of covariances between the coefficients ak. GenLSFit uses the following equation to compute the covariance matrix C.

C = (H0TH0)–1 | (R)

### Building the Observation Matrix

When you use GenLSFit, you must build the observation matrix H. For example, Equation P defines a model for data from a transducer.

y = a0 + a1sin(ωx) + a2cos(ωx) + a3x2 | (S)

In Equation P, each aj has the following different functions as a multiplier:

- One multiplies a0.
- sin(ωx) multiplies a1.
- cos(ωx) multiplies a2.
- x2 multiplies a3.

To build H, set each column of H to the independent functions evaluated at each x value x[i]. If the data set contains 100 x values, the following equation defines H.

| (T)

If the data set contains N data points and if k coefficients (a0, a1, …, ak – 1) exist for which to solve, H is an N &#times; k matrix with N rows and k columns. Therefore, the number of rows in H equals the number of data points N. The number of columns in H equals the number of coefficients k.

## Nonlinear Curve Fit

NonLinearFit fits data to the curve according to the nonlinear Levenberg-Marquardt method, as described by the following equation.

y[i] = f(x[i]), a0, a1, a2, … | (U)

where a0, a1, a2, … are the parameters.

The nonlinear Levenberg-Marquardt method is the most general curve fitting method and does not require y to have a linear relationship with a0, a1, a2, …. You can use the nonlinear Levenberg-Marquardt method to fit linear or nonlinear curves. However, the most common application of the method is to fit a nonlinear curve because the general linear fit method is better for linear curve fitting. You must verify the results you obtain with the Levenberg-Marquardt method because the method does not always guarantee a correct result.