To find intersection point of two lines ?

x1=7.8;
x2=8.5;
y1=0.96;
y2=0.94;
p1 = polyfit([x1 x2], [y1 y2], 2);
b1= polyval(p1,1);
m1=polyval(p1,2)-b1;
x3=8.25;
x4=8.25;
y3=0;
y4=.99;
p2 = polyfit([x3 x4], [y3 y4], 2);
b2 = polyval(p2, 1);
m2 = polyval(p2, 2) - b2;
I got x value = -1.2867; from which co-ordinate this value corresponds to? Actually I want to compute intersection of two line with respect to x=[7.8 8.25 8.5]; y=[0.96 0.99 0.94]; over which those two lines are plotted?
x=[7.8 8.25 8.5];
y=[0.96 0.99 0.94];
p=polyfit(x,y,2);
f=polyval(p,x);
plot(x,y,'o',x,f,'-');
hold on;
line([7.8 8.5],[0.96 0.94]);
hold on;
line([8.25 8.25],[0 0.99]);

 채택된 답변

Friedrich
Friedrich 2013년 4월 13일

10 개 추천

Hi,
not sure if I understand correctly what you want but is this what you are looking for?
%line1
x1 = [7.8 8.5];
y1 = [0.96 0.94];
%line2
x2 = [8.25 8.25];
y2 = [0 0.99];
%fit linear polynomial
p1 = polyfit(x1,y1,1);
p2 = polyfit(x2,y2,1);
%calculate intersection
x_intersect = fzero(@(x) polyval(p1-p2,x),3);
y_intersect = polyval(p1,x_intersect);
line(x1,y1);
hold on;
line(x2,y2);
plot(x_intersect,y_intersect,'r*')

댓글 수: 3

hu
hu 2013년 10월 31일
How do you solve a special case when the slope is 0, Inf or –Inf?
Zhengxuan Yuan
Zhengxuan Yuan 2022년 7월 6일
very useful! Thanks!
John D'Errico
John D'Errico 2022년 7월 6일
fzero?

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

추가 답변 (1개)

Matt J
Matt J 2022년 7월 6일
편집: Matt J 2022년 7월 6일

0 개 추천

How do you solve a special case when the slope is 0, Inf or –Inf?
Use linexlines2D (Download) which doesn't make any assumptions on slope.
%line1
x1 = [7.8 8.5];
y1 = [0.5 0.5];
XY1=[x1;y1];
%line2
x2 = [8.25 8.25];
y2 = [0 0.99];
XY2=[x2;y2];
xy_intersect=linexlines2D( XY1(:,1), XY1(:,2) , XY2(:,1), XY2(:,2))
xy_intersect = 2×1
8.2500 0.5000
plot(xy_intersect(1),xy_intersect(2),'ro')
line(x1,y1);
line(x2,y2);

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

질문:

RS
2013년 4월 6일

편집:

2022년 7월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by