%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%Methode de Jacobi pour le calcul des valeurs propres d'une matrice
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function B=jacobi(A,niter,eps)
%initialisation
B=A;
n=max(size(A));
for k=1:niter
    %
    % recherche du plus grand element en module hors diagonale
    %
    m=0;
    for i=1:n
        for j=1:i-1
            if abs(B(i,j))>abs(m)
               ii=i;jj=j;
               m=B(i,j);
            end
        end
    end
    %
    % test de sortie
    %
       if abs(m) < eps
           break
       end
    %
    % Elimination de l'element via rotation de Givens
    %
       %
       %calcul de la cotangente de l'angle double
       %
       c=0.5*(B(jj,jj)-B(ii,ii))/m;
       %calcul de t la plus petite racine de x^2+2ct-1=0
       if c==0
       t=1;
            else
       t=-c+sign(c)*sqrt(c^2+1);
       end
       co=1/sqrt(1+t^2);
       si=t*co;
       %
       %matrice de rotation de Givens
       %
       G=sparse(eye(n,n));
       G(ii,ii)=co;G(ii,jj)=si;G(jj,jj)=co;G(jj,ii)=-si;
       %
       %multiplication
       %
       B=G'*B*G;
       mesh(B)
       drawnow
   end
   

