I'm trying to make a program that solvas a linear system Ax =b with a n x n symmetric matriz and a n x 1 vector by gaussian elimination with partial pivotization. The program must search for the pivot element in each column. If the pivot isn't placed in the main diagonal, the program change this line for the line that has the pivot. And it solves the system by gaussian elimination.
I tried this algorithm but it works mainly for 2x2 and 3x3 matrices (it must work for n x n). My program is changing some whole lines in the matrix to zero. So, when it calculates the solution x a division by zero occurs. The higher is n, the higher the chance of getting this error. For some 10 x 10 matrices the program worked too. The algorithm:
Procedure gauss (n: integer;
a: matriz;
b: vetor);
Var
x: vetor;
k,w,e,l :integer;
c,aux,m,soma :real;
Begin
w:=1; {w is a variable for fixed column}
for k:=1 to n do {k is the number of iterations}
begin
e:=k; {e is the adress of the pivot}
c:=a[k,w];
if k<n
then
begin
for i:=1 to n do {searching for the pivot}
begin
if k+1 <= n
then
begin
if abs(a[i+1,w]) > c
then
begin
c:=a[k+i,w];
e:=k+i;
end;
end;
end;
for j:=1 to n do {changing lines}
begin
aux:=a[k,j];
a[k,j]:=a[e,j];
a[e,j]:=aux;
end;
if c <> 0
then
begin
for i:=1 to n do {gaussian elimination}
begin
if i > k then
begin
m:=a[i,w]/a[k,w];
for j:=1 to n do
begin
a[i,j]:=a[i,j]-m*a[k,j];
end;
b[i]:=b[i]-m*b[k];
end;
end;
end;
end;
w:=w+1;
end;
if a[n,n] <> 0 {calculating x}
then
begin
x[n]:=b[n]/a[n,n];
for l:=n-1 downto 1 do
begin
soma:=0;
for j:=n downto l+1 do
begin
soma:=soma+x[j]*a[l,j];
end;
x[l]:=(b[l]-soma)/a[l,l];
end;
end
else
begin
writeln('Error');
end;
end;
If someone could help me with this...