function [v,xD,yD] = LeastSq(Choice,D)
% [v,xD,yD] = LeastSq(Choice,D)
%if Choice == 1;
%[v,xD,yD] = LinLeastSq(D);
%elseif Choice == 2 
%[v,xD,yD] = ExpLinLeastSq(D);
%elseif Choice == 3 
%[v,xD,yD] = QuadLeastSq(D);
%end



if Choice == 1;
[v,xD,yD] = LinLeastSq(D);
elseif Choice == 2 
[v,xD,yD] = ExpLinLeastSq(D);
elseif Choice == 3 
[v,xD,yD] = QuadLeastSq(D);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [v,xD,yD] = LinLeastSq(D)

% LINEAR EXAMPLE
xD = 1:length(D);
yD = D;
%xD = D(1,:)
%yD = D(2,:);

M  = [ones(length(xD),1) xD'];


% Fill in the last two columns 
for i = 1:length(xD);
	M(i,2) = xD(i);
end

% The normal system and solution
A = M' * M;
b = M' * yD';

v = inv(M'*M)*M'*yD';

end



function [v,xD,yD] = QuadLeastSq(D) 

% QUADRATIC EXAMPLE
xD = [.1 .2 .3 .4 .5];
yD = [-.18 .31 1.03 2.48 3.73];
xD = 1:length(D);
yD = D;

% Initialize M with 1's everywhere
M = [ones(length(xD),3)];

% Fill in the last two columns 
for i = 1:length(xD);
	M(i,2) = xD(i);
	M(i,3) = xD(i)^2;
end

% The normal system and solution
A = M' * M;
b = M' * yD';

v = inv(M'*M)*M'*yD';

end



function [v,xD,yD] = ExpLinLeastSq(D) 

% LINEAR EXAMPLE
xD = [0 1 2 3];
xD = 0:7;
%yD = [1 3 4 4];
yD = [3.9 5.3 7.2 9.6 12 17 23 31];
%xD = 1:length(D);
%yD = D;
yDlog = log(yD);

M  = [ones(length(xD),1) xD'];


% Fill in the last two columns 
for i = 1:length(xD);
	M(i,2) = xD(i);
end

% The normal system and solution
A = M' * M;
b = M' * yDlog';

v = inv(M'*M)*M'*yDlog';

aa = v(1);
b  = v(2);
a  = exp(aa);

v = [a b];


end

end
