// Question 0
function [A] = initialiserMatriceH(n)

A=zeros(n,n);

for i=1:n-1
  A(n,n) = 2;
  A(i,i) = 2;
  A(i,i+1) = -1;
  A(i+1,i) = -1;

end

endfunction;


function xi = initialiseVecteurXi(n)

xi = zeros(n,1);
  
  for i=1:n
    xi(i,1)=1;   
  end

  
endfunction;



// Question 1
function [D] = initialiseMatD(n,k)
    [D]= zeros(n,n)
    D(1,1)=1
    for i=2:n
        D(i,i)=k**(-1/(n-i+1))
    end
endfunction;



//Question 3

function[A,b,xi, x1]=ortho(H,D)
[U,S,V]=svd(H);

//Question 4
[A]=U*D*V';
C=cond(A);

disp(C,"Conditionnement");
//Question 5
xi=initialiseVecteurXi(n);
b=A*xi;

[x1]=linsolve(A,-b);
endfunction;
//Question 6

function [i,Xk]=Jacobi(A,b,n,max_i)

[X0]=zeros(n,1)
xi== initialiseVecteurXi(n);

i=1;
c = 10^(-8);
nr1=0;
LeP = -inv(diag(diag(A)))*(A-diag(diag(A)));
Db = inv(diag(diag(A)))*b;

while (i <= max_i)

Xk = LeP * X0 + Db;
nr = norm(A*Xk - b)/norm(b);
    if (nr-nr1>0 & i~=1) then
        disp("Divergence !");
    disp(i,"Iterations");
        break;
    elseif (nr < c) then
    disp(i,"Convergence iterations");
     disp(nr,"Erreur residuelle");
     disp(norm(Xk-xi),"Erreur ");
     break;
     elseif (i==max_i) then
         disp("Itération max atteint, voulez-vous continuer ?");
         disp("1. pour oui");
         disp("nimporte quelle touche sinon");
         r=0;
         r=scanf('%d');
             disp(r);
         if(r==1) then
             max_i=max_i*100;
          else  break;
              end;
    end;
     X0 =Xk
     nr1=nr
     i=i+1;
end;
endfunction;


function m=m_min(A,n)
    xi=initialiseVecteurXi(n);
    [X0]=zeros(n,1)
LeP = -inv(diag(diag(A)))*(A-diag(diag(A)));
[lambda]= diag(bdiag(LeP));
rayonSpectral = max(abs(lambda));
 m=log(10^(-8)*norm(xi))/log(rayonSpectral);
 if(rayonSpectral>=1)  then
     disp("Attention divergence")
  else
    printf("Iter min %e",round(m)); ;
 end
endfunction;


function [i,Xk]=Jacobi_relax(A,b,n,max_i,w)

[X0]=zeros(n,1)
I=eye(n);

i=1;
c = 10^(-8);
nr1=0;
U=triu(A,-1);
L=tril(A,-1);
D=diag(diag(A));

LeP = ((1-w)*I)-((w*inv(D))*(A-D));
Db = w*inv(diag(diag(A)))*b;

//disp(D);
//disp(U);
//disp(L);
//Xk = ( inv(D-w*L) * ((1-w) * D + w*U) * X0 + inv(D-w*L)* w*b);
while (i <= max_i)

Xk=LeP*X0+Db;
nr = norm(A*Xk - b)/norm(b);
//    if (nr-nr1>0 & i~=1) then
//        disp("Divergence !");
//    disp(i,"Iterations");
//        break;
//printf("%e - %e\n",norm(Xk),norm(X0));
printf("%e\n",nr);
    if (nr < c) then
    disp(i,"Convergence iterations");
     disp(nr,"Erreur residuelle");
     disp(norm(Xk-xi),"Erreur k-ième itération");
     break;
     elseif (i==max_i) then
         printf("%d itérations atteintes, voulez-vous continuer ?",i);
         disp("1. pour oui");
         disp("nimporte quelle touche sinon");
         r=0;
         r=scanf('%d');
             disp(r);
         if(r==1) then
             max_i=max_i*100;
          else  break;
              end;
    end;
     X0 =Xk
     nr1=nr
     i=i+1;
end;
endfunction;

function [wopt] =omega_opti(A,n)

xi=initialiseVecteurXi(n);
[X0]=zeros(n,1)
D=diag(diag(A));
I=eye(n);

w=0.1;
i=1;

while (w<2)
LeP = ((1-w)*I)-((w*inv(D))*(A-D));
[lambda]= diag(bdiag(LeP));
rayonSpectral(i) = max(abs(lambda));
w=w+0.1;
i=i+1;
end
disp(atan(imag(min(0.99)) , real(min(0.99))))
wopt = atan(imag(min(rayonSpectral)) , real(min(rayonSpectral)));

disp(wopt,"w optimal");

endfunction;
