Introduction to Curve Fitting
The NI LabVIEW development environment and the G programming language are excellent tools for curve fitting applications. The technique of curve fitting analysis extracts a set of curve parameters or coefficients from a data set to obtain a functional description of the data set. In some applications, parameters such as humidity, temperature, and pressure can affect the data you collect. You can model the statistical data by performing regression analysis and gain insight into the parameters that affect the data.
For the Fitting VIs included in LabVIEW, the input sequences Y and X represent the data set y(x). A sample or point in the data set is (xi, yi), where xi is the i th element of the sequence X and yi is the i th element of the sequence Y. The LabVIEW Advanced Analysis Library offers both linear and nonlinear curve fitting algorithms, including linear, exponential logarithmic, cubic spline, and polynomial.
Figure 1. VIs for Curve Fitting in LabVIEW
As an example, use the following LabVIEW program to generate the data you will apply to the linear curve fitting example.
Figure 2. VI for Generating Data
As you can see, white Gaussian noise is used. Use this program, saved as “Linear Data.vi,” in the following example to generate data. This program creates data according to the following equation:
y = a*x + b + noise, where noise = White Gaussian Noise
Using the linear least squares (LS) curve fitting method, the following program both generates the data (50 points with a=1, b=0, and a noise magnitude of 0.8) and fits the data.
Figure 3. Linear Least Squares Curve Fitting Program
The results are shown in Figure 4. The fitted line (red color) and the data point (white color) are very close.
Figure 4. Linear Least Squares Curve Fitting Results
For exponential fit, the following equation is used for generating the data you will use in the example:
y = a*exp(b*x) + c + noise , where noise = White Gaussian Noise
The following program (saved as Exp Data.vi) generates the data to be fitted using the exponential curve fit:
Figure 5. Program for Generating Data for the Exponential Curve Fitting Example
The following program implements both data generation and exponential curve fitting:
Figure 6. Program for Generating and Fitting Data for the Exponential Curve Fitting Example
Figure 7. Exponential Curve Fitting Results
The following program creates data according to the following model:
y = a*(b^x)
The following program generates data according to the previous model:
Figure 8. Program that Generates Data for the Power Fitting Example
The following program generates data and fits it using the power curve fitting method:
Figure 9. Program that Generates Data and Fits It Using the Power Curve Fitting Method
Figure 10. Results of the Power Fitting Example
Polynomial curve fitting is also included in the LabVIEW Mathematics Library. The following example shows a polynomial curve fitting for a uniform white noise signal:
Figure 11. LabVIEW Program for Generating Data and Polynomial Curve Fitting Example
Figure 12. Polynomial Curve Fitting
LabVIEW also includes the generalized least squares linear fit function, which finds the k-dimension linear curve values and the set of k-dimension linear fit coefficients that describe the k-dimension linear curve that best represents the input data set using the least squares solution. As an example, suppose you have collected samples from a transducer (yValues) and you want to solve for the coefficients of the model:
In this case, a matrix H (observation matrix) is needed. To build H, you set each column to the independent functions evaluated at each x value. Assuming there are 100 x values, the following equation gives H:
Given that you have the independent X values and observed Y values, the following block diagram builds H and uses the General LS Linear Fit VI:
Figure 13. Application of the General LS Linear Fit VI
For nonlinear curve fitting, use the Levenberg-Marquardt (Lev-Mar Fit) to determine the least squares set of coefficients that best fits the set of input data points (X, Y) as expressed by a nonlinear function y = f(x,a), where “a” is the set of coefficients.
This example creates a general exponential signal using the following model:
Signal = a*exp(b*x) + c + noise
Use the Nonlinear Lev-Mar Fit VI to fit the data and get the best guess coefficients, a, b, and c, of the general exponential signal. The following program both generates the data and fits it using the Lev-Mar Fit VI:
Figure 14. Nonlinear Curve Fitting Using Lev-Mar Fit VI
The results obtained are as follows:
Figure 15. Nonlinear Curve Fitting Results Using Lev-Mar Fit VI
In the Nonlinear Curve Fit LM Formula String instance of the Nonlinear Curve Fit VI, you can describe the nonlinear function f in model description. The following block diagram shows how to define f according to the following equation:
f(x) = a1 + a2*x + a3*x^2
where x is the independent variable and a1, a2, and a3 are three unknown coefficients. If needed, you can define models with a string.
Figure 16. Nonlinear Curve Fit LM Formula String Instance
As indicated in Figure 16, the model is defined with the following string:
a1 + a2*x + a3*x^2
You can develop other curve fitting methods in LabVIEW or add other methods to it via external tools. In summary, LabVIEW is an excellent platform for curve fitting − for both online and offline data analysis. You can use other external tools such as NAG (DLLs) with LabVIEW via the Call Library Function VI for additional curve fitting options.
Related Links
Scientific Computing with Graphical System Design
Scientific Computing with NI LabVIEW
Scripting Languages and NI LabVIEW
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).
