0% found this document useful (0 votes)
301 views

Gauss Elimination With Partial Pivoting

This document contains a function that performs Gaussian elimination with partial pivoting to solve a system of linear equations. It takes in the coefficient matrix A and right hand side vector b. It performs the following steps: 1) Finds the size of A and augments A with b. 2) Loops through rows, selecting the pivot element in each row and interchanging rows if needed. 3) Eliminates elements below the pivot in each row. 4) Checks for a unique solution or lack thereof. 5) Performs back substitution to solve for the unknown variables.

Uploaded by

Ryan Kaneshiro
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)
301 views

Gauss Elimination With Partial Pivoting

This document contains a function that performs Gaussian elimination with partial pivoting to solve a system of linear equations. It takes in the coefficient matrix A and right hand side vector b. It performs the following steps: 1) Finds the size of A and augments A with b. 2) Loops through rows, selecting the pivot element in each row and interchanging rows if needed. 3) Eliminates elements below the pivot in each row. 4) Checks for a unique solution or lack thereof. 5) Performs back substitution to solve for the unknown variables.

Uploaded by

Ryan Kaneshiro
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/ 2

%Gauss elimination with partial pivoting

%Sample test : x = GaussPartialPiv([1 2 -1;5 2 2;-3 5 -1],[2;9;1])


%Author : Reo Rickie H Bating - BK11110257 - HK20 - Numerical Method
%Problem 9.18
function x = GaussPartialPiv(A,b)
%A = matrix coeficient
%b = right-hand side matrix vector
n = size(A,1);
A = [A,b];

%getting n
%produces the augmented matrix

%Start elimination
for i = 1:n-1
p = i;
%select 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
%pivot null check
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

%nonzero last entry check


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

end

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);

Output:
x =

1.0000

1.0000

1.0000

You might also like