# Calculating an interpolated value ....

## Recommended Posts

i kinda have a grip on 1D interpolatpion but now i have to do it 2D for this particular problem. i have data that is a higher resolution that the other. so for instance my grid for the lower resolution goes from 0, 1, 2, 3, 4. the higher resolution grid would go from 0. 0.2, 0.4, 0.6, 0.8, 1.0... and so on , all the way to 4. the height value is what needs to be interpolated. here is an example problem. lets say at the point 0 i have a height of 12 and at 1 i have a height of 32. in my y column i have a height of 16 at 3 and 27 at point 4. so in essence in my lower grid i have: (0, 0, 12) & (1, 0, 32) for x's. (0, 1, 16) & (1, 1, 27) for y's. how would i acheive at getting an interpolated height if i send in the (0.34, 0.66) from the high resolution grid? in other words what would z(the height be for the point (0.34, 0.66, ? )? sorry my drawing isnt the best. thanks all in advance for your help!!
(0, 1, 16)-->  |     | <----(1, 1, 27)
----------
| *   |    *this is about where (0.34, 0.66, ?) might be
----------
|     |
----------
|     |
----------
|     |
----------
|     |
----------
(0, 0, 12)-->  |     | <----(1, 0, 32)


##### Share on other sites
if this is the only data you have, bilinear interpolation is what you need.

the idea is to first interpolate those 4 values along one axis, giving you two new values, which you can interpolate along the other axis to find your final point.

if that isnt clear enough, google will fill in the gaps if you ask it about bilinear interpolation.

##### Share on other sites
thanks for you help, i guess i didnt know the wording which is bilinear interpolation. i had it almost right but there was something i didnt do.

the answer for the curious was given by:

x*z2 + (1-x)*z1) = .34*32 + .66*12 = 18.80 = h1 (height 1 along the x-axis)

x*z4 + (1-x)*z3 = .34*27 + .66*16 = 19.74 = h2 (height 2 along y axis)

now interpolate the 2 heights(hence BI-linear interpolation)

(1-x)*h2 + (1-y)*h1 = .66*19.74 + .34*18.80 = 13.0284 + 6.392 = 19.4204.

hence the answer would be: (0.34, 0.66, 19.4204)

ill rate you up Eelco if i havent before :)

##### Share on other sites
hmmm maybe with negative height values this doesnt work so well...

##### Share on other sites
Quote:
 Original post by OpenGL_Gurunow interpolate the 2 heights(hence BI-linear interpolation)(1-x)*h2 + (1-y)*h1 = .66*19.74 + .34*18.80 = 13.0284 + 6.392 = 19.4204.

This should be
y*h2 + (1-y)*h1
Exactly like you did for X, but replacing the variable names. It's perfectly fine to use on negative values.

There's also a nice optimization you can do to the formula using some algebra. I'll do it on the x/z1/z2 version, although you can again just swap the variable names:

x*z2 + (1-x)*z1
x*z2 + (z1-x*z1)
x*(z2-z1) + z1

Saves one multiply, and is mathematically equivalent. Only difference you might get is in tiny floating point rounding errors, but for all practical purposes it's exactly the same.

##### Share on other sites
thanks Deku -- i meant to say y instead of (1-x), i still think i got the numbers right though :)

heres my next problem, lets say you have x and y values that are not in the classic 0 --> 1 form for instance:

(10, 0 -10) & (34, 0, -1) for the x's
(10, 24, 20) & (34, 24, -6) for the y's.

i am workingon it right now. lets say you want to get the height for the point 31, 17, so it would be (31, 17, ?).

right now i am thinking that you have to convert your 4 points into a 0 -- > 1 scale, so trying that part out now..

any thoughts or suggestions?

##### Share on other sites
Quote:
 Original post by OpenGL_Gururight now i am thinking that you have to convert your 4 points into a 0 -- > 1 scale, so trying that part out now..

yup, thats how id do it.

there are probably other methods, but they probably all boil down to the same if you simplify all operations. i dont think youll be able to go faster nor simpler if you simplify the resulting formulas for this properly.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627658
• Total Posts
2978474

• 10
• 12
• 22
• 13
• 33