Matlab:输出向量给出交点的x和y值
如何编写一个具有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要向量 x 来求解两条线的交点:
它将计算
矩阵,
以便
You don't need the vector x to solve for the intersection of the two lines:
which would compute for
the matrices
so that