How do I find points of intersection between a curve and horizontal line?
3 views (last 30 days)
Show older comments
MathWorks Support Team
on 24 Feb 2021
Answered: MathWorks Support Team
on 25 Feb 2021
I have a curve, not defined via a function handle, but evaluated at a number of points. I would like to find all the points of intersection of this curve with a horizontal line.
Accepted Answer
MathWorks Support Team
on 24 Feb 2021
In general, the intersection points of a horizontal line and a curve can be found by looking for the changes in sign in the difference of the horizontal line and curve. For example,
X = (0:0.01:10)';
Y = sin(X); % curve
H = 0.5 + 0*X; % horizontal line
To determine the points of intersection, examine neighbouring x-values that flip the sign of the difference vector.
sign_flip = (Y(2:end)-H(2:end)).*(Y(1:end-1)-H(1:end-1)); % when the curve crosses the line the difference vector will flip sign and the product of neighbouring points will be negative
X(sign_flip < 0) % gives approximate x-values for where the curve and line crossed
The 'X' values reported will be an approximation to the points of intersection. This approximation can be improved by increasing the sampling rate.
0 Comments
More Answers (0)
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!