I have wondered about the same thing myself, but the most complicated algorithm I have implemented to date is the ASTM conversion polynomials for Type G thermocouples (ASTM Vol. 14.03, Designation E 1751 - 95). I used a 5th order from the Omega catalog to convert the CJC signal in a 1746-NT8 to the Type-G's milivolts, adjusted the thermocouple signals, and ran the result through either a 10th or an 11th order polynomial, depending on the voltage/temperature range, to convert the milivols to degrees C. The 10th and 11th order polynomials required two computation blocks each.
Why am I babbling about temperatures and polynomials? Because this seemingly simple task quickly became a large effort when implementing it in ladder. I fear that a linear regression algorithm would become a monster in ladder. You may want to run your regression on an HMI/SCADA PC and feed the results back down to your PLC.
I have been interested in doing such algorithms for advanced control purposes so if I am wrong about the "monster" and you find a way to do it, please let me know ([email protected]).
Our original intent was to transfer the data from the plc into a pc, where it would be a really simple matter to perform the analysis. But (there's always a but) our system needs to perform the analysis quickly and it takes too long to perform the data transfer. Hence my need to do the analysis in the plc.
I think I may have found a decent algorithm that will work. Its a snippet of basic code that looks like it will convert nicely into a plc subroutine:
Dim P as long ' Number of Points
Sum_xy=0
Sum_x=0
Sum_y=0
Sum_x2=0
Sum_y2=0
FOR I=1 TO P
Sum_xy=Sum_xy+X(I)*Y(I)
Sum_x=Sum_x+X(I)
Sum_y=Sum_y+Y(I)
Sum_x2=Sum_x2+X(I)*X(I)
Sum_y2=Sum_y2+Y(I)*Y(I)
NEXT I
Slope=P*(Sum_xy)-(Sum_x)*(Sum_y)
Denom=P*(Sum_x2)-(Sum_x)^2
IF Denom<>0 THEN
Slope=Slope/Denom
ELSE
Slope=0
END IF
Intercept=Sum_y/P-Slope*(Sum_x/P)
D=P*(Sum_xy)-(Sum_x)*(Sum_y)
Denom=SQRT((P*Sum_x2-Sum_x^2)*(P*Sum_y2-Sum_y^2))
IF Denom<>0 THEN
D=D/Denom
ELSE
D=0
END IF
This looks nice and straightforward to me - as long as the results are correct. I'll let you know how it works.