% The command gauss(M) is used to perform a sequence % of EROs on the matrix M that has already been input % into the MATLAB workspace % MAIN FUNCTION GAUSS function U=gauss(M) stop=1; last=0; % used to control choice M [p,n]=size(M); U=M; while stop ~= 0 % begin while if last ~= -1 disp(' ') disp('[0] stop') disp('[1] Replacement row(i)- m*row(j) ==> row(i)') disp('[2] Interchange row(i) <==> row(j)') disp('[3] Scale c*row(i) ==> row(i)') disp('[4] turn on rational format') disp('[5] turn off rational format') disp('[-1] undo last operation') else disp(' ') disp('[0] stop') disp('[1] Replacement row(i)- m*row(j) ==> row(i)') disp('[2] Interchange row(i) <==> row(j)') disp('[3] Scaling c*row(i) ==> row(i)') disp('[4] turn on rational format') disp('[5] turn off rational format') last=0; end disp(' ') choice = input('choice '); if last~=-1 switch choice case 0 stop=0; case 1 last=1; done=0; while done==0 done=1; m=input('Enter nonzero multiplier m '); if m==0 disp(' ') disp('ERROR: m=0 ') disp(' ') done=0; end end done=0; while done==0 done=1; j=input('Enter row = j to scale '); i=input('Enter row = i to replace '); if i<1 | i>p | j<1 | j>p disp(' ') disp('ERROR: i or j out of range ') disp(' ') done=0; end if i==j disp(' ') disp('ERROR: i=j ') disp(' ') done=0; end end U=replacement(U,i,j,m) case 2 last=2; done=0; while done==0 done=1; i=input('Enter row index i '); j=input('Enter row index j '); if i<1 | i>p | j<1 | j>p disp(' ') disp('ERROR: i or j out of range ') disp(' ') done=0; end if i==j disp(' ') disp('ERROR: i=j ') disp(' ') done=0; end end U=interchange(U,i,j) case 3 last=3; done=0; while done==0 done=1; c=input('Enter nonzero scalar c '); i=input('Enter row index i '); if i<1 | i>p disp(' ') disp('ERROR: i or j out of range ') disp(' ') done=0; end if c==0 disp(' ') disp('ERROR: c=0 ') disp(' ') done=0; end end U=scaling(U,i,c) case 4 format rat; U case 5 format; U case -1 switch last case 1 disp(' ') disp('Inverse ERO applied. Previous matrix is ') U=replacement(U,i,j,-m) case 2 disp(' ') disp('Undone. Previous matrix is ') U=interchange(U,i,j) case 3 disp(' ') disp('Undone. Previous matrix is ') U=scaling(U,i,1/c) end % switch case -1 last=-1; end %switch end % if last end % end while % SUBFUNCTIONS % REPLACEMENT function B=replacement(A,i,j,m) [p n]=size(A); if i<1 | i>p | j<1 | j>p error('row index out of range') end if i==j error('i=j, rows equal') end B=A; B(i,:)=A(i,:)-m*A(j,:); % INTERCHANGE function B=interchange(A,i,j) B=A; B(i,:)=A(j,:); B(j,:)=A(i,:); % SCALING function B=scaling(A,i,c) [p n]=size(A); if i<1 | i>p error('row index out of range') end B=A; B(i,:)=c*A(i,:); % Linear Algebra for Engineers and Scientists % by Kenneth Hardy % Prentice Hall, U.S.A. % ISBN 0--13--906728--0