0

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
Nzbuu
  • 5,241
  • 1
  • 29
  • 51
  • 2
    ax+bx=c is not an equation of a line. Did you mean ax+bu=c? – Kavka Dec 07 '11 at 06:05
  • Your question at the top and the code you posted are inconsistent. Are you looking for intersection of lines or a polynomial fit. You should clarify your question. – Kavka Dec 07 '11 at 22:38
  • I wanted to use this for my input creating vectors. one for my x-values,y values and coefficients, – George Griffin Dec 08 '11 at 06:01

1 Answers1

2

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]
Kavka
  • 4,191
  • 16
  • 33