24小时热门版块排行榜    

查看: 3729  |  回复: 9

小鬼木木

新虫 (小有名气)

[求助] MATLAB求两直线交点 已有2人参与

已知一条直线L1上两点坐标分别为A(x1,y1)、B(x2,y2),直线L2上两点坐标分别为C(x3,y3)、D(x4,y4),如何用MATLAB求取直线L1、L2的交点坐标?
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

月只蓝

主管区长 (职业作家)

【答案】应助回帖

感谢参与,应助指数 +1
两点式可确定两条直线的线性方程,两个方程联立即可求得交点坐标。
MATLAB、MS小问题、普通问题请发帖求助!时间精力有限,恕不接受无偿私信求助。
2楼2016-10-09 18:43:11
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

小鬼木木

新虫 (小有名气)

引用回帖:
2楼: Originally posted by 月只蓝 at 2016-10-09 18:43:11
两点式可确定两条直线的线性方程,两个方程联立即可求得交点坐标。

这我知道,怎么用MATLAB程序来求呢?
3楼2016-10-09 18:49:07
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

小鬼木木

新虫 (小有名气)

引用回帖:
2楼: Originally posted by 月只蓝 at 2016-10-09 18:43:11
两点式可确定两条直线的线性方程,两个方程联立即可求得交点坐标。

这我知道,怎么用MATLAB程序来求呢?
4楼2016-10-09 18:49:26
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

FMStation

至尊木虫 (知名作家)

【答案】应助回帖

★ ★ ★
感谢参与,应助指数 +1
小鬼木木: 金币+3 2016-10-10 18:36:21
https://www.mathworks.com/matlabcentral/answers/70287-to-find-intersection-point-of-two-lines
CODE:
%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*')

5楼2016-10-09 23:09:27
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

小鬼木木

新虫 (小有名气)

引用回帖:
5楼: Originally posted by FMStation at 2016-10-09 23:09:27
https://www.mathworks.com/matlabcentral/answers/70287-to-find-intersection-point-of-two-lines

%line1
x1  = ;
y1  = ;
%line2
x2 = ;
y2 = ;
%fit linear polynomial
p1 = polyfit(x1,y1,1);
p2 = ...

非常感谢您的帮助,但是结果怎么不对呢?就拿你给的数据来说,通过自己来计算得出的交点坐标为(4.932  5.33);但是根据你的程序得到的交点坐标为(8.25  0.9471);还望指教!
6楼2016-10-10 16:50:48
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

FMStation

至尊木虫 (知名作家)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
小鬼木木: 金币+12, ★★★★★最佳答案 2016-10-10 18:37:28
https://www.mathworks.com/matlabcentral/fileexchange/22441-curve-intersections/content/InterX.m
CODE:
function P = InterX(L1,varargin)
%INTERX Intersection of curves
%   P = INTERX(L1,L2) returns the intersection points of two curves L1
%   and L2. The curves L1,L2 can be either closed or open and are described
%   by two-row-matrices, where each row contains its x- and y- coordinates.
%   The intersection of groups of curves (e.g. contour lines, multiply
%   connected regions etc) can also be computed by separating them with a
%   column of NaNs as for example
%
%         L  = [x11 x12 x13 ... NaN x21 x22 x23 ...;
%               y11 y12 y13 ... NaN y21 y22 y23 ...]
%
%   P has the same structure as L1 and L2, and its rows correspond to the
%   x- and y- coordinates of the intersection points of L1 and L2. If no
%   intersections are found, the returned P is empty.
%
%   P = INTERX(L1) returns the self-intersection points of L1. To keep
%   the code simple, the points at which the curve is tangent to itself are
%   not included. P = INTERX(L1,L1) returns all the points of the curve
%   together with any self-intersection points.
%   
%   Example:
%       t = linspace(0,2*pi);
%       r1 = sin(4*t)+2;  x1 = r1.*cos(t); y1 = r1.*sin(t);
%       r2 = sin(8*t)+2;  x2 = r2.*cos(t); y2 = r2.*sin(t);
%       P = InterX([x1;y1],[x2;y2]);
%       plot(x1,y1,x2,y2,P(1,:),P(2,:),'ro')

