Performs Gaussian elimination with partial pivoting on A to transform the system Ax=b to Ux=c, where U is upper triangular. Input arguments: A, square matrix b, vector Output arguments: U, upper triangular matrix c, vector
0001 function [ U,c ] = Gaussian_partial( A,b ) 0002 % Performs Gaussian elimination with partial pivoting on A 0003 % to transform the system Ax=b to Ux=c, where U is upper triangular. 0004 % Input arguments: 0005 % A, square matrix 0006 % b, vector 0007 % Output arguments: 0008 % U, upper triangular matrix 0009 % c, vector 0010 0011 U=A; c=b; % initialize U,c 0012 [m,n]=size(A); % finding the size of A 0013 if n~= m 0014 error('input is not a square matrix'); 0015 elseif size(b,1) ~= n 0016 error('input dimensions do not match'); 0017 end 0018 0019 for i=1:n; 0020 [~,index]=max(abs(U(i:n,i))); % choose the element with the 0021 % greatest absolute value from those below the 0022 % diagonal in the ith column 0023 index=index+i-1; 0024 0025 largest=max(abs(U(index,:))); % check comparative size of pivot 0026 if abs(U(index,i)/largest)<1e-12; % element to others in its row 0027 error(['pivot element is comparatively small, '... 0028 'alternative pivoting strategy required']); 0029 else 0030 P=eye(n); % create permutation matrix to interchange rows 0031 P(:,[index,i])=P(:,[i,index]); 0032 0033 U=P*U; c=P*c; % apply permutation to both sides 0034 end 0035 0036 for j=i+1:n; 0037 temp=(U(j,i)/U(i,i)); % eliminate elements below the diagonal 0038 U(j,:)=U(j,:)-U(i,:)*temp; % by subtracting a linear combination 0039 % of rows 0040 0041 c(j)=c(j)-c(i)*temp; % perform the same row operation on the 0042 % right hand side 0043 end 0044 end 0045 0046 end