Polynomial fitting on Mitsubishi FX5u

I can't see where you have provided a solution that runs exclusively on a PLC.

"Living is easy with eyes closed ...;);" click here to see an earlier post with a solution running on an S2-1200.

That is a lot of matrix manipulation if the OP has 10 points or for a general solution for 5 to 10 points.

The number of sample points is irrelevant to the size of the problem; the matrix to satisfy OP's request of a cubic fit, equivalent to numpy.polyfit with deg=3, is always a 4x4 matrix (4x5 augmented).

We still don't know why a least squares fit is required instead of a cubic spline that goes through all the points ( knots ).

Why would we care why they prefer a cubic fit? Maybe the samples have measurement error so a cubic fit is a better model, but whatever the reason, a cubic fit is what OP asked for, and we have stated many times that Lagrange interpolation (as originally suggested by you) or a spline might be simpler, so it is up to them to choose.
 
So does the FX5 have ST? I know that some PLCs can do some array math because they have ST. Your solution will result in a cubic fit because you have 4 equations with 4 unknowns. The OP seems to be avoiding the cubic fit in favor of the least squares fit.I still think you solution is more difficult than solving the problem symbolically then hard coding the equations using the static area. We still don't know if the motion is periodic/cyclic.
 
It does have ST, you can easily do almost anything you want it has a rich set of instructions I think it would be possible to do this in ladder or FBD as well, the only thing is how much processing time it would take but the FX5 is definitely faster than the older FX3U whichj could do a fair bit of this type of processing.
The only thing it does lack is 64 bit (double precision maths)
 
I'll put this thread out of its misery: the attached PDF demonstrates a PLC-based cubic fitting routine; it uses Siemens S7-1200, but the SCL should be straightforward to port to ST in the FX5.

The cubic fit routine marshals the values used in the augmented matrix elements from the [X,Y] sample input pairs using two cascaded FOR-loops per the formulae at this link. The fit routine then does a simple Gauss-Jordan Elimination, without pivoting or any other optimization to improve numerical stability.
  • Page 1, ladder rung ("Network") 1 (only rung): if the value of boolean m.trigger is 1, then the main program resets that value to 0, and calls
    • the fitting function (FC) "cubic_fit"
      • Calculate cubic fit polynomial, using the following parameters:
      • xsamples
        • input
        • sample of the independent variable "X"
        • ARRAY[0..9] of REAL
      • ysamples
        • input
        • samples of the dependent variable "Y"
        • ARRAY[0..9] of REAL
      • iN
        • input
        • the number of pairs of samples in each input parameter array m.xsamples and m.ysample to fit
        • DINT
        • allowable range is [4:10]
      • coeffs
        • input/output
        • the array to receive the cubic fit's polynomial coefficients
        • ARRAY[0..3] of REAL
          • m.coeffs[0] is the zeroth-order term
          • m.coeffs[3] is the third-order term
      • cubic_fit
        • FC return value
        • BOOL
        • TRUE if fit was successful
        • FALSE if not, e.g.
          • ill-conditioned matrix,
          • invalid value for input N
    • the evaluation function FC "eval_xsamples"
      • evaluate polynomial at the value of the m.xsamples array, using the following parameters
      • xsamples
        • input
        • sample of the independent variable "X"
        • ARRAY[0..9] of REAL
      • coeffs
        • input
        • the cubic fit's polynomial coefficients
        • ARRAY[0..3] of REAL
      • iN
        • input
        • the number of pairs of samples in each input parameter array m.xsamples and m.ysample to fit
        • DINT
        • allowable range is [4:10]
      • yevals
        • input/output
        • the array to receive the cubic fit's results evaluates at the first m.N elements of m.xsamples
        • ARRAY[0..9] of REAL
      • eval_xsamples
        • FC return value
        • BOOL
        • TRUE if evaluation was successful
        • FALSE if not
      • FC "eval_xsample" loops over the first m.N elements of m.xsamples, and calls
        • the evaluation function FC "eval_cubic"
          • evaluates polynomial at one X value, using the following parameters
            • coeffs
              • input
              • the cubic fit's four polynomial coefficients, Ci for i=0 to 3
              • ARRAY[0..3] of REAL
            • xsample
              • input
              • the X value at which to evaluate the cubic polynomial
            • eval_success
            • FC return value
            • REAL
            • value = Σi=0 to 3(C * xi)
              • Yes @Peter Nachtwey, I used the quick method, not the method that raises x to the various powers
 

Attachments

  • polyfit_tia_portal.pdf
    275.5 KB · Views: 16
Thanks to all for your patience if you need you can ask me for the gxwork3 file by mail, sorry in the forum the max admitted is 400 KB so I can't attach the file
Luca
 
I'll put this thread out of its misery:
What? Its not dead yet?
FOR-loops per the formulae at this link.
Just above the expanded formulas is what I posted long ago. Also, it uses the Vondermonde matrix as the OP said he uses. The Savitsky-Golay is the same thing only it is only applied to a small window of all the data. It is used for smoothing data. To do it the hard way it requires doing some matrix multiplies and an inversion. However, in my example I assign variables to all the entries and solve symbolically. Now there is no matrix math. Normally one fits to all the data, but one can fit to 4 points too and get a true cubic fit but then Lagrange interpolation looks simpler for the same result. It is amazing how much effort you guys are putting into avoiding the simple earlier solutions.

I would use sympy to solve the problem symbolically and then export the formulas. They can be used in ST by just editing the = to :=.
 
Actually, m > n = 3 (both zero-based), so it's not the Vandermonde matrix that needs solving here.

Savitzky-Golay requires samples at equally-spaced independent variable values, which OP does not have, so it is useless here.

And simple row-by-row elimination is good enough for n=3. I have said all along that Lagrange interpolation might be simpler, but the least-squares solution is what OP asked for.

I am the only one who has provided an actual solution that runs on a PLC, and not just critiques and suggestions for a different approach. If OP understood @Peter Nachtwey's suggestions, then this post wouldn't have been created in the first place.
 
Actually, m > n = 3 (both zero-based), so it's not the Vandermonde matrix that needs solving here.

Savitzky-Golay requires samples at equally-spaced independent variable values, which OP does not have, so it is useless here.

And simple row-by-row elimination is good enough for n=3. I have said all along that Lagrange interpolation might be simpler, but the least-squares solution is what OP asked for.

I am the only one who has provided an actual solution that runs on a PLC, and not just critiques and suggestions for a different approach. If OP understood @Peter Nachtwey's suggestions, then this post wouldn't have been created in the first place.
The array is is the same Vandermonde matrix and the formula is the same.
 

Similar Topics

If a polynomial is used in a cam-profile for motion control, you normally have the option to displace the inflection point from the standard 0.5...
Replies
1
Views
1,448
We recently purchased a used piece of equipment and i am trying to modernize the control system, but I'm not very familiar with it. This machine...
Replies
8
Views
2,357
I have two NEMA cabinets that I need to pass some cables between. The two cabinets are mounted on the outside, either side of a 90 degree corner...
Replies
5
Views
2,714
Hi There, We have a hydraulic power unit we are building for a Class 1 Div 1 application. The customer wanted a specific kind of proportional...
Replies
0
Views
5,144
Back
Top Bottom