I implement a function to gaussian elimination with scaled column pivoting. this function checks if matrix is singular but if not singular it solves wrong . I can't see where function fails on calculation . Can you help me?

function[singular, x]=mygauss(A,b)
%GaussPP(A,b) solves the n-by-n linear system of equations using scaled column %pivoting %A is the coeficient matrix %b the right-hand column vector %OUTPUT: x-> solution vector as a column vector %'no solution exist' - if pivoting is not possible %Determine the size of the system %take_matrix = 'please enter an nxn matrix as a first input :'; %A=input(take_matrix)
%take_b = 'please enter a vector size of n :';
% b = input(take_b)
%A=[1 2 3; 4 5 6;7 8 9];
%b=[13 14 15];
A=[0 0; -2 -7];
b=[7;-5];
n =length(A(:,1));
%disp(n);
%STEP 1 NROW=1:n; x=zeros(n,1); disp(x) flag = 0; Scale = (max(abs(A')))' %gives a vector of max elements of each row sn=length(Scale(:)); %disp('sn'); %disp(sn);
if min(Scale) == 0 %there is a row of zeros in coefficient matrix flag=1; end %Build the augmented matrix %temprow=A(NROW(i:n),i); %for i=1:n+1 Aug=cat(n,A,b) %Aug=[A;b];
if flag==0; for i=1:n % Determine the max scaled quantity in the column and below the diagonal M=max(abs(Aug(NROW(i),i:n)) ./ Scale(NROW(i))); disp('M') disp(M) %if max is zero, then no solution %else, find first place where max is gained if M==0; flag=1; disp('NO SOLUTION EXIST'); else
I=find(abs(Aug(NROW(i),i:n)) ./(Scale(i,1)));
%İnterchange rows if neccessary
p=I(1)+i-1;
if p>i
NCOPY=NROW(i);
NROW(i)=NROW(p);
NROW(p)=NCOPY;
end;
end;
if flag == 0;
for j=i+1:n
m(NROW(j),i)=Aug(NROW(j),i) / Aug(NROW(i),i);
Aug(NROW(j), i:n+1) = Aug(NROW(j), i:n+1) -m(NROW(j),i)*Aug(NROW(i), i:n+1);
end;
p=int2str(NROW(i));
col=int2str(NROW(i));
gall=gallery('uniformdata', [n n],0);
fprintf( 'COLUMN %s PIVOT ROW %s\n',col,p)
disp(gall)
%Aug(NROW,:)
%Aug
end
end
end
if flag ==0;
if Aug(NROW(n),n)==0
flag=1;
end;
end;
%begin bacward substitution
if flag==0;
x=Aug(NROW(n),n+1) / Aug(NROW(n),n);
for i=n:-1:1;
x(i,1)=(Aug(NROW(i),n)-sum(x(i+1:n,1)' .* Aug(NROW(i),i+1:n)) )/Aug(NROW(i),i);
end
disp('x');
disp(x)
else
singular=1;
fprintf('NO UNIQUE SOLUTION EXIST.THE SYSTEM IS SINGULAR. Sıngularıty : %d',singular)
end; %mygauss(A,b) end

Answers (0)

Categories

Find more on Linear Algebra in Help Center and File Exchange

Asked:

on 22 May 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!