How to plot vertical lines at the intersection of two curves in matlab?

39 Views Asked by At

I am trying to find the multiple points of intersection of sin(x) and a horizontal line at y = 0.5 so that I can plot vertical lines at those points.

I tried to get the points of intersection with find() function and used for loop to draw the vertical line. I only get a single vertical line plotted.

intersection_indices = find(abs(curve - horizontal_line) < 1e-3);
x_intersections = x(intersection_indices);

for i = 1:length(x_intersections)
    plot([x_intersections(i), x_intersections(i)], [min(curve), max(curve)], 'g:', 'LineWidth', 2);
end
1

There are 1 best solutions below

1
Abdo On

I'd recommend using the xline function and hold on/off. It would look something like this:

plot(x,curve);
hold on
xline(x_intersections);
hold off
legend('Curve','Intersections')