No BSD License  

Highlights from
Numerical Methods for Physics

from Numerical Methods for Physics by Alejandro Garcia
Companion Software

naivege(a,b)
function x = naivege(a,b)
% x=naivege(a,b) performs naive (no pivoting) Gaussian elimination
% Solves the linear system a*x=b where x and b are column vectors
[n m] = size(a);
nb = length(b);
if( n ~= m | nb ~= m ),
  error('Mismatched dimensions in naivege');
end
% Forward elimination
for k=1:n-1 	  % Go column by column (k) and only operate
  for i=k+1:n 	% on the rows (i) below column k
    coeff = a(i,k)/a(k,k);
	   for j=k+1:n
	     a(i,j) = a(i,j) - coeff*a(k,j);
    end
    a(i,k) = coeff;
    b(i) = b(i) - coeff*b(k);
  end
end
% Backsubstitution
x(n) = b(n)/a(n,n);	% Start from the bottom
for i=n-1:-1:1 		   % and work upward (Note: This for loop
  sum = b(i);		     % goes from n-1 to 1 in steps of -1)
  for j=i+1:n 		    % Skip lower triangular part of a(i,j)
    sum = sum - a(i,j)*x(j);
  end
  x(i) = sum/a(i,i);
end
x = x';  % Convert x into column vector
return;

Contact us at files@mathworks.com