function x = GE3(A,b) % Input: a square matrix A and a vector b % Output: the approximate solution to Ax=b % Gaussian elimination, w/o pivoting % JG for Math 436, October 2008 [m, n] = size(A); [mb , nb] = size(b); % A few checks, make sure that the input meets our expectations if m ~= mb error('you screwed up, b must have as many rows as A') end if nb ~= 1, error('b must be a column vector'), end if m ~= n, error('A must be s square matrix'), end A = eliminationstep(A,b); x = backsub(A); % ----------------------------------------- function AA = eliminationstep(A,b) % this is the old GE1 as a subfunction % modified for partial pivoting warn = 0; % no warnings yet AA = [A , b]; [m, n] = size(A); for p = 1:m-1 for k=p+1:m mult = AA(k,p)/AA(p,p); AA(k,:) = AA(k,:) - mult*AA(p,:); end end % ----------------------------------------- function x = backsub(A) % Backsubstitution [m n] = size(A); x = zeros(m, 1); x(m) = A(m,n)/A(m,n-1); for j=(m-1):-1:1 S = A(j,n); for i=j+1:m S = S - A(j,i)*x(i); end x(j) = S/A(j,j); end