5
votes

Ok guys. I have the following problem:

I have the data of the following plot. enter image description here

So the data file of this plot contains three columns. The 2nd and 3rd ones the x,y points. And the 1st one is to which system those points belong. In this case the red ones are for the system of 20 years. The blue ones for the 30 years.

What I want to find is the curve at 25 years. So if I plot it it should be between the red and blue curves.

I have no idea how interpolate the data in order to obtain what I want. Actually I want to have for 21,22,...29 years, I guess if we can find it for a time in between these two, then the method should work for any time between 20 and 30.

PS: I guess the interpolation for each curve (in this case red or blue one) is quite easy. Just using interp1(x,y,xx) will work. But what happened with the other "dimension" (M)

The data.

20.0000    3.4076         0
20.0000    3.4226   99.5405
20.0000    3.4701  196.3360
20.0000    3.5592  287.0781
20.0000    3.6248  328.8516
20.0000    3.6643  348.3373
20.0000    3.7091  367.2823
20.0000    3.7591  385.4784
20.0000    3.8077  402.7170
20.0000    3.8957  437.5221
20.0000    4.0314  506.9907
30.0000    3.6335         0
30.0000    3.6373   49.8884
30.0000    3.6488   99.5405
30.0000    3.6685  148.5936
30.0000    3.7363  243.2204
30.0000    3.7876  287.7398
30.0000    3.8537  329.6097
30.0000    3.8935  349.9452
30.0000    3.9384  368.9776
30.0000    3.9892  387.2576
30.0000    4.0410  404.5759
30.0000    4.1350  439.5416
30.0000    4.2153  474.2420
30.0000    4.2813  509.3309
2
What are your data columns ? Are they defining a surface (3D object, the columns represent x,y and z coordinate) or simply a series of decoupled data columns with the first columns as an X coordinate ? - Hoki
Well actually the 2nd and 3rd columns are the one which define an usual xy plane ((x,y) points). And the first column is actually to which "system" these (x,y) points belongs to. But is pretty straightforward interpolate in the xy plane, but not in the "3D" one if I consider the first column as another variable. I am not sure if my answer helps you, please let me know. - Nikko
Well, i am still not sure to understand completely what you expect from the interpolation. As a first hint based on your last comment I would recommend you to consider your first column as a kind of Z plane/dimension (swap the columns if necessary), and see if that helps. But ultimately, I think you should edit your original question and better define your problem. - Hoki
Ok I will. But I don't see the difference, I mean the position of the columns is irrelevant? I have no idea. I thought it is. (But thank you I will fix it asap) - Nikko
Much clearer indeed. It is also good to see the plots. I see you have a different number of points between your 2 defined curve. It will probably help to reinterp each curve on a common X or Y. In your example curve, I can't be sure for the X span but I see each curve has the same Y span (from 0 to 500). So I would recommend to first resample your existing curves on a common Y grid (you can use interp1 for that). Then for each 'Y' line, you will have a corresponding X value for each time (and you can interpolate X values for your desired times in between). - Hoki

2 Answers

6
votes

Actually, by looking at the Matlab documentation I found a simpler way. You can use the function griddata. (The doc in matlab help shows visual example). The resampling on a common grid and the interpolation is embedded in the function.

%// First separate (and name your column to identify them better)
t = d(:,1) ;
x = d(:,2) ;
y = d(:,3) ;

%// use the function 'griddata'
[TI,YI] = meshgrid( 20:30 , 0:20:500 ) ; %// change these values to change the grid limits
XI = griddata(t,y,x,TI,YI) ;

%// show result in 3D ... but could be projected in X-Y plane if necessary
plot3(TI,YI,XI , 'Marker','o' )
xlabel('Time') ; ylabel('Y') ; zlabel('X')

The last line of the code shows this plot: figure

All your interpolated data are in the XI matrix. The way to retrieve them depends on how you want to organize them ultimately.


InterpData
nLine = numel(XI) ;
InterpData = [ reshape(TI,nLine,[]) reshape(XI,nLine,[]) reshape(YI,nLine,[]) ] ;


NaN
NaNYTime(column 1)Y(column 3)X
3
votes

Try this code, which implements @Hoki 's comment:

m20=[3.4076 0; 3.4226 99.5405; 3.4701 196.3360; 3.5592 287.0781; 3.6248 328.8516; 3.6643 348.3373; 3.7091 367.2823; 3.7591 385.4784; 3.8077 402.7170; 3.8957 437.5221; 4.0314 506.9907];
m30=[3.6335 0; 3.6373 49.8884; 3.6488 99.5405; 3.6685148.5936; 3.7363 243.2204; 3.7876 287.7398; 3.8537 329.6097; 3.8935 349.9452; 3.9384 368.9776; 3.9892 387.2576; 4.0410 404.5759; 4.1350 439.5416; 4.2153 474.2420; 4.2813 509.3309];
yy = [0:50:500];
xx20 = interp1(m20(:,2),m20(:,1),yy);
xx30 = interp1(m30(:,2),m30(:,1),yy);
for m = 1:9
    mm(:,m) = xx20 + (xx30-xx20)*(m/(30-20));
end
plot(m20(:,1),m20(:,2),xx20,yy,xx30,yy,m30(:,1),m30(:,2),mm,yy)

You interpolate the given M vectors to find the x coordinate of a set of y values - these are the interp1 lines. Then, you linearly interpolate as a function of m between the interpolated x-coordinates.