Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Interpolation with coordinates

Subject: Interpolation with coordinates

From: Jose Miguel

Date: 28 May, 2012 11:53:05

Message: 1 of 16

Dear Matlab users,

I have four sets of data, (u1,v1), (u2,v2), (u3,v3) and (u4,v4) located at coordinates (x1,y1), (x2,y2), (x3,y3) and (x4,y4). I would like to obtain a new set of data (u0,v0) at given coordinates (x0,y0) using a bilinear interpolation.

I guess it has to do with interp2, but I can't figure out exactly how to do it,

Thank you very much for your help,
Regards
Jose

Subject: Interpolation with coordinates

From: John D'Errico

Date: 28 May, 2012 12:59:07

Message: 2 of 16

"Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jpvov1$ae2$1@newscl01ah.mathworks.com>...
> Dear Matlab users,
>
> I have four sets of data, (u1,v1), (u2,v2), (u3,v3) and (u4,v4) located at coordinates (x1,y1), (x2,y2), (x3,y3) and (x4,y4). I would like to obtain a new set of data (u0,v0) at given coordinates (x0,y0) using a bilinear interpolation.
>
> I guess it has to do with interp2, but I can't figure out exactly how to do it,
>
> Thank you very much for your help,
> Regards
> Jose

No. You have ONE set of data, most simply stored as
perhaps a 4x2 array of (x,y) data, and then another
4x2 array of values containing the (u,v) values. Get
used to storing your data in arrays, not as scalar
variables. Or, perhaps you might choose to store the
data as a single 4x4 array, the columns or rows
representing respectively [x,y,u,v]. At worse you
might store things as 4 vectors, each of length 4:
x, y, u, v.

Now, can you use interp2 This is highly unlikely, as it
sounds like your data still is a set of scattered points.
You MIGHT be able to use griddata, if the points are
not collinear. But without knowing something more,
like what the data values are exactly, the most I can
suggest is you start with griddata.

John

Subject: Interpolation with coordinates

From: Roger Stafford

Date: 28 May, 2012 13:33:09

Message: 3 of 16

"Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jpvov1$ae2$1@newscl01ah.mathworks.com>...
> I have four sets of data, (u1,v1), (u2,v2), (u3,v3) and (u4,v4) located at coordinates (x1,y1), (x2,y2), (x3,y3) and (x4,y4). I would like to obtain a new set of data (u0,v0) at given coordinates (x0,y0) using a bilinear interpolation.
- - - - - - - - - -
  Normally bilinear interpolation is used on a regular grid of four points, that is, four points that form a rectangle with sides parallel to the x-y axes.

  However for the more general case you are asking about you could try solving for the four coefficients in the equation

 u = K1*x*y + K2*x + K3*y + K4

that satisfy the four equations:

 u1 = K1*x1*y1 + K2*x1 + K3*y1 + K4
 u2 = K1*x2*y2 + K2*x2 + K3*y2 + K4
 u3 = K1*x3*y3 + K2*x3 + K3*y3 + K4
 u4 = K1*x4*y4 + K2*x4 + K3*y4 + K4

and then evaluate u0 as

 u0 = K1*x0*y0 + K2*x0 + K3*y0 + K4 .

A similar operation could be performed on v with four other coefficients.

  In matlab this could be done as:

 x = [x1;x2;x3;x4];
 y = [y1;y2;y3;y4];
 u = [u1;u2;u3;u4];
 KU = [x.*y,x,y,ones(4,1)]\u;
 u0 = [x0*y0,x0,y0,1]*KU;

and similarly for v0.

  If your four points are anywhere near colinear, this method would not work very well.

Roger Stafford

Subject: Interpolation with coordinates

From: Roger Stafford

Date: 28 May, 2012 14:47:07

Message: 4 of 16

"Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jpvov1$ae2$1@newscl01ah.mathworks.com>...
> I have four sets of data, (u1,v1), (u2,v2), (u3,v3) and (u4,v4) located at coordinates (x1,y1), (x2,y2), (x3,y3) and (x4,y4). I would like to obtain a new set of data (u0,v0) at given coordinates (x0,y0) using a bilinear interpolation.
- - - - - - - - - -
  As an afterthought, with your general placement of points it would seem much more natural to ask for a six-point interpolation with a general 2D quadratic function involving six coefficients:

 f(x,y) = K1*x^2 + K2*x*y + K3*y^2 + K4*x + K5*y + K6.

After all, when the bilinear form

 K1*x*y + K2*x + K3*y + K4

is subjected to rotation, the result is a 2D quadratic involving terms in x^2 and y^2. A bilinear surface when rotated is no longer bilinear in the new coordinates, and restriction to bilinearity is therefore somewhat arbitrary.

