function ratmovie(A,tol) %RREFMOVIE Movie of the computation of the reduced row echelon form. % RREFMOVIE(A) produces the reduced row echelon form of A. % RREFMOVIE, by itself, supplies its own 8-by-6 matrix with rank 4. % RREFMOVIE(A,tol) uses the given tolerance in the rank tests. % Copyright (c) 1984-94 by The MathWorks, Inc. % Roger Jang, adapted from rrefmovie, mar-5-97 % Sample matrix if none was provided. if nargin < 1 A = [ 9 4 1 6 12 7 4 0 4 15 1 14 7 0 7 8 10 9 16 0 16 3 13 2 0 2 -4 0 0 0 0 6 -12 0 0 0 9 0 9 6 12 7 5 0 5 10 8 11]; A = fix(10*randn(5, 6)); end format rat more off [m,n] = size(A); % Compute the default tolerance if none was provided. if (nargin < 2), tol = max([m,n])*eps*norm(A,'inf'); end % Loop over the entire matrix. i = 1; k = 0; while i <= min(m, n), % Find value and index of largest element in the remainder of column i. [p, k] = max(abs(A(i:m,i))); k = k+i-1; % 'home' is more reliable than 'clc'. home if i==1, fprintf('Original matrix:\n'); end A [up, down]= rat(A(k,i)); fprintf('The max. of abs(A(%d:%d,%d)) is %g/%g at (%d, %d).\n', ... i, m, i, up, down, k, i); if (p > tol) if i ~= k fprintf('So we are gonna swap row %d and %d.\n', i, k); fprintf('\nPress any key to continue ...\n'); pause % Swap i-th and k-th rows. home fprintf('After swapping rows %d and %d:\n', i, k); A([i k],:) = A([k i],:) else fprintf('So we don''t need to do any swapping.\n'); end fprintf('Now we need to normalize A(%d,%d)\n', i, i); fprintf('\nPress any key to continue ...\n'); pause % Divide the pivot row by the pivot element. home fprintf('After normalizing A(%d,%d):\n', i, i); A(i,i:n) = A(i,i:n)/A(i,i) fprintf('Now we need to subtract multiples of the pivot row from all the other rows.\n'); fprintf('\nPress any key to continue ...\n'); pause for k = 1:m if k ~= i home disp(' ') A(k,i:n) = A(k,i:n) - A(k,i)*A(i,i:n) end end fprintf('We are done with column % i.\n', i); else fprintf('Since A(%d, %d) <= %g, column %d is negligible.\n', ... k, i, tol, i); % The column is negligible, zero it out. A(i:m,i) = zeros(m-i+1,1); end if i < min(m, n) fprintf('\nPress any key to process column %d...\n',i+1); pause else fprintf('\nDone with matrix A!\n'); end i = i + 1; end