Last Updated: February 25, 2016
· alexanderbrevig

Extrapolate values between known values

From time to time, especially if I make some proof of concept games or anything real-time in nature I quickly need to generate values between known wanted values.

Exactly what a function does, right?
Well, some things are not easily expressed as a function. You might know where you want some item at a discrete set of points in time, and you simply want the item to get there from the previous position in time.

Enter extraploation!
"In mathematics, extrapolation is the process of estimating, beyond the original observation interval, the value of a variable on the basis of its relationship with another variable" -

To do this relatively easily I wrote a small JavaScript module:

You can set it up like this:

// create an object for training
var extrapolate = new EXTRAPOLATE.LINEAR();

// for this example, ket's keep it simple
// f(x) = 2x

Then you can ask for extrapolated values like this:

extrapolate.valueFor(-1); // -2
extrapolate.valueFor(2); // 4
extrapolate.valueFor(10); // 20

2 Responses
Add your response

A graphical implementation of this, some sort of simple 2D game in which you can walk around and have the code predict your most likely path, drawing a matching curve for the path you have already walked, and the probable path, would be a lovely way to see this in action.
What type of functions can this module detect?

over 1 year ago ·

Right now it's just simple linear extrapolation between the closest two known values. This means, that for the example you're thinking about this module would really only use the two latest known points, and extrapolate linearly from those two values.

So, if you had visited [0,0] then [1,2] this module would predict [2,4] as the next coordinate which for small interval would be pretty accurate. If one want the curve to be taken into consideration a polynomial would be better, but a spline would probably be best :)

over 1 year ago ·