# How to get x value for a given y value in a interp1 figure?

4 ビュー (過去 30 日間)
wuwei han 2024 年 3 月 28 日
コメント済み: Star Strider 2024 年 3 月 28 日
If I want to find x value for y==0.5, how can I find that? We only have 9 points.
##### 2 件のコメントなしを表示なしを非表示
Dyuman Joshi 2024 年 3 月 28 日
Use the interp1 function as you have mentioned.
What seems to be the problem?
Refer to its documentation for information regarding accepted syntaxes.
wuwei han 2024 年 3 月 28 日
Sorry, I described wrong situation.
vv=[0.190934428093434,0.277000826121106,0.477820361464529,0.703789686451856,1,0.703789686451856,0.477820361464529,0.277000826121106,0.190934428093434]
plot(-40/3/8:10/3/8:40/3/8,vv, 'r*-')
like this, how can I find x for y==0.5?

サインインしてコメントする。

### 採用された回答

John D'Errico 2024 年 3 月 28 日

You need to understand there is no unique solution. So asking for THE value of x has no answer, since there are two possible solutions.
vv=[0.190934428093434,0.277000826121106,0.477820361464529,0.703789686451856,1,0.703789686451856,0.477820361464529,0.277000826121106,0.190934428093434]
vv = 1×9
0.1909 0.2770 0.4778 0.7038 1.0000 0.7038 0.4778 0.2770 0.1909
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
plot(-40/3/8:10/3/8:40/3/8,vv, 'r*-')
Can you solve the problem? Well, yes. It not not that difficult, as long as you accept the idea of multiple non-unique solutions, and you use a tool that can do the job correctly. One such tool is intersections, as found on the file exchange (written by Doug Schwarz.)
You asked for the x-value of those points where the curve crosses y==0.5.
[xint,yint] = intersections(-40/3/8:10/3/8:40/3/8,vv,[-2 2],[.5 .5])
xint =
-0.79244
0.79244
yint =
0.5
0.5
intersections is fast and robust. It can handle cases where the curve has an infinite slope for example, where interp1 will fail miserably.
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
wuwei han 2024 年 3 月 28 日
Thank you very much! I have already known that.

サインインしてコメントする。

### その他の回答 (2 件)

Star Strider 2024 年 3 月 28 日

To get both of them —
vv=[0.190934428093434,0.277000826121106,0.477820361464529,0.703789686451856,1,0.703789686451856,0.477820361464529,0.277000826121106,0.190934428093434];
L = numel(vv)
L = 9
xv = -40/3/8:10/3/8:40/3/8;
dv = 0.5;
zxi = find(diff(sign(vv - dv)));
for k = 1:numel(zxi)
idxrng = max(1, zxi(k)-1) : min(L,zxi(k)+1);
xp(k) = interp1(vv(idxrng), xv(idxrng), dv);
end
format long
xp
xp = 1×2
-0.792436118381859 0.792436118381859
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
plot(xv, vv, '*-r')
hold on
plot(xp, ones(size(xp))*dv, 'sg', 'MarkerSize', 10)%, 'MarkerFaceColor','g')
plot(xp, ones(size(xp))*dv, '+k', 'MarkerSize',10)
hold off
yline(0.5, '--k')
.
##### 9 件のコメント7 件の古いコメントを表示7 件の古いコメントを非表示
wuwei han 2024 年 3 月 28 日
Star Strider 2024 年 3 月 28 日
My pleasure!

サインインしてコメントする。

Yash 2024 年 3 月 28 日
Hi Wuwei,
You can use the "interp1" function to find the values from the interpolation.
Given below is an example:
x_ref = [-1.7 -1.2 -0.7 -0.4 0 0.4 0.7 1.2 1.7];
y_ref = [0.2 0.3 0.5 0.7 1 0.7 0.5 0.3 0.2];
x_test = [0.5];
y_test = interp1(x_ref, y_ref, x_test)
y_test = 0.6333
plot(x_ref, y_ref, 'r', x_test, y_test, 'ko')
Since "x" is not a function of "y" here (one value of "y" leads to multiple values of "x"), you cannot directly use "interp1" for "x" vs "y". Either use half of the points in that case, or use some other techniques to make "x" a function of "y".
You can refer to the following documentation of the interp1 function for more details: https://www.mathworks.com/help/matlab/ref/interp1.html
Hope this helps!
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
wuwei han 2024 年 3 月 28 日
Thank you very much. I have known there may be no way to find 'x' by 'y' directly.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by