% lsfit.m MatLab least square fit, built in
diary('lsfit_lect_m.out')
format compact
x = [0.0 0.0 0.0];
y = [0.0 0.0 0.0];
xd = [[1.0 2.5 3.7]; [2.0 2.5 3.6]; [3.0 2.7 3.5]; ...
      [2.2 2.1 3.1]; [1.5 2.0 2.6]; [1.6 2.0 3.1]]
yd = [32.5  7.2  6.9  22.4  10.4  11.3] % Y actual
A = zeros(3,3);
X = zeros(3);
Y = zeros(3);     % |A| * |X| = |Y|  |X| will be  a, b, c

for i=1:3
  for k=1:6
    Y(i) = Y(i) + xd(k,i)*yd(k); % Xi * Yk
  end
  for j=1:3
    for k=1:6  % run through observations
        A(i,j) = A(i,j) + xd(k,j)*xd(k,i);  % Xj * Xi
     end  % k
   end  % j
end % i
X = A\Y; % simeq
fprintf(1, 'a=%f, b=%f, c=%f \n', X(1), X(2), X(3))

fprintf(1,'really bad fit \n')
for k=1:6
  ya = X(1)*xd(k,1)+X(2)*xd(k,2)+X(3)*xd(k,3);
  fprintf(1,'y=%f,  yapp=%f \n', yd(k), ya)
end % k
diary off
% end lsfit_lect.m
