please solve above matrix using partial pivoting on Matlab and display pp use th
ID: 3847871 • Letter: P
Question
please solve above matrix using partial pivoting on Matlab and display pp
Explanation / Answer
% matlab code
function x = ppGaussian(matrixA,columnVector)
n = size(matrixA,1);
%determine augmented matrix
matrixA = [matrixA,columnVector];
idx = 1;
pp = zeros(n,2);
for i = 1:n-1
p = i;
%comparison to select the pivot
for j = i+1:n
if abs(matrixA(j,i)) > abs(matrixA(i,i))
U = matrixA(i,:);
matrixA(i,:) = matrixA(j,:);
matrixA(j,:) = U;
pp(idx,1) = i;
pp(idx,2) = j;
idx = idx + 1;
end
end
while matrixA(p,i)== 0 & p <= n
p = p+1;
end
if p == n+1
disp('No unique solution');
break
else
if p ~= i
T = matrixA(i,:);
matrixA(i,:) = matrixA(p,:);
matrixA(p,:) = T;
end
end
for j = i+1:n
m = matrixA(j,i)/matrixA(i,i);
for k = i+1:n+1
matrixA(j,k) = matrixA(j,k) - m*matrixA(i,k);
end
end
end
if matrixA(n,n) == 0
disp('No unique solution');
return
end
x(n) = matrixA(n,n+1)/matrixA(n,n);
for i = n - 1:-1:1
sumax = 0;
for j = i+1:n
sumax = sumax + matrixA(i,j)*x(j);
end
x(i) = (matrixA(i,n+1) - sumax)/matrixA(i,i);
end
disp('pp: ');
disp(pp);
end
ppGaussian([2 -1 0 0; 0 0 -1 1; 0 -1 2 -1; -1 2 -1 0],[1;0;0;0])
%{
output:
pp:
2 3
2 4
3 4
ans =
1.00000 1.00000 1.00000 1.00000
%}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.