%   Author : NS
%   Version: 3.0, 21 Sept. 2010

%   Two words about the algorithm: Most of the code is self-explanatory.
%   The only trick lies in the calculation of C1 and C2. To be brief, this
%   is essentially the two-dimensional analog of the condition that needs
%   to be satisfied by a function F(x) that has a zero in the interval
%   [a,b], namely
%           F(a)*F(b) <= 0
%   C1 and C2 exactly do this for each segment of curves 1 and 2
%   respectively. If this condition is satisfied simultaneously for two
%   segments then we know that they will cross at some point.
%   Each factor of the 'C' arrays is essentially a matrix containing
%   the numerators of the signed distances between points of one curve
%   and line segments of the other.

    %...Argument checks and assignment of L2
    error(nargchk(1,2,nargin));
    if nargin == 1,
        L2 = L1;    hF = @lt;   %...Avoid the inclusion of common points
    else
        L2 = varargin{1}; hF = @le;
    end
      
    %...Preliminary stuff
    x1  = L1(1,:)';  x2 = L2(1,:);
    y1  = L1(2,:)';  y2 = L2(2,:);
    dx1 = diff(x1); dy1 = diff(y1);
    dx2 = diff(x2); dy2 = diff(y2);
   
    %...Determine 'signed distances'   
    S1 = dx1.*y1(1:end-1) - dy1.*x1(1:end-1);
    S2 = dx2.*y2(1:end-1) - dy2.*x2(1:end-1);
   
    C1 = feval(hF,D(bsxfun(@times,dx1,y2)-bsxfun(@times,dy1,x2),S1),0);
    C2 = feval(hF,D((bsxfun(@times,y1,dx2)-bsxfun(@times,x1,dy2))',S2'),0)';

    %...Obtain the segments where an intersection is expected
    [i,j] = find(C1 & C2);
    if isempty(i),P = zeros(2,0);return; end;
   
    %...Transpose and prepare for output
    i=i'; dx2=dx2'; dy2=dy2'; S2 = S2';
    L = dy2(j).*dx1(i) - dy1(i).*dx2(j);
    i = i(L~=0); j=j(L~=0); L=L(L~=0);  %...Avoid divisions by 0
   
    %...Solve system of eqs to get the common points
    P = unique([dx2(j).*S1(i) - dx1(i).*S2(j), ...
                dy2(j).*S1(i) - dy1(i).*S2(j)]./[L L],'rows')';
              
    function u = D(x,y)
        u = bsxfun(@minus,x(:,1:end-1),y).*bsxfun(@minus,x(:,2:end),y);
    end
end

7楼2016-10-10 17:40:37
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

FMStation

至尊木虫 (知名作家)

【答案】应助回帖

★ ★ ★ ★ ★
小鬼木木: 金币+5, ★★★很有帮助 2016-10-10 18:37:13
Subtract element-by-element.
CODE:
x = -4 : .1 : 4;
y1 = x + 3;
y2 = x.^2 -4;

s = y1 - y2;
ix = find(s > -.1 & s < .1);
x_sol = x(ix)
y1_sol = y1(ix)
y2_sol = y2(ix)

The results are:
CODE:
x_sol =     -2.2000    3.2000
y1_sol =    0.8000    6.2000
y2_sol =    0.8400    6.2400

8楼2016-10-10 18:00:25
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

小鬼木木

新虫 (小有名气)

引用回帖:
7楼: Originally posted by FMStation at 2016-10-10 17:40:37
https://www.mathworks.com/matlabcentral/fileexchange/22441-curve-intersections/content/InterX.m


function P = InterX(L1,varargin)
%INTERX Intersection of curves
%   P = INTERX(L1,L2) returns th ...

谢谢!
9楼2016-10-10 18:36:47
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
强!
10楼2016-10-11 07:21:44
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 小鬼木木 的主题更新
信息提示
请填处理意见