Find X-value given Y

55 views (last 30 days)
Adaptine
Adaptine on 24 Sep 2015
Hello
I've been doing some research regarding this but I can't seem to find a solution for when I want to find the X-value given Y.
I have the following data:
>> X
ans =
20 50 100 200 500 1000 2000 5000 10000 20000
>> Y
ans =
-0.1755 -0.1755 -0.9151 -2.8534 -8.4043 -13.1515 -20.0000 -27.5350 -33.9794 -40.0000
Where the X is plotted on a logarithmic scale. I tried interp1 with X and Y switched but that dosent work since its not strictly monotonic increasing.
Does anyone have a solution on this? Say I want to find the value of X given Y=-5

Accepted Answer

Star Strider
Star Strider on 24 Sep 2015
The ‘x’ and ‘y’ vectors were not the same sizes, so I added 25 to ‘x’, then added a small amount to the duplicated element in ‘y’ and did the interpolation:
x = [25 50 100 200 500 1000 2000 5000 10000 20000];
y = [ -0.1755 -0.1755 -0.9151 -2.8534 -8.4043 -13.1515 -20.0000 -27.5350 -33.9794 -40.0000];
dy = diff([0 y]);
dyix = find(dy == 0);
y(dyix) = y(dyix-1)+1E-8;
xint = interp1(y, x, -10); % Find The Value Of ‘x’ Corresponding To y=-10’
  5 Comments
Star Strider
Star Strider on 25 Sep 2015
My pleasure.
Exactly. The interp1 function has no idea how to interpret two values of the dependent variable for one value of the independent variable. (Nor would I, without making some assumptions that interp1 wisely avoids.) By clearing up that ambiguity (so that the independent variable is monotonically increasing and the duplication in it no longer exists), interp1 proceeeds. (It also needs its first two arguments to have the same size in one dimension.)
the cyclist
the cyclist on 25 Sep 2015
Note that his X and Y vectors did actually have equal numbers of elements. They scroll off the right-hand side of the window, though.

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 24 Sep 2015
Here is a terribly kludgy way to do it, but it gets the job done. It sorts Y, and then in the case of equal entries in Y, it will add a tiny offset. (This will work even if there are long series of consecutive equal entries.)
X = [20 50 100 200 500 1000 2000 5000 10000 20000];
Y = [-0.1755 -0.1755 -0.9151 -2.8534 -8.4043 -13.1515 -20.0000 -27.5350 -33.9794 -40.0000];
[sortedY sortingIndex] = sort(Y);
sortedX = X(sortingIndex);
for ny = 2:numel(sortedY);
if sortedY(ny)==sortedY(ny-1)
sortedY(ny) = sortedY(ny) + eps(sortedY(ny));
end
end
interpolated_X = interp1(sortedY,sortedX,-5)
  1 Comment
Sean Eruppakkattu
Sean Eruppakkattu on 1 Jun 2019
Nice solution for preventing consecutive equal entries, though maybe I would use for the sake of intuitivity the following code to order the X and Y arrays:
A = [X,Y]; % matrix
res = sortrows(A,2); % matrix is ordered with respect with the Y-column
with also significant one-order speed improvement. Then the for-loop would simple refer to the column vector of the matrix.

Sign in to comment.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!