Intersection of Discretized Curves

33 次查看(过去 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.
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
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
Prasanna Routray
Prasanna Routray 2024-6-21
Hi.. thanks for the reply. How do I do that?

请先登录,再进行评论。

采纳的回答

Star Strider
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
intxx = 0.0447
intxy = interp1(x1, y1, intxx) % X-Coordinate Of Intersection
intxy = -0.0020
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.
.
  6 个评论
Star Strider
Star Strider 2024-10-5
编辑:Star Strider 2024-10-5
It would be helpful to have a plot call or something similar, so I understand the result you want. (I added onee.)
Also, the code is incomplete. For example, the vectors ‘x1Coord’ and ‘y2Coord’ seem to be missing. Also, how would they be plotteed (since I assume they should be)?
Please provide the missing information, and explain what you want to do.
EDIT — (5 Oct 2024 at 16:16)
For two orthogonal lines, one with a zero slope and another with an infinite slope, the eassiest way would bee to use the ismember (or ismembertol) —
x1 = -100:1:100;
y1 = 5*ones(length(x1),1);
y2 = -100:1:100;
x2 = 5*ones(length(y2),1);
Lv = ismembertol(x1, y1, 1E-10);
Nv = find(Lv)
Nv = 106
intxx = x1(Lv)
intxx = 5
intxy = y1(Lv)
intxy = 5
% 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)
figure
plot(x1, y1)
hold on
plot (x2, y2)
plot(intxx, intxy, 'cs', 'MarkerSize',10)
hold off
grid
The typical intersection approaches, such as in my original answer, will not work in this instance. They will only work with finite slopes.
.
Prasanna Routray
Prasanna Routray 2024-10-5
Hi...
The idea was to plot the two lines, get the point of intersection and plot the intersection point.
yes, the code had some typo. I edited the above one and posted the new code below.
x1 = -100:1:100;
y1 = 5*ones(length(x1),1);
y2 = -100:1:100;
x2 = 5*ones(length(y2),1);
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)
% the following should plot everything if all goes well
figure
plot(x1, y1)
hold on
plot (x2, y2)
plot(intxx, intxy,'or')
hold off
grid

请先登录,再进行评论。

更多回答(1 个)

Bruno Luong
Bruno Luong 2024-10-5
编辑:Bruno Luong 2024-10-5
This code is robust, it doesn't break for repeating x or y coordinates.
The curves are assumed to me line segments connectings data points. Intersection coordinates of two line segments that meet are returned, and all of them.
x1 = -100:1:100;
y1 = 5*ones(size(x1));
y2 = -100:1:100;
x2 = 5*ones(size(y2));
X = InterX([x1; y1],[x2; y2]);
plot(x1,y1,'-',x2,y2,'-.',X(1,:),X(2,:),'or')

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by