Polynomial fitting on Mitsubishi FX5u

Such a fiery speech...
I have been on this and other forums for a long time now. 20+ years. I see the same thing over and over again. Matlab users are very bad. They get answers without understanding.
@MaxK, did you recognize the least-squares algorithm?
Although I have to admit that I agree with this speech... (Although the Runge-Kuta method...)
What about Runge-Kutta? It is easy to restart if a boundary is hit. It isn't the most accurate but when working on real data that isn't accurate to 6 decimal places, it doesn't make much difference. It is simple enough to write on your own and implement in a motion controller. It may be tedious in a PLC. Mathcad does have a RK function.
In any case, the engineer would have stated the requirements and constraints from the process side, rather than what is stated in #1….
But you know this without me - so the question is: why did you burst into such a fiery speech?
Because I see the same thing over and over again and the OPs don't provide the information needed up front. The OP just needs to decide what he wants to do. I have provided two algorithms now. One that is a least squares fit and another that will go exactly through the data points ( knots ).
PS I don’t entertain myself with the illusion of being able to change the world. Human laziness is a circumstance that cannot be overcome
Yes, but what gets me is that everyone comes to the forum thinking they are the first to have their particular problem when it has all be solved long ago.
No one can refute that.

Also, drbitboy wasn't doing a good job of extracting teeth.
 
I have been on this and other forums for a long time


Another demonstration of the uselessness (ineffectiveness) of human communication

For Peter Nachtwey special:

Sarcasm/irony mode OFF

99% of what I write is sarcasm/irony, but you completely lack the ability to perceive sarcasm/irony. You are a very serious person (sorry, sarcasm again)

In view of the above, your perception of my messages is extremely ineffective, so I absolutely kindly recommend that you do not read or respond to my posts.

Sarcasm/irony mode ON

What about Runge-Kutta?

All demonstrations of the Runge-Kutta approximation that you published on this resource were made using the program (“library”).
If you coded the RK approximation yourself, then for me it is even more surprising that you ignore the assumption of the initial conditions of the method.

Based on your surprise at my statement about the significantly greater difficulty of finding roots for systems with dead time (transcendental equations an*s^n +…+ am*s^m*exp(-T*s) +…+ a0 = 0) compared with algebraic equations (an*s^n +…+ am*s^m +…+ a0 = 0), I conclude that you did not make codes to solve these equations, but used programs (“libraries”)

I.e. everyone uses libraries craves a panacea: me, you, OP... it doesn’t matter.

in view of the above, all your fiery speeches, as well as 99.99...% of human communication are useless (except for narcissism, which is what I am doing now)
 
All demonstrations of the Runge-Kutta approximation that you published on this resource were made using the program (“library”).
If you coded the RK approximation yourself, then for me it is even more surprising that you ignore the assumption of the initial conditions of the method.
I wrote my first Runge-Kutta program as a college assignment my sophomore year of college which is 1972-1973, It was written in Fortran using a card punch machine and a deck of cards. The dark ages. I now use the built in libraries because they are written in C and execute much faster than my interpreted code. On python I used odeint. However, I have a python RK script that I wrote before I discovered odeint.
Here is an example of writing Runge-Kutta from scratch from 2012. See the top of page 5/11. I was giving a senior year engineering college student grief for not knowing the basics. I was showing him how easy it is. The student was trying to control the water level in the second tank for a senior project.
 
Can the FX5 do matrix math? Does it have structured text? Otherwise, you will need to type in some long equations in ladder. Not fun.
Also, this is made harder because the x locations are not evenly spaced.
Can't you just translate the python code into ladder or are you using a python library.
Also must the third order equation go through each point or just to a best fit.
It takes only 4 points and 4 equations to calculate the 4 coefficients for a 3rd order equation. The system of equations is over determined so you need to settle for a best fit.
Hello, FX5 doesn't have matrix math
yes he has st
I can't translate the Python code to Ladder because I use Numpy
I can have from 5 to 10 points
 
Hello, FX5 doesn't have matrix math
yes he has st
I can't translate the Python code to Ladder because I use Numpy
I can have from 5 to 10 points
Before someone can suggest a useful answer (although Lagrange interpolation looks like the best bet so far), there are more questions that were asked that have not been answered (e.g. "Also must the third order equation go through each point or just to a best fit").

Also, here are a couple more:
  • how closely to do you want the FX5-calculated polynomial coefficients to match the Python example?
  • what level of accuracy is required?
  • how would you measure that accuracy?
 
By the way, this is probably what that Python module is solving. Coding a general solution for that into ladder, or even into ST, is a non-trivial exercise; Lagrange interpolation is looking better and better, even though it would still be ... interesting ... in ladder.
 
