1. % Gaussian/Gauss-Jordan elimination - General progran
% just to be safe
clear
% Choosing the method
a = input('input ''g'' for Gauss or anything else for Gauss-Jordan', 's');
% Please input your augmented matrix
% Use square brackets at either side of your matrix
% Use commas to seperate elements on the same row
% Use semi-colons to seperate the different rows
% For example, x+2y=3, 4x+5y=6 becomes [1,2,3;4,5,6]
A = input('Input your matrix ');
% checking it's possible
for n = 1:size(A,1)
if A(n,n) == 0
fprintf('Matrix cannot be solved by Gaussian elimination unless you swap rows
n')
else
A
% Choosing each pivot
for b = 1:size(A,1)-1
%For each row
for r = size(A,1):-1:2
if b<r
T = A(r,b)/A(b,b);
else
break
end
% For each column
for c = 1:size(A,2)
A(r,c) = A(r,c) - T*A(b,c);
end
% Printing the row transformation
if (T~=0)
fprintf('R%f = R%f - %f*R%f n', r, r, T, b)
fprintf('n')
A
end
% Use all the ends
end
end
end
end
% Setting up a matrix for x values
x = zeros(1,size(A,1));
2. % Finding the x values using algebra
if a == 'g'
for r = size(A,1):-1:1
x(r) = A(r,size(A,2));
for b = (size(A,2)-1):-1:2
if r<b
x(r) = x(r) - x(b)*A(r,b);
end
end
x(r) = x(r)/A(r,r);
end
% Printing results
for b = 1:size(A,1)
fprintf('x%i=%f n', b, x(b))
end
return
end
% Gauss-Jordan elimination
c = size(A,2);
for r = size(A,1):-1:1
T = A(r,r);
A(r,r) = A(r,r)/T;
A(r,c) = A(r,c)/T;
% Printing row transformation
if (T~=1)
fprintf('R%f = R%f/%f n',r, r, T )
A
end
for b = 1:size(A,1)-1
if b<r
T = A(b,r)/A(r,r);
A(b,r) = A(b,r) - T*A(r,r);
A(b,c) = A(b,c) - T*A(r,c);
end
% Printing row transformation
if (T~=0)
if (r>b)
fprintf('R%f = R%f - %fR%f n', r, r, T, b)
A
end
end
end
end