http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488
MATLAB Central Newsreader  Interpolation with coordinates
Feed for thread: Interpolation with coordinates
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 28 May 2012 11:53:05 +0000
Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878162
Jose Miguel
Dear Matlab users,<br>
<br>
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.<br>
<br>
I guess it has to do with interp2, but I can't figure out exactly how to do it,<br>
<br>
Thank you very much for your help,<br>
Regards<br>
Jose

Mon, 28 May 2012 12:59:07 +0000
Re: Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878169
John D'Errico
"Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jpvov1$ae2$1@newscl01ah.mathworks.com>...<br>
> Dear Matlab users,<br>
> <br>
> 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.<br>
> <br>
> I guess it has to do with interp2, but I can't figure out exactly how to do it,<br>
> <br>
> Thank you very much for your help,<br>
> Regards<br>
> Jose<br>
<br>
No. You have ONE set of data, most simply stored as<br>
perhaps a 4x2 array of (x,y) data, and then another<br>
4x2 array of values containing the (u,v) values. Get<br>
used to storing your data in arrays, not as scalar<br>
variables. Or, perhaps you might choose to store the<br>
data as a single 4x4 array, the columns or rows <br>
representing respectively [x,y,u,v]. At worse you<br>
might store things as 4 vectors, each of length 4:<br>
x, y, u, v.<br>
<br>
Now, can you use interp2 This is highly unlikely, as it<br>
sounds like your data still is a set of scattered points.<br>
You MIGHT be able to use griddata, if the points are<br>
not collinear. But without knowing something more,<br>
like what the data values are exactly, the most I can<br>
suggest is you start with griddata.<br>
<br>
John

Mon, 28 May 2012 13:33:09 +0000
Re: Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878171
Roger Stafford
"Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jpvov1$ae2$1@newscl01ah.mathworks.com>...<br>
> 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.<br>
         <br>
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 xy axes. <br>
<br>
However for the more general case you are asking about you could try solving for the four coefficients in the equation<br>
<br>
u = K1*x*y + K2*x + K3*y + K4<br>
<br>
that satisfy the four equations:<br>
<br>
u1 = K1*x1*y1 + K2*x1 + K3*y1 + K4<br>
u2 = K1*x2*y2 + K2*x2 + K3*y2 + K4<br>
u3 = K1*x3*y3 + K2*x3 + K3*y3 + K4<br>
u4 = K1*x4*y4 + K2*x4 + K3*y4 + K4<br>
<br>
and then evaluate u0 as<br>
<br>
u0 = K1*x0*y0 + K2*x0 + K3*y0 + K4 .<br>
<br>
A similar operation could be performed on v with four other coefficients.<br>
<br>
In matlab this could be done as:<br>
<br>
x = [x1;x2;x3;x4];<br>
y = [y1;y2;y3;y4];<br>
u = [u1;u2;u3;u4];<br>
KU = [x.*y,x,y,ones(4,1)]\u;<br>
u0 = [x0*y0,x0,y0,1]*KU;<br>
<br>
and similarly for v0.<br>
<br>
If your four points are anywhere near colinear, this method would not work very well.<br>
<br>
Roger Stafford

Mon, 28 May 2012 14:47:07 +0000
Re: Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878174
Roger Stafford
"Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jpvov1$ae2$1@newscl01ah.mathworks.com>...<br>
> 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.<br>
         <br>
As an afterthought, with your general placement of points it would seem much more natural to ask for a sixpoint interpolation with a general 2D quadratic function involving six coefficients:<br>
<br>
f(x,y) = K1*x^2 + K2*x*y + K3*y^2 + K4*x + K5*y + K6.<br>
<br>
After all, when the bilinear form<br>
<br>
K1*x*y + K2*x + K3*y + K4<br>
<br>
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.<br>
<br>
Roger Stafford

