avelardo
avelardo

Reputation: 175

Why is this code failing to process a diferent matrix?

This code is managing to get some matrix operations but when the initial values on the matrix is modified, the results are showed as NaN, I dont understand why because it is supposed to be able to manage (almost) any size of matrices.

%data matrix

   data=[0   1   0   1j
   0   2   0 0.8j
   1   2   0 0.4j
   1   3   0 0.2j
   2   3   0 0.2j
   3   4   0 0.08j];

 % from  to R   X
%Z=[0   1   0   1j
 %  0   2   0 0.8j
 %  1   2   0 0.4j
  % 1   3   0 0.2j
   %2   3   0 0.2j
   %3   4   0 0.08j];

%finding order of matrix in C language way
%o1=max(Z(:,1));
%o2=max(Z(:,2));
%order=(max(o1,o2))
%find number of rows of Z= Toatl number of nodes
%row=length(Z(:,1))

[row,col]=size(data);
order=col

%Change last column into admittance, now last column also inculdes R
for m=1:row
    data(m,4)=1/(data(m,3)+data(m,4));
end

Z2adm=data;

%Yadmittance as a matrixo of zeros first
Y=zeros(order,order);

%finding ybus matrix

%1-for off-digonal vlaues
for i=1:row
    for j=1:order
        %discard source node
        if data(i,1)==0||data(i,2)==0    
           a=0;
         %for off digonal entries
        elseif data(i,1)~=0||data(i,2)~=0
            Y(data(i,1),data(i,2))=-data(i,4);
            Y(data(i,2),data(i,1))=-data(i,4);
        end
        
    end
end
%2-digonal values 
for a=1:order     %for k
    for b=1:row
        if data(b,1)==a ||data(b,2)==a
           
            Y(a,a)=(Y(a,a)+data(b,4));
        end
    end
   
end
Ybus=Y

%To find Z bus
Zbus=inv(Y)

%As Ibus=Ybus*Vbus so we can find too if we know Ibus. As here two currnet
%sources so suppose
Ibus=[1;1;0;0];
Vbus=Ybus\Ibus

When the matrix original is used it works out without problem, but if you add or remove a line, this fails on data.

i.e. modyfing the data from the one showed to this data=[0 1 0 1j 0 2 0 0.8j 1 2 0 0.4j 1 3 0 0.2j 2 3 0 0.2j]; % 3 4 0 0.08j

gives the next
enter image description here

Upvotes: 1

Views: 80

Answers (1)

JAC
JAC

Reputation: 466

In the nested loops for i and j, you use the elements of the first two columns of data as indices. When you remove the last row from data, you remove the only row with an index = 4. As a result the fourth row and column of Y are unchanged, both remain at zero and Y is singular.

Upvotes: 1

Related Questions