What is wrong with this Gauss-Pivoting algorithm?

Goodevening everyone,
My name's Antonio and I'm from Italy. I am currently doing some projects in MATLAB, and there's this easy one giving me some problems. The algorithm is about resolving a linear system using Gauss and partial pivoting.
% initializing the permutation vector
for i = 1:n
piv(i) = i;
end
% using Gauss
for k = 1:(n-1)
[maximum, max_position] = max(abs(A(piv(k:n), k)));
if maximum <= eps*norm(A)
error('Pivot is too small.')
end
if A(piv(max_position), k) ~= 0
if piv(max_position) ~= k
piv([k piv(max_position)]) = piv([piv(max_position) k]);
end
for i = (k+1):n
A(piv(i), k) = A(piv(i), k)/A(piv(k), k);
for j = (k+1):n
A(piv(i), j) = A(piv(i), j)-[A(piv(i), k)*A(piv(k), j)];
end
b(piv(i)) = b(piv(i)) - A(piv(i), k)*b(piv(k));
end
else
error('singular')
end
end
if A(piv(n), n) == 0
error('singular')
end
x(n) = b(piv(n))/A(piv(n), n);
for i = (n-1):-1:1
sum = 0;
for k = (i+1):n
sum = sum + A(piv(i), k)*x(k);
end
x(i) = (b(piv(i)) - sum)/A(piv(i), i);
end
end
This is the code. But when i try it with this script, the solution of the systems, which should be [1, ..., 1], is wrong.
clear num = input('Inserire la massima dimensione delle matrici di prova: ');
for k = 1:100
A = rand(k);
x = ones(k, 1);
b = A*x;
x = GaussPivoting(A, b)
end
any idea of what could be the problem?
Thank you in advance

Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Asked:

on 29 May 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!