%program for building zbus
clc;
clear all;
z=zdata_pai()%call zdata_pai.m
[elements columns]=size(z);
nbus=max(max(z(:,2)),max(z(:,3)))
%to begin with [zbus] is a null matrix
zbus=[]
%current bus no. indicates the maximum no. of buses added till now
currentbusno=0
%Process each row of z
for count=1:elements
if (count==1)
rows=0;
cols=0;
else
[rows cols]=size(zbus)
end
from=z(count,2);
to=z(count,3);
value=z(count,4);
%newbus variable indicates the maximum no. of the two buses-frombus and
tobus
%newbus bus may or may not be a part of existing zbus
newbus=max(from,to);
%ref variable indicates the minimum no. of the two buses-frombus and tobus
%and it is not necessarily the reference bus
%ref bus must always exist in the existing bus
ref=min(from,to)
%Modification of type1
%A new element is added from new bus to reference bus
if newbus>currentbusno & ref ==0
zbus=[zbus zeros(rows,1)
zeros(1,cols) value];
currentbusno=newbus;
continue
end
%Modification of type2
%A new bus is added from new bus to old bus other than reference bus
if newbus>currentbusno & ref~=0
zbus=[zbus zbus(:,ref)
zbus(ref,:) value+zbus(ref,ref)];
currentbusno=newbus;
continue
end
%Modification of type3
%A new element is added between an old bus and reference bus
if newbus <= currentbusno & ref==0
zbus=zbus-1/(zbus(newbus,newbus)+value)*zbus(:,newbus)*zbus(newbus,:);
continue
end
%Modification of type4
%A new element is added between two old buses
if newbus <= currentbusno & ref~=0
zbus=zbus-1/(value+zbus(from,from)+zbus(to,to)2*zbus(from,to))*((zbus(:,from)-zbus(:,to))*((zbus(from,:)-zbus(to,:))));
continue
end
end
disp('Bus impedance matrix [Zbus]:')
disp(zbus)
%Calculation of symmetrical fault bus voltages and line currents
zf=input('Enter the fault impedance:');
vf=complex(1,0);
disp('Fault voltage:');
disp(vf);
ter=1;
for i=1:nbus
fc(i)=(vf/(zbus(i,i)+zf));
fprintf('Fault current at bus %d:%d',i);
disp(fc(i));
end
while ter==1
bn=input('Enter fault bus number:');
bv(bn)=zf*fc(bn);
fprintf('Bus voltages for fault at bus %d:\n',bn);
for i=1:nbus
if i~=bn
bv(i)=vf-(zbus(i,bn)*fc(bn));
end
end
disp([bv(1) bv(2) bv(3)]);
from=z(:,2);
to=z(:,3);
nlength=length(from);
for i=1:nlength
if to(i)==0
linech(from(i))=((vf-bv(from(i)))/(z(i,4)));
end
if to(i)~=0
linec(from(i),to(i))=((bv(from(i))-bv(to(i)))/(z(i,4)));
end
end
for i=1:nlength
if to(i)==0
fprintf('line current %d - %d:',from(i),to(i));
disp(linech(from(i)));
end
end
for i=1:nlength
if to(i)~=0
fprintf('line current %d - %d:',from(i),to(i));
disp(linec(from(i),to(i)));
end
end
fprintf('\n If you want to find fault at other buses: yes=1,no=0:');
ter=input('');
end
function z=zdata();
% elementno frombus tobus Zvalue
z=[ 1
0.25j;
0.08j;
0.13j;
0.2j;
0.03j];%(5 x 4 matrix)
%care should be taken to begin with an element is added
%to reference bus and both from and to nodes should not be new
nodes
z=
1.0000 + 0.0000i 1.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.2500i
2.0000 + 0.0000i 2.0000 + 0.0000i 1.0000 + 0.0000i 0.0000 + 0.0800i
3.0000 + 0.0000i 3.0000 + 0.0000i 1.0000 + 0.0000i 0.0000 + 0.1300i
4.0000 + 0.0000i 3.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.2000i
5.0000 + 0.0000i 2.0000 + 0.0000i 3.0000 + 0.0000i 0.0000 + 0.0300i
zbus =
[]
currentbusno =
ref =
rows =
cols =
ref =
rows =
cols =
ref =
rows =
cols =
ref =
rows =
cols =
ref =
Bus impedance matrix [Zbus]:
0.0000 + 0.1274i 0.0000 + 0.1061i 0.0000 + 0.0981i
0.0000 + 0.1061i 0.0000 + 0.1345i 0.0000 + 0.1151i
0.0000 + 0.0981i 0.0000 + 0.1151i 0.0000 + 0.1215i
Enter the fault impedance:0.1j
Fault voltage:
1.0000 + 0.0000i
Fault current at bus 1: 0.0000 - 4.3985i
Fault current at bus 2: 0.0000 - 4.2647i
Fault current at bus 3: 0.0000 - 4.5146i
Enter fault bus number:1
Bus voltages for fault at bus 1:
0.4398
0.5334
0.5684
line current 1 - 0: 0.0000 - 2.2406i
line current 3 - 0: 0.0000 - 2.1579i
line current 2 - 1: 0.0000 - 1.1689i
line current 3 - 1: 0.0000 - 0.9890i
line current 2 - 3: 0.0000 + 1.1689i
If you want to find fault at other buses: yes=1,no=0:1
Enter fault bus number:2
Bus voltages for fault at bus 2:
0.5475
0.4265
0.5090
line current 1 - 0: 0.0000 - 1.8098i
line current 3 - 0: 0.0000 - 2.4549i
line current 2 - 1: 0.0000 + 1.5134i
line current 3 - 1: 0.0000 + 0.2964i
line current 2 - 3: 0.0000 + 2.7513i
If you want to find fault at other buses: yes=1,no=0:1
Enter fault bus number:3
Bus voltages for fault at bus 3:
0.5570
0.4803
0.4515
line current 1 - 0: 0.0000 - 1.7719i
line current 3 - 0: 0.0000 - 2.7427i
line current 2 - 1: 0.0000 + 0.9598i
line current 3 - 1: 0.0000 + 0.8121i
line current 2 - 3: 0.0000 - 0.9598i
If you want to find fault at other buses: yes=1,no=0:0
>>