Roger Stafford

Subject: Interpolation with coordinates

From: Jose Miguel

Date: 28 May, 2012 15:15:07

Message: 5 of 16

Thanks John and Roger for your answers,
I'll try to explain a little bit better the problem.

I have four sets of hourly data, (u1,v1), (u2,v2), (u3,v3) and (u4,v4) corresponding to wind data; (292176,2) is the size of each of this 4 sets of data.
I also have the (x1,y1), (x2,y2), (x3,y3) and (x4,y4) lat/long coordinates of the 4 geographic location points. Each of the sets of data is located at its coodinate pair.

I also have the (x0,y0) lat/long point of the new site as a given data.
I need to obtain the hourly set of data (u0,v0) in the given (x0,y0) lat/long point interpolating the other 4 sets of data.

So the result would be a new (292176,2) matrix containing the interpolation at the new coordinate.
(x1,y1), (x2,y2), (x3,y3) and (x4,y4) form a square regular grid and variable u and v are independent, so the interpolation might be done independently.

I hope that I've made the definition of the problem clearer now, sorry for not defining well the problem before,

Thanks,
Jose

Subject: Interpolation with coordinates

From: Roger Stafford

Date: 28 May, 2012 16:45:07

Message: 6 of 16

"Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jq04pr$poc$1@newscl01ah.mathworks.com>...
> I have four sets of hourly data, (u1,v1), (u2,v2), (u3,v3) and (u4,v4) corresponding to wind data; (292176,2) is the size of each of this 4 sets of data.
> I also have the (x1,y1), (x2,y2), (x3,y3) and (x4,y4) lat/long coordinates of the 4 geographic location points. Each of the sets of data is located at its coodinate pair.
> I also have the (x0,y0) lat/long point of the new site as a given data.
> I need to obtain the hourly set of data (u0,v0) in the given (x0,y0) lat/long point interpolating the other 4 sets of data.
>
> So the result would be a new (292176,2) matrix containing the interpolation at the new coordinate.
> (x1,y1), (x2,y2), (x3,y3) and (x4,y4) form a square regular grid and variable u and v are independent, so the interpolation might be done independently.
- - - - - - - - -
  If your points are in a regular grid, then rename the x and y coordinates and corresponding u and v values: (u1,v1) at (x1,y1), (u2,v2) at (x2,y1), (u3,v3) at (x1,y2), and (u4,v4) at (x2,y2). Then the bilinear form:

 u = A*(x-x1)*(y-y1) + B*(x-x1) + C*(y-y1) + D

would have the solution

 A = (u4-u3-u2+u1)/((x2-x1)*(y2-y1));
 B = (u2-u1)/(x2-x1);
 C = (u3-u1)/(y2-y1);
 D = u1;
 u0 = A*(x0-x1)*(y0-y1) + B*(x0-x1) + C*(y0-y1) + D;

This could be applied to your four sets of 292176 u values as a single vector equation. A similar solution would apply to the v sets.

  If the points are really a square grid, then you have y2-y1 = x2-x1 in the above.

Roger Stafford

Subject: Interpolation with coordinates

From: Jose Miguel

Date: 28 May, 2012 19:43:06

Message: 7 of 16

Thank you very much Roger,

I've applied the equations you mentioned and it seems to work perfectly.
I'm having though slightly different results than the ones obtained with matlab function griddada (I had to do an iteration for each row, so it was not an option).

Nevertheless for one point:
These are the coordinates of u1, u2, u3 and u4
         42.86 356.25
         44.76 356.25
         42.86 358.13
         44.76 358.13
This is the coordinates of the point: x0=42.56, y0=356.84
u1=17.53
u2=20.37
u3=20.60
u4=16.19
The result I obtain with your equations is:
u0=18.44
With the matlab griddata function I obtained:
u0=18.46

But for the v0 point
v1=-10.24
v2=-9.72
v3=-10.05
v4=-6.54
The result I obtain with your equations is:
v0=-10.24
With the matlab griddata function I obtained:
v0=-8.38
Which is really quite a difference,

There must be something I'm doing wrongly,
The code I'm using to calculate v0 is
A = (v4-v3-v2+v1)./((x2-x1).*(y3-y1));
B = (v2-v1)./(x2-x1);
C = (v3-v1)./(y3-y1);
D = v1;
serieintv = A.*(x0-x1).*(y0-y1) + B.*(x0-x1) + C.*(y0-y1) + D;
clear A B C D

Subject: Interpolation with coordinates

From: dpb

Date: 28 May, 2012 20:45:41

