【问题标题】:Matlab:output vector giving the x and y values of point of intersectionMatlab:输出向量给出交点的 x 和 y 值
【发布时间】:2011-12-07 05:52:18
【问题描述】:

如何编写一个函数,该函数具有 3 个输入(由系数 [a b c] 和 x 值的向量组成的 2 个向量)的两个直线方程,其形式为 ax+by=c,输出一个向量,给出点的 x 和 y 值交点。

示例:solveSystem([1 -1 -1],[3 1 9],-5:5 ) 应该产生 [2 3]

到目前为止:

function coeff=fitPoly(mat)

% this function takes as an input an nx2 matrix consisting of the
% coordinates of n points in the xy-plane and give as an output the unique
% polynomial (of degree <= n-1) passing through those points.


[n,m]=size(mat);  % n=the number of rows=the number of points
% build the matrix C
if m~=2
    fprintf('Error: incorrect input');
    coeff=0;
  else
  C=mat(:,2);  % c is the vector of y-coordinates which is the 2nd column of mat

  % build the matrix A
  for i=1:n
    for j=1:n
        A(i,j)=(mat(i,1))^(n-j);
    end
end
coeff=inv(A)*C;
end

【问题讨论】:

  • ax+bx=c 不是直线方程。你是说 ax+bu=c 吗?
  • 您置顶的问题与您发布的代码不一致。您是在寻找线的交点还是多项式拟合。你应该澄清你的问题。
  • 我想用它来创建向量的输入。一个用于我的 x 值、y 值和系数,

标签: matlab line-intersection


【解决方案1】:

你不需要向量 x 来求解两条线的交点:

function xy = solve(c1,c2)
  A = [c1(1:2); c2(1:2)];
  b = [c1(3); c2(3)];
  xy = A\b;
end

这将计算

xy = solve([1 -1 -1],[3 1 9])

矩阵

A = [1 -1;
     3 1]
b = [-1
     9]

这样

xy = A^-1 b = [2
               3]

【讨论】:

  • 额外 +1 用于使用 `` 运算符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2018-06-03
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多