Peter Ponders Linearization.

Join Date
Apr 2002
Location
No income tax, no capital gains tax. Freedom!
Posts
8,380
The have been a couple of threads lately about finding outputs as a function of non-linear inputs. PhillipW had a problem where he needed an output as a function of 5 inputs and JPolidore needed to linearize the flow as a function of an output to a valve.

As it turns out I too need to be able to linear hydraulic valves so I thought I would put some extra effort in to this topic.

I don't expect that there would be more than about 5 to 10 people that will understand the data in the link below but then there is a tradition that must be upheld.

Linearization using Least Squares.

This is not the least squares fit that is taught in the basic statistics class. This is the general case that is able to appriximate an equation of just about any complexity.

I use least squares all the time. Often I will will have 1000 or so points and need to calculate 6 or 7 coefficients. Now you may want to know why I would want to do that. I can determine the coefficents of a equation that models a system. This allows me to predict how a system will respond up to 1000 iterations into the the future. This is handy when trying to control a system.

PhillipW, why would you want to use fuzzy logic when it is possible to empirically calculate the function you need with minimal error? You do know that the least square method minimizes the error squared?
 
Interesting link. I got lost in the middle of the document
once they introduced "matemagical formula" :eek:

Personally, I will still rather use lookup tables because
this approach in my opionion is way better:

- exact values instead of approximated calculation
(for second value which is "1", calculated result
using floating point math is still good 6-7% off),
- it is much simpler to use (everyone can do it),
- it is way more flexible (can be used even with random numbers),
- doesn't require lot's of floating point math which is not
available on all PLCs.
- lookup table is much faster in case scan time is critical.



To be fair, lookup tables need more memory resources
than calculation.

So the score is 5:1 for the lookup table :nodi: .

The bigger number of points, the bigger lookup table,
but same table table can be used many times.
Memory use in lookup tables is not so big deal anymore.
With most of my customers insisting on ControlLogix or
CompactLogix PLCs I usually get about 200kByte or more
to work with so 1000 point table would need only 1% of available memory.

Anyway, the topic can be stretched in favour of one or another
approach.

The nice thing is that we have a choice...
 
Last edited:
I call and raise you....

panic mode said:
Interesting link. I got lost in the middle of the document
once they introduced "matemagical formula" :eek:


It is easy if you have a math package like MathCad or MatLab or a C++ matrix class. In C++ is write the mathemagical formula like this:

theta = !(~phi*phi)*~phi*y;

It is still only one line in C++.


panic mode said:

Personally, I will still rather use lookup tables because
this approach in my opionion is way better:

- exact values instead of approximated calculation
(for second value which is "1", calculated result
using floating point math is still good 6-7% off),


You are right if you have perfect data. However you are assuming that you can measure the inputs and outputs very accurately. The Least Square statistically minimizes errors. I used only 7 points.


panic mode said:

- it is much simpler to use (everyone can do it),


Do you want any one to do it? To me, the least squares is easier because I already have the worksheet developed. I don't need to take 100 readings.


panic mode said:

- it is way more flexible (can be used even with random numbers),


We must use the right tool for the right job. In this case we are not generating random numbers. Otherwise the least square can be use to generate a function with many coefficients

panic mode said:

- doesn't require lot's of floating point math which is not
available on all PLCs.


Again you are right IF your application can tolerate outputs with only 1% resolution. This would make having a 14 or 16 bit analog output card a waste.

panic mode said:

- lookup table is much faster in case scan time is critical.


I will take issue with the MUCH. Multiplies and adds are much faster than divids. I doubt that that using the SCP function would be faster if it needs a divide. The CompactLogix does not have a native SCP function. If it is an ARM microcontroller like the Control Logix it doesn't have an integer divide either. It must do that in firmware and I bet the SCP instruction has a divide. That is a lot of shifting and subtracting.

Now I will raise you...

To calculate your 100 point lookup table you would need to control the process and change the set point in 1% increments and wait for the output to settle. At each setpoint you would need the record the control signal required for the current output and put that into your lookup table. Give me a break. Doing this with just 10 points would be a pain. Building a 1000 point look up table? No way. To have just any body doing this? Not a chance.
 
More fuzzification

Peter,

Many thanks for your thought provoking feedback. If I outline my application maybe you migh care to comment on my reasoning.