Message: 8 of 16

On 5/28/2012 2:43 PM, Jose Miguel wrote:
...

> But for the v0 point
> v1=-10.24
> v2=-9.72
> v3=-10.05
> v4=-6.54
> The result I obtain with your equations is:
> v0=-10.24
> With the matlab griddata function I obtained:
> v0=-8.38
> Which is really quite a difference,
>
> There must be something I'm doing wrongly,
> The code I'm using to calculate v0 is
> A = (v4-v3-v2+v1)./((x2-x1).*(y3-y1));
> B = (v2-v1)./(x2-x1);
> C = (v3-v1)./(y3-y1);
> D = v1;
> serieintv = A.*(x0-x1).*(y0-y1) + B.*(x0-x1) + C.*(y0-y1) + D;
> clear A B C D

Well, I wasn't sure what to plug where and didn't try too hard but the
obvious is that the result is the value of v1 which by the formulation
is D so in

serieintv = A.*(x0-x1).*(y0-y1) + B.*(x0-x1) + C.*(y0-y1) + D;

it would appear that the other terms canceled for some reason...

Did you look at the results of the coefficients before you cleared them
all away or the x|y0-x|y1 terms? I'm guessing if you do you'll see
where the result came from.

--

Subject: Interpolation with coordinates

From: Jose Miguel

Date: 28 May, 2012 21:30:07

Message: 9 of 16

Thanks to all,

New update, I had some data wrong.
These are the coordinates of u1, u2, u3 and u4
         37.50 -2.67
         37.00 -2.67
         37.50 -2.00
         37.00 -2.00
This is the coordinates of the point: x0=37.40, y0=-2.57
u1=17.53
u2=20.37
u3=20.60
u4=16.19
The result I obtain with your equations is:
u0=18.41

I used this equation
A = (u4-u3-u2+u1)./((x2-x1).*(y3-y1));
B = (u2-u1)./(x2-x1);
C = (u3-u1)./(y3-y1);
D = u1;
serieintu = A.*(x0-x1).*(y0-y1) + B.*(x0-x1) + C.*(y0-y1) + D;
clear A B C D

Using griddata matlab function
u0=griddata([x1;x2;x3;x4],[y1;y2;y3;y4],[u1 u2 u3 u4],x0,y0,'linear')
I'm havong, u0=17.99

The difference is almost 0.5m/s which is significant,
With this code you can all run the code, thanks dpb for your help,
Any help would be very much appreciated,
Thanks
Jose

Subject: Interpolation with coordinates

From: Roger Stafford

Date: 28 May, 2012 21:59:07

Message: 10 of 16

"Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jq0kga$q8i$1@newscl01ah.mathworks.com>...
> I'm having though slightly different results than the ones obtained with matlab function griddada (I had to do an iteration for each row, so it was not an option).
> .........
> u0=18.44
> With the matlab griddata function I obtained:
> u0=18.46
> ........
> v0=-10.24
> With the matlab griddata function I obtained:
> v0=-8.38
- - - - - - - -
  On my computer I get:

 u0 = 18.40428891377374
 v0 = -10.41063829787234

for the two bilinear results using my formulas, and these differ from yours somewhat. You should really get these same results if you use precisely the same values you quoted. I suspect the values you quoted are only the displayed values to four decimal places of those you actually used. Am I right? Remember, what you see displayed using 'format short' is not exactly the quantity being used but only its value rounded to four decimal places.

  As for the 'griddata' results, Mathworks states that the default interpolation method used is "Triangle-based linear interpolation" which I believe you will find is not the same as bilinear four-point interpolation, and that probably accounts for the differences you see.

Roger Stafford

Subject: Interpolation with coordinates

From: Jose Miguel

Date: 29 May, 2012 06:39:07

Message: 11 of 16

Hello Roger,

Yes, the results can't be identical due to the issue you commented, I'm just giving the short values for the values.
Nevertheless could you please run the interpolation with the values I wrote in second place, (x0=37.40, y0=-2.57).

What is the name of the theorem or equations you wrote? Just to make some comments in the matlab routine.
If you find values closed to the ones I got I think we can close the issue, as I also understood from your points that griddata is using some kind of triangle interpolation,

Thank you very much,
Regards

Subject: Interpolation with coordinates

From: Jose Miguel

Date: 29 May, 2012 09:28:07

Message: 12 of 16

Dear Roger,

It works perfectly, thank you very much,
I had a wrong data that was causing tricky things, but now it gives the same results as you (taking into account what you said about decimal places).

Could you please tell me the paper, theorem or where I can find the explanation and equations you told me? (it's to include it in the matlab routine and don't forget about it!, otherwise in 2-3 months I will not know how this was done)

