function exam3_student_version

%DRIVER_power_blind
DRIVER_jacobi_adaptive

return

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

function DRIVER_power

load matrix_Y

keyboard
return

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

function DRIVER_jacobi_adaptive

rng(0)
m = 100;                  % Matrix dimension
npass = 10;               % Number of passes through the entire matrix.
niter = npass*m*(m-1)/2;  % Number of Givens rotations to be applied.

%%% Generate a symmetric matrix of size m x m:
A = triu(randn(m));
A = A + triu(A,1)';

%%% Jacobi iteration.
T = A;
errorvec = zeros(1,niter); % Error at step j (Frob norm of offdiag part)
ndone = 1;
for k = 1:npass
  for i = 1:(m-1)
    for j = (i+1):m
      beta = (T(j,j) - T(i,i))/(2*T(i,j));
      t    = sign(beta)/(abs(beta) + sqrt(beta*beta+1));
      c    = 1/sqrt(t*t+1);
      G    = [c,c*t;-c*t,c];
      T([i,j],:) = G'*T([i,j],:);
      T(:,[i,j]) = T(:,[i,j])*G;
      errorvec(ndone) = sqrt(sum(sum(triu(T,1).^2)));
      ndone = ndone + 1;
    end
  end
end

hold off
semilogy(1:niter,errorvec,'b',...
         'LineWidth',2)
hold on
plot([1;1]*linspace(0,niter,npass+1),[1e-15;max(errorvec)]*ones(1,npass+1),'k--')
legend('Without pivoting')
axis([0,niter,1e-15,max(errorvec)])

keyboard
return

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