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

 Accepted Answer

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 Comment

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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!