Mon, 28 May 2012 15:15:07 +0000
Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878177
Jose Miguel
Thanks John and Roger for your answers,<br>
I'll try to explain a little bit better the problem.<br>
<br>
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.<br>
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.<br>
<br>
I also have the (x0,y0) lat/long point of the new site as a given data.<br>
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. <br>
<br>
So the result would be a new (292176,2) matrix containing the interpolation at the new coordinate.<br>
(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.<br>
<br>
I hope that I've made the definition of the problem clearer now, sorry for not defining well the problem before,<br>
<br>
Thanks,<br>
Jose

Mon, 28 May 2012 16:45:07 +0000
Re: Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878181
Roger Stafford
"Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jq04pr$poc$1@newscl01ah.mathworks.com>...<br>
> 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.<br>
> 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.<br>
> I also have the (x0,y0) lat/long point of the new site as a given data.<br>
> 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. <br>
> <br>
> So the result would be a new (292176,2) matrix containing the interpolation at the new coordinate.<br>
> (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.<br>
        <br>
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:<br>
<br>
u = A*(xx1)*(yy1) + B*(xx1) + C*(yy1) + D<br>
<br>
would have the solution<br>
<br>
A = (u4u3u2+u1)/((x2x1)*(y2y1));<br>
B = (u2u1)/(x2x1);<br>
C = (u3u1)/(y2y1);<br>
D = u1;<br>
u0 = A*(x0x1)*(y0y1) + B*(x0x1) + C*(y0y1) + D;<br>
<br>
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.<br>
<br>
If the points are really a square grid, then you have y2y1 = x2x1 in the above.<br>
<br>
Roger Stafford

Mon, 28 May 2012 19:43:06 +0000
Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878190
Jose Miguel
Thank you very much Roger,<br>
<br>
I've applied the equations you mentioned and it seems to work perfectly.<br>
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).<br>
<br>
Nevertheless for one point:<br>
These are the coordinates of u1, u2, u3 and u4<br>
42.86 356.25 <br>
44.76 356.25<br>
42.86 358.13<br>
44.76 358.13<br>
This is the coordinates of the point: x0=42.56, y0=356.84<br>
u1=17.53<br>
u2=20.37<br>
u3=20.60<br>
u4=16.19<br>
The result I obtain with your equations is:<br>
u0=18.44<br>
With the matlab griddata function I obtained:<br>
u0=18.46<br>
<br>
But for the v0 point<br>
v1=10.24<br>
v2=9.72<br>
v3=10.05<br>
v4=6.54<br>
The result I obtain with your equations is:<br>
v0=10.24<br>
With the matlab griddata function I obtained:<br>
v0=8.38<br>
Which is really quite a difference,<br>
<br>
There must be something I'm doing wrongly,<br>
The code I'm using to calculate v0 is<br>
A = (v4v3v2+v1)./((x2x1).*(y3y1));<br>
B = (v2v1)./(x2x1);<br>
C = (v3v1)./(y3y1);<br>
D = v1;<br>
serieintv = A.*(x0x1).*(y0y1) + B.*(x0x1) + C.*(y0y1) + D;<br>
clear A B C D

Mon, 28 May 2012 20:45:41 +0000
Re: Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878196
dpb
On 5/28/2012 2:43 PM, Jose Miguel wrote:<br>
...<br>
<br>
> But for the v0 point<br>
> v1=10.24<br>
> v2=9.72<br>
> v3=10.05<br>
> v4=6.54<br>
> The result I obtain with your equations is:<br>
> v0=10.24<br>
> With the matlab griddata function I obtained:<br>
> v0=8.38<br>
> Which is really quite a difference,<br>
><br>
> There must be something I'm doing wrongly,<br>
> The code I'm using to calculate v0 is<br>
> A = (v4v3v2+v1)./((x2x1).*(y3y1));<br>
> B = (v2v1)./(x2x1);<br>
> C = (v3v1)./(y3y1);<br>
> D = v1;<br>
> serieintv = A.*(x0x1).*(y0y1) + B.*(x0x1) + C.*(y0y1) + D;<br>
> clear A B C D<br>
<br>
Well, I wasn't sure what to plug where and didn't try too hard but the <br>
obvious is that the result is the value of v1 which by the formulation<br>
is D so in<br>
<br>
serieintv = A.*(x0x1).*(y0y1) + B.*(x0x1) + C.*(y0y1) + D;<br>
<br>
it would appear that the other terms canceled for some reason...<br>
<br>
Did you look at the results of the coefficients before you cleared them <br>
all away or the xy0xy1 terms? I'm guessing if you do you'll see <br>
where the result came from.<br>
<br>


Mon, 28 May 2012 21:30:07 +0000
Re: Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878202
Jose Miguel
Thanks to all,<br>
<br>
New update, I had some data wrong.<br>
These are the coordinates of u1, u2, u3 and u4<br>
37.50 2.67<br>
37.00 2.67<br>
37.50 2.00<br>
37.00 2.00<br>
This is the coordinates of the point: x0=37.40, y0=2.57<br>
u1=17.53<br>
u2=20.37<br>
u3=20.60<br>
u4=16.19<br>
The result I obtain with your equations is:<br>
u0=18.41<br>
<br>
I used this equation<br>
A = (u4u3u2+u1)./((x2x1).*(y3y1));<br>
B = (u2u1)./(x2x1);<br>
C = (u3u1)./(y3y1);<br>
D = u1;<br>
serieintu = A.*(x0x1).*(y0y1) + B.*(x0x1) + C.*(y0y1) + D;<br>
clear A B C D<br>
<br>
Using griddata matlab function<br>
u0=griddata([x1;x2;x3;x4],[y1;y2;y3;y4],[u1 u2 u3 u4],x0,y0,'linear')<br>
I'm havong, u0=17.99<br>
<br>
The difference is almost 0.5m/s which is significant,<br>
With this code you can all run the code, thanks dpb for your help,<br>
Any help would be very much appreciated,<br>
Thanks<br>
Jose

Mon, 28 May 2012 21:59:07 +0000
Re: Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878207
Roger Stafford
"Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jq0kga$q8i$1@newscl01ah.mathworks.com>...<br>
> 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).<br>
> .........<br>
> u0=18.44<br>
> With the matlab griddata function I obtained:<br>
> u0=18.46<br>
> ........<br>
> v0=10.24<br>
> With the matlab griddata function I obtained:<br>
> v0=8.38<br>
       <br>
