Hello I am plotting X vs Y and would like to know what is the value of X when Y is equal to zero. I think it is the x intercept. the graph is not really simple its like a wave and I would like to know the first x intercept only. thanks

 採用された回答

Star Strider
Star Strider 2016 年 10 月 26 日

0 投票

Something like this will do what you want:
x = linspace(0, 10, 250); % Create Data
y = cos(2*pi*x) + x/5; ` % Create Data
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
y_idx = zci(y); % Approximate Indices Of Zero-Crossings
idx_rng = y_idx(1)-1:y_idx(1)+1; % Index Range Near First Zero-Crossing
first_zero = interp1(y(idx_rng), x(idx_rng), 0, 'linear','extrap'); % Interpolate To Find ‘x’ Of First Zero Crossing, Allow For Extrapolation If Necessary
figure(1)
plot(x, y)
hold on
plot(first_zero, 0, 'gp', 'MarkerSize',10, 'MarkerFaceColor','g')
hold off
grid

1 件のコメント

aryan abedi
aryan abedi 2017 年 7 月 19 日
I used the same code but I got an error. I was wondering if you could help me please. I know I have to zero crossing analysis but I have no idea how to.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by