How to find intersection between two array line plots

29 ビュー (過去 30 日間)
Omaima Azoui
Omaima Azoui 2022 年 4 月 5 日
編集済み: Star Strider 2022 年 4 月 6 日
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.

回答 (1 件)

Star Strider
Star Strider 2022 年 4 月 5 日
編集済み: Star Strider 2022 年 4 月 6 日
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.
.

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by