From 85ff9aa1d05beda0894896e2231369c21800692b Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Tue, 7 Oct 2014 13:35:34 +0200 Subject: [PATCH] Creato algoritmi di risoluzione sistemi lineari mediante metodo di gauss con pivoting parziale --- complete_liner_equation_system_resolver.m | 61 +++++++++++++++++++ convert_matrix_to_triangular_matrix.m | 44 +++++++++++++ ... liner_system_resolver_triangular_matrix.m | 3 + test.m | 61 +++++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 complete_liner_equation_system_resolver.m create mode 100644 convert_matrix_to_triangular_matrix.m rename liner_system_resolver.m => liner_system_resolver_triangular_matrix.m (70%) create mode 100644 test.m diff --git a/complete_liner_equation_system_resolver.m b/complete_liner_equation_system_resolver.m new file mode 100644 index 0000000..7bcaa5e --- /dev/null +++ b/complete_liner_equation_system_resolver.m @@ -0,0 +1,61 @@ +% completa risoluzione di un sistema lineare usando la riduzione +% in matrice triangolare superiore con gauss pivoting parziale +% e algoritmo di bottom up con sostituzione + +U=[0,20,30;0,5,3;0,56,34]; % matrice di input +b=[1;2;17]; %termini noti + + + + +n= length(b); +U= [U,b]; +U + + +for i=1:1:n-1 + x_max = max ( abs(U(i:n,i)) ); + if x_max == 0 + disp('errore matrice di input, det 0') + break + else + [x,y]= ind2sub(size(U), find (abs(U(i:n,i)) == x_max) ); + x= x + i -1; + y = i; + + if x~= i + U([i x],:) = U([x i],:); + + + end + + for j=i+1:1:n + + U(j,:) = U(j,:) + ( U(i,:) * (- U(j,i) / U(i,i) ) ) ; + + end + end +end + +if x_max ~= 0 + + b=U(:,n+1); + U=U(1:n,1:n); + + + x=[]; + n= length(b); + x(n) = b(n)/U(n,n); + + for i=n-1:-1:1 + somma = 0; + for k=i+1:n + somma=somma+U(i,k) * x(k); + end + + x(i) = (b(i) - somma)/ U(i,i); + end + disp ('La soluzione è ') + x +end + diff --git a/convert_matrix_to_triangular_matrix.m b/convert_matrix_to_triangular_matrix.m new file mode 100644 index 0000000..465b064 --- /dev/null +++ b/convert_matrix_to_triangular_matrix.m @@ -0,0 +1,44 @@ + +% questo algoritmo converte una matrice quadrata generica in +% triangolare superiore mediante metodo di gauss con pivot parziale + +U=[10,20,30;1,5,3;4,56,34]; % matrice di input +b=[1;2;17]; %termini noti +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 %stampiamo la matrice +det(U(:,1:n)) % stampiamo il determinante della matrice + + +% convertiamo la matrice in una triangolare superiore +for i=1:1:n-1 + x_max = max ( abs(U(i:n,i)) ); + if x_max == 0 + disp('errore') + else + [x,y]= ind2sub(size(U), find (abs(U(i:n,i)) == x_max) ); + x= x + i -1; + y = i; + + if x~= i + U([i x],:) = U([x i],:); + + + end + + for j=i+1:1:n + + U(j,:) = U(j,:) + ( U(i,:) * (- U(j,i) / U(i,i) ) ) ; + + end + end +end +U % stampiamo la nuova matrice convertita + +det ( U(:,[1 : n]) ) % stampiamo il nuovo determinante + % non considerando i termini noti + % e verichiamo che è uguale al vecchio + % determinante diff --git a/liner_system_resolver.m b/liner_system_resolver_triangular_matrix.m similarity index 70% rename from liner_system_resolver.m rename to liner_system_resolver_triangular_matrix.m index 569340d..3e866a5 100644 --- a/liner_system_resolver.m +++ b/liner_system_resolver_triangular_matrix.m @@ -1,3 +1,6 @@ +%risoluzione matrice triangolare superiore con algoritmo +% di sostituzione botton up (all'indietro) + U=[2,2,4;0,-7,-11;0,0,2]; b=[5;-8;-2]; diff --git a/test.m b/test.m new file mode 100644 index 0000000..71a800c --- /dev/null +++ b/test.m @@ -0,0 +1,61 @@ +&% completa risoluzione di un sistema lineare usando la riduzione +% in matrice triangolare superiore con gauss pivoting parziale +% e algoritmo di bottom up con sostituzione + +U=[0,20,30;0,5,3;0,56,34]; % matrice di input +b=[1;2;17]; %termini noti + + + + +n= length(b); +U= [U,b]; +U + + +for i=1:1:n-1 + x_max = max ( abs(U(i:n,i)) ); + if x_max == 0 + disp('errore matrice di input, det 0') + break + else + [x,y]= ind2sub(size(U), find (abs(U(i:n,i)) == x_max) ); + x= x + i -1; + y = i; + + if x~= i + U([i x],:) = U([x i],:); + + + end + + for j=i+1:1:n + + U(j,:) = U(j,:) + ( U(i,:) * (- U(j,i) / U(i,i) ) ) ; + + end + end +end + +if x_max ~= 0 + + b=U(:,n+1); + U=U(1:n,1:n); + + + x=[]; + n= length(b); + x(n) = b(n)/U(n,n); + + for i=n-1:-1:1 + somma = 0; + for k=i+1:n + somma=somma+U(i,k) * x(k); + end + + x(i) = (b(i) - somma)/ U(i,i); + end + disp ('La soluzione è ') + x +end +