Non-Linear functions
One of the great ironies of engineering is that we try to control a non-linear world with a linear circuitry. Entire text books have been written just on the subject of linearization of sensors, and until recently, this linearization usually entailed complex op-amp circuits and tedious/expensive calibration techniques in manufacturing.
However, now that we are firmly entrenched in the age of software; this traditionally hardware based problem has become a new headache for programmers. So, how do we handle non-linearity in software? Well, there are two common solutions; piecewise linear approximations, and curve fitting.
Piecewise linear approximati ons take the non-linear curve and break it into multiple segments that can be represented by a straight line. For example, you can approximate a sine function with a set of equations like the following;
Now the approximation is not very good, but it does fit the general outline of a sine wave. In addition, their can be significant discontinuities where the segments meet, which is all dependant upon the number of segments, or pieces, and how closely the linear segments approximates the function over their range.
Curve fitting is the process of finding a equation that fits the non-linear function over a restricted range of values, for example, an approximation of the same sine wave, for the range 0-180 degrees, is the following function;
Y = -0.00808+(0.0217*X)-(0.000121*X2)
The new function is closer, but even with all the floating point math, it still has an error of 3-4% over the range of the function, and the more cyclic the function, the harder it will become to find one function that fits the entire range.
Usually, for periodic functions, the function is approximated over its period, or some segment of its period and the result is applied like the linear segments in piece wise linear approximations. That is, for 0-180, the function is used as is; for 180-360, we subtract 180 from the input, and multiply the output by -1.
Regardless of the method used, it is important to determine the overall system error, over the range that the system will be used. Spread sheets are particularly handy for this, just enter a column of reasonable data in the first column, copy your function into the second column, and calculate the error in the third column. If the error is more than you can live with, then increase the number of pieces in the approximation, or increase the order of the curve fit.
|
Input |
Ideal |
Curve Fit |
Error |
|
PWL |
Error |
|
0 |
0.0000 |
-0.00808 |
1% |
0 |
0% |
|
|
30 |
0.5000 |
0.53402 |
-3% |
0.333333 |
17% |
|
|
60 |
0.8660 |
0.85832 |
1% |
0.666667 |
20% |
|
|
90 |
1.0000 |
0.96482 |
4% |
1 |
0% |
|
|
120 |
0.8660 |
0.85352 |
1% |
0.666667 |
20% |
|
|
150 |
0.5000 |
0.52442 |
-2% |
0.333333 |
17% |
|
|
180 |
0.0000 |
-0.02248 |
2% |
0 |
0% |
