how to mark and print the intersecting coordinates of two lines

조회 수: 48 (최근 30일)
Anitha Limann
Anitha Limann 2021년 10월 9일
댓글: Star Strider 2021년 10월 10일
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?
  댓글 수: 2
Matt J
Matt J 2021년 10월 10일
Since it is discrete data, you need a more formal definition of what it means for them to "intersect". Suppose we have
[x1,y1,x2]=deal([0,1]);
y2=[1,0];
plot(x1,y1,'--x', x2,y2,'-o')
These seem to intersect at (x,y)=[0.5,0.5] but only because I've chosen to connect the points with straight lines instead of some other kind of connecting curve. Is that what you want?
Anitha Limann
Anitha Limann 2021년 10월 10일
Yes. i have these datasets
x1=[10,20,30,40,50,75,100,125,150,175,200,225,250,275];
t1=[1.6,3.2,4.8,6.4,8,12,16,20,24,28,32,36,40,44];
x2=[150,175,200,225,250,275,300,325,350,375,400];
t2=[26.62,29.69,32.75,35.82,38.89,41.96,45.02,48.09,51.16,54.23,57.29];
some point values. where i get this graph
so i want to find this intersecting point and print it on the graph.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2021년 10월 10일
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]
Intercepts = 2×2
0.3001 6.7004 2.9004 22.1012
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.
.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by