RISOLUZIONE DI UN SISTEMA
LINEARE TRAMITE FATTORIZZAZIONE LR
Contenuto
Script per la risoluzione di un sistema lineare denso
clear all
clc
Inizializzazione matrice quadrata piena
a = [5. , 3. , 1. ; 2. , -10. , 1. ; 1. , 1. , 7.];
b = [38. , -36. , 64.];
Chiamata alla funzione che fattorizza e risolve il sistema lineare
[xSol iEnd] = DvdSolveA(a,b);
Visualizzazione dei risultati
if iEnd
n = length(b);
for i = 1: n
disp(['x(',num2str(i),') = ',num2str(xSol(i))]);
end
end
x(1) = 3
x(2) = 5
x(3) = 8
Fattorizzazione LR tramite
trasformazione di Gauss di una matrice piena
function [xSol iSuccesso] = DvdSolveA(A,b)
iSuccesso = false;
nRowsCols = size(A);
m = length(nRowsCols);
if m ~= 2
disp('La matrice non ha dimensione 2. Impossibile continuare !!!')
return
end
if nRowsCols(1) ~= nRowsCols(2)
disp('La matrice non è quadrata. Impossibile continuare !!!')
return
end
if nRowsCols(1) ~= length(b)
disp('Dimensioni non consistenti tra matrice coefficienti e vettore termini noti. Impossibile continuare !!!')
return
end
xSol = b;
a = A;
n = nRowsCols(1);
for k = 1: n-1
for i = k+1: n
if a(k,k) == 0
disp('Errore !!! Impossibile continuare. Pivot NULLO !!!');
return
end
if a(k,k) == 0
disp(['Pivot nullo. Elemento a(',num2str(k),',',num2str(k),') == 0. Impossibile continuare!!!'])
return
end
a(i,k) = a(i,k) / a(k,k);
for j = k+1: n
a(i,j) = a(i,j) - a(i,k)*a(k,j);
end
end
end
R = zeros(n, n);
for i = 1: n
for j = i: n
R(i,j) = a(i,j);
end
end
L = eye(n);
for i = 1: n
for j = 1: i-1
L(i,j) = a(i,j);
end
end
[xSol iSuccesso] = DvdSolveL(L,xSol);
if iSuccesso
[xSol iSuccesso] = DvdSolveR(R,xSol);
end
Risoluzione forward di
un sistema triangolare sinistro
function [xSol iSuccesso] = DvdSolveL(L,b)
iSuccesso = false;
nRowsCols = size(L);
m = length(nRowsCols);
if m ~= 2
disp('La matrice non ha dimensione 2. Impossibile continuare !!!')
return
end
if nRowsCols(1) ~= nRowsCols(2)
disp('La matrice non è quadrata. Impossibile continuare !!!')
return
end
if nRowsCols(1) ~= length(b)
disp('Dimensioni non consistenti tra matrice coefficienti e vettore termini noti. Impossibile continuare !!!')
return
end
xSol = b;
n = nRowsCols(1);
for i = 1: n
for k = 1: i-1
xSol(i) = xSol(i) - L(i,k) * xSol(k);
end
if L(i,i) == 0
disp(['La matrice è singolare. Elemento L(',num2str(i),',',num2str(i),') == 0. Impossibile continuare!!!'])
return
end
xSol(i) = xSol(i) / L(i,i);
end
iSuccesso = true;
Risoluzione backward di
un sistema triangolare destro
function [xSol iSuccesso] = DvdSolveR(R,b)
iSuccesso = false;
nRowsCols = size(R);
m = length(nRowsCols);
if m ~= 2
disp('La matrice non ha dimensione 2. Impossibile continuare !!!')
return
end
if nRowsCols(1) ~= nRowsCols(2)
disp('La matrice non è quadrata. Impossibile continuare !!!')
return
end
if nRowsCols(1) ~= length(b)
disp('Dimensioni non consistenti tra matrice coefficienti e vettore termini noti. Impossibile continuare !!!')
return
end
xSol = b;
n = nRowsCols(1);
for i = n: -1: 1
for k = i+1: n
xSol(i) = xSol(i) - R(i,k) * xSol(k);
end
if R(i,i) == 0
disp(['La matrice è singolare. Elemento R(',num2str(i),',',num2str(i),') == 0. Impossibile continuare!!!'])
return
end
xSol(i) = xSol(i) / R(i,i);
end
iSuccesso = true;