migliorati gli algoritmi gauss natif e pivot correggendo bug su moltiplicatori, verifica 0 sulla diagonale durante il navi e aggiungendo il vettore pivot al gauss pivot

This commit is contained in:
2014-10-15 15:08:35 +02:00
parent ed6268c665
commit 660d633b37
3 changed files with 30 additions and 10 deletions

View File

@@ -29,16 +29,28 @@ function [U,b] = convert_matrix_to_triangular_matrix_gauss_naif (U,b)
else else
% convertiamo la matrice in una triangolare superiore % convertiamo la matrice in una triangolare superiore
for i=1:1:n-1 for i=1:1:n-1
if U(i,i) == 0
ok=0;
break
end
for j=i+1:1:n for j=i+1:1:n
m= U(j,i) / U(i,i); m= U(j,i) / U(i,i);
U(j,:) = U(j,:) + ( U(i,:) * (- m ) ) ; U(j,i+1:n+1) = U(j,i+1:n+1) + ( U(i,i+1:n+1) * (- m ) ) ;
U(j,i)=m; U(j,i)=m;
end end
end end
b=U(:,n+1); if ok == 0 || U(n,n)== 0
U=U(:,1:n); disp('impossibile convertire')
b=[];
U=[];
else
b=U(:,n+1);
U=U(:,1:n);
end
end end
end end

View File

@@ -6,11 +6,13 @@
%U=[10,20,30;1,5,3;4,56,34]; matrice di input %U=[10,20,30;1,5,3;4,56,34]; matrice di input
%b=[1;2;17]; termini noti %b=[1;2;17]; termini noti
function [U,b] = convert_matrix_to_triangular_matrix_gauss_pivoting (U,b) function [U,b,pivot] = convert_matrix_to_triangular_matrix_gauss_pivoting (U,b)
n= length(b); % lunghezza del vettore dei termini noti o numero equazioni n= length(b); % lunghezza del vettore dei termini noti o numero equazioni
U= [U,b]; % uniamo la matrice con la colonna dei termini noti U= [U,b]; % uniamo la matrice con la colonna dei termini noti
pivot = 1:1:n;
pivot = pivot';
% convertiamo la matrice in una triangolare superiore % convertiamo la matrice in una triangolare superiore
for i=1:1:n-1 for i=1:1:n-1
@@ -18,12 +20,17 @@ function [U,b] = convert_matrix_to_triangular_matrix_gauss_pivoting (U,b)
if x_max == 0 if x_max == 0
break break
else else
%trovo gli indici di tutti i numeri uguali a max
[x,y]= ind2sub(size(U), find (abs(U(i:n,i)) == x_max) ); [x,y]= ind2sub(size(U), find (abs(U(i:n,i)) == x_max) );
x=x(1);%prendo solo il primo indice
y=y(1);%prendo solo il primo indice
x= x + i -1; x= x + i -1;
y = i; y = i;
if x~= i if x~= i
U([i x],:) = U([x i],:); U([i x],:) = U([x i],:);
pivot([i x]) = pivot([x i]);
end end
@@ -31,7 +38,7 @@ function [U,b] = convert_matrix_to_triangular_matrix_gauss_pivoting (U,b)
for j=i+1:1:n for j=i+1:1:n
m= U(j,i) / U(i,i); m= U(j,i) / U(i,i);
U(j,:) = U(j,:) + ( U(i,:) * (- m ) ) ; U(j,i+1:n+1) = U(j,i+1:n+1) + ( U(i,i+1:n+1) * (- m ) ) ;
U(j,i)=m; U(j,i)=m;
end end
@@ -42,6 +49,7 @@ function [U,b] = convert_matrix_to_triangular_matrix_gauss_pivoting (U,b)
disp('errore') disp('errore')
b=[]; b=[];
U=[]; U=[];
pivot = [];
else else
b=U(:,n+1); b=U(:,n+1);
U=U(:,1:n); U=U(:,1:n);

View File

@@ -2,10 +2,10 @@
% in matrice triangolare superiore con gauss pivoting parziale % in matrice triangolare superiore con gauss pivoting parziale
% e algoritmo di bottom up con sostituzione % e algoritmo di bottom up con sostituzione
U=[4,8,12,16;2,9,6,8;0,1,1,4;6,2,2,4]; % matrice di input
b=[1;-1;2;1]; %termini noti
[x,y] = convert_matrix_to_triangular_matrix_gauss_pivoting(U,b); U=[2,4,-2;1,-1,5;4,1,-2] % matrice di input
[x1] = linear_system_resolver_triangular_matrix(x,y); b=[6;-3;-10] %termini noti
x1
[x,y,z] = convert_matrix_to_triangular_matrix_gauss_pivoting(U,b)
[x1] = linear_system_resolver_triangular_matrix(x,y)