% This function is An implementation of the 
% gradient descent optimization.
function x = GradientDescent(transformations, originals, g, upperBound)
% transformations - a set of equation coefficients that 
%                               represent the LR images
% originals - matrix of data set pictures
% g - guess of the answer to the equations
% upperBound - number of iterations in the loop
   i = 0;
   pepsilon = 0.01;
   x = g;
   res = transformations' * (originals - transformations * x);
   disp(res);
   err = res' * res;
   firstErr = err;
   while (i < upperBound && err > firstErr * pepsilon * pepsilon)
       temp=(originals - transformations * x);
       res = transformations' * temp;
       x = x + res;
       err = updateError(res);
       i = i + 1;
   end
end

function err = updateError (temp)
    tranTemp = temp';
    err = tranTemp * temp;
end