% find the polynomial of degree 5 passing through 6 given points

% create augmented matrix based on the given points
x = 1; y = 1;
% each point defines an equation/row in the matrix
A(x,:) = [x^0 x^1 x^2 x^3 x^4 x^5 y]
x = 2; y = 3;
% each point defines an equation/row in the matrix
A(x,:) = [x^0 x^1 x^2 x^3 x^4 x^5 y]
x = 3; y = 5;
% each point defines an equation/row in the matrix
A(x,:) = [x^0 x^1 x^2 x^3 x^4 x^5 y]
x = 4; y=-2;
% each point defines an equation/row in the matrix
A(x,:) = [x^0 x^1 x^2 x^3 x^4 x^5 y]
x = 5; y = 11;
% each point defines an equation/row in the matrix
A(x,:) = [x^0 x^1 x^2 x^3 x^4 x^5 y]
x = 6; y = -12;

% from the rref we can read off the required coeffs 
r = rref(A);
% a contains the coeffs
a = r(:,end);

% domain
x = 0:.01:7;

% the polynomial equation
y = a(1) + a(2)*x + a(3)*x.^2 + a(4)*x.^3 + a(5)*x.^4 + a(6)*x.^5
plot(x,y,1,1,'r*',2,3,'r*',4,-2,'r*',5,11,'r*',6,-12,'r*');
