Advertisement
nigu

es6a

Nov 14th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.98 KB | None | 0 0
  1. % Eserc 6
  2. % Risoluzione Ax=b A matrice nxn, A=RRT con Cholesky
  3. % Problemi generati a caso
  4. clc, clear;
  5. fprintf('\n Risoluzione Ax=b A matrice nxn A=RRT con Cholesky\n');
  6. % generazione del problema
  7. n=input('Dai la dimensione del problema: ');
  8. C=rand(n);
  9. A=C'*C;
  10. AA=A;
  11. x_esatto=ones(n,1);
  12. b=A*x_esatto;
  13. % Fattorizzazione RR
  14.  
  15. for k=1:n
  16.         A(k,k)=sqrt(A(k,k)-A(k,1:k-1)*A(k,1:k-1)');
  17.     for i=k+1:n
  18.        A(i,k)=(A(i,k)-A(i,1:k-1)*A(k,1:k-1)')/A(k,k);
  19.     end
  20. end
  21. R=tril(A);
  22. fprintf('\nerrore fattorizzazione: %e\n', norm(AA-R*R'));
  23.  
  24. % Risoluzione triangolare inferiore
  25.  
  26. y(1)=b(1)/A(1,1);
  27. for i=2:n
  28.     y(i)=b(i);
  29.     for j=1:i-1
  30.         y(i)=y(i)-A(i,j)*y(j);
  31.     end
  32.     y(i)=y(i)/A(i,i);
  33. end
  34.  
  35. % Risoluzione triangolare superiore
  36. x=zeros(n,1);
  37. x(n)=y(n)/A(n,n);
  38. for i=n-1:-1:1
  39.     x(i)=y(i);
  40.     for j=i+1:n
  41.         x(i)=x(i)-A(j,i)*x(j);
  42.     end
  43.     x(i)=x(i)/A(i,i);
  44. end
  45. fprintf('\nerrore soluzione norm(x-x_esatto).%e\n', norm(x-x_esatto, inf));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement