MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

how to get the x value for a given y value?

ahmed salah さんによって質問されました 2019 年 5 月 22 日 11:17
最新アクティビティ ahmed salah さんによって コメントされました 2019 年 5 月 22 日 12:16
I plot x-y graph (Gaussian function) and want to get the x-axis value (will be two points in this case) at a certain y-axis value (half of the maximum)
I tried this but it didn't work:
clc;
clear all;
Fs = 150; % Sampling frequency
t = -0.5:1/Fs:0.5; % Time vector of 1 second
x = 1/(sqrt(2*pi*0.01))*(exp(-t.^2/(2*0.01)));
figure;
plot(t,x);
xi = 0.5*max(x) ;
z=find(x==xi);
ti = x(z) ;
hold on
plot(ti,xi,'*r')

  0 件のコメント

サインイン to comment.

2 件の回答

回答者: Geoff Hayes
2019 年 5 月 22 日 11:36
 採用された回答

ahmed - your code assumes that there is an x value that is identical to xi
z=find(x==xi);
This need not be true. And comparing doubles in this manner is not generally a good idea (due to precision, see Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero?). Usually a tolerance of some kind should be used (i.e. abs(x - y) < eps). In your case, a tolerance might not work as well because you will not know what that tolerance should be. You could try different values...the following seems to work for this dataset
z=find(abs(x-xi)< 0.10);
ti = t(z) ;
hold on
plot(ti,xi,'*r')
(Note how ti is obtained from the t array instead of x.)

  1 件のコメント

ahmed salah 2019 年 5 月 22 日 12:16
thank you for your help

サインイン to comment.


Jan
回答者: Jan
2019 年 5 月 22 日 11:52

You cannot expect that any of the points at t = -0.5:1/Fs:0.5 is exactly 0.5*max(x). Remember that you evaluate x at some time steps only and rounding errors have to be considered also.
There is an analytical solution also, but you can use fzero to find the searched points. But you have to find the maximum value of the curve at first. Setting the derivative to 0 will help you.

  0 件のコメント

サインイン to comment.



Translated by