Performs Gaussian elimination with scaled partial pivoting on A to transform the system Ax=b to Ux=c, where U is upper triangular. Input arguments: A, square matrix b, column vector Output arguments: U, square, upper triangular matrix c, column vector
0001 function [ U,c ] = Gaussian_scaledpartial( A,b ) 0002 % Performs Gaussian elimination with scaled 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, column vector 0007 % Output arguments: 0008 % U, square, upper triangular matrix 0009 % c, column vector 0010 0011 U=A; c=b; % initialize U,c 0012 % check user inputs 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 match'); 0018 end 0019 0020 for i=1:n; % considering the rows in turn 0021 temp1 = U(i:n,i); 0022 for k=i:n; 0023 temp2 = max(abs(U(k,:))); % greatest absolute value in each row 0024 if temp2 > 1e-12; % otherwise entire row close to zero 0025 temp1(k-i+1) = temp1(k-i+1)/temp2; 0026 end 0027 end 0028 0029 % element with the largest scaled value is chosen as pivot. 0030 [~,index]=max(abs(temp1)); 0031 % adjust the index to make it relative to the whole matrix 0032 index=index+i-1; 0033 0034 P=eye(n); % create permutation matrix to interchange rows 0035 P(:,[index,i])=P(:,[i,index]); 0036 U=P*U; c=P*c; % apply permutation to both sides 0037 0038 for j=i+1:n; 0039 temp=(U(j,i)/U(i,i)); % eliminate elements below the diagonal 0040 U(j,:)=U(j,:)-U(i,:)*temp; % by subtracting a linear combination 0041 % of rows 0042 0043 c(j)=c(j)-c(i)*temp; % perform the same row operation on the 0044 % right hand side 0045 end 0046 end 0047 0048 end 0049