On my computer I get:<br>
<br>
u0 = 18.40428891377374<br>
v0 = 10.41063829787234<br>
<br>
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.<br>
<br>
As for the 'griddata' results, Mathworks states that the default interpolation method used is "Trianglebased linear interpolation" which I believe you will find is not the same as bilinear fourpoint interpolation, and that probably accounts for the differences you see.<br>
<br>
Roger Stafford

Tue, 29 May 2012 06:39:07 +0000
Re: Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878235
Jose Miguel
Hello Roger,<br>
<br>
Yes, the results can't be identical due to the issue you commented, I'm just giving the short values for the values.<br>
Nevertheless could you please run the interpolation with the values I wrote in second place, (x0=37.40, y0=2.57).<br>
<br>
What is the name of the theorem or equations you wrote? Just to make some comments in the matlab routine.<br>
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,<br>
<br>
Thank you very much,<br>
Regards

Tue, 29 May 2012 09:28:07 +0000
Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878241
Jose Miguel
Dear Roger,<br>
<br>
It works perfectly, thank you very much,<br>
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).<br>
<br>
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 23 months I will not know how this was done)<br>
<br>
My mail is josemijauregui@hotmail.com<br>
Thank you very much,<br>
Regards<br>
Jose

Tue, 29 May 2012 10:03:06 +0000
Re: Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878243
Jose Miguel
"Roger Stafford" wrote in message <jq0sfb$qov$1@newscl01ah.mathworks.com>...<br>
> "Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jq0kga$q8i$1@newscl01ah.mathworks.com>...<br>
> > 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).<br>
> > .........<br>
> > u0=18.44<br>
> > With the matlab griddata function I obtained:<br>
> > u0=18.46<br>
> > ........<br>
> > v0=10.24<br>
> > With the matlab griddata function I obtained:<br>
> > v0=8.38<br>
>        <br>
> On my computer I get:<br>
> <br>
> u0 = 18.40428891377374<br>
> v0 = 10.41063829787234<br>
> <br>
> 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.<br>
> <br>
> As for the 'griddata' results, Mathworks states that the default interpolation method used is "Trianglebased linear interpolation" which I believe you will find is not the same as bilinear fourpoint interpolation, and that probably accounts for the differences you see.<br>
> <br>
> Roger Stafford<br>
<br>
Dear Roger,<br>
<br>
It works perfectly, thank you very much,<br>
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).<br>
<br>
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 23 months I will not know how this was done)<br>
<br>
My mail is josemijauregui@hotmail.com<br>
Thank you very much,<br>
Regards<br>
Jose

Tue, 29 May 2012 11:27:06 +0000
Re: Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878253
Jose Miguel
Dear Roger,<br>
<br>
It works perfectly, thank you very much,<br>
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).<br>
<br>
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 23 months I will not know how this was done)<br>
<br>
My mail is josemijauregui@hotmail.com<br>
Thank you very much,<br>
Regards<br>
Jose

Tue, 29 May 2012 11:38:06 +0000
Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878254
Jose Miguel
Dear Roger,<br>
<br>
It works perfectly, thank you very much,<br>
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).<br>
<br>
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 23 months I will not know how this was done)<br>
<br>
Thank you very much,<br>
Regards<br>
Jose

Tue, 29 May 2012 20:38:05 +0000
Re: Interpolation with coordinates
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320488#878314
Roger Stafford
"Jose Miguel " <josemijauregui@hotmail.com> wrote in message <jq1qua$nsp$1@newscl01ah.mathworks.com>...<br>
> Nevertheless could you please run the interpolation with the values I wrote in second place, (x0=37.40, y0=2.57).<br>
> <br>
> What is the name of the theorem or equations you wrote?<br>
            <br>
The name of the interpolation on which those equations are based is just "bilinear interpolation". For example, see the Wikipedia article at:<br>
<br>
<a href="http://en.wikipedia.org/wiki/Bilinear_interpolation">http://en.wikipedia.org/wiki/Bilinear_interpolation</a><br>
<br>
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.)<br>
<br>
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.<br>
<br>
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, twodimensional 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 sixcoefficient quadratic function.<br>
<br>
I will try to do your requested computations as soon as time permits.<br>
<br>
Roger Stafford