Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Eserc 6
- % Risoluzione Ax=b A matrice nxn, A=RRT con Cholesky
- % Problemi generati a caso
- clc, clear;
- fprintf('\n Risoluzione Ax=b A matrice nxn A=RRT con Cholesky\n');
- % generazione del problema
- n=input('Dai la dimensione del problema: ');
- C=rand(n);
- A=C'*C;
- AA=A;
- x_esatto=ones(n,1);
- b=A*x_esatto;
- % Fattorizzazione RR
- for k=1:n
- A(k,k)=sqrt(A(k,k)-A(k,1:k-1)*A(k,1:k-1)');
- for i=k+1:n
- A(i,k)=(A(i,k)-A(i,1:k-1)*A(k,1:k-1)')/A(k,k);
- end
- end
- R=tril(A);
- fprintf('\nerrore fattorizzazione: %e\n', norm(AA-R*R'));
- % Risoluzione triangolare inferiore
- y(1)=b(1)/A(1,1);
- for i=2:n
- y(i)=b(i);
- for j=1:i-1
- y(i)=y(i)-A(i,j)*y(j);
- end
- y(i)=y(i)/A(i,i);
- end
- % Risoluzione triangolare superiore
- x=zeros(n,1);
- x(n)=y(n)/A(n,n);
- for i=n-1:-1:1
- x(i)=y(i);
- for j=i+1:n
- x(i)=x(i)-A(j,i)*x(j);
- end
- x(i)=x(i)/A(i,i);
- end
- fprintf('\nerrore soluzione norm(x-x_esatto).%e\n', norm(x-x_esatto, inf));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement