Matlab:输出向量给出交点的x和y值

发布于 2024-12-20 09:57:46 字数 796 浏览 1 评论 0原文

如何编写一个具有 3 个输入(由系数 [abc] 和 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

How do I write a function having 3 inputs (2 vectors consisting of coefficients[a b c] and vector of x values) of two line equations of form ax+by=c that outputs a vector giving x and y values of the point of intersection.

Example: solveSystem([1 -1 -1],[3 1 9],-5:5 ) should produce [2 3]

So far:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼前雾蒙蒙 2024-12-27 09:57:46

您不需要向量 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]

You don't need the vector x to solve for the intersection of the two lines:

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

which would compute for

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

the matrices

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

so that

xy = A^-1 b = [2
               3]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文