Problem with reordering using Gaussian Elimination.
Show older comments
Hi, I am writing code to solve a system of equations using Gaussian Elimination, and partial pivoting. My code is below and is returning me with the correct values for each co-efficient, however the reordering code I have tried to use is not working. Thanks!
Ra = size(A); n = Ra(1); Id = eye(n);
P_tot = Id; A1 = A; b1 = b;
% Checking size of inputs is correst
if ~isequal(size(A),[n n])
error("This function can only be used with square (n x n) matrices of co-efficients.")
end
if ~isequal(size(b),[n 1])
error("This function can only be used with n x 1 vectors of constants.")
end
for m = 1:n-1
% Selecting row containing pivotal co-efficeint.
row_index = m:n;
[~,Ir] = max(abs(A1(row_index, row_index)));
row_num = Ir(1);
% Partial Pivoting
p = [1:n]; p(m) = m-1+row_num; p(m-1+row_num) = m;
P = Id(p,:);
A1 = P*A1; b1 = P*b1; P_tot = P*P_tot;
% eliminate
I_vec = zeros(1,n); I_vec(1,m) = 1;
L1 = Id - ([zeros(m,1); A1(m+1:n,m)]*I_vec)/A1(m,m);
A1 = L1*A1; b1 = L1*b1;
end
% Back Substitiution
v = zeros(n,1);
for m = 0:n-1
v(n-m,1) = (b1(n-m,1) - (A1(n-m,:)*v))/A1(n-m,n-m);
end
v = P_tot*v; % reorder
end
1 Comment
Alan Stevens
on 10 Mar 2024
What do you mean by "not working"? What do you expect the result to be (i.e. what should the new order be?)?
Accepted Answer
More Answers (0)
Categories
Find more on Dynamic System Models in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!