Interpolate Data and find the matching x-Value

I have got an arry with 151937x2 double wicht discribes a Curve.
I am trying to find the x-Value to the matching y- Value for example:
data=[1 2,
2 3,
3 3,
4 5,
7 9]
I have tryed it with:
interp1(data(:,1), data(:,2),y-Value ,'PCHIP')
But the function returns the according y-Value, logically. So I tryed to swap x and y datas. The Problem is that the y-Values arent unique.
I hope someone can help me.

 Accepted Answer

Try this:
data=[1 2,
2 3,
3 3,
4 5,
7 9];
xval = @(yval) fsolve(@(x) interp1(data(:,1), data(:,2), x, 'linear') - yval, median(data(:,1)));
that with these calls to it:
Out3 = xval(3)
Out6 = xval(6)
Out8 = xval(8)
produces:
Out3 =
3.0000
Out6 =
4.7500
Out8 =
6.2500

4 Comments

Thank you, it is working, but i dont understand how it works.
Could you discribe it to me please?
I will do my best!
The fsolve function is a root-finder (or zero-finder), so I used it here to find an ‘x’ value such that the interp1 funciton returns a value for that interpolation that is equal to the ‘yval’ argument.
So fsolve tries different ‘x’ values until it finds one that makes the interp1 result equal to ‘yval’.
I understand. Thank you a lot, this was a great help.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Release

R2017a

Community Treasure Hunt

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

Start Hunting!