Getting x from y using the interp1 function

9 views (last 30 days)
Dear all,
I have a set of data where y increases in a non-linear way with increasing x (picture attached). I am trying to find a way to get the x value from the y value using the interp1 function. I can use this function to get a y value from an x value (i.e. interp1(x,y,x_value)), but not the other way round (i.e. interp1(y,x,y_value)). When I enter the function I get this error:
>> interp1(y,x,10)
Error using griddedInterpolant
The grid vectors are not strictly monotonic increasing.
Error in interp1 (line 186)
F = griddedInterpolant(X,V,method);*
I have tried different versions of interp1 (namely 'PCHIP' and 'cubic') and these do not work either.
I have attached the data file I am using in an excel spreadsheet.
Any help on this issue would be very much appreciated as it would save me a lot of time doing my analysis.
Regards,
Tim

Answers (2)

Matt J
Matt J on 19 Oct 2015
Edited: Matt J on 19 Oct 2015
Your data, as it appears in the .xls file, is not monotonic. One thing you could try is this,
fun=@(xi) abs(interp1(x,y,xi)-10,'cubic');
result=fzero(fun,506)
  1 Comment
Timothy Olsen
Timothy Olsen on 20 Oct 2015
Thanks for the answer. I see now that a non-monotonic data file can be used in the interp1 function. I am not sure I understand your function you have given.

Sign in to comment.


Star Strider
Star Strider on 19 Oct 2015
The part of your data below about x = 505.8 is quite noisy, and this will keep interp1 from working with it. Since you’re only interested in the data greater that that, you can restrict the data you give to interp1 to that range:
d = xlsread('rise.xlsx');
x = d(:,1);
y = d(:,2);
ys = find(y >= -40, 1, 'first'); % Define Monotonically Increasing Region
xi = interp1(y(ys:end),x(ys:end),10)
xi =
506.2905
  2 Comments
Timothy Olsen
Timothy Olsen on 20 Oct 2015
Thanks for the answer. I think this is the way I am going to do my analysis from now on. Do you know by any chance of a way that I can first turn the data into a steadily increasing line, and then use the interp1 function on this line?
Star Strider
Star Strider on 20 Oct 2015
My pleasure.
When I analysed your data (using the diff function), the long straight section was extremely noisy, making it unsuitable for inverse interpolation. You might fit a straight line to the linear portion, since it appears linear. Additionally, you would have to find some sort of segmentation criteria that would separate the relatively linear portion from the steadily increasing portion, then create a function to combine the linear and nonlinear portions.
You can test a polynomial fit, for instance with polyfit and polyval (I did not), but I doubt it would give you the result you need. Polynomial fits can have significant limitations in these instances.
Otherwise, your best possibility would be to fit a function to your data that you know describes the process that created your data. MATLAB has some nonlinear curve fitting functions that would allow you to do that. In the absence of a specific model, a logistic function could work, although dealing with the linear portion is probably going to be a challenge.
Another option is to use a lowpass filter (my filter design procedure is here), or use a Savitzsky-Golay filter to smooth it. (You need the Signal Processing Toolbox to use those functions.)
Anything that makes the noisy, relatively linear portion, monotonically increasing would work. The best I can do is to describe the approaches I would explore.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!