I asked some questions in #6 that weren't answered. I have solutions if a least squares are required.
The OP didn't say if the resulting equations need to go through the points. IT MAKES A HUGE DIFERERENCE!

The OP highlighted what I don't like about using libraries or packages. They are great at getting answers AS LONG AS YOU CAN USE THE LIBRARIES.
I see the same thing happen on other forums where they use Matlab to solve their problem but have NO CLUE as to how to move their solution to a different platform. Getting answers without understanding isn't much good.

I know the algorithm that is used in np.polyfit(). One can look up the source code. The easiest way to implement it is to use matrix math. However, I have generated the solutions symbolically.
Look at the solution %04. It is the symbolic solution ( the perfect solution ) to fitting a 11 points to a 3rd order polynumial. The difference is that I have assumed that all the points are equations spaced by interval T. If the points aren't evenly spaced then I must use intervals like T12 for the distance between point 1 and point 2. Now the symbolic solution is much more complicated.
If you don't have matrix multiplication, then you need to computer the symbolic solution like in %04. Then you can plug those formulas into the PLC and solve for the 4 coefficients for least squares fit 3rd order polynomial

%O30 has a the equations for computing the coefficients for a 3rd order polynomial using 5 points but again, I assumed all the points are equally spaced. The equation gets to be much more complicated when there are intervals like T01,T12,T23, T34, I would calculate the coefficients for the third order polynomial and then just execute the 3rd order polynomial in the PLC. This works as long as the points don't change.

Why can't the OPs give us the info we need in the first post instead of wasting everyone's time. I could have had the OP's solution computed within an hour of his first post. Instead a lot of time has been wasted.
Hello,if you know the algorithm inside the polyfit and you are able to repeat this code on structured text or in python without use of external libs can you explain me how to do it, the value that I need to find is not necessarily in the middle of the array of measurements, I understand that polyfit can be used for least-squares curve fit for any order.
The method of polyfit is to construct the vansermonde matrix and solve it via qr factorisation.
But as I told you before now I'm only able to build the vansermonde matrix in the plc, but I'm not able to go on.
I use polyfit because the results that I get give me good results.
 
there are many methods to "solve" the equations represented by the matrix. numpy.polyfit is essentially a wrapper for numpy.linalg.lstsq, and numpy.linalg used SVD and QR-factorization, as you note.

Do you need the calculation of polynomial coefficients to happen in one scan cycle when new sample values appear, or could it wait for many scan cycles, e.g. a few seconds? Because your problem has a small matrix, and there are slower methods (e.g. Gaussian elimination, which is O[N³]) that might be easier to code if speed is not an issue.
 
Here is how to build the matrix (it's not the vander-whatever matrix).

Here is the Gaussian Elimination method to solve a 3x3 matrix.

Getting both of those into ladder will take a lot of effort to be sure, but the steps are straightforward.

I still think Lagrange Interpolation would be simpler.
 
Dear all sorry if i'm not able to reply in a correct way.
I just say that i don't need better results of what polyfit do, this result are the one that i need that help me to get the value that i can use .
the procedure that we do and satisfy us is as follow.
We have a bending machine
we make some tests minimum on 5 value max 10 of y, for each value we measure the radius of the profile
than for know the position where to set the y axes for obtain a radius that is in the range of the 5 or 10 measured we just use polyfit of 3rd degree and we fit perfectly de desired radious.
My question for me that probably i'm stupid was simple, there is a way to do the same with a mitsubishi fx5u without the use of the pc.
Sorry if you have lose time
Regards
Luca
 
My question ... was simple, [is there] a way to do the same with a mitsubishi fx5u without the use of the pc?
Yes there is: Writing code to do Gaussian Elimination (actually Gauss-Jordan; see here; there is a flowchart that may be helpful for converting this to Ladder Logic) of a 4x4* array is probably the easiest way to replicate, on the PLC, the functionality of the Python numpy.polyfit routine to calculate the third-order polynomial coefficients to model a least-squares fit to the 5 to 10 [position, radius] pairs. It will be much easier with Structured Text, if that is available, but it can still be done with Ladder Logic. There are other, faster ways to factor the array, but they are more complex and would be harder to code and debug. With Gaussian Elimination, there are only a few operations: scaling a row; adding one (scaled) row to another; swapping rows, It might not be necessary to swap rows. The only problems will be an ill-conditioned array and numerical instability.

The time we lost waiting for the answer is small; the time you will spend, or that you pay someone to spend, coding that solution will not be small.

* When I wrote "3x3" earlier that was a mistake; I was forgetting the zeroth-order term.
 

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,356
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