We have been asked to control the gas richness regulators on some 900kW Gas Engines that are fueled by a CO2/Methane mix reclaimed from a landfill. The control problem is that the methane concentration varies by a number of external variables, such as season, temp, rainfall etc and this affects the engine performance.

Currently the operator uses an external gas analyser to directly measure the concentration about three times a day, and then uses this to make an informed manual adjustment to the gas regulator valves. The variables he is looking at are the cylinder head temperatures (6 per side, 12 per engine), throttle position and power output.

Our problem is to devise a control strategy that implements the following rules:

1. If the cylinder head temps are too high, reduce methane richness. (This is counterintuitive, but the operator insists this is how it works.)

2. If the throttle position is too high (ie wide open) then increase methane richness.

At least this is what we understand so far from the operators description. Actually I don't think this is a complete rule set, but it might do for a start. Other variables such as power control error may well play a part and I am not at all sure exactly what the optimum strategy will turn out to be. Required reponse times are probably very slow; in the order of many minutes rather than seconds.

The project budgets don't support on-line gas analysers. Nor do we have any idea of the process functions that will relate these variables at this stage. All we I am trying to do at this point is pull together a proposal and price for a trial project that stands a decent chance of working. I realise that it is quite possible to derive empirical functions for each engine, but with many engines to deal with (each somewhat different) and not being able to easily measure the independent variable (methane richness), my wandering mind turned to the possibility of a fuzzy logic solution to essentially turn the operators ad-hoc rules into something I could implement in ladder without getting caught up in interacting PID loops. The other advantage I foresaw using fuzzy was the ability to readily add new variables and rules if the need arose during our testing phase.
 
PhillipW, Your problem is not so simple.

First you will need to find the set of rules empirically and this will take time. I don't see an easy way. I can see from your description of rules that it is doubtful that a simple polynomial will to the job.

For instance, the cylinder temperature will probably need a maximum function. This is because a cylinder temperature that is 1 degree below the limit does make up for a cylinder that is 1 degree above the limit. It could be that the the maximum temperature is used in the polynomial. This would simplify the polynomial. What you need to do is to find these relationships and limits.

Fuzzy logic has its place somewhere although I don't know where. If I can find a text that gives a prodecure ( Auto Tune ) that would set up the fuzzy logix I would be more impressed. What bothers me is that it is a very resource hungry ( $ ) way of doing control. If a PLC had a built in Fuzzy block it could be optimized in assembly language. I just can imagine it being done in ladder. Maybe in a S7 STL Function Block. I just haven't found a application where I couldn't do the control using something much simpler.

As you know I make motion controllers. We don't, and I don't know of any of my competition that uses fuzzy logic in their motion controllers. Why?

Keep us updated with the project status.
 
Da Fuzzy Blob thingy

Peter,

It may be a few weeks but yes I will post progress (if any) under a new thread. I am certain it can be done, indeed whatever we do is likely to be an improvement on the current manual approach.

Also you are spot on about the cylinder head temps; clearly there will need to be a maximum fucntion somewhere. From observation there is usually about a 15 degC variation between cylinders, and this can vary according for all manner of mechanical reasons.
 
It is generally true that an equation of the form of Y = AX2 + BX + C will be more accurate than a look up table, unless the relationship is already linear. Unless you have a huge table, which can be unwieldy and is prone to data entry error, you need to do some interpolation between values which will result in more error than the equation. Even poorly behaved relationships or empirical data can get very accurate results with a third order (X cubed) analysis.

Most of us have ready access to regression analysis without the math if that's what we want. Lotus and Excell have regression or trendline analysis that will calculate the constants for an equation, and will even give a correlation coeficient that identifes the error in the results.
 
Making Matrix Calculations Easier.

Tom Jenkins said:
Most of us have ready access to regression analysis without the math if that's what we want. Lotus and Excell have regression or trendline analysis that will calculate the constants for an equation, and will even give a correlation coeficient that identifes the error in the results.

Almost every one has Excel, but what function would you use? I don't see any functions built in that does a general case least squares. Excel is very weak at matrix manipulation. I have tried using VBA but that is too slow.

Does any one know of a VB library for matrix manipulation?
How about Java?

I have MathCad which makes array manipulation easy and permits me to export my worksheets in html format which is what you see in the link above.

For making real products we use a C++ Matrix Class from:

TechSoft Pvt. Ltd.
Email: [email protected]
Web: http://www.techsoftpl.com/matrix/

I think it cost only $20 and has been worth every penny.

BTW, one can see that I had to resort to a third order equation to get acceptable accuracy in the second part of the html link. If more accuaracy is required, a fourth coefficient could be added. This would only and another multiply and add.
 
Regression

In Lotus-123 I use the @regression function, and it is fairly straightforward. For a first order correlation (Y = MX + B) use a column for X and a second column for Y. For a second order correlation (Y = AX^2 + BX + C) create three columns - the first is X, the second is X-squared, and the third is Y. For a third order correlation use an additional column for X cubed, and so on. The regression function allows for quite a few independent variables.

In Excel you can do the same thing with the LINEST function. It is a little harder to use, but will give the same results and the same multiple column independent variable range can be used.

I use this technique all the time, for everything from developing a correlation for blower and pump curves to corelating water vapor pressure and relative humidity with temperature. I rarely have to go above third order to get engineering accuracy.

P.S. Peter: I love MathCAD too, and I have been using it for years. It can't be beat for developing methods or relationships, but I usually convert to a spreadsheet format after I have the math worked out because spreadsheets are more readily available and data entry is easier for multiple use procedures.
 
Last edited:
Thanks, Tom. I learned something.

I never knew the LINEST function would to all of that! I never got past the LIN part of LINEST. The function need another name.

I agree with your reasoning about everyone has access to Excel. I used to post links to PID simulators in Excel but no one seemed interested. I started posting the Mathcad .html files because they don't require much effort to view. Mathcad files are also smaller.
 
Try this the next time you are in excel. Use two colums of data to generate a XY scatter plot.

Next right click on one of the datapoints on the plot, you will see an option to "Add Trendline...".

After selecting that option you will get a box that allows you to choose from Linear, Polynomial (up to 6th order), Exponential, Logarithmic, Power or Exponential Trend/Regression analysis.

After selecting type of analysis you can click on the Options tab and you can have excel forcast Forward/Backward beyond your dataset, display the equation on the chart, select the intercept value and display the R-Squared value (which indicates how well your curve fit matches the dataset).

After completing the dialog box you will see a trendline that represents how close the calculation is to your dataset.

Simple example:

[X, Y]
[5, 15]
[10, 30]
[15, 85]

Use third order polynomial regression will give you

Y=0.8*x^2 - 9*x + 40

With R^2 of 1.

Cool stuff,

Darren

Wow, I checked out Brian's link and that is one comprehensive piece of software for the money. Thanks for the link.
 
Last edited:
[X, Y]
[5, 15]
[10, 30]
[15, 85]

Use third order polynomial regression will give you

Y=0.8*x^2 - 9*x + 40


It's a little off the topic really but I tried the above example on Excel which gives me a resulting formula on the plot of:
Y = 20X^2 - 45X^2 + 40 which gives the wrong result. The question is what am I doing wrong in Excel to get such a result
 
I followed Darren's instruction and got the results in the attached file.

Thanks for the Excel lesson Darren.

Calistodwt, you must have mistyped something.

This is not off topic. This does go beyond the basic problem of linearization. It is still good stuff to know because the abilty to find the coefficients to an equation is handy for many applications. Dylan_fc needs to know this least squares technique to do his project the rotary encoder. He just doesn't know it. Hint.

Least Squares using Excels LINEST function.
 

Similar Topics

This is meant for those that have been through college and know how to use Laplace transforms and differential equations. I am making a series of...
Replies
17
Views
10,960
Peter, I have been following all of your posts since 2009. We (PLCS.NET forum), are all amazed at your acquired empirical knowledge to identify a...
Replies
8
Views
2,433
I am working on a small press that will utilize a Delta controller and a double acting hydraulic cylinder. The customer needs very precise...
Replies
11
Views
4,986
Hi, I have on a machine, a damage motor from Kieback & Peter: KIEBACK & PETER DS670-R1-S3 1000OHM...
Replies
0
Views
2,580
This question is relevant to the 'search' function capability. After reading this post, which took a while, I've decided to ask a question, which...
Replies
3
Views
6,861
Back
Top Bottom