0% found this document useful (0 votes)
117 views3 pages

Gaussian Elimination Matlab

This document describes a function that performs Gaussian elimination with partial pivoting to solve a system of linear equations. It takes the coefficient matrix A and right-hand side vector b as inputs. It performs partial pivoting row exchanges to select optimal pivots during elimination. It returns the solution vector x. The function commentary explains that it follows the Gaussian elimination algorithm outlined in a numerical analysis textbook.

Uploaded by

Muhammad Sohil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views3 pages

Gaussian Elimination Matlab

This document describes a function that performs Gaussian elimination with partial pivoting to solve a system of linear equations. It takes the coefficient matrix A and right-hand side vector b as inputs. It performs partial pivoting row exchanges to select optimal pivots during elimination. It returns the solution vector x. The function commentary explains that it follows the Gaussian elimination algorithm outlined in a numerical analysis textbook.

Uploaded by

Muhammad Sohil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

function x = GaussPP(A,b)

%GaussPP(A,b) solves the n-by-n linear system of equations using partial


%pivoting
%A is the coeficient matrix
%b the right-hand column vector
%This file was generated after following a course in numerical analysis
%at the University of Pretoria
%The present partial pivoting function follows the algorithmic guidelines
%given in the book of Burden & Faires, Numerical Analysis, 7th ed.
%Author: Alain G. Kapitho
%Date : Nov 2005
n = size(A,1);
A = [A,b];

%getting n
%produces the augmented matrix

%elimination process starts


for i = 1:n-1
p = i;
%comparison to select the pivot
for j = i+1:n
if abs(A(j,i)) > abs(A(i,i))
U = A(i,:);
A(i,:) = A(j,:);
A(j,:) = U;
end
end
%cheking for nullity of the pivots
while A(p,i)== 0 & p <= n
p = p+1;
end
if p == n+1
disp('No unique solution');
break
else
if p ~= i
T = A(i,:);
A(i,:) = A(p,:);
A(p,:) = T;
end
end

end

for j = i+1:n
m = A(j,i)/A(i,i);
for k = i+1:n+1
A(j,k) = A(j,k) - m*A(i,k);
end
end

%checking for nonzero of last entry


if A(n,n) == 0

disp('No unique solution');


return
end
%backward substitution
x(n) = A(n,n+1)/A(n,n);
for i = n - 1:-1:1
sumax = 0;
for j = i+1:n
sumax = sumax + A(i,j)*x(j);
end
x(i) = (A(i,n+1) - sumax)/A(i,i);
end

_________________________________________________________
________________________________________

function [x_soln,nrow,A_aug]= timo_gausselimination_fun(A,B);


%Note that this program does not directly change the rows of the augmented
%matix. Instead it uses the nrow vector to keep track of the row changes.
%create the augmented matrix A|B
Aug=[A B];
n=rank(A);
%initialize the nrow vector
for i=1:n
nrow(i)=i;
end
nrow=nrow';
for k=1:n-1;
max=0;
index=0;
%find the maximum value in the column under the current checked element and
%return its row position
for j=k:n
if abs(Aug(nrow(j),k))>max
max=abs(Aug(nrow(j),k));
index=j;
end
end
%perform row exchange in the nrow vector

if nrow(k)~=nrow(index)
ncopy=nrow(k);
nrow(k)=nrow(index);
nrow(index)=ncopy;
disp(sprintf ('row changed '))
else
disp(sprintf ('no change '))
end
%Gaussian elimination
for i=(k+1):n
m(nrow(i),k)=Aug(nrow(i),k)/Aug(nrow(k),k);
for j=k:n+1
Aug(nrow(i),j)=Aug(nrow(i),j)-m(nrow(i),k)*Aug(nrow(k),j);
end
end
end
%backward subsitution
x(n)=0;
x=x';
x(n)=Aug(nrow(n),n+1)/Aug(nrow(n),n);
i=n-1;
while i>0
x(i)=(Aug(nrow(i),n+1)-Aug(nrow(i),i+1:n)*x(i+1:n))/(Aug(nrow(i),i));
i=i-1;
end
x_soln=x;
A_aug=Aug;

____________________________________________________________
____________________________________________

You might also like