Intersection of Discretized Curves
5 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a scatter plot of two curves.
The curves intersect each other and ideally they should have one or more intersection points.
However, as I have discrete points and two curves do not intersect at any of those points, I can not get the point of intersection.
I'm attaching a figure for more clarity.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1719756/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1719756/image.png)
I believe I will have a set of points here instead a single point.
How do I get those set of points from the scatter plot data or the data points I have now?
Thanks..
2 个评论
Tony
2024-6-21
The pair of points on the two curves with the smallest Euclidean distance between them will be the intersection point. If you are looking for multiple, then you can put a threshold; e.g. find the pairs of points whose distance is less than 1e-6
采纳的回答
Star Strider
2024-6-21
It would help to have the data.
TThe approach to these problems is straightforward. In this instance, the independent variable values are different, so the second curve needs to to be interpoalted to the values of the first independent variable (or the reverse of this, depending on what you want to do). After that, it is relatively straightforward to interpolate to find the intersection. (If there are more than one intersection, it will first be necessary to find all the approximate indices of the intersections, and then loop to find the exact values of each intersection.)
Try this —
x1 = linspace(0, 0.06);
y1 = -x1.^2;
x2 = linspace(0, 0.058);
y2 = 1E-3 - x2.^2 * 1.5;
figure
plot(x1, y1, '.')
hold on
plot(x2, y2, '.')
hold off
y2i = interp1(x2, y2, x1, 'pchip', 'extrap'); % Values Of 'y2' Interpolated To 'x1'
intxx = interp1(y1-y2i, x1, 0) % Y-Coordinate Of Intersection
intxy = interp1(x1, y1, intxx) % X-Coordinate Of Intersection
figure
plot(x1, y1, '.-')
hold on
plot(x1, y2i, '.-')
plot(intxx, intxy, 'cs')
hold off
text(intxx, intxy, sprintf('$Intersection\\ at\\ (%.4f, %.4f) \\rightarrow$',intxx,intxy), 'Horiz','right', 'Interpreter','LaTeX')
This should work with your data, as written. I will of course help with any problems if necessary in adapting this to your data.
.
3 个评论
Star Strider
2024-6-21
As always, my pleasure!
I am not certain what you intend by ‘set of points’. Since my code detects and plots the exact intersection, one option for ‘set of points’ could be to consider all the points within a specific Euclidian distance of the intersection, perhaps with the knnsearch function.
Consider something like this (untested):
edist = 1E-3; % Distance Threshold (Limit)
[C1_idx,D1] = knnsearch((x1-intxx), (y1-intxy));
[C2_idx,D2] = knnsearch((x2-intxx), (y2-intxy));
Then, compare ‘D1’ and ‘D2’ separately with ‘edist’ to return a logical vector that can then be used with the index vectors to return the points referenced to them.
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!