Gaussian Elimination algorithm is not properly partial pivoting
显示 更早的评论
Hello, I am working through some practice exercises for MatLab practice and one of the problems ask me to include partial pivoting to a previous problem that asks to write an algorithm for Gaussian Elimination (which I have done). Here's what I have so far:
r = RandStream('mt19937ar','Seed',1234);
A = r.randn(6,7);
n = 6;
out = [];
for ii = 1:n-1
[m,t] = max(abs(A));
if A(ii,ii) < m(1,ii)
temp = A(ii,:);
A(ii,:) = A(t(1,ii),:);
A(t(1,ii),:) = temp;
else
for j = ii+1:n
if A(ii,ii) == 0
print('Method failed: matrix is rank deficient');
else
A(j,:) = A(j,:) - (A(j,ii)/A(ii,ii))*A(ii,:);
end
end
end
out = vertcat(t4_out,A);
end
The input is a matrix A that is n x m (m >= n) and the output should be an n x m upper triangular matrix. At the end of each iteration, I have a matrix A(i) with zeros below the diagonal on the ith column and I would need to construct the matrix [A(1), A(2), ..., A(n-1)] (hence the use of vertcat) and the output should be a 30x7 matrix. I tried my best to implement partial pivoting, but my output doesn't end up being an upper triangular matrix. I don't really get what's wrong with my partial pivoting code. Without the partial pivoting, my regular Gaussian Elimination algorithm still works and I get an upper triangular matrix. Can someone help me figure out why my partial pivoting code doesn't get me to an upper triangular matrix? Any help will be appreciated.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!