function x=mnhf_naiveGauss(A,b) %MNHF_NAIVEGAUSS implements Gaussian elimination % without pivoting. % % A - n-by-n matrix % b - RHS column vector of length n. % % Usage mnhf_naiveGauss(A,b); [rA,cA] = size(A); % Compute the size of A. [rb,cb] = size(b); % Compute the size of b. if cb==1 if rA==cA if rA==rb % Step 1: form n-by-n+1 augmented matrix. A(:,rA+1) = b; for i=1:rA % Step 2: Make diagonal elements unity. A(i,i+1:rA+1) = A(i,i+1:rA+1)/A(i,i); % Step 3: Make elements below diagonal 0. for j=i+1:rA A(j,i+1:rA+1) = A(j,i+1:rA+1)-A(j,i)*A(i,i+1:rA+1); end end % Step 4: Back substitution. for j=rA-1:-1:1 A(j,rA+1) = A(j,rA+1)-A(j,j+1:rA)*A(j+1:rA,rA+1); end x = A(:,rA+1); else fprintf('Rows of A ~= rows of b.') end else fprintf('A is not a square matrix.') end else fprintf('b is not a column vector.') end