how to mark and print the intersecting coordinates of two lines
38 views (last 30 days)
Show older comments
I have x1,y1 and x2, y2 data sets that give me two lines graphs.
I want to find the intersecting point of these two lines and and print it on the graph.
I tried following different functions like intesect but did not work.
Could someone please help me with this?
Accepted Answer
Star Strider
on 10 Oct 2021
Much depends on what ‘x1’, ‘y1’ and the rest are.
One approach (sssuming multiple intersections, although thiis will also work for only one intersection) would be —
x1 = linspace(-2, 15);
y1 = 2 + 3*x1;
x2 = linspace(-10, 10);
y2 = (x2-2).^2;
xq = linspace(min([x1,x2],[],2), max([x1,x2],[],2)); % Create Common 'x' Vector
y1q = interp1(x1, y1, xq, 'pchip','extrap'); % Interpolate To It
y2q = interp1(x2, y2, xq, 'pchip','extrap'); % Interpolate To It
idx = find(diff(sign(y1q-y2q))); % Approximate Indices Of Intersections
for k = 1:numel(idx)
idxrng = max(1,idx(k)-2) : min(numel(xq),idx(k)+2); % Index Range
xi(k) = interp1(y1q(idxrng)-y2q(idxrng), xq(idxrng), 0); % Interpolate X-Intersection
yi(k) = interp1(xq(idxrng), y1q(idxrng), xi(k)); % Interpolate Y-Intersection (Either Y-Vector Will Work)
end
Intercepts = [xi; yi]
figure
plot(x1, y1)
hold on
plot(x2, y2)
plot(xi, yi, '+r', 'MarkerSize',10)
hold off
grid
legend('y1','y2','Intersections', 'Location','best')
Experiment to get the desired result.
.
2 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!