// Barré Elodie 
// Ferrié Vincent 
// Ing 1 Groupe D 

// partie A

function [Delta]=MatDiag(K,N)

Delta=zeros(N,N);
Delta(1,1)=1;
Delta(N,N)=1/K;

for i=2:N
    Delta(i,i)=K^(-1/(N-i+1))  // construit la matrice diagonale 
end

endfunction

function [K]=Veri(Delta)

K=norm(Delta,2)*norm(inv(Delta),2) // multiplie la norme 2 de A et la norme 2 de l'inverse de A
endfunction

function [H]=TriDiag(N)

H=diag(ones(1,N)*2)+diag(ones(1,N-1)*-1,-1)+diag(ones(1,N-1)*-1,1) // construit la matrice tri-diagonal 

endfunction

function [A]=Dec(H) // décomposition en valeur singulière 

[U,S,V]=svd(H)
A=U*Delta*V'

endfunction

function [Kb]=Verif(A)
    Kb=norm(A,2)*norm(inv(A),2) 
endfunction


K=10;
N=10;
delta= MatDiag(K,N)
H=TriDiag(N)
delta
H
[U,S,V]=svd(H)
A=U*delta*V'
A
k10=Veri(A) // a modifier 
n10=Veri(delta) // a modifier

K=1000;
N=50;
delta= MatDiag(K,N)
H=TriDiag(N)
delta
H
[U,S,V]=svd(H)
A=U*delta*V'
A
k1000=Veri(A) // a modifier 
n1000=Veri(delta) // a modifier 

K=10^16;
N=150;
delta= MatDiag(K,N)
H=TriDiag(N)
delta
H
[U,S,V]=svd(H)
A=U*delta*V'
A
k16=Veri(A) // a modifier 
n16=Veri(delta) // a modifier 

// partie B 

function [b]=ScdM(A,N)  
    Q=ones(1,N)
    b=A*Q'
endfunction

function [x]=Solv(A,b)
   x=linsolve(A,-b)
endfunction

function [E]=Erreur(N,x) // évaluation de l'erreur
    E=norm(ones(N,1)-(x))
endfunction
    
function [Jaco]=Jacobi(k,A,N) //on a N la dimension et A l'ordre
    
    D=diag(diag(A)) // définition des variables
    L=tril(A)-D
    U=triu(A)-D
    b=A*ones(N,1)
     
     if k==0 then Jaco=zeros(N,1)
        else Jaco=-inv(D)*(L+U)*Jacobi(k-1,A,N)+inv(D)*b
    end
endfunction

function [i]=kJac(A,N,k)
     b=A*ones(N,1)
     i=0
     kJ=norm(A*Jacobi(i,A,N)-b,2)/norm(b,2)// on prend comme seuil une valeur de 10^8
     while kJ>10^-8 & k>i
         i=i+1 
         kJ=norm(A*Jacobi(i,A,N)-b,2)/norm(b,2)
     end
endfunction

function kJacobi=kJ(i,A,N)
     b=A*ones(N,1)
     kJacobi=norm(A*Jacobi(i,A,N)-b,2)/norm(b,2)    
endfunction

function [ErJ]=ErreurJ(i,A,N)
    ErJ=norm((ones(N,1))-(Jacobi(i,A,N)))
endfunction

function DErr=DErreur(ErJ,Er)
    DErr=ErJ/Er
endfunction

function [ibis]=kJacb(A,N,k)
     b=A*ones(N,1)
     ibis=0
     kJ=norm(A*Jacobi(ibis,A,N)-b,2)/norm(b,2)
     while kJ>10^-8 & k>ibis
         ibis=ibis+1 
         kJ=norm(A*Jacobi(ibis,A,N)-b,2)/norm(b,2)
     end
endfunction

b=ScdM(A,N)
x=Solv(A,b)
E=Erreur(N,x)

// partie C 

function [Lambda]=Spect(A)
    D=diag(diag(A))
    L=tril(A)-D
    U=triu(A)-D
    Rj=-inv(D)*(L+U)
    Lamb=abs(spec(Rj))
endfunction

function [LambM]=RSpec(Lambda)
    Lambda=gsort(Lambda)
    LambM=Lambda(1)    
endfunction

function [l]=mLamb(LambM)
    l=round(abs(8/log(LambM)))
endfunction

A
Lambda
LambM






