Hi all,
I have a plot from experimental data. I want to find the two values of x that corresponds to y=0. Basically, I want to find the coordinates A and B ( in both coordinates y=0) in the file attached below. I tried by using " find (x==0)" but it gave me blank. Can you please help how can I find them?
Thank you

 採用された回答

Matt J
Matt J 2020 年 10 月 2 日

0 投票

fzero(@(x)interp1(A,x), [1,numel(A)])

13 件のコメント

Babu Sankhi
Babu Sankhi 2020 年 10 月 2 日
編集済み: Babu Sankhi 2020 年 10 月 2 日
I am sorry it did not work? It gave me the error like this:
Error using fzero (line 246)
FZERO cannot continue because user-supplied function_handle ==> @(x)interp1(0,x) failed with the error below.
Interpolation requires at least two sample points in each dimension.
Error in Hallode (line 42)
fzero(@(x)interp1(0,x), [1,numel(0)])
>>
Perhaps I donot have any function. I just have a plot (y versus x) from my experimental data. I need to find the the points on the plots at which y becomes 0. There are two coordinates corresponding to this. Can you help me more?
Matt J
Matt J 2020 年 10 月 2 日
編集済み: Matt J 2020 年 10 月 2 日
It looks like instead of passing in a vector A, you passed in the scalar 0. To see how it's suposed to work, suppose we have, for example,
A=[-ones(1,10), -.25,0.5, +ones(1,10)]; plot(A,'x-');
Now, do the thing:
>> fzero(@(x)interp1(A,x), [1,numel(A)])
ans =
11.3333
Babu Sankhi
Babu Sankhi 2020 年 10 月 2 日
編集済み: Babu Sankhi 2020 年 10 月 2 日
It gave me error like that . ( BTW I have to get two values of x at y=0). Can you please help me more?
>> fzero(@(x)interp1(y,x), [1,numel(y)])
Error using fzero (line 290)
The function values at the interval endpoints must differ in sign.
thank you
Matt J
Matt J 2020 年 10 月 2 日
BTW I have to get two values of x at y=0
Just apply the method twice, once to each curve.
Babu Sankhi
Babu Sankhi 2020 年 10 月 2 日
編集済み: Babu Sankhi 2020 年 10 月 2 日
I tried that way as well. But it did nt give the right one!
Moreover I should get the two values of X from single plot can we do that?
can you please help me ?
Matt J
Matt J 2020 年 10 月 3 日
編集済み: Matt J 2020 年 10 月 3 日
In what way is the answer wrong? You realize that a=60.46 is the index at which the root occurs, correct?
Babu Sankhi
Babu Sankhi 2020 年 10 月 3 日
編集済み: Babu Sankhi 2020 年 10 月 3 日
Rather than this , interpo1( y,x,0) worked well. BTW thank you. But still I am needing idea if Y=0 does not have unique value( in my case there are two values of X at Y=0 and i need to get both) . It would great if you can help me in this regard.
Matt J
Matt J 2020 年 10 月 3 日
編集済み: Matt J 2020 年 10 月 3 日
I told you before. You should split the data into two separate curves.
Babu Sankhi
Babu Sankhi 2020 年 10 月 4 日
I tried by splitting the data and tried to find the x values for certain y value by using interpolation. It always gave me the error as seen clear in the figure attached below. I have also attached the one set of data tha I used. Can you please help me to figure out the problem?
Matt J
Matt J 2020 年 10 月 4 日
編集済み: Matt J 2020 年 10 月 4 日
It is not terribly useful to see an image of your code. It is best to present the code in text form so that we can copy, paste and run it. Also, this data does not have any locations where Y=0. Are you trying to find the X for which Y=-4.54?
Babu Sankhi
Babu Sankhi 2020 年 10 月 4 日
編集済み: Babu Sankhi 2020 年 10 月 4 日
figure(3);
dt=importdata('try.txt');
x=dt(:,1);
y=dt(:,2);
plot(x,y,'r--o')
xlabel('X');
ylabel('Y');
set(gca,'Fontsize',18)
xvalue =interp1(y,x,-4.54);% x value at y=-4.54
I have pasted codes above,
Yes I am trying to find the X for which Y=-4.54. Can you help me more?
Thank you
Matt J
Matt J 2020 年 10 月 4 日
編集済み: Matt J 2020 年 10 月 4 日
xvalue = fzero( @(xq)interp1(x,y+4.54,xq) , [min(x),max(x)] )% x value at y=-4.54
Babu Sankhi
Babu Sankhi 2020 年 10 月 4 日
Thank you ,
It worked now.

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

その他の回答 (0 件)

質問済み:

2020 年 10 月 1 日

コメント済み:

2020 年 10 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by