Programming a curve for 4-20 mA input

The best way I know of is to use a look up table
You could be able to create a table with as many points as you need to get accuracy you want.
 
That is what I love about this Forum!! I have been using the Excel Polynomial method for years. It has working for the most part but was always off at the extreme ends of the curve.
According the the documentation, FGEN uses a simple linear piecewise interpolation. FGEN does not use a polynomial or even splines. A low order polynomial cannot fit all the points which is why a polynomial will be off somewhere.
 
According the the documentation, FGEN uses a simple linear piecewise interpolation. FGEN does not use a polynomial or even splines. A low order polynomial cannot fit all the points which is why a polynomial will be off somewhere.

I agree with Peter.

When you develop a polynomial in Excel (or many other regression methods) you can select the order of polynomial. I've found that third or fourth order will accurately model some pretty complex data. Excel will also provide R² which is an indication of how well the calculation fits the data. In my experience you usually need to carry the coefficients to four or five significant digits to maintain accuracy in the prediction.
 
Context: I am not necessarily advocating a curve-fit approach to every problem like this. There are times when a function-based model does not estimate actual measurements with sufficient accuracy, or it is simply preferred to use another approach such as piecewise linear or more complex fits.

That said, I was working with the data in this example to show a few different options for generating function-based models with Excel. The data table and chart below shows four different models, with a visual depiction of their performance based on residuals. The residual is just the difference between the model's estimation and the actual measurement at each known measurement value.

The four examples are: (1) Linear, as a baseline to which improvement is desired, (2) Poly 2, a second order polynomial, (2) Poly 2+, also second order, but with the 0 and 100 level measurements repeated 4 times, and (3) Poly 3, a third order polynomial.

One take-away is that you can improve the fit (reduce the residual) over certain regions of the fit by repeating those measurements in the curve fit routine. This was the point of Poly 2 vs. Poly 2+ which reduced residuals at the ends, but increased them in other regions.

Another is that the third order fit was great until about 40% on the input, where the residuals took off (literally) exponentially.

Since the piecewise linear model, by definition, sets the estimate to the actual at each point, it is not really possible to compare its performance in a similar manner. And since the values between each measurement are not known, it is not really possible to precisely assess the error due to measurement non-linearity between those points. Of course, the more points the better. (The extreme being just the end points, which is the linear model shown below.)

In any case, if the model (linear, curve, piecewise) error is significantly less than the instrument measurement error, it will not make that much difference in overall performance which is selected. In that case, other factors such as programability, supportability, run-time load, documentation, etc. would drive the decision on which to use.

residuals.jpg
 
One method that i have used for complex curves is to break up the data into two seperaelte sets of data create curves on each set. You try to slice the data into 2 reasonably linearish sets. You are trying to make it so that the curves get a better r2 value so a better aproximation. Then in plc you compare input value to a threshold to determine if you should use equation a or b. I successfully used this method for calculating saturation temperature of refrigerant R507 for doing superheat calculation and control.
 
awesome!

I wonder if I can apply this to my drives not following a linear decel ramp?
Dang it. I'm going to have to aquire a lot more data.
 
There are times when a function-based model does not estimate actual measurements with sufficient accuracy, or it is simply preferred to use another approach such as piecewise linear or more complex fits.

I usually express the residual as percent error. That helps put it in context.
 
Last edited:
First, In think the name FGEN is misleading because it it really does imply a function is used rather than piecewise linear interpolation.

There are higher order methods of interpolation. Cubic splines is one example. Cubic splines are used for motion cam tables. The spline always goes through the data points. Often cam blocks are build in functions of the PLC. Use them if you have them.

Ditto the comment about once the residual is less than the instrument measuring error the estimating function is good enough. Piecewise linear interpolation can be good enough if the points are close enough together.
 
If your NH3 transmitter is accurate to 0.1% of Full Scale, a third order polynomial will calculate as accurately as you can measure. Fourth order will calculate more accurately than the measurement.

Check your specs, but I doubt if an analytic transmitter is going to be better than 0.1% of FS.
 
According the the documentation, FGEN uses a simple linear piecewise interpolation. FGEN does not use a polynomial or even splines. A low order polynomial cannot fit all the points which is why a polynomial will be off somewhere.

