function x = gauss_no_pivot(A, b) % gauss_no_pivot computes the solution to AX=b % Assumes that gaussian elimination can be done without pivots % and that a solution exits (matrix is not singular) AA = [A b]; %% create augmented matrix [n,m] = size(A); %% n = #rows, m = # columns x = zeros(n,1); %% initialize a vector for solution x if n ~= m; error('A is not a square matrix'); elseif n ~= size(b); error('b must be the same size as A'); else %% Carry out row reduction to upper triangular form for k = 1:n-1 %% for each row (except last) if AA(k,k) == 0, error('Null diagonal element'); end for i = k+1:n %% for row i below row k m = AA(i,k)/AA(k,k); %% m = scalar for row i for j = k:n+1 %% subtract m*row k from row i -> row i AA(i,j) = AA(i,j) - m*AA(k,j); end end end %% At this point, AA should be in upper triangular form x(n) = AA(n,n+1)/AA(n,n); %% Start of back-substitution for i = n-1:-1:1 %% for each row going from row n-1 to row 1 s=0; %% Variable to hold a partial sum for j = i+1:n s = s + AA(i,j)*x(j); %% add up all entries except (i,i) end x(i) = (AA(i,n+1)-s)/AA(i,i); %% solve for x(i) end end