Guassian elimination

by

 

Guassian elimination method

GuassinaElimination.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% Guassian Elimination and Partial Pivoting%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%solve the answer for matrix:
%ԪȥL, U P det(A)ͽXֵ   

function GuassinaElimination()
%------------ I --------------------------
%a=[10,-7,0,1;-3,2.099999,6,2;5,-1,5,-1;2,1,0,2];    b=[8;5.900001;5;1];
%a=[0,2,0,1;2,2,3,2;4,-3,0,1;6,1,-6,-5];    b=[0;-2;-7;6];
%a=[1,2,3,4;5,6,7,8;9,10,11,10;16,1,-6,-5];    b=[0;-2;-7;6];
%NumRow=4;

%a=[0,2,0,1,3;2,2,3,2,1;4,-3,0,1,4;6,1,-6,-5,6;1,2,3,4,5];    b=[0;-2;-7;6;8];
%NumRow=5;
%------------ II --------------------------
%a=[3.01,6.03,1.99;1.27,4.16,-1.23;0.987,-4.81,9.34];   b=[1;1;1];
a=[3.00,6.03,1.99;1.27,4.16,-1.23;0.990,-4.81,9.34];   b=[1;1;1];
%a=[1,2,3;2,5,7;0,0,1];  b=[1;1;1];
NumRow=3;

%------------Test with matlab function--------------------------
Problem=[a,b]
[L,U,P]=lu(a);% find L,U,P of a matrix
Determinant=det(a) % determinant of a matrix
X=a\b

%-----------Partial Pivoting------------------------------------
LPivoting=zeros(NumRow,NumRow); 
UPivoting=zeros(NumRow,NumRow); %LU decomposition 
result=zeros(1,NumRow); %1row NumRow columns
detPivoting=1;

for i=1:NumRow % loop for row 
    max=a(i,i); %initialize value for max
    rowTemp=i;
    for nextI=(i+1):NumRow %next row from i
        if max<=a(nextI,i)
            max=a(nextI,i); % the bigest number is found
            rowTemp=nextI; %note the biggest number of the row
        end
    end
    if max==0
        disp('No unique solution exists')
        break;
    end
    %to change the row of i with the row that max in
    if rowTemp~=i
        for j=1:NumRow % to control column
            %change row for matrix a
            a(i,j)=a(i,j)+a(rowTemp,j);
            a(rowTemp,j)=a(i,j)-a(rowTemp,j);
            a(i,j)=a(i,j)-a(rowTemp,j);
        end
        %change row for matrix b
        b(i)=b(i)+b(rowTemp);
        b(rowTemp)=b(i)-b(rowTemp);
        b(i)=b(i)-b(rowTemp);
        detPivoting=detPivoting*(-1); 
    end
        
    %;to make row i+1 to 0
    for j=(i+1):NumRow %change of row
        m=a(j,i)/a(i,i); %leave 10 small digits
        LPivoting(j,i)=m; %to make the next row to 0 coefficient
       for k=1:NumRow %change of every column 
            a(j,k)=a(j,k)-(m*a(i,k)); % to make it to 0
        end
        b(j)=b(j)-(m*b(i)); %result matrix also needs to change
    end
     LPivoting(i,i)=1; %value 1 for diagonal element of L
    detPivoting=detPivoting*a(i,i);
    if a(NumRow,NumRow)==0 %determinant of a is 0, then finish
        disp('No unique solution exists')
        break;
    end
end %end for i

%start backward substitution to find X
sum=0;
for i=(NumRow):-1:1
    for j=i+1:NumRow
        sum=sum+(result(j)*a(i,j));
    end
    result(i)=(b(i)-sum)/a(i,i);
    sum=0;
end
LPivoting  
UPivoting=a %print the last after changing
X=result' %output result X
detPivoting



        
        

Contact us