I agree with Peter.

When you develop a polynomial in Excel (or many other regression methods) you can select the order of polynomial. I've found that third or fourth order will accurately model some pretty complex data. Excel will also provide R² which is an indication of how well the calculation fits the data. In my experience you usually need to carry the coefficients to four or five significant digits to maintain accuracy in the prediction.

I have been using the polynomial method to calculate tank volumes based on a tank strapping chart. The chart gives many different levels with a volume for each level. So in this case the FGEN is exactly what I need. At the data points I need the exact number shown in the charts and between the data points do a linear Interpolation between the point above and below the current reading.

For the most part with vertical tanks the equation would be very accurate. When I applied the polynomial method to recent job that used horizontal Propane bullet tanks the equation was way off at the bottom and at the top of the range. in the middle is was pretty accurate. With the FGEN now when the levels are right on the data points it shows the same number as the charts. This makes the customer feel good although who knows how accurate the charts are in the first place.

I am sure that for a complicated curve the Polynomial would be the best way to go.
 
I am sure that for a complicated curve the Polynomial would be the best way to go.
It depends. If you KNOW the data points are accurate then piece wise linear or cubic interpolation is the way to go. If the data is noisy then fitting a polynomial or some other function is better but you really need to know which function to use. Sometimes it is best to try many functions then choose the one with the smallest sum of squared errors.
 
Quoting myself:

...Another is that the third order fit was great until about 40% on the input, where the residuals took off (literally) exponentially.

When I looked at Mr. Jenkins' model in post #24, I saw that his third order results, in terms of residuals, did not show this exponential increase. We both used Excel and there are no typos in the data. What's up?

Cutting to the chase...

When you develop a polynomial in Excel (or many other regression methods) you can select the order of polynomial. I've found that third or fourth order will accurately model some pretty complex data. Excel will also provide R² which is an indication of how well the calculation fits the data. In my experience you usually need to carry the coefficients to four or five significant digits to maintain accuracy in the prediction.

When I implemented the third order estimation function, I used the default Excel number formatting for the regression equation. Most importantly, my third power coefficient for the chart in post #19 is -0.0001. When that coefficient is displayed in scientific notation with 6 digits of precision, it is really -1.49516E-04.

The impact of this at the 100% signal level where measured concentration is 1000 PPM: the Low Precision x-cubed coefficient (-0.0001) estimates 1048.8 [4.9% FS error] and High Precision (-0.000149516) estimates 998.9 PPM [-0.1% FS error].
There is no exponential increase; it all had to do with rounding off a critically important parameter.

Thank you for the follow-up, Mr. Jenkins. Lesson Learned.
 
Last edited:
Thank you for the follow-up, Mr. Jenkins. Lesson Learned.

You are quite welcome.

For those not used to using the trendline method, it can be a bit tricky getting more decimal places to display. As Mispeld indicates, the default display is inadequate.

The easiest way to get more decimal places is to right-click on the trendline equation displayed on the chart. From the pop-up menu select "Format Trendline Label". The format box should pop up. Click the far right (three bar) menu item, select Scientific in the Category menu, and enter the desired number of decimal places below. You can then cut and paste each coefficient into a spreadsheet cell.

I am explaining this because in the past I have spent a lot of time, punctuated by much profanity, trying to figure out how to get more decimal places.

There are built-in Excel functions for regression, but for a quick one-time development, it's hard to beat the chart method.
 

Similar Topics

Hello all, I need to write a code for S-curve motion profiles (7 segments) to control speed of motor via PLC. however, I cannot find the...
Replies
56
Views
16,742
Does anyone can explain me how to do s-curve (for increasing and decreasing speed of the process ) programming in siemens simatic S7. If...
Replies
1
Views
1,856
Hello colleagues, Some time ago I started my adventure with programming. With your help and the courses, things are starting to come together...
Replies
13
Views
581
Dear All, I need a sample PLC program to count the output pulse of a mass flow meter so that a specific amount of mass (for example 100gm)can be...
Replies
2
Views
77
Hi Please I have zeilo smart relay #SR2A201BD but I don't have it's programming cable. Can I use any general usb/rs232 converter? Or need...
Replies
2
Views
78
Back
Top Bottom