Clear Filters
Clear Filters

Gaussian elimination by partial pivoting and reducing matrix

5 views (last 30 days)
I have used a code from online and modified it to perform gaussian elimination of certain rows. The rows are defined by a cell array. The cell array is YB size 119x119 and within each cell is [3x3] matrix. The row that should remain is given by yb = [1 2 119]. YB is ordered and sparse meaning all the diagonal elements should be greater than the off-diagonal elements and off-diagonal elements have large number of zeros. The code is running without debugging errors but the output is not what I was hoping to get. I was hoping to get in the end YB size [3x3] instead the code is not operating on row 119 and YB is still size 119x119. How can I improve this to reorder row 119 to third row and reduce YB after elimination process?
Code is below:
function yreduced=gauss_eliminationv3(Y_busnew_idxfull,YB, yb,Nb,ybidx,Nbidx)
tic
indexyb=Y_busnew_idxfull(1,2:end);
indexybtemp= int16(indexyb);
ybustemp=Y_busnew_idxfull(2:end,2:end);
%% Input Check sequence
D=1:1:length(YB);
LYb=length(yb);
LNb=length(Nb);
[n m]=size(YB); % n= # of rows in A, m= # of columns in A
if(sum(D)~=sum(yb)+sum(Nb)||n ~= m )
display('Input bus matrices error, Please check');
return
end
%% Reorganising the input matrix
LYbtemp=sort(yb);
LNbtemp=sort(Nb);
%************************");
% disp("Augmented Matrix M=[A b]")
MM=YB;
% MM=sym(MM);
%%%%%%%%%%%%%%%% Gauss elimination method %%%%%%%%%%%%%%
disp('Gauss elimination method:');
[n,m]=size(YB)
y1=size(Nb);
y2=sort(Nb,'descend')
LYb=length(yb);
LNb=length(Nb);
%%Reorder the YB 3-phase cell array by moving cells that are observable yb
%%to the top then the cells to remove after the 3-phase nodes to keep
M=[];
for i=1:LYb
M=[M;YB(yb(i),:)];
end
for i=1:LNb
M=[M;YB(Nb(i),:)];
end
%%Step one is to rearrange the ybus by moving rows with zero diagonal
%%elements to the bottom.
for j=1:m
for z=j+1:m-1
diag_YB = any(cell2mat(M(j,j)));
if any(diag_YB(:)~=1)
disp("replace row"+j + " with row"+z)
tmp=M(j,:); M(j,:)=M(z,:);
M(z,:)=tmp;
end
end
for k=1:y1
for j=1:n
if y2(k)==(j)
z=j-1;
t=k+1;
disp("Eliminate ybus("+z+","+j+")")
for x=1:m
M{z,x}=M{z,x}-(M{j,x}.*M{z,j})./M{j,j};
end
end
end
end
end
toc
yreduced=cell2mat(M);

Answers (1)

Nipun
Nipun on 22 Dec 2023
Hi Leonie,
I understand that you are attempting Gaussian elimination on a cell array YB representing a sparse matrix with 3x3 matrices in each cell. The goal is to reorder the rows based on the yb and Nb vectors and then perform Gaussian elimination.
Based on the provided code, here are a few issues in your code that might be causing unexpected behavior. I have a few recommendations to improve your code:
  1. It seems that you are using a cell array to represent a sparse matrix. This might be unnecessary complexity. If your matrix is mostly zero, consider using a sparse matrix representation directly. More information on "sparse" matrices in MATLAB can be found here: Create sparse matrix - MATLAB sparse - MathWorks India
  2. he code includes indexing operations like YB(yb(i),:), but the values in yb are not sorted. This can lead to incorrect row operations during Gaussian elimination. Ensure that the rows are correctly ordered. I have attached the mended code to handle this behaviour.
  3. The check for diagonal elements (any(cell2mat(M(j,j)))) may not be effective (as it checks for exact zero values). Instead, you might want to check if the diagonal element is close to zero (consider using a tolerance) before swapping rows.
Here is the modified code with some improvements:
function yreduced = gauss_eliminationv3(Y_busnew_idxfull, YB, yb, Nb)
% ... (your existing code)
% Sort yb and Nb
yb = sort(yb);
Nb = sort(Nb);
% Reorder the YB matrix
M = YB([yb, Nb], :);
% Perform Gaussian elimination
for j = 1:m
% Check if diagonal element is close to zero (consider using a tolerance)
if abs(M{j, j}) < tol
% Find the first row with a non-zero diagonal element below
z = find(abs(cell2mat(M(j+1:end, j))) > tol, 1) + j;
% Swap rows
tmp = M{j, :};
M{j, :} = M{z, :};
M{z, :} = tmp;
end
% Perform Gaussian elimination
for k = 1:length(yb)
% ... (your existing code)
end
end
% Update yb and Nb indices if needed
% Extract the reduced matrix
yreduced = cell2mat(M);
end
Hope this helps.
Regards,
Nipun

Categories

Find more on Performance and Memory 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!