Docsity

Gaussian Elimination with Pivoting - Computing Techniques - Notes | ME 2016, Study notes of Computer Science

Toggle sidebar
bg1
Page 1
GUASSIAN ELIMINATION WITH PIVOTING
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.
MATLAB CODE
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-1
% 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], : );
pf2

Partial preview of the text

Download Gaussian Elimination with Pivoting - Computing Techniques - Notes | ME 2016 and more Study notes Computer Science in PDF only on Docsity!

Page 1

GUASSIAN ELIMINATION WITH PIVOTING

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.

MATLAB CODE

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