Home > K25104 > LinearSystems > Gaussian_elementary.m

Gaussian_elementary

PURPOSE ^

Performs elementary Gaussian elimination (no pivoting) on A

SYNOPSIS ^

function [U,c] = Gaussian_elementary(A,b)

DESCRIPTION ^

 Performs elementary Gaussian elimination (no pivoting) on A 
   to transform the system Ax=b to Ux=c, where U is upper triangular.
   A warning is given if a diagonal element is close to zero.
 Input arguments:
   A, square matrix
   b, vector
 Output arguments:
   U, upper triangular matrix
   c, vector

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [U,c] = Gaussian_elementary(A,b)
0002 % Performs elementary Gaussian elimination (no pivoting) on A
0003 %   to transform the system Ax=b to Ux=c, where U is upper triangular.
0004 %   A warning is given if a diagonal element is close to zero.
0005 % Input arguments:
0006 %   A, square matrix
0007 %   b, vector
0008 % Output arguments:
0009 %   U, upper triangular matrix
0010 %   c, vector
0011 
0012 U=A; c=b;   % initialize U,c
0013 [m,n]=size(A); % finding the size of A
0014 if n~= m
0015     error('input is not a square matrix');
0016 elseif size(b,1) ~= n
0017     error('input dimensions do not agree');
0018 end
0019 
0020 for i=1:n;
0021     if abs(U(i,i))< 1e-12;
0022         error('diagonal element is close to zero, pivoting strategy required');
0023     end
0024     for j=i+1:n;
0025         if abs(U(j,i)) < 1e-12;
0026             U(j,i) = 0;                 % if U(j,i) is close to zero, treat as zero
0027         else
0028             temp=(U(j,i)/U(i,i));       % eliminate elements below the diagonal by
0029             U(j,:)=U(j,:)-U(i,:)*temp;  % subtracting a linear combination of rows
0030                                      
0031             c(j)=c(j)-c(i)*temp;        % perform the same row operation on the
0032                                         % right hand side
0033         end
0034     end
0035 end 
0036 
0037 end
0038

Generated on Mon 18-Jan-2016 10:25:49 by m2html © 2005