My mail is josemijauregui@hotmail.com
Thank you very much,
Regards
Jose

Subject: Interpolation with coordinates

From: Jose Miguel

Date: 29 May, 2012 10:03:06

Message: 13 of 16

"Roger Stafford" wrote in message <jq0sfb$qov$1@newscl01ah.mathworks.com>...
> "Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jq0kga$q8i$1@newscl01ah.mathworks.com>...
> > I'm having though slightly different results than the ones obtained with matlab function griddada (I had to do an iteration for each row, so it was not an option).
> > .........
> > u0=18.44
> > With the matlab griddata function I obtained:
> > u0=18.46
> > ........
> > v0=-10.24
> > With the matlab griddata function I obtained:
> > v0=-8.38
> - - - - - - - -
> On my computer I get:
>
> u0 = 18.40428891377374
> v0 = -10.41063829787234
>
> for the two bilinear results using my formulas, and these differ from yours somewhat. You should really get these same results if you use precisely the same values you quoted. I suspect the values you quoted are only the displayed values to four decimal places of those you actually used. Am I right? Remember, what you see displayed using 'format short' is not exactly the quantity being used but only its value rounded to four decimal places.
>
> As for the 'griddata' results, Mathworks states that the default interpolation method used is "Triangle-based linear interpolation" which I believe you will find is not the same as bilinear four-point interpolation, and that probably accounts for the differences you see.
>
> Roger Stafford

Dear Roger,

It works perfectly, thank you very much,
I had a wrong data that was causing tricky things, but now it gives the same results as you (taking into account what you said about decimal places).

Could you please tell me the paper, theorem or where I can find the explanation and equations you told me? (it's to include it in the matlab routine and don't forget about it!, otherwise in 2-3 months I will not know how this was done)

My mail is josemijauregui@hotmail.com
Thank you very much,
Regards
Jose

Subject: Interpolation with coordinates

From: Jose Miguel

Date: 29 May, 2012 11:27:06

Message: 14 of 16

Dear Roger,

It works perfectly, thank you very much,
I had a wrong data that was causing tricky things, but now it gives the same results as you (taking into account what you said about decimal places).

Could you please tell me the paper, theorem or where I can find the explanation and equations you told me? (it's to include it in the matlab routine and don't forget about it!, otherwise in 2-3 months I will not know how this was done)

My mail is josemijauregui@hotmail.com
Thank you very much,
Regards
Jose

Subject: Interpolation with coordinates

From: Jose Miguel

Date: 29 May, 2012 11:38:06

Message: 15 of 16

Dear Roger,

It works perfectly, thank you very much,
I had a wrong data that was causing tricky things, but now it gives the same results as you (taking into account what you said about decimal places).

Could you please tell me the paper, theorem or where I can find the explanation and equations you told me? (it's to include it in the matlab routine and don't forget about it!, otherwise in 2-3 months I will not know how this was done)

Thank you very much,
Regards
Jose

Subject: Interpolation with coordinates

From: Roger Stafford

Date: 29 May, 2012 20:38:05

Message: 16 of 16

"Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jq1qua$nsp$1@newscl01ah.mathworks.com>...
> Nevertheless could you please run the interpolation with the values I wrote in second place, (x0=37.40, y0=-2.57).
>
> What is the name of the theorem or equations you wrote?
- - - - - - - - - - - - -
  The name of the interpolation on which those equations are based is just "bilinear interpolation". For example, see the Wikipedia article at:

 http://en.wikipedia.org/wiki/Bilinear_interpolation

The particular form in which I gave the equations is just a matter of applying simple algebra. (Your algebra teacher could have given the derivation of that formula to you as an assignment.)

  Bilinear interpolation has the characteristic that it gives the x and y axes rather unique properties. Any surface defined using this interpolation will be such that a curve along the surface parallel to either axis will give a straight line, hence the name 'bilinear', but in other directions a parabola is produced. Taking the contours of this surface always produces hyperbolas whose two asymptotes are parallel respectively to the x and y axes. Thus the interpolated surface is not invariant with respect to a rotation of the coordinate system.

  If one is dealing with two qualitatively different quantities such as time and distance as coordinates, such a lack of invariance may not be important (except in some Einsteinian relativistic system,) but with, say, two-dimensional spatial coordinates such a lack of invariance makes this kind of interpolation somewhat unnatural in a sense. As I mentioned earlier it would be more natural if it were based on the six vertices of a hexagon so as to produce a general six-coefficient quadratic function.

  I will try to do your requested computations as soon as time permits.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us