Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolation advice (linear, cubic?) [closed]

I need to find good approximations of the points where an undefined function intersect a threshold value. I'm stepping through my space and whenever I find that two subsequent steps are on different sides of the threshold, I add a point somewhere in between:

ActualSituation

(source: ning.com)

My first approach was to just pick the mid-point, but this is obviously a terrible solution:

Midpoint

(source: ning.com)

I'm now using linear interpolation which gives a reasonable result, but the underlying function will practically never be linear. Thus, this only works well if my stepsize is small enough:

LinearInterpolation

(source: ning.com)

Sampling the base function can be quite expensive, but adding one or two additional samples in order to get a much better approximation is something I'd like to try. Is it possible to use Cubic interpolation here? Like so:

CubicInterpolation
(source: ning.com)

Or are there better ways?

Much obliged, David Rutten

ps. I'm writing in C#, but this is a language agnostic problem.

like image 336
David Rutten Avatar asked Sep 20 '09 19:09

David Rutten


3 Answers

The magic word is "root solver"; a mathematical root is the value where the function equals zero. By adding/subtracting the threshold, you can use root finders.

If you have a clue what function you are interpolating you can set up a very fast root finder. If you don't have a clue as suggested by your post ("undefined"), the best method is "Brent's method", a combination of "secant method" and "bisection", or the "secant method" alone. Wikipedia has an entry for this method.

Contrary to your opinion it is not a good idea to use more complicated functions. The main performance hurdle are function evaluations which increase with more points/getting the derivative or more complex interpolation functions.

The Newton-Raphson method is VERY BAD if you are near a maximum/minimum/inflection point because the near zero derivative sends you far away from the point and there are some other problems with it. Do not use it until you know what you are doing.

like image 62
Thorsten S. Avatar answered Sep 28 '22 04:09

Thorsten S.


Your last picture shows only three points, which only suffice to define a quadratic polynomial, not cubic. For cubic interpolation, you'll need four points. A cubic polynomial can be fitted in different ways; here are two.

The most straightforward way is to simply let the (unique) polynomial pass through all four points.

Another way is to use tangents. Again, we need four points. Let the left two points define a slope. Have the polynomial pass through the second point (in general, it doesn't pass through the first point), and match the computed slope in that point. And same on the right side for the fourth and third point.

By the way, any higher-order polynomial is probably a bad idea, because they tend to become very unstable in the presence of even a little bit of input noise.

If you give some more details about your problem domain, I might be able to give a more specific answer. For example, where do your data points come from, what kind of curve can you generally expect, and can you go back and sample more if required? I can provide equations and pseudo-code too, if needed.

Update: silly me left a sentence referring to two ways without typing them out. Typed them out now.

like image 44
Thomas Avatar answered Sep 28 '22 03:09

Thomas


My maths is incredibly rusty, but you may find the Newton Raphson method gives you good results. In general it converges very quickly on an accurate solution, assuming the iteration begins "sufficiently near" the desired root.

like image 39
Rich Seller Avatar answered Sep 28 '22 05:09

Rich Seller