function x = GEppiv(A,b,ptol) % GEPivShow Show steps in Gauss elimination with partial pivoting and % back substitution % % Funciton call: x = GEppiv(A,b) % x = GEppiv(A,b,ptol) % % Input: A,b = coefficient matrix and right hand side vector % ptol = (optional) tolerance for detection of zero pivot % Default: ptol = 50*eps % % Output: x = solution vector, if solution exists if nargin<3, ptol = 50*eps; end [m,n] = size(A); if m~=n, error('A matrix needs to be square'); end nb = n+1; Ab = [A b]; % Augmented system % --- Elimination for i = 1:n-1 % loop over pivot row [pivot,p] = max(abs(Ab(i:n,i))); % value and index of largest available pivot ip = p + i - 1; % p is index in subvector i:n if ip~=i % ip is true row index of desired pivot fprintf('\nSwap rows %d and %d; new pivot = %g\n',i,ip,Ab(ip,i)); Ab([i ip],:) = Ab([ip i],:); % perform the swap else fprintf('\nNo swapping done. Pivot %d = %d.\n', i, Ab(i,i)) end pivot = Ab(i,i); if abs(pivot)