Process Control: PLC Curve Fitting via Structured Text

April 24, 2012
By Abdel Hezzini

Measuring Efficiency To Help Increase Production

In order to ensure the efficiency of an automated process, data is often collected, logged, and subsequently analyzed for continuous improvement. However, extracting the most useable information from the collected data from applications, such as temperature, flow, positioning, or velocity control, requires optimized models that best represent the data itself. Once optimized, the resulting model will help increase production yields by minimizing the sources of error. This, in turn, frees up valuable resources that otherwise would have been wasted in the non-optimized state.

For example, a measurement is taken from the analog output of a laser measurement sensor, which detects the position of an incision needle. That measurement is sent to a PLC and is logged at a specified sampling cycle. To analyze this measurement data, like inferring values where no data is logged, or determining velocity over a set interval, an analytical linear model function, f(x) = mx + b, can be created.

Creating Linear Equations

There are two methods that can be used to create a linear equation from the collected data.

  • Linear interpolation, where the two sets of data are connected resulting in multiple functions across the entire range.Linear interpolation is efficient when the data is reliable but in other cases, where the some data is susceptible to corruption, this function is limited due to the small groups of small f (x)’s. It also does not work very well when the data has noise (scatter). 
  • Curve fitting, a single trend in the data resulting in a single function across the entire range. Curve fitting, however, is less susceptible to noise (scatter) since it assigns a single f(x).    


The idea behind linear curve fitting is to find a straight line ((x) = mx + b) that fits the data points well, or that captures their trend well. Finding the coefficients of the line m and b are the key in establishing that the line has a “good fit.” In order to determine a good fitting line, the distance between the recorded data and the points on the line (Figure 2: blue lines) can be considered.   


The difference between the y value of the function (f(x)=mx+b) and the y value of the data point (yi) is the deviation. This is the vertical distance between the known data and the fit line. The summation of the deviation (Figure 2: blue lines) can be perceived as the error. This error is used in determining the best fitting line (Figure 2: red line). The function with the least error will best represent the data points, and an ideal line will have zero error. An algorithm that will compute the unknown parameters can now be modeled. What is known is the data points, the function of a line, and the fact that the line will have a minimum error. Calculation of the error is already known. It will be the summation of the deviation which is:

Optimizing the error function (partial differentiation with respect to m and b) yields the minimum, and now the constants (m and b), of the line that represents that data points, can be solved for. This yields two equations with two unknowns (the unknowns being m and b). Rearranging the equations into matrix form yields:

Programming Data As a PLC

Now that the formula has been derived, it can be programmed as a PLC project (e.g. FPWIN PRO PLC Programming Suite) and designed as a function block that can be used in multiple instances. The inputs are the collected data samples, and the outputs are the constants of the function for the linear curve fitting model.

This function block can be programmed in the structured text language, and has the capability of having instances when it can be recreated. Each instance retains its own private data, therefore multiple data sets can be analyzed, and multiple line fittings can be outputted. Figure 3 shows the structured text for the curve fitting function block (characters in green are the values of the variables in each instruction). Program for Curve Fitting Function Block

Sample data points were moved into the inputs of created function block.

After the function block is executed, the best fitting line constants (m and b of f(x)=mx +b) are output.

The resulting line can be fed to excel, and the linear model is shown in the graph below (Figure 4).   


With the use of one function block, this linear model was created for the set of collected data points. Values can now be calculated for where no data had been collected, and data samples that were corrupted (e.g. inaccurate measurements due to vibration), can now be neglected. Ultimately, using Linear Curve Fitting to analyze your data can aide in quality control, lead to  increased production line efficiency and better product design.

For information on how create a curve fitting function block using structured text please check back to read my upcoming article, Programming in Structured text: Curve Fitting.

Contact Us With Questions