%section 7.1 computer problem 3

%first we store the augmented matrix as A
n = 3;
A = zeros(n,n+1);

for i=1:n
    for j=1:n
        A(i,j) = i+j;
    end
    A(i,n+1) = i+1;
end

A

[m,n] = size(A);       %A is m by n

%now we do naive Gaussian elimination on A
%we display A after each column elimination

%forward elimination

disp('first we do forward elimination...')

for k=1:m-1
    for i=k+1:m
        c = A(i,k)/A(k,k);   %c = multiple of row k to subtract from row i
        for j=1:n
            A(i,j) = A(i,j)-c*A(k,j);
        end
    end
    A
end

%backward elimination

disp('now we do backward elimination...')

for k=m:-1:2          %loop goes from m to 2 (the -1 means it's backwards)
    for i=1:k-1
        c = A(i,k)/A(k,k);   %c = multiple of row k to subtract from row i
        for j=1:n
            A(i,j) = A(i,j)-c*A(k,j);
        end
    end
    A
end

%put into reduced row echelon form

disp('now we scale rows to get rref...')

for i=1:m
    c = A(i,i);
    for j=1:n
        A(i,j) = A(i,j)/c;
    end
end

disp('the reduced matrix is...')
A 

%note that the answer has entries NAN; what happened?
