SPLat Logo

Example: Non-linear math functions using polynomials

NOTICE: Our web site is being updated, but is currently experiencing extreme slowness due to host issues. Please contact us directly.
NOTICE: SPLat Controls has moved. We are now at 1/85 Brunel Rd, Seaford, 3198. map

Example: Non-linear math functions using polynomials

I'll try and keep this as non-mathematical as possible.

For thermistors (specifically) we now have a much easier way.

What this is about is calculating "weird" functions of a single variable, i.e translating one number into another. A common example in my work is linearizing a thermistor. Other examples might be motor speed versus applied voltage, engine torque versus rpm, the flow rate in a pipe versus pressure, etc. What these all have in common is that the "output" (say flow) is not a linear function of the "input" (say voltage). In fact, you may not even have a formula, just a manually plotted curve.

For example, the curve below is a plot of the air flow in kg/hour (Y axis) versus output voltage of a Mass Air Flow (MAF) sensor used in an engine management system. The curve is clearly very non-linear.

Suppose we need to translate analog input readings into engineering units (kg/hr) according to above curve. Here are the steps.

Step 1. Make sure the horizontal axis represents the value read by the SPLat.

This may sound silly, but often the tendency will be to plot the output of a sensor (volts) on the Y axis versus the measured quantity (say flow) on the X-axis. In the procedure we are using we need the data the other way around. What we know when we perform our calculation is the input variable, and it should be plotted on the X (horizontal) axis.

Step 2. Get the data into an Excel spreadsheet and plot it.

Later on we will be using some magic in Excel to do the really hard work. Use Excel's Chart function to plot the data as a scatter diagram. Select the format with a smooth line and no data points. You should get something like I have above.

Step 3. Scale the horizontal (input) axis.

Convert the units on the input axis into the units the SPLat will start out with. Frequently this will be an analog input read using fAnIn, so the input axis should run from 0 to 1. In the current example I will assume the analog input is set for a 0-5V range, so 5V in on the above graph will become 1.0 on the scaled graph, like this:

The only thing that has changed here is the X-axis scale.

Step 4. Find the polynomial

This could be hard, but Excel makes it easy.

Step 5. Identify the "polynomial coefficients"

The coefficients are the number that appear in front of (as multipliers for) the powers of x.

Here the "4th order coefficient" is 8.6811250E+02. With a little practice you can learn to highlight individual coefficients with your mouse, copy them and paste directly into your SPLat program. Just be aware that Excel adds a space after the minus signs. You must reproduce the minus signs correctly, without a space after them.

Step 6. Use the example code:

Program examples are no longer installed with SPLat/PC. They are now located on our website in the File Resources area. In folder examples\math\poly\ is a program polygen.spt. polygen.spt contains a general purpose polynomial calculator.

The calculator gets its coefficient from a table. All you have to do is build a table of coefficients and point the code to it. The comments in the program show you how to do this.

Other uses

This technique can be used to generate otherwise difficult mathematical functions like trig functions, logs and exponentials. The key to success is to limit the input numbers (arguments) to the narrowest possible range.