

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Page 1
If the diagonal element (the pivot element) of a row is zero, the algorithm will divide by zero. To prevent this, switch this row with one below it.
Decide which row to use based on the element with the largest absolute value. This rule also reduces roundoff error in the computation.
function [L, U, piv] = GEpiv(A) % Gaussian elimination with pivoting. % input: A, a square matrix. % output: L and U, the LU factors of A. % piv, the pivoting vector of A.
[m, n] = size(A); % should check to insure that n = m piv = 1:n; % define pivoting vector
for k = 1:n- % pivot rows if necessary [maxr, r] = max( abs(A(k:n,k)) ); q = r + k - 1; piv([k q]) = piv([q k]); A([k q], : ) = A([q k], : );
Page 2
if A(k, k) ~= 0 % compute multipliers for row k and store under the % the diagonal in column k of A. A(k+1:n, k) = A(k+1:n, k)/A(k, k);
% do the elimination step. A(k+1:n, k+1:n) = A(k+1:n, k+1:n) - A(k+1:n, k)*A(k, k+1:n);
else fprintf(‘*** The matrix A is singular. ***\n\n’); L = []; U = []; piv = []; % return empty matrices. return end end
if A(n, n) == 0 fprintf(‘*** The matrix A is singular. ***\n\n’); L = []; U = []; piv = []; % return empty matrices. return end
% Extract the upper and lower triangular parts of the modified % matrix A. Form L with ones on the diagonal and the % triangular part of A below the main diagonal.
L = eye(n, n) + tril(A, -1); U = triu(A); % end of function