![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/926954/image.png)
How to determine the exact intersection between line and two curves
33 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hossam Mosbah
le 14 Mar 2022
Réponse apportée : Star Strider
le 14 Mar 2022
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/926949/image.png)
0 commentaires
Réponse acceptée
Star Strider
le 14 Mar 2022
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/926954/image.png)
.
0 commentaires
Plus de réponses (1)
Torsten
le 14 Mar 2022
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.
0 commentaires
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!