|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionSometimes you don't have data where you need it. Maybe you have figures collected sporadically but you need to present them as if they were collected at regular intervals, say once a week. Or maybe you need to make your best guess at missing data points. This is a job for interpolation. Sometimes instead of wanting to guess at output values for inputs that you choose, you want to do the opposite: You have an output in mind and you need to guess what input would give you the output that you want. This calls for inverse interpolation. This article presents the simplest and most widely applicable forms of interpolation — linear interpolation and quadratic interpolation. The article concludes with some recommendations for what to try next when these forms of interpolation are not good enough. Linear InterpolationThere are many ways to fill in missing data, ranging from very simple to very sophisticated. Linear interpolation is the simplest method. It is also one of the most robust methods, i.e. it is likely to give reasonable answers under a wide variety of circumstances, including when substantial noise is added to the inputs. Unfortunately linear interpolation is also the least accurate method. However, accuracy is over-rated. Or at least robustness is under-rated. Robustness is often more important than extra accuracy, especially if you're not exactly confident that you know what you're doing. I would give the same advice for interpolation that the Extreme Programming folks give for software development: first try the simplest thing that could possibly work. If you have two inputs, y = y0(x - x1)/(x0 - x1) + y1(x - x0)/(x1 - x0)
With this formula, you can stick in any value of y2 = y0(x2 - x1)/(x0 - x1) + y1(x2 - x0)/(x1 - x0)
Linear interpolation is most reliable if the Now suppose you have the data points x2 = x0(y2 - y1)/(y0 - y1) + x1(y2 - y0)/(y1 - y0)
As before, this works best if the new value The formulas above are mathematically correct for any values you stick in. When I say that you should be suspicious of the output in extreme circumstances, it's not because some approximation is going on. However, in practice, the assumption that the points When you write code to do linear interpolation, the only thing to be careful about is input validation. For (ordinary) interpolation, it's important to verify that if (x[0] == x[1] || x[0] == x[2] || x[1] == x[2])
// report error
else
y[2] = y[0]*(x[2] - x[1])/(x[0] - x[1]) + y[1]*(x[2] - x[0])/(x[1] - x[0]);
Similarly, for inverse interpolation you need to make sure your if (y[0] == y[1] || y[0] == y[2] || y[1] == y[2])
// report error
else
x[2] = x[0]*(y[2] - y[1])/(y[0] - y[1]) + x[1]*(y[2] - y[0])/(y[1] - y[0]);
If you just need to do a quick interpolation on a small amount of data rather than write a program that does interpolation, you may want to use an online linear interpolation calculator. This interpolator is implemented in hand-written client-side JavaScript and so you can read the source. Quadratic InterpolationAs the Extreme Programming folks would recommend, when the simplest thing doesn't work, try the next simplest thing that could possibly work. For interpolation, that means quadratic interpolation. Instead of fitting a straight line to two points, quadratic interpolation fits a parabola to three points. To figure out how to generalize the formulas above to quadratics, look back at the equation for linear interpolation. The term For quadratic interpolation, we follow a similar pattern, constructing quadratic polynomials that are y0P0(x) + y1P1(x) + y2P0(x)
where:
The polynomials If your inputs are free of noise, quadratic interpolation can give much better accuracy than linear interpolation. For example, in mathematical tables, the given values are precise to many decimal places, but you may be interested in a value not in the table. Say a function is tabulated at To implement the above equations in software, we need to verify that all the if (x[0] == x[1] || x[0] == x[2] || x[1] == x[2] || x[0] == x[3] ||
x[1] == x[3] || x[2] == x[3])
{
// report error;
}
else
{
y[3] = y[0]*(x[3] - x[1])*(x[3] - x[2])/((x[0] - x[1])*(x[0] - x[2]));
y[3] += y[1]*(x[3] - x[0])*(x[3] - x[2])/((x[1] - x[0])*(x[1] - x[2]));
y[3] += y[2]*(x[3] - x[0])*(x[3] - x[1])/((x[2] - x[0])*(x[2] - x[1]));
}
As with the linear interpolator, there is an online quadratic interpolation calculator implemented with client-side JavaScript. Now what if we want to use interpolation to solve for an Note that this is where quadratic interpolation differs from linear interpolation. With linear interpolation, reversing the roles of What If Linear and Quadratic Interpolation Aren't Good Enough?In theory, you could fit higher order polynomials. You could fit a third degree polynomial to four points, or a fourth degree polynomial to give points, etc. This is generally not a good idea. Fitting higher degree polynomials amplifies errors in the data. If quadratic interpolation isn't good enough, you may need some more sophisticated form of interpolation. For example, natural cubic splines are useful in many contexts. However, it's hard to say much in general. Beyond simple linear or quadratic interpolation, the best technique depends heavily on the problem context. You may need to abandon interpolation entirely and use a DSP algorithm, such as a low-pass filter, or do some sort of statistical regression. A good place to start looking for more information would be the Numerical Recipes book. History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||