How to find intersection between two array line plots

29 visualizaciones (últimos 30 días)
Omaima Azoui
Omaima Azoui el 5 de Abr. de 2022
Editada: Star Strider el 6 de Abr. de 2022
The problem is the array is to plot a payload diagram so the array is made up of 4 x and y points. So basically 4 x,y coordinates but the lines intersect and I want to know at what point they intersect.
The first array has 3 lines horizontal then diagonal then diagonal again but steeper. And the other array has only 2 x,y coordinates and intersects the first diagnonal line! Can you help me find the value at that intersection AKA teh coordinates there.

Respuestas (1)

Star Strider
Star Strider el 5 de Abr. de 2022
Editada: Star Strider el 6 de Abr. de 2022
It would help to have the actual data.
x = 1:4;
y1 = rand(size(x));
y2 = rand(size(x));
xi = linspace(min(x), max(x), numel(x)*10); % Increase 'x' Resolution
y1i = interp1(x,y1,xi); % Increase 'y1' Resolution
y2i = interp1(x,y2,xi); % Increase 'y2' Resolution
zxi = find(diff(sign(y1i-y2i))); % Appriximate Indices Of Intersections
for k = 1:numel(zxi)
idxrng = max(1,zxi(k)-1) : min(numel(xi),zxi(k)+1); % Index Range For Interpolation
xix(k,:) = interp1(y1i(idxrng)-y2i(idxrng),xi(idxrng),0); % 'x' Value At Intersection
yix(k,:) = interp1(xi(idxrng),y1i(idxrng),xix(k)); % 'y' Value At Intersection
end
Intersections = table(xix,yix)
Intersections = 2×2 table
xix yix ______ _______ 1.3419 0.35144 2.4677 0.42242
figure
plot(x, y1, x, y2)
hold on
plot(xix, yix, 'sm')
hold off
grid
This will be reliable if there is only one intersection between each pair of indices. It may fail to discriminate intersections that are close together, even with the increased point resolution.
EDIT — (6 Apr 2022 at 13:00)
Made ‘xi’ length a function of the original ‘x’ length.
.

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by