A few weeks ago I wrote about using Horner’s rule to evaluate polynomials. Well today I’m following up on this posting because I made a classic mistake when I implemented it. On the premise that one learns more from one’s mistakes than one’s successes, I thought I’d share it with you.
First, some background. I had some experimental data on the behavior of a sensor against temperature. I needed to be able to fit a regression curve through the data, and so after some experimentation I settled on a quadratic polynomial fit. This is what the data and the curve looked like:
On the face of it, everything looks OK. However, if you look carefully, you will notice two things:
- The bulk of the experimental data cover the temperature range of 5 – 48 degrees.
- There is a very slight hook on the right hand side of the graph
So where’s the mistake? Well actually I made two mistakes:
- I assumed that my experimental data covered the entire expected operating temperature range.
- I failed to check at run time that the temperature was indeed bounded to the experimental input range.
Why is this important? Well, what happened, was that in some circumstances the sensor would experience temperatures somewhat higher than I expected when the experimental data was gathered, e.g. 55 degrees. Well that doesn’t sound too bad – until you take the polynomial and extend it out a bit. This is what it looks like:
You can see that at 55 degrees, the polynomial generates a value which is about the same as at 25 degrees. Needless to say, things didn’t work too well!
So what advice can I offer?
- Ensure that when fitting a polynomial to experimental data, that the experimental data covers all the possible range of values that can be physically realized.
- Always plot the polynomial to see how it performs outside your range of interest. In particular, if it ‘takes off’ in a strange manner, then treat it very warily.
- At run time, ensure that the data that you are feeding into the polynomial is bounded to the range over which the polynomial is known to be valid.
The maddening thing about this for me, was that I ‘learned’ this lesson about polynomial fits many years ago. I just chose to ignore it this time.
Before I leave this topic, I’d like to offer one other insight. If you search for Horner’s rule, you’ll find a plethora of articles. The more detailed ones will opine on topics such as evaluation stability, numeric overflow issues and so on. However, it’s rare that you’ll find this sort of information on polynomial evaluation posted. I think it’s because we tend to get wrapped up in the details of the algorithm while losing sight of the underlying mathematics of what is going on. The bottom line, the next time you find a neat algorithm posted on the web for ’solving’ your problem, take a big step back and think hard about what is really going on and what are the inherent weaknesses in what you are doing.
Home
