如何编写具有3个输入的函数(2个矢量,包括系数[abc]和x值的矢量),形式为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
答案 0 :(得分:2)
你不需要向量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]