Hi! If you’re ever in a situation, where you don’t know the formula for something, for example how much force to apply to a part to move it to a certain point or maybe the formula you have doesn’t solve your problems read this tutorial.
When to use this method?
If you know that you will be using whatever you’re using multiple times. If you need something to happen only once and won’t rely on it afterwards, then just go with trial and error. But this may save your time if you will use it in many different situations.
What is interpolation?
Let’s say that you’ve run just a few tests. Let’s stick to the example with force. So our X axis is the amount of force you’ve used and Y axis is the distance the part has travelled. You can intuitively connect those dots to find an argument(X) and it’s value(Y) that you haven’t tested yet. What you’ve done was interpolation and we can do that with precision.
So first of all you need to run a few tests, let’s say 5. The more you do, the more precision you get, but that’s not necessary. We’re going to use Lagrange interpolation to calculate a formula for any value (note: whatever value you’re going to insert must be somewhere between the smallest and largest argument)
This is the formula we’re going to use. Scripted it looks like this:
local forceApplied = {1,2,3,4,5} -- Arguments
local distanceTravelled = {2,4,6,8,10} -- Values
-- those are the things you got from testing. First argument corresponds with first value etc.
-- now here we define a function to which we'll pass an argument and it will estimate a value for it:
local function estimate(X)
local estimatedValue = 0
for i,v in ipairs(forceApplied) do -- we iterate through all the arguments and calculate every section of the polynomial
local fraction = 1 -- the fraction part of one section
for ii,vv in ipairs(forceApplied) do -- we iterate again to use all the values BUT one
if ii ~= i then
fraction = fraction * (X - vv)/(forceApplied[i]-vv)
end
end
estimatedValue = estimatedValue + distanceTravelled[i] * fraction -- we're done with one section
end
print("For argument "..X.." the estimated value is "..estimatedValue)
return estimatedValue
end
local distance = estimate(X) -- here you can pass any argument from your tested range and get results with a good precision
Thank you for reading, I hope this helps. In case you’re wondering, I’ve used it for finding out what will be the radius of a circle drawn by a turning car when its front wheels are tilted a certain degree. Now I can make it follow basically any curved path.

