Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolation between two curves (matlab)

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
like image 375
Nikko Avatar asked May 06 '14 12:05

Nikko


2 Answers

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.


EDIT: To place all the interpolated data in a single table InterpData organized the same way of your original table, use the following:
nLine = numel(XI) ;
InterpData = [ reshape(TI,nLine,[]) reshape(XI,nLine,[]) reshape(YI,nLine,[]) ] ;


Regarding the NaNs. They will come to bother you every time you ask to do an interpolation outside of the initially known values.
For example, if your time in the original data is in the [20 to 30] interval, matlab will gladly interpolate anything within that interval, but will return NaN if you ask to return a value for time = 19 for example. Same goes for Y, the grid on which to interpolate has to be within the initial range. (as in this implementation we use a base grid formed by Time (column 1) and Y(column 3), to interpolate the X column).
like image 55
Hoki Avatar answered Sep 25 '22 01:09

Hoki


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.

like image 43
darthbith Avatar answered Sep 24 '22 01:09

darthbith