How to determine the exact intersection between line and two curves

38 次查看(过去 30 天)
Data is attached.
Hi everyone,
I am trying to find the exact intersection between line and curve and I tried everything and all they gave me an approximate intersection. Can anyone help me to determine the exact intersection. Thanks in advance,
I used " intersect points=find(p1==p2)"
intersectionpoints =
0×1 empty double column vector
However, If i used intersectionpoints=find(p1>=p2)
intersectionpoints =
9 10 11 12 13 14 18 19 20 21 22
So 9 is not the exact point, it is the point after the intersection between p1 and p2, how to find the exact intersected point between the red line and the blue curve.

采纳的回答

Star Strider
Star Strider 2022-3-14
Try this —
LD = load(' matlab.mat');
p1 = LD.p1;
p2 = LD.p2;
x = linspace(0, numel(p1)-1, numel(p1));
idx = find(diff(sign(p1-p2))); % Approximate Indices Of The Intersections
for k = 1:numel(idx)
idxrng = max(1,idx(k)-1) : min(numel(p1),idx(k)+1);
xi(k) = interp1(p1(idxrng)-p2(idxrng), x(idxrng), 0);
yi(k) = interp1(x(idxrng),p2(idxrng),xi(k));
end
Intersections = table(xi,yi)
figure
plot(x, p1, x, p2)
hold on
plot(xi, yi, 'ms')
hold off
grid
legend('p_1','p_2','Intersections', 'Location','best')
Intersections =
4×2 table
xi yi
______ __________
7.5374 1.5534e+08
13.133 1.5534e+08
16.345 1.5534e+08
21.731 1.5534e+08
.

更多回答(1 个)

Torsten
Torsten 2022-3-14
Depending on how you interpolate between the points of the blue curve, you will get different intersection points.
So there is no "exact" intersection you can expect to get.
If (x,y) are the points of the blue curve and y0 is the y-value of the orange curve, you can do something like:
i = 0;
for j = 1:numel(x)-1
xl = x(j);
yl = y(j);
xn = x(j+1);
yn = y(j+1);
if yl <= y0 & yn >= y0
i = i+1;
xi(i) = (y0*(xn-xl)+xl*yn-xn*yl)/(yn-yl);
end
end
xi
This will give you an approximation for the x-coordinates of the intersection points.

类别

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

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by