How to intersect these lines?

조회 수: 1 (최근 30일)
Emmanuel
Emmanuel 2014년 5월 29일
댓글: Emmanuel 2014년 5월 29일
Hi all! Given below is the code for plottong three lines given any two arbitrary points per line. I have to find the vertices of the intersection points of these lines. I am getting the plot , but they are not intersecting. I have attached the image below, please help!!
%For Line 1
[x1] = input('Enter the value of 1st coord');
[y1] = input('Enter the value of 1st coord');
t = linspace(0,1);
xa = x1(1)+ (y1(1)-x1(1))*t;
ya = y1(1) + (y1(2)-x1(1))*t;
%For Line 2
[x2] = input('Enter the value of 1st coord');
[y2] = input('Enter the value of 1st coord');
u = linspace(0,1);
xb = x2(1)+ (y2(1)-x2(1))*u;
yb = y2(1) + (y2(2)-x2(1))*u;
%For Line 3
[x3] = input('Enter the value of 1st coord');
[y3] = input('Enter the value of 1st coord');
v = linspace(0,1);
xc = x3(1)+(y3(1)-x3(1))*v;
yc = y3(1)+(y3(2)-x3(1))*v;
plot(xa,ya,xb,yb,xc,yc);

답변 (1개)

David Sanchez
David Sanchez 2014년 5월 29일
I give you the code for two lines, you extend it to three (do it by pairs
if true
% code
end):
%For Line 1
[x1] = [2 3];%input('Enter the value of 1st coord');
[y1] = [3 4];%input('Enter the value of 1st coord');
ma = (y1(2)-y1(1))/(x1(2)-x1(1));
na = y1(1) - ma*x1(1);
%For Line 2
[x2] = [2 4];%input('Enter the value of 1st coord');
[y2] = [2 -3];%input('Enter the value of 1st coord');
mb = (y2(2)-y2(1))/(x2(2)-x2(1));
nb = y2(1) - mb*x2(1);
syms x
solve(x*ma + na == x*mb + nb)
ans =
12/7
  댓글 수: 2
David Sanchez
David Sanchez 2014년 5월 29일
syms x
x=solve(x*ma + na == x*mb + nb)
x=
12/7
y=x*ma+na
y =
19/7
Emmanuel
Emmanuel 2014년 5월 29일
Hi! Thankyou for your reply. With regard to your reply for the previous post, the code is here:
for i=1:1:3
[A] = input('enter the 1st co-ordinate of the line');
[B] = input('enter the 2nd co-ordinate of the line');
xlim = [-10 10];
m = (B(2)-B(1))/(A(2)-A(1));
n = B(2)*m - A(2);
y1 = m*xlim(1) + n;
y2 = m*xlim(2) + n;
hold on
line([xlim(1) xlim(2)],[y1 y2])
plot(y1,y2)
hold off
end
%SAMPLE OUTPUT
%enter the 1st co-ordinate of the line[1,5]
%enter the 2nd co-ordinate of the line[3,2.5]
%enter the 1st co-ordinate of the line[3,1]
%enter the 2nd co-ordinate of the line[4,1]
%enter the 1st co-ordinate of the line[4,1]
%enter the 2nd co-ordinate of the line[4.5,5.8]
When I plot this on a paper and the output graph by matlab varies a lot. if i change the xlim to [0 20] , the lines do not intersect at all. Why does the output from matlab varies from that of the paper graph even if I use the same scale? Ihave attached the output plot. Can we like rectify it?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by