clear


// DONNEES 

// EXEMPLE DU LAPLACIEN 1D 
n = 10; 
dx = 1/(n+1)**2

u = 2*dx*ones(n,1);
v = - dx*ones(n-1,1)


A = diag(u)+diag(v,1)+diag(v,-1);


y = 1:1:n
y = y'/n


b=A*y



// METHODE DE RICHARDON A PAS OPTIMAL (CAS SDP)

x=zeros(n,1) // choix initial de x 
r = b-A*x; // residu initial 
nr1 = norm(r); // norme du residu initial 
nr = nr1;

eps = 1.0E-10; // precision sur le residu relatif 

lambda = spec(A)
alpha = 2/(lambda(1)+lambda(n));


k=0
while (nr/nr1>eps)&(k<10000) do 
    k = k + 1;
    res(k) = log(nr/nr1)/log(10);
    p = A*r;
    x = x + alpha*r;
    r = r - alpha*p;
    nr = norm(r);   
end

plot(res,'-xb')
disp('richardson a pas fixe')
disp('x',x)
disp('nr',nr)
disp('nr/nr1',nr/nr1)
disp('k',k)

// METHODE DE RICHARDSON A PAS VARIABLE (CAS SDP)

x=zeros(n,1) // choix initial de x 
r = b-A*x; // residu initial 
nr1 = norm(r); // norme du residu initial 
nr = nr1;

eps = 1.0E-10; // precision sur le residu relatif 

k = 0
while (nr/nr1>eps)&(k<10000) do 
    k = k + 1;
    res1(k) = log(nr/nr1)/log(10);
    p = A*r;
    alpha = (r'*r)/(r'*p);
    x = x + alpha*r;
    r = r - alpha*p;
    nr = norm(r);   
end

plot(res1,'-xr')
disp('richardson a pas variable')
disp('x',x)
disp('nr',nr)
disp('nr/nr1',nr/nr1)
disp('k',k)







