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
% convertiamo la matrice in una triangolare superiore
for i=1:1:n-1
if U(i,i) == 0
ok=0;
break
end
for j=i+1:1:n
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;
end
end
b=U(:,n+1);
U=U(:,1:n);
if ok == 0 || U(n,n)== 0
disp('impossibile convertire')
b=[];
U=[];
else
b=U(:,n+1);
U=U(:,1:n);
end
end
end

View File

@@ -6,11 +6,13 @@
%U=[10,20,30;1,5,3;4,56,34]; matrice di input
%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
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
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
break
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=x(1);%prendo solo il primo indice
y=y(1);%prendo solo il primo indice
x= x + i -1;
y = i;
if x~= i
U([i x],:) = U([x i],:);
pivot([i x]) = pivot([x i]);
end
@@ -31,7 +38,7 @@ function [U,b] = convert_matrix_to_triangular_matrix_gauss_pivoting (U,b)
for j=i+1:1:n
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;
end
@@ -42,6 +49,7 @@ function [U,b] = convert_matrix_to_triangular_matrix_gauss_pivoting (U,b)
disp('errore')
b=[];
U=[];
pivot = [];
else
b=U(:,n+1);
U=U(:,1:n);

View File

@@ -2,10 +2,10 @@
% in matrice triangolare superiore con gauss pivoting parziale
% 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);
[x1] = linear_system_resolver_triangular_matrix(x,y);
x1
U=[2,4,-2;1,-1,5;4,1,-2] % matrice di input
b=[6;-3;-10] %termini noti
[x,y,z] = convert_matrix_to_triangular_matrix_gauss_pivoting(U,b)
[x1] = linear_system_resolver_triangular_matrix(x,y)