You are on page 1of 63

DELHI TECHNOLOGICAL UNIVERSITY

DEPARTMENT OF ELECTRICAL ENGINEERING

EE-304:POWER SYSTEM ANALYSIS

PRACTICAL FILE

SUBMITTED TO : SUBMITTED BY :
Mr. Kuldeep Singh Siddartha Aggarwal
Assistant professor 2K20/EE/266
Department of EE, DTU
INDEX

S NO. NAME OF THE EXPERIMENT DATE SIGNATURE

1 To compute the bus admittance matrix Ybus 16/01/23


for the small power system network given
below using MATLAB.
2 To carry out power-flow / load-flow of a 23/01/23
given power system network using
Gauss-Seidel method.
3 To compute the power-flow / load-flow 30/01/23
solution of the given power system network
using Newton-Raphson method.

4 To compute the power-flow solution of the 06/02/23


given power system network using the
fast-decoupled power-flow algorithm.

5 To compute the bus impedance matrix of a 27/02/23


given power system network in MATLAB
using Z bus building algorithm.

6 To carry out symmetrical fault analysis in a 03/04/23


given power system network using MATLAB

7 To compute the optimal dispatch in the 10/04/23


three-generator system given below
(a) without and (b) with transmission losses.

8 To determine the critical clearing time for the 17/04/23


single machine infinite bus system given
below.
Experiment-1
Siddartha Aggarwal (2K20/EE/266)

Aim: To compute the bus admittance matrix Ybus for the small power system network given
below using MATLAB.

The branch data / line data for the above network is given below:

From Bus # To Bus # R (p.u) X (p.u) B (p.u)

1 2 0.004 0.0533 0

2 3 0.02 0.25 0.22

3 4 0.02 0.25 0.22

2 4 0.01 0.15 0.11

4 5 0.006 0.08 0

Theory:
In an AC power system network, the relationship between the injected node currents and the
node voltages is given by
I = Ybus V
Where,
I= vector of injected node currents, Ybus = Bus admittance matrix,

V = vector of node voltages


Each component element of the interconnected network is referred to as a branch. The branch or
the component is connected between two nodes of the network or between a reference node and
a node. For the purpose of modeling, While modeling we represent a branch by its impedance or
admittance. The branch impedance is also referred to as primitive impedance and the admittance
as primitive admittance.
Steps in construction of Ybus matrix:
1. The Ybus matrix is symmetric.
2. Yii the self admittance (diagonal term), is equal to the sum of the primitive admittances
of all the components connected to the ith node.
3. Yij (off diagonal element) is equal to negative of the primitive admittance of all
components connected between nodes i and j (the equivalent primitive admittance is
used, if there is no component between the two then Yij is zero).

Procedure:
1. Using the method as explained, compute the Ybus for the given power system network
manually.
2. Write codes given below to build the Ybus from the line / branch data. Use the
following steps:
a. Type in the branch data (i.e. R, X and B as given) in a matrix ‘D’. ‘D’ should
contain five columns and as many rows as that in the given branch data / line
data of the given network. The five columns correspond to “From bus number”,
“To bus number”, R, X and B, respectively. Check if data of B corresponds to
Btotal or B1/2.
b. Type the code and save it in a MATLAB m file.
c. Debug and run the code to generate Ybus.
d. Check the program results with the manually calculated Ybus.
e. Manually computed matrix:

9.007 - 12.007j -9.007 + 12.007j 0 0 0


-9.007 + 12.007j 9.766 - 22.453j -0.317 + 3.974j -0.442 + 6.632j 0
0 -0.317 + 3.974j 0.634 - 7.728j -0.317 + 3.974j 0
0 -0.442 + 6.632j -0.317 + 3.974j 1.568 - 20.213j -0.934 + 12.430j
0 0 0 -0.934 + 12.430j 0.934 - 12.430j

MATLAB Code:
14 BUS NETWORK
Line Charging
Line Number From Bus To Bus Resistance Reactance Susceptance
(p.u) (p.u) (p.u)

1 1 2 0.01938 0.05917 0.0528


2 1 5 0.05403 0.22304 0.0492
3 2 3 0.04699 0.19797 0.0438
4 2 4 0.05811 0.17632 0.0347
5 2 5 0.05695 0.17388 0.034
6 3 4 0.06701 0.17103 0.0346
7 4 5 0.01335 0.04211 0.0128
8 4 7 0 0.20912 0
9 4 9 0 0.55618 0
10 5 6 0 0.25202 0
11 6 11 0.09498 0.1989 0

12 6 12 0.12291 0.25581 0
13 6 13 0.06615 0.13027 0
14 7 8 0 0.17615 0
15 7 9 0 0.11001 0
16 9 10 0.03181 0.0845 0
17 9 14 0.12711 0.27038 0
18 10 11 0.08205 0.19207 0
19 12 13 0.022092 0.19988 0
20 13 14 0.17093 0.34802 0

CODE

Same as above except D is changed to the give


Result:
The Ybus matrix for the provided power system network was computed using the manual method
and the MATLAB program, The resultant Ybus matrix using thetwo methods were equivalent.
EXPERIMENT-2
Siddartha Aggarwal (2K20/EE/266)

AIM: To carry out power-flow / load-flow of a given power system network using Gauss-Seidel
method.

CIRCUIT DIAGRAM:

THEORY:

In the above network, all the transmission lines are identical. For each line, the shunt element is a
capacitor with an admittance of ysh=j 0.01 while each series element is an inductor with an
impedance of zse=j 0.1. Consider Case I of the power flow problem: given V1,S2,S3,...,Sn, find
S1,V2,V3,...,Vn.
Note: If we know V1,V2,...,Vn we can solve for S1 explicitly using (10.3a). Since we already do
know V1, it only remains to find V2,V3,...,Vn. These n-1 unknowns may be found from the n-1
equations of (10.3b). Thus, the heart of the problem is the solution of n-1 implicit equations in
the unknown V2,V3,...,Vn, where V1 and S2,S3,...,Sn are known. Equivalently, taking complex
conjugates we can get

We now rearrange (10.6) in a form in which a solution by iteration may be attempted. It should
be noted that there are alternative ways of setting up the problem.

CODE:
OUTPUT:

RESULT: The program was successfully executed in MATLAB for the given power system
network using Gauss-Seidel method.
EXPERIMENT-3
Siddartha Aggarwal (2K20/EE/266)

AIM: To compute the power-flow / load-flow solution of the given power system network using
Newton-Raphson method.

THEORY:

For the above network, all the transmission lines are identical. In the equivalent-pi representation
of each line, the series impedance equals zse = 0.1 while each of the shunt admittances (equal on
both sides) equal ysh = j0.01. This gives the bus admittance matrix Ybus as

In the above 3-bus system, there are three buses – the first bus is the slack bus, the second bus is
a PV bus and the third bus is a PQ bus. Hence the unknown variables to be
computed are 𝝷2, 𝝷3 and V3 . Hence, the Newton-Raphson algorithm for power-flow of
the above three bus system is given by
where

and ∆f on the right-hand side is given by


OUTPUT:

RESULT: The program was successfully executed in MATLAB for the given power system
network using Newton-Raphson method.
EXPERIMENT- 4
Siddhartha Aggarwal (2K20/EE/266)

AIM: To compute the power-flow solution of the given power system network using the
fast-decoupled power-flow algorithm.

CIRCUIT DIAGRAM:

THEORY:

For the above network, all the transmission lines are identical. In the
equivalent-pi representation of each line, the series impedance equals zse = j0.1 while each of the
shunt admittances (equal on both sides) equal ysh = j0.01. This gives the bus admittance matrix
Ybus as

The advantage of the fast-decoupled power-flow algorithm is that the Jacobian matrices are
constants. Hence, their inverses are precalculated and stored before the iterative loop starts.
Since the usually large Jacobian matrices are not required to be inverted in every iteration, time
taken by each iteration is very less. The form of the fast-decoupled power-flow algorithm is

given by
where

In the above 3-bus system, there are three buses – the first bus is the slack
bus, the second bus is a PV bus and the third bus is a PQ bus. Hence the unknown variables to be
computed are and . Hence, the fast-decoupled power-flow algorithm for the above three bus
system is given by

And

CODE:
OUTPUT:

RESULT: The program was successfully executed in MATLAB for the given power system
network using fast-decoupled power-flow method
EXPERIMENT-5
Siddartha Aggarwal (2K20/EE/266)

AIM: To compute the bus impedance matrix of a given power system network in MATLAB
using Z bus building algorithm.

THEORY:

For the above network, take z1 = j1.0, z2 = j1.25, z3 = j0.1, z4 = j0.2, z5 = j0.1

The Ybus / Zbus matrix constitutes the models of the passive portions of the power network. The
impedance matrix is a full matrix and is most useful for short circuit studies. An algorithm for
formulating [Zbus] is described in terms of modifying an existing bus impedance matrix
designated as [Zbus]old. The modified matrix isdesignated as [Zbus]new. The network consists
of a reference bus and a number ofother buses.
When a new element having self impedance Zb is added, a new bus may be created (if the new
element is a tree branch) or a new bus may not be created (if the new element is a link). Each of
these two cases can be subdivided into two cases so that Zb may be added in the following ways
or modifications:
1. Adding Zb from a new bus to reference bus.
2. Adding Zb from a new bus to an existing bus.
3. Adding Zb from an existing bus to reference bus.
4. Adding Zb between two existing buses.

Type 1 Modification
In type 1 modification, an impedance Zb is added between a new busp and reference bus as
shown in Figure 1.
Figure 1. Type 1 modification of Zbus
Let the current through bus p be Ip, then the voltage across the bus p is given by, Vp = Ip Zb
The potential at other buses remains unaltered and the system equations can be written as,

Type 2 Modification
In type 2 modification, an impedance Zb is added between a new bus p and an existing bus k as
shown in Figure 2. The voltages across the bus k and p can be expressed as, Vk(new) = Vk + Ip
Zkk Vp
= Vk(new) + Ip Zp
= Vk + Ip(Zb + Zkk)
where, Vk is the voltage across bus k before the addition of impedance Zb Zkk is the sum of all
impedance connected to bus k.

The system of equations can be expressed as,

Type 3 Modification
In this modification, an impedance Zb is added between an existing bus k and a reference bus.
Then the following steps are to be followed:
1. Add Zb between a new bus p and the existing bus k and the modificationsare done as in type
2.
2. Connect bus p to the reference bus by letting Vp = 0.
3. To retain the symmetry of the Bus Impedance Matrix, network reduction technique can be
used to remove the excess row or column.

Type 4 Modification
In this type of modification, an impedance Zb is added between two existing buses j and k as
shown in Figure 3. From Figure 3, the relation between the voltages of bus k and j can be written
as,
Vk – Vj = IbZb (3)
Figure 3.Type 4 Modification of Zbus
The voltages across all the buses connected to the network changes due to the

addition of impedance Zb and they can be expressed as,

On solving the Equations (3) and (4), the system of equations can be rewritten as,

Where Zbb = Zjj + Zkk – 2 Zjk + Zb

CODE:
clear all Zbus=0; Quit =0; i=0;
while Quit ==0
Case = input(' which case is to be implemented \n 1.New bus to reference bus \n
2. Existing bus to new bus \n
3. existing bus to refernce bus \n
4. between two existing buses \n'); if Case ==1
if i==0
Zb = input('enter the value of impedance= '); Zbus=[Zb]
i=i+1; else if i>0
Zb = input('enter the value of impedance = '); ord = length(Zb1);
for d = 1:ord+1
for e = i:ord+1
if d<=ord && e<=ord Zbus1(d,e)=Zb1(d,e);
end
if d==ord+1 && e==ord+1 Zbus1(d,e)=Zb;
end
if d==ord+1 && d~=e || e==ord+1 && d~=e Zbus1(d,e)=0;
end
end
end
end
endend Zbus=[Zbus1]
if Case == 2
Z_new = input('Enter the Value of impedance for new bus ='); m=length(Zbus);
for a=1:m
for b=1:m
Z_temp(a,b) = Zbus(a,b);
end
end
for c=1:m
Z_temp(c,m+1) = Zbus(c,m); Z_temp(m+1,c) = Zbus(c,m); Z_temp(m+1,m+1) =
Zbus(m,m)+Z_new;
end Zbus=[Z_temp] i=i+1;

end

if Case == 3
Z_new = input('enter the value of impedance for new bus = ') m= length(Zbus);
for a=1:m
for b=1:m
Z_temp(a,b) = Zbus(a,b);
end
end
for c=1:m
Z_temp(c,m+1) = Zbus(c,m); Z_temp(m+1,c) = Zbus(c,m);
Z_temp(m+1,m+1) = Zbus(m,m)+Z_new;
end
fprintf('Zbus before Kron Reduction: \n') Zbus=[Z_temp]
m=length(Zbus); for i=1:m-1
for k= 1:m-1
Z(i,k)= Zbus(i,k)-Zbus(i,m)*Zbus(m,k)/Zbus(m,m);
end
end end fprintf('Zbus after kron \n') Zbus=[Z]
if Case == 4
Z1 = input('Enter the value of impedance ='); j= input('Enter th evalue of bus j = ');
k= input ('Enter the value of bus k ='); m= length (Zbus);
for a=1:m
for b=1:m
Z_temp(a,b)=Zbus(a,b); end
end
for c=1:m
Z_temp(c,m+1)= Zbus(c,j)-Zbus(c,k); Z_temp(m+1,c) = Z_temp(c,m+1);
end
Z_temp(m+1,m+1)= Z1+Zbus(j,j)+Zbus(k,k)-2*Zbus(j,k); fprintf('Zbus before Kron
reduction:\n')
Zbus = [ Z_temp] m=length(Zbus); for i=1:m-1
for k= 1:m-1
Z(i,k)= Zbus(i,k)-Zbus(i,m)*Zbus(m,k)/Zbus(m,m);
end
end
end Zbus=[Z]
fprintf('Zbus after kron Reduction: \n')
Quit = input('do u want to quit= '); Zb1 = [Zbus];
end
OUTPUT:
RESULT: The bus impedance matrix using building algorithm for the given system was formed
and the results were verified using MATLAB program.
EXPERIMENT-6
Siddartha Aggarwal (2K20/EE/266)

AIM: To carry out symmetrical fault analysis in a given power system network using MATLAB.

THEORY:

The short circuit problem essentially consists of determining the steady state solution of a linear
network with a balanced three-phase sinusoidal excitation. The linear network comprises of a
major subnetwork fault. The transmission network and the generator network consist of an
interconnection of a three-phase balanced component. If the lines are assumed to be completely
transposed the impedance matrix for a three-phase transmission line is of form:

This type of symmetry is called complete symmetry. For symmetrical fault two types of faults
are there, three-phase to ground fault and three-phase fault. In this type of network we have a
three-phase network with balanced three-phase components and balanced excitation. Both
negative and zero sequence networks are not needed for symmetrical fault calculation.

CODE:
SINGLE LINE - GROUND FAULT

function done=slgfault(nb,ng,nl,nt)
%formation of positive sequence Ybus Yp=Ypositive(nb,ng,nl,nt);
%formation of zero sequence Ybus Yz=Yzero(nb,ng,nl,nt);
%formation of positive and negative sequence Zbus Zp=inv(Yp);
Zn=Zp;
%formation of zero sequence Zbus check=diag(Yz);
k=find(check==0) if length(k)==0
Zz=inv(Yz); end
if length(k)~=0 Zz=pinv(Yz);
end
%preparation of pre-fault voltage vectors xxx=ones(nb);
vp0=xxx(:,1); yyy=zeros(nb); vn0=yyy(:,1); vz0=vn0;
%calculation of fault current fid=fopen('faultdata.txt','r'); b=textread('faultdata.txt'); fclose(fid);
bus=b(1);
imp=b(3); ifp=vp0(bus)/((Zp(bus,bus)+Zn(bus,bus)+Zz(bus,bus)+3*imp)*j); ifn=ifp;
ifz=ifp;
%post fault voltage calculations for i=1:nb
vpf(i)=vp0(i)-Zp(i,bus)*ifp*j; vnf(i)=vn0(i)-Zn(i,bus)*ifn*j; vzf(i)=vz0(i)-Zz(i,bus)*ifz*j;
end vpf_mag=abs(vpf); vpf_phase=angle(vpf); vnf_mag=abs(vnf); vnf_phase=angle(vnf);
vzf_mag=abs(vzf); vzf_phase=angle(vzf);

%calculation of post fault flows in generators, transformers and line


%reading of elements and classifying them as lines, transformers or
%generators
%the convention for the status of elements is as follows
% 1=lines
% 2=generators
% 3=transformers without phase shift
% 0=transformers with phase shift

fid=fopen('linedata.txt','r'); bb=textread('linedata.txt'); fclose(fid);


x=bb(:,1);
y=bb(:,2);
zp=bb(:,3);
zz=bb(:,4); ele_num=1:length(x); start_bus=x; end_bus=y;
for i=1:length(x) status(i)=1;
end
for i=1:nl poslineflowp(i)=(vpf_mag(x(i))(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j)-
vpf_mag(y(i))(cos(vpf_phase(y(i)))+sin(vpf_phase(y(i)))*j))/(zp(i)*j);
poslineflown(i)=(vnf_mag(x(i))(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j)-
vnf_mag(y(i))(cos(vnf_phase(y(i)))+sin(vnf_phase(y(i)))*j))/(zp(i)*j);
poslineflowz(i)=(vzf_mag(x(i))(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j)-
vzf_mag(y(i))(cos(vzf_phase(y(i)))+sin(vzf_phase(y(i)))*j))/(zz(i)*j);
neglineflowp(i)=(vpf_mag(y(i))(cos(vpf_phase(y(i)))+sin(vpf_phase(y(i)))*j)-
vpf_mag(x(i))(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j))/(zp(i)*j);
neglineflown(i)=(vnf_mag(y(i))(cos(vnf_phase(y(i)))+sin(vnf_phase(y(i)))*j)-
vnf_mag(x(i))(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j))/(zp(i)*j);
neglineflowz(i)=(vzf_mag(y(i))(cos(vzf_phase(y(i)))+sin(vzf_phase(y(i)))*j)-
vzf_mag(x(i))(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j))/(zz(i)*j); end
poslineflowp_mag=abs(poslineflowp); poslineflowp_phase=angle(poslineflowp);
poslineflown_mag=abs(poslineflown); poslineflown_phase=angle(poslineflown);
poslineflowz_mag=abs(poslineflowz); poslineflowz_phase=angle(poslineflowz);
neglineflowp_mag=abs(neglineflowp); neglineflowp_phase=angle(neglineflowp);
neglineflown_mag=abs(neglineflown); neglineflown_phase=angle(neglineflown);
neglineflowz_mag=abs(neglineflowz); neglineflowz_phase=angle(neglineflowz)

fid=fopen('transformerdata.txt','r'); bb=textread('transformerdata.txt'); fclose(fid);


x=bb(:,1);
y=bb(:,3);
z=bb(:,5);
zz=bb(:,6);
hv=bb(:,2);
lv=bb(:,4); hvbuses=x;
ele_num=1:length([ele_num y']); start_bus=[start_bus' x']; end_bus=[end_bus' y'];
for i=1:length(x)
if (hv(i)==1 & lv(i)==2) | (hv(i)==2 & lv(i)==1) | (hv(i)==0 & lv(i)==2) | (hv(i)==2 & lv(i)==0)
status(i+nl)=0;

else
status(i+nl)=3; end
end s=length(status); for i=1:nt
postransflowp(i)=(vpf_mag(x(i))(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j)-
vpf_mag(y(i))(cos(vpf_phase(y(i)))+sin(vpf_phase(y(i)))*j))/(z(i)*j);
postransflown(i)=(vnf_mag(x(i))(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j)-
vnf_mag(y(i))(cos(vnf_phase(y(i)))+sin(vnf_phase(y(i)))*j))/(z(i)*j);
postransflowz(i)=(vzf_mag(x(i))(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j)-
vzf_mag(y(i))(cos(vzf_phase(y(i)))+sin(vzf_phase(y(i)))j))(-j)*Yz(x(i),y(i));
negtransflowp(i)=(vpf_mag(y(i))(cos(vpf_phase(y(i)))+sin(vpf_phase(y(i)))*j)-
vpf_mag(x(i))(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j))/(z(i)*j);
negtransflown(i)=(vnf_mag(y(i))(cos(vnf_phase(y(i)))+sin(vnf_phase(y(i)))*j)-
vnf_mag(x(i))(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j))/(z(i)*j);
negtransflowz(i)=(vzf_mag(y(i))(cos(vzf_phase(y(i)))+sin(vzf_phase(y(i)))*j)-
vzf_mag(x(i))(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))j))(-j)*Yz(x(i),y(i));
end postransflowp_mag=abs(postransflowp); postransflowp_phase=angle(postransflowp);
postransflown_mag=abs(postransflown); postransflown_phase=angle(postransflown);
postransflowz_mag=abs(postransflowz); postransflowz_phase=angle(postransflowz);
negtransflowp_mag=abs(negtransflowp); negtransflowp_phase=angle(negtransflowp);
negtransflown_mag=abs(negtransflown); negtransflown_phase=angle(negtransflown);
negtransflowz_mag=abs(negtransflowz); negtransflowz_phase=angle(negtransflowz);

fid=fopen('gendata.txt','r'); bb=textread('gendata.txt'); fclose(fid);


x=bb(:,1);
z=bb(:,2);
zz=bb(:,3);
gnd=bb(:,4); ele_num=1:length([ele_num y']);
%length(ele_num) start_bus=[start_bus x']; for i=1:length(x)
temp(i)=nb+1; %nb+1 is taken as the code for the reference bus i.e ground end
end_bus=[end_bus temp]; for i=1:ng
status(s+i)=2; end
for i=1:ng
posgenflowp(i)=(vpf_mag(x(i))*(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j)-1)/(z(i)*j);
posgenflown(i)=(vnf_mag(x(i))*(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j))/(z(i)*j);
posgenflowz(i)=(vzf_mag(x(i))*(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j))/((zz(i)+3*gnd(i))
*j);
neggenflowp(i)=-(vpf_mag(x(i))*(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j)-1)/(z(i)*j);
neggenflown(i)=-(vnf_mag(x(i))*(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j))/(z(i)*j);
neggenflowz(i)=-(vzf_mag(x(i))*(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j))/((zz(i)+3*gnd(i)
)*j);

end posgenflowp_mag=abs(posgenflowp); posgenflowp_phase=angle(posgenflowp);


posgenflown_mag=abs(posgenflown); posgenflown_phase=angle(posgenflown);
posgenflowz_mag=abs(posgenflowz); posgenflowz_phase=angle(posgenflowz);
neggenflowz_mag=abs(neggenflowz); neggenflowz_phase=angle(neggenflowz);
neggenflowp_mag=abs(neggenflowp); neggenflowp_phase=angle(neggenflowp);
neggenflown_mag=abs(neggenflown); neggenflown_phase=angle(neggenflown);

posflowp=[poslineflowp postransflowp posgenflowp]; posflown=[poslineflown postransflown


posgenflown]; posflowz=[poslineflowz postransflowz posgenflowz];

posflowp_phase=[poslineflowp_phase postransflowp_phase posgenflowp_phase];


posflown_phase=[poslineflown_phase postransflown_phase posgenflown_phase];
posflowz_phase=[poslineflowz_phase postransflowz_phase posgenflowz_phase];

negflowp=[neglineflowp negtransflowp neggenflowp]; negflown=[neglineflown negtransflown


neggenflown]; negflowz=[neglineflowz negtransflowz neggenflowz];

negflowp_phase=[neglineflowp_phase negtransflowp_phase neggenflowp_phase];


negflown_phase=[neglineflown_phase negtransflown_phase neggenflown_phase];
negflowz_phase=[neglineflowz_phase negtransflowz_phase neggenflowz_phase];

%correction for star-delta connected transformers for i=1:(nb+1)


M(i)=1000; %phase adjusting parameter of each bus end
M(1)=0;
k=1; %running variable for element number kk=find(M==1000);
while length(kk)~=0 flag=k; ib=start_bus(k); jb=end_bus(k);

if M(ib)==1000
if M(jb)==1000 k=k+1;
end
if M(jb)~=1000
if status(k)~=0 M(ib)=M(jb); k=k+1;
end temp=find(hvbuses==ib);
if k<=(length(ele_num)) & k==flag if status(k)==0
if (length(temp)==0)

M(ib)=M(jb)-1;
k=k+1; else
M(ib)=M(jb)+1;
k=k+1; end
end end end
kk=find(M==1000); if length(kk)==0
break; end
if length(kk)~=0
if k<=length(ele_num) continue;
end
if k>length(ele_num) k=1;
continue; end
end end

if M(ib)~=1000
if M(jb)~=1000 k=k+1;
end
if M(jb)==1000
if status(k)~=0 M(jb)=M(ib); k=k+1;
end temp=find(hvbuses==jb);
if k<=(length(ele_num)) & k==flag if status(k)==0
if (length(temp)==0) M(jb)=M(ib)-1;
k=k+1; else
M(jb)=M(ib)+1;
k=k+1; end
end end end
kk=find(M==1000); if length(kk)==0
break; end
if length(kk)~=0
if k<=length(ele_num) continue;
end
if k>length(ele_num) k=1;

continue; end
end end
end

fid=fopen('faultdata.txt','r'); b=textread('faultdata.txt'); fclose(fid);


FB=b(1);
flag=M(FB); for i=1:(nb+1)
M(i)=M(i)-flag; end
M;
%correction of voltage and flows
value=input('Enter the phase shift that you desire in degrees. The choice that you have are 30 and
90 degrees.');
for i=1:nb
if value==30 vpf_phase(i)=vpf_phase(i)+M(i)*(value*pi/180);
vnf_phase(i)=vnf_phase(i)-M(i)*(value*pi/180);
end
if value==90
vpf_phase(i)=vpf_phase(i)-M(i)*(value*pi/180);
vnf_phase(i)=vnf_phase(i)+M(i)*(value*pi/180);
end end
for i=1:length(posflowp_phase) if value==30
posflowp_phase(i)=posflowp_phase(i)+M(start_bus(i))*(value*pi/180);
posflown_phase(i)=posflown_phase(i)-M(start_bus(i))*(value*pi/180);
end
if value==90
posflowp_phase(i)=posflowp_phase(i)-M(start_bus(i))*(value*pi/180);
posflown_phase(i)=posflown_phase(i)+M(start_bus(i))*(value*pi/180);
end
if value==30 negflowp_phase(i)=negflowp_phase(i)+M(end_bus(i))*(value*pi/180);
negflown_phase(i)=negflown_phase(i)-M(end_bus(i))*(value*pi/180);
end
if value==90
negflowp_phase(i)=negflowp_phase(i)-M(end_bus(i))*(value*pi/180);
negflown_phase(i)=negflown_phase(i)+M(end_bus(i))*(value*pi/180);
end end

%conversion to phase quantities a=cos(2*pi/3)+sin(2*pi/3)*j; asq=cos(4*pi/3)+sin(4*pi/3)*j; for


i=1:nb
va(i)=vzf(i)+vpf(i)+vnf(i); vb(i)=vzf(i)+asq*vpf(i)+a*vnf(i); vc(i)=vzf(i)+a*vpf(i)+asq*vnf(i);
end
for i=1:length(ele_num)

posflowa(i)=posflowz(i)+posflowp(i)+posflown(i);
posflowb(i)=posflowz(i)+asq*posflowp(i)+a*posflown(i);
posflowc(i)=posflowz(i)+a*posflowp(i)+asq*posflown(i);
end
for i=1:length(ele_num) negflowa(i)=negflowz(i)+negflowp(i)+negflown(i);
negflowb(i)=negflowz(i)+asq*negflowp(i)+a*negflown(i);
negflowc(i)=negflowz(i)+a*negflowp(i)+asq*negflown(i);
end

%data entry into output file fid=fopen('output.txt','w');


fprintf(fid,'The positive sequence Ybus\n\n'); for i=1:nb
for k=1:nb
fprintf(fid,'%-10.3f\t',Yp(i,k)); end
fprintf(fid,'\n\n'); end
fprintf(fid,'The positive sequence Zbus\n\n'); for i=1:nb
for k=1:nb
fprintf(fid,'%-10.3f\t',Zp(i,k)); end
fprintf(fid,'\n\n'); end

fprintf(fid,'The zero sequence Ybus\n\n'); for i=1:nb


for k=1:nb
fprintf(fid,'%-10.3f\t',Yz(i,k)); end
fprintf(fid,'\n\n'); end
fprintf(fid,'The zero sequence Zbus\n\n'); for i=1:nb
for k=1:nb
fprintf(fid,'%-10.3f\t',Zz(i,k)); end
fprintf(fid,'\n\n'); end

fprintf(fid,'Fault current\n\n'); fprintf(fid,'Magnitude Angle(radians)\n'); fprintf(fid,'I1\n');


fprintf(fid,'%-10.3f %-10.3f\n\n',abs(ifp),angle(ifp)); fprintf(fid,'I2\n');
fprintf(fid,'%-10.3f %-10.3f\n\n',abs(ifn),angle(ifn)); fprintf(fid,'I0\n');
fprintf(fid,'%-10.3f %-10.3f\n\n',abs(ifz),angle(ifz)); fprintf(fid,'Printing of sequence
components\n'); fprintf(fid,'Post Fault Voltages\n\n');
fprintf(fid,'Bus-number Pos-seq-Magnitude Pos-seq-Angle(radians) Neg-seq-Magnitude
Neg-seq- Angle(radians) Zero-seq-Magnitude Zero-seq-Angle(radians)\n');
for i=1:length(vpf)

fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-


10.3f\n',i,abs(vpf(i)),vpf_phase(i),abs(vnf(i)),vnf_phase(i),abs(vzf(i)),angle(vzf(i))); end
fprintf(fid,'\n\n');
fprintf(fid,'Post fault flows\n\n');
fprintf(fid,'The extra bus represents the reference bus\n');
fprintf(fid,'Start-bus End-bus pos-Magnitude pos-Phase neg-mag neg-phase zero-mag
zero-phase\n'); fprintf(fid,'This represents the I to J flows\n\n');
for i=1:length(posflowp)
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',start_bus(i),end_bus(i),abs(posflowp(i)),posflowp_phase(i),abs(posflown(i)),posflown_p
hase(i),ab s(posflowz(i)),posflowz_phase(i));
end fprintf(fid,'\n\n');
fprintf(fid,'This represents the J to I flows\n\n');
fprintf(fid,'Start-bus End-bus pos-Magnitude pos-Phase neg-mag neg-phase zero-mag
zero-phase\n'); kk=find(end_bus==(nb+1));
for i=1:length(negflowp) test=find(kk==i);
if length(test)==0
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',end_bus(i),start_bus(i),abs(negflowp(i)),negflowp_phase(i),abs(negflown(i)),negflown_
phase(i),ab s(negflowz(i)),negflowz_phase(i));
end end
fprintf(fid,'\n\n');

fprintf(fid,'Printing of phase quantities\n');


fprintf(fid,'Bus-number A-Magnitude A-Angle(radians) B-Magnitude B-Angle(radians)
C-Magnitude C- Angle(radians)\n');
for i=1:length(vpf)
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',i,abs(va(i)),angle(va(i)),abs(vb(i)),angle(vb(i)),abs(vc(i)),angle(vc(i))); end
fprintf(fid,'\n\n');
fprintf(fid,'Post fault flows\n\n');
fprintf(fid,'The extra bus represents the reference bus\n');
fprintf(fid,'Start-bus End-bus A-Magnitude A-Phase B-mag B-phase C-mag C-phase\n');
fprintf(fid,'This represents the I to J flows\n\n');
for i=1:length(posflowp)
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',start_bus(i),end_bus(i),abs(posflowa(i)),angle(posflowa(i)),abs(posflowb(i)),angle(posfl
owb(i)),abs (posflowc(i)),angle(posflowc(i)));
end fprintf(fid,'\n\n');
fprintf(fid,'This represents the J to I flows\n\n');
fprintf(fid,'Start-bus End-bus A-Magnitude A-Phase B-mag B-phase C-mag C-phase\n');
kk=find(end_bus==(nb+1));
for i=1:length(negflowp) test=find(kk==i);
if length(test)==0
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',end_bus(i),start_bus(i),abs(negflowa(i)),angle(negflowa(i)),abs(negflowb(i)),angle(negfl
owb(i)),ab s(negflowc(i)),angle(negflowc(i)));
end end
fprintf(fid,'\n\n'); fclose(fid);

done=1;

Three Phase Fault

function done=threephfault(nb,ng,nl,nt) nb=5;


ng=2; nl=3; nt=2;
Yp=Ypositive(nb,ng,nl,nt); Zbus=inv(Yp); v0_matrix=ones(nb); v0=v0_matrix(:,1);
fid=fopen('faultdata.txt','r'); b=textread('faultdata.txt'); fclose(fid);
bus=b(1);
imp=b(3); fault_current=v0(bus)/((Zbus(bus,bus)+imp)*j); for i=1:length(v0)
vf(i)=v0(i)-Zbus(i,bus)*fault_current*j; end
vf_mag=abs(vf); vf_phase=angle(vf); fid=fopen('linedata.txt','r'); bb=textread('linedata.txt');
fclose(fid);
x=bb(:,1);
y=bb(:,2);
zp=bb(:,3); ele_num=1:length(x); start_bus=x; end_bus=y;
for i=1:length(x) status(i)=1;
end
for i=1:nl poslineflow(i)=(vf_mag(x(i))(cos(vf_phase(x(i)))+sin(vf_phase(x(i)))*j)-
vf_mag(y(i))(cos(vf_phase(y(i)))+sin(vf_phase(y(i)))*j))/(zp(i)*j);
neglineflow(i)=(vf_mag(y(i))(cos(vf_phase(y(i)))+sin(vf_phase(y(i)))*j)-
vf_mag(x(i))(cos(vf_phase(x(i)))+sin(vf_phase(x(i)))*j))/(zp(i)*j); end
poslineflow_mag=abs(poslineflow); poslineflow_phase=angle(poslineflow);
neglineflow_mag=abs(neglineflow); neglineflow_phase=angle(neglineflow);
fid=fopen('transformerdata.txt','r'); bb=textread('transformerdata.txt'); fclose(fid);
x=bb(:,1);

y=bb(:,3);
z=bb(:,5);
hv=bb(:,2);
lv=bb(:,4); hvbuses=x;
ele_num=1:length([ele_num y']); start_bus=[start_bus' x']; end_bus=[end_bus' y'];
for i=1:length(x)
if (hv(i)==1 & lv(i)==2) | (hv(i)==2 & lv(i)==1) | (hv(i)==0 & lv(i)==2) | (hv(i)==2 & lv(i)==0)
status(i+nl)=0;
else
status(i+nl)=3; end
end s=length(status); for i=1:nt
postransflow(i)=(vf_mag(x(i))(cos(vf_phase(x(i)))+sin(vf_phase(x(i)))*j)-
vf_mag(y(i))(cos(vf_phase(y(i)))+sin(vf_phase(y(i)))*j))/(z(i)*j);
negtransflow(i)=(vf_mag(y(i))(cos(vf_phase(y(i)))+sin(vf_phase(y(i)))*j)-
vf_mag(x(i))(cos(vf_phase(x(i)))+sin(vf_phase(x(i)))*j))/(z(i)*j); end
postransflow_mag=abs(postransflow); postransflow_phase=angle(postransflow);
negtransflow_mag=abs(negtransflow); negtransflow_phase=angle(negtransflow);
fid=fopen('gendata.txt','r'); bb=textread('gendata.txt');
fclose(fid); x=bb(:,1);
z=bb(:,2); ele_num=1:length([ele_num y']); start_bus=[start_bus x'];
for i=1:length(x) temp(i)=nb+1;
end
end_bus=[end_bus temp]; for i=1:ng
status(s+i)=2; end
for i=1:ng
posgenflow(i)=(vf_mag(x(i))*(cos(vf_phase(x(i)))+sin(vf_phase(x(i)))*j)-1)/(z(i)*j);
neggenflow(i)=-(vf_mag(x(i))*(cos(vf_phase(x(i)))+sin(vf_phase(x(i)))*j)-1)/(z(i)*j); end
posgenflow_mag=abs(posgenflow); posgenflow_phase=angle(posgenflow);
neggenflow_mag=abs(neggenflow); neggenflow_phase=angle(neggenflow);
posflow_mag=[poslineflow_mag postransflow_mag posgenflow_mag];
posflow_phase=[poslineflow_phase postransflow_phase posgenflow_phase];
negflow_mag=[neglineflow_mag negtransflow_mag neggenflow_mag];
negflow_phase=[neglineflow_phase negtransflow_phase neggenflow_phase]; status
for i=1:(nb+1) M(i)=1000;
end
M(1)=0; k=1;
kk=find(M==1000); while length(kk)~=0
flag=k; ib=start_bus(k); jb=end_bus(k); if M(ib)==1000
if M(jb)==1000 k=k+1;
end
if M(jb)~=1000
if status(k)~=0 M(ib)=M(jb); k=k+1;
end temp=find(hvbuses==ib);
if k<=(length(ele_num))& k==flag if status(k)==0
if (length(temp)==0) M(ib)=M(jb)-1;
k=k+1; else
M(ib)=M(jb)+1;
k=k+1; end
end end end
kk=find(M==1000); if length(kk)==0
break; end
if length(kk)~=0
if k<=length(ele_num) continue;
end
if k>length(ele_num) k=1;
continue; end
end end

if M(ib)~=1000
if M(jb)~=1000 k=k+1;
end
if M(jb)==1000
if status(k)~=0 M(jb)=M(ib); k=k+1;
end temp=find(hvbuses==jb);
if k<=(length(ele_num)) & k==flag if status(k)==0
if (length(temp)==0) M(jb)=M(ib)-1;
k=k+1; else
M(jb)=M(ib)+1;
k=k+1; end
end end end
kk=find(M==1000); if length(kk)==0
break; end
if length(kk)~=0
if k<=length(ele_num) continue;
end
if k>length(ele_num) k=1;
continue; end
end end
end
fid=fopen('faultdata.txt','r'); b=textread('faultdata.txt'); fclose(fid);
FB=b(1);
flag=M(FB); for i=1:(nb+1)
M(i)=M(i)-flag; end
M
value=input('Enter the phase shift that you desire in degrees. The choice that you have are 30 and
90 degrees.');
for i=1:nb vf_phase(i)=vf_phase(i)+M(i)*(value*pi/180);
end
for i=1:length(posflow_phase)
posflow_phase(i)=posflow_phase(i)+M(start_bus(i))*(value*pi/180);
negflow_phase(i)=negflow_phase(i)+M(end_bus(i))*(value*pi/180);
end fid=fopen('out.txt','w'); fprintf(fid,'The Ybus\n\n'); for i=1:nb
for k=1:nb
fprintf(fid,'%-10.3f\t',Yp(i,k)); end
fprintf(fid,'\n\n'); end
fprintf(fid,'The Zbus\n\n'); for i=1:nb
for k=1:nb
fprintf(fid,'%-10.3f\t',Zbus(i,k));
end fprintf(fid,'\n\n');
end
fprintf(fid,'Fault current\n\n'); fprintf(fid,'Magnitude Angle(radians)\n');
fprintf(fid,'%-10.3f %-10.3f\n\n',abs(fault_current),angle(fault_current)); fprintf(fid,'Post Fault
Voltages\n\n');
fprintf(fid,'Bus-number Magnitude Angle(radians)\n'); for i=1:length(vf)
fprintf(fid,'%-10.3f %-10.3f %-10.3f\n',i,vf_mag(i),vf_phase(i)); end
fprintf(fid,'\n\n');
fprintf(fid,'Post fault flows\n\n');
fprintf(fid,'The extra bus represents the reference bus\n'); fprintf(fid,'Start-bus End-bus
Magnitude Phase\n'); fprintf(fid,'This represents the I to J flows\n\n');
for i=1:length(posflow_mag) fprintf(fid,'%-10.3f %-10.3f %-10.3f %-
10.3f\n',start_bus(i),end_bus(i),posflow_mag(i),posflow_phase(i)); end
fprintf(fid,'\n\n');
fprintf(fid,'This represents the J to I flows\n\n'); fprintf(fid,'Start-bus End-bus Magnitude
Phase\n'); kk=find(end_bus==(nb+1));
for i=1:length(negflow_mag) test=find(kk==i);
if length(test)==0
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-
10.3f\n',end_bus(i),start_bus(i),negflow_mag(i),negflow_phase(i));
end end
fprintf(fid,'\n\n'); fclose(fid); done=1;

LINE-LINE-GROUND FAULT

function done=llgfault(nb,ng,nl,nt)
%formation of positive sequence Ybus Yp=Ypositive(nb,ng,nl,nt);
%formation of zero sequence Ybus Yz=Yzero(nb,ng,nl,nt);
%formation of positive and negative sequence Zbus Zp=inv(Yp);
Zn=Zp;
%formation of zero sequence Zbus check=diag(Yz);
k=find(check==0) if length(k)==0
Zz=inv(Yz); end
if length(k)~=0 Zz=pinv(Yz);
end
%preparation of pre-fault voltage vectors xxx=ones(nb);

vp0=xxx(:,1); yyy=zeros(nb); vn0=yyy(:,1); vz0=vn0;


%calculation of fault current fid=fopen('faultdata.txt','r'); b=textread('faultdata.txt'); fclose(fid);
bus=b(1);
imp=b(3);
%calculation of Thevenin impedances
Zth=((Zz(bus,bus)+3*imp)*j*Zn(bus,bus)*j/((Zz(bus,bus)+3*imp+Zn(bus,bus))*j))+Zp(bus,bus
)*j; ifp=vp0(bus)/Zth;
ifn=-ifp*(Zz(bus,bus)+3*imp)*j/(Zz(bus,bus)+3*imp+Zn(bus,bus));
ifz=-ifp*(Zn(bus,bus))*j/(Zz(bus,bus)+3*imp+Zn(bus,bus));
%post fault voltage calculations for i=1:nb
vpf(i)=vp0(i)-Zp(i,bus)*ifp*j; vnf(i)=vn0(i)-Zn(i,bus)*ifn*j; vzf(i)=vz0(i)-Zz(i,bus)*ifz*j;
end vpf_mag=abs(vpf); vpf_phase=angle(vpf); vnf_mag=abs(vnf); vnf_phase=angle(vnf);
vzf_mag=abs(vzf); vzf_phase=angle(vzf);

%calculation of post fault flows in generators, transformers and line


%reading of elements and classifying them as lines, transformers or
%generators
%the convention for the status of elements is as follows
% 1=lines
% 2=generators
% 3=transformers without phase shift
% 0=transformers with phase shift fid=fopen('linedata.txt','r'); bb=textread('linedata.txt');
fclose(fid);
x=bb(:,1);
y=bb(:,2);
zp=bb(:,3);
zz=bb(:,4); ele_num=1:length(x); start_bus=x; end_bus=y;
for i=1:length(x) status(i)=1;
end
for i=1:nl poslineflowp(i)=(vpf_mag(x(i))(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j)-
vpf_mag(y(i))(cos(vpf_phase(y(i)))+sin(vpf_phase(y(i)))*j))/(zp(i)*j);
poslineflown(i)=(vnf_mag(x(i))(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j)-
vnf_mag(y(i))(cos(vnf_phase(y(i)))+sin(vnf_phase(y(i)))*j))/(zp(i)*j);
poslineflowz(i)=(vzf_mag(x(i))(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j)-
vzf_mag(y(i))(cos(vzf_phase(y(i)))+sin(vzf_phase(y(i)))*j))/(zz(i)*j);

neglineflowp(i)=(vpf_mag(y(i))(cos(vpf_phase(y(i)))+sin(vpf_phase(y(i)))*j)-
vpf_mag(x(i))(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j))/(zp(i)*j);
neglineflown(i)=(vnf_mag(y(i))(cos(vnf_phase(y(i)))+sin(vnf_phase(y(i)))*j)-
vnf_mag(x(i))(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j))/(zp(i)*j);
neglineflowz(i)=(vzf_mag(y(i))(cos(vzf_phase(y(i)))+sin(vzf_phase(y(i)))*j)-
vzf_mag(x(i))(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j))/(zz(i)*j);
end poslineflowp_mag=abs(poslineflowp); poslineflowp_phase=angle(poslineflowp);
poslineflown_mag=abs(poslineflown); poslineflown_phase=angle(poslineflown);
poslineflowz_mag=abs(poslineflowz); poslineflowz_phase=angle(poslineflowz);
neglineflowp_mag=abs(neglineflowp); neglineflowp_phase=angle(neglineflowp);
neglineflown_mag=abs(neglineflown); neglineflown_phase=angle(neglineflown);
neglineflowz_mag=abs(neglineflowz); neglineflowz_phase=angle(neglineflowz)

fid=fopen('transformerdata.txt','r'); bb=textread('transformerdata.txt'); fclose(fid);


x=bb(:,1);
y=bb(:,3);
z=bb(:,5);
zz=bb(:,6);
hv=bb(:,2);
lv=bb(:,4); hvbuses=x;
ele_num=1:length([ele_num y']); start_bus=[start_bus' x']; end_bus=[end_bus' y'];
for i=1:length(x)
if (hv(i)==1 & lv(i)==2) | (hv(i)==2 & lv(i)==1) | (hv(i)==0 & lv(i)==2) | (hv(i)==2 & lv(i)==0)
status(i+nl)=0;
else
status(i+nl)=3; end
end s=length(status); for i=1:nt
postransflowp(i)=(vpf_mag(x(i))(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j)-
vpf_mag(y(i))(cos(vpf_phase(y(i)))+sin(vpf_phase(y(i)))*j))/(z(i)*j);
postransflown(i)=(vnf_mag(x(i))(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j)-
vnf_mag(y(i))(cos(vnf_phase(y(i)))+sin(vnf_phase(y(i)))*j))/(z(i)*j);
postransflowz(i)=(vzf_mag(x(i))(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j)-
vzf_mag(y(i))(cos(vzf_phase(y(i)))+sin(vzf_phase(y(i)))j))(-j)*Yz(x(i),y(i));
negtransflowp(i)=(vpf_mag(y(i))(cos(vpf_phase(y(i)))+sin(vpf_phase(y(i)))*j)-
vpf_mag(x(i))(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j))/(z(i)*j);
negtransflown(i)=(vnf_mag(y(i))(cos(vnf_phase(y(i)))+sin(vnf_phase(y(i)))*j)-
vnf_mag(x(i))(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j))/(z(i)*j);
negtransflowz(i)=(vzf_mag(y(i))(cos(vzf_phase(y(i)))+sin(vzf_phase(y(i)))*j)-
vzf_mag(x(i))(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))j))(-j)*Yz(x(i),y(i));
end postransflowp_mag=abs(postransflowp);
postransflowp_phase=angle(postransflowp); postransflown_mag=abs(postransflown);
postransflown_phase=angle(postransflown); postransflowz_mag=abs(postransflowz);
postransflowz_phase=angle(postransflowz); negtransflowp_mag=abs(negtransflowp);
negtransflowp_phase=angle(negtransflowp); negtransflown_mag=abs(negtransflown);
negtransflown_phase=angle(negtransflown); negtransflowz_mag=abs(negtransflowz);
negtransflowz_phase=angle(negtransflowz);

fid=fopen('gendata.txt','r'); bb=textread('gendata.txt'); fclose(fid);


x=bb(:,1);
z=bb(:,2);
zz=bb(:,3);
gnd=bb(:,4); ele_num=1:length([ele_num y']);
%length(ele_num) start_bus=[start_bus x']; for i=1:length(x)
temp(i)=nb+1; %nb+1 is taken as the code for the reference bus i.e ground end
end_bus=[end_bus temp]; for i=1:ng
status(s+i)=2; end
for i=1:ng
posgenflowp(i)=(vpf_mag(x(i))*(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j)-1)/(z(i)*j);
posgenflown(i)=(vnf_mag(x(i))*(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j))/(z(i)*j);
posgenflowz(i)=(vzf_mag(x(i))*(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j))/((zz(i)+3*gnd(i))
*j);
neggenflowp(i)=-(vpf_mag(x(i))*(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j)-1)/(z(i)*j);
neggenflown(i)=-(vnf_mag(x(i))*(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j))/(z(i)*j);
neggenflowz(i)=-(vzf_mag(x(i))*(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j))/((zz(i)+3*gnd(i)
)*j); end
posgenflowp_mag=abs(posgenflowp); posgenflowp_phase=angle(posgenflowp);
posgenflown_mag=abs(posgenflown); posgenflown_phase=angle(posgenflown);
posgenflowz_mag=abs(posgenflowz); posgenflowz_phase=angle(posgenflowz);
neggenflowz_mag=abs(neggenflowz); neggenflowz_phase=angle(neggenflowz);
neggenflowp_mag=abs(neggenflowp); neggenflowp_phase=angle(neggenflowp);
neggenflown_mag=abs(neggenflown); neggenflown_phase=angle(neggenflown);

posflowp=[poslineflowp postransflowp posgenflowp]; posflown=[poslineflown postransflown


posgenflown]; posflowz=[poslineflowz postransflowz posgenflowz];

posflowp_phase=[poslineflowp_phase postransflowp_phase posgenflowp_phase];


posflown_phase=[poslineflown_phase postransflown_phase posgenflown_phase];

posflowz_phase=[poslineflowz_phase postransflowz_phase posgenflowz_phase];

negflowp=[neglineflowp negtransflowp neggenflowp]; negflown=[neglineflown negtransflown


neggenflown]; negflowz=[neglineflowz negtransflowz neggenflowz];

negflowp_phase=[neglineflowp_phase negtransflowp_phase neggenflowp_phase];


negflown_phase=[neglineflown_phase negtransflown_phase neggenflown_phase];
negflowz_phase=[neglineflowz_phase negtransflowz_phase neggenflowz_phase];

%correction for star-delta connected transformers for i=1:(nb+1)


M(i)=1000; %phase adjusting parameter of each bus end
M(1)=0;
k=1; %running variable for element number kk=find(M==1000);
while length(kk)~=0 flag=k; ib=start_bus(k); jb=end_bus(k);

if M(ib)==1000
if M(jb)==1000 k=k+1;
end
if M(jb)~=1000
if status(k)~=0 M(ib)=M(jb); k=k+1;
end temp=find(hvbuses==ib);
if k<=(length(ele_num)) & k==flag if status(k)==0
if (length(temp)==0) M(ib)=M(jb)-1;
k=k+1; else
M(ib)=M(jb)+1;
k=k+1; end
end end end
kk=find(M==1000); if length(kk)==0
%M
break; end
if length(kk)~=0
if k<=length(ele_num)
%k=k+1;
%M
continue; end

if k>length(ele_num) k=1;
%M
continue; end
end end

if M(ib)~=1000
if M(jb)~=1000 k=k+1;
end
if M(jb)==1000
if status(k)~=0 M(jb)=M(ib); k=k+1;
end temp=find(hvbuses==jb);
if k<=(length(ele_num)) & k==flag if status(k)==0
if (length(temp)==0) M(jb)=M(ib)-1;
k=k+1; else
M(jb)=M(ib)+1;
k=k+1; end
end end end
kk=find(M==1000); if length(kk)==0
%M
break; end
if length(kk)~=0
if k<=length(ele_num)
%k=k+1;
%M
continue; end
if k>length(ele_num) k=1;
%M
continue; end
end end
end

fid=fopen('faultdata.txt','r'); b=textread('faultdata.txt'); fclose(fid);


FB=b(1);
flag=M(FB); for i=1:(nb+1)

M(i)=M(i)-flag; end
M;
%correction of voltage and flows
value=input('Enter the phase shift that you desire in degrees. The choice that you have are 30 and
90 degrees.');
for i=1:nb
if value==30 vpf_phase(i)=vpf_phase(i)+M(i)*(value*pi/180);
vnf_phase(i)=vnf_phase(i)-M(i)*(value*pi/180);
end
if value==90
vpf_phase(i)=vpf_phase(i)-M(i)*(value*pi/180);
vnf_phase(i)=vnf_phase(i)+M(i)*(value*pi/180);
end end
for i=1:length(posflowp_phase) if value==30
posflowp_phase(i)=posflowp_phase(i)+M(start_bus(i))*(value*pi/180);
posflown_phase(i)=posflown_phase(i)-M(start_bus(i))*(value*pi/180);
end
if value==90
posflowp_phase(i)=posflowp_phase(i)-M(start_bus(i))*(value*pi/180);
posflown_phase(i)=posflown_phase(i)+M(start_bus(i))*(value*pi/180);
end
if value==30 negflowp_phase(i)=negflowp_phase(i)+M(end_bus(i))*(value*pi/180);
negflown_phase(i)=negflown_phase(i)-M(end_bus(i))*(value*pi/180);
end
if value==90
negflowp_phase(i)=negflowp_phase(i)-M(end_bus(i))*(value*pi/180);
negflown_phase(i)=negflown_phase(i)+M(end_bus(i))*(value*pi/180);
end end

%conversion to phase quantities a=cos(2*pi/3)+sin(2*pi/3)*j; asq=cos(4*pi/3)+sin(4*pi/3)*j; for


i=1:nb
va(i)=vzf(i)+vpf(i)+vnf(i); vb(i)=vzf(i)+asq*vpf(i)+a*vnf(i); vc(i)=vzf(i)+a*vpf(i)+asq*vnf(i);
end
for i=1:length(ele_num) posflowa(i)=posflowz(i)+posflowp(i)+posflown(i);
posflowb(i)=posflowz(i)+asq*posflowp(i)+a*posflown(i);
posflowc(i)=posflowz(i)+a*posflowp(i)+asq*posflown(i);
end
for i=1:length(ele_num) negflowa(i)=negflowz(i)+negflowp(i)+negflown(i);
negflowb(i)=negflowz(i)+asq*negflowp(i)+a*negflown(i);
negflowc(i)=negflowz(i)+a*negflowp(i)+asq*negflown(i);
end

%data entry into output file fid=fopen('output.txt','w');

fprintf(fid,'The positive sequence Ybus\n\n'); for i=1:nb


for k=1:nb
fprintf(fid,'%-10.3f\t',Yp(i,k)); end
fprintf(fid,'\n\n'); end
fprintf(fid,'The positive sequence Zbus\n\n'); for i=1:nb
for k=1:nb
fprintf(fid,'%-10.3f\t',Zp(i,k)); end
fprintf(fid,'\n\n'); end

fprintf(fid,'The zero sequence Ybus\n\n'); for i=1:nb


for k=1:nb
fprintf(fid,'%-10.3f\t',Yz(i,k)); end
fprintf(fid,'\n\n'); end
fprintf(fid,'The zero sequence Zbus\n\n'); for i=1:nb
for k=1:nb
fprintf(fid,'%-10.3f\t',Zz(i,k)); end
fprintf(fid,'\n\n'); end

fprintf(fid,'Fault current\n\n'); fprintf(fid,'Magnitude Angle(radians)\n'); fprintf(fid,'I1\n');


fprintf(fid,'%-10.3f %-10.3f\n\n',abs(ifp),angle(ifp)); fprintf(fid,'I2\n');
fprintf(fid,'%-10.3f %-10.3f\n\n',abs(ifn),angle(ifn)); fprintf(fid,'I0\n');
fprintf(fid,'%-10.3f %-10.3f\n\n',abs(ifz),angle(ifz)); fprintf(fid,'Printing of sequence
components\n'); fprintf(fid,'Post Fault Voltages\n\n');
fprintf(fid,'Bus-number Pos-seq-Magnitude Pos-seq-Angle(radians) Neg-seq-Magnitude
Neg-seq- Angle(radians) Zero-seq-Magnitude Zero-seq-Angle(radians)\n');
for i=1:length(vpf)
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',i,abs(vpf(i)),vpf_phase(i),abs(vnf(i)),vnf_phase(i),abs(vzf(i)),angle(vzf(i))); end
fprintf(fid,'\n\n');
fprintf(fid,'Post fault flows\n\n');
fprintf(fid,'The extra bus represents the reference bus\n');
fprintf(fid,'Start-bus End-bus pos-Magnitude pos-Phase neg-mag neg-phase zero-mag
zero-phase\n'); fprintf(fid,'This represents the I to J flows\n\n');
for i=1:length(posflowp)
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',start_bus(i),end_bus(i),abs(posflowp(i)),angle(posflowp(i)),abs(posflown(i)),angle(posfl
own(i)),abs (posflowz(i)),angle(posflowz(i)));
end

fprintf(fid,'\n\n');
fprintf(fid,'This represents the J to I flows\n\n');
fprintf(fid,'Start-bus End-bus pos-Magnitude pos-Phase neg-mag neg-phase zero-mag
zero-phase\n'); kk=find(end_bus==(nb+1));
for i=1:length(negflowp) test=find(kk==i);
if length(test)==0
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',end_bus(i),start_bus(i),abs(negflowp(i)),angle(negflowp(i)),abs(negflown(i)),angle(negfl
own(i)),ab s(negflowz(i)),angle(negflowz(i)));
end end
fprintf(fid,'\n\n');

fprintf(fid,'Printing of phase quantities\n');


fprintf(fid,'Bus-number A-Magnitude A-Angle(radians) B-Magnitude B-Angle(radians)
C-Magnitude C- Angle(radians)\n');
for i=1:length(vpf)
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',i,abs(va(i)),angle(va(i)),abs(vb(i)),angle(vb(i)),abs(vc(i)),angle(vc(i))); end
fprintf(fid,'\n\n');
fprintf(fid,'Post fault flows\n\n');
fprintf(fid,'The extra bus represents the reference bus\n');
fprintf(fid,'Start-bus End-bus A-Magnitude A-Phase B-mag B-phase C-mag C-phase\n');
fprintf(fid,'This represents the I to J flows\n\n');
for i=1:length(posflowp)
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',start_bus(i),end_bus(i),abs(posflowa(i)),angle(posflowa(i)),abs(posflowb(i)),angle(posfl
owb(i)),abs (posflowc(i)),angle(posflowc(i)));
end fprintf(fid,'\n\n');
fprintf(fid,'This represents the J to I flows\n\n');
fprintf(fid,'Start-bus End-bus A-Magnitude A-Phase B-mag B-phase C-mag C-phase\n');
kk=find(end_bus==(nb+1));
for i=1:length(negflowp) test=find(kk==i);
if length(test)==0
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',end_bus(i),start_bus(i),abs(negflowa(i)),angle(negflowa(i)),abs(negflowb(i)),angle(negfl
owb(i)),ab s(negflowc(i)),angle(negflowc(i)));
end end
fprintf(fid,'\n\n'); fclose(fid);

done=1;

LINE-LINE FAULT

function done=llfault(nb,ng,nl,nt)
%formation of positive sequence Ybus Yp=Ypositive(nb,ng,nl,nt);
%formation of zero sequence Ybus Yz=Yzero(nb,ng,nl,nt);
%formation of positive and negative sequence Zbus Zp=inv(Yp);

Zn=Zp;
%formation of zero sequence Zbus check=diag(Yz);
k=find(check==0) if length(k)==0
Zz=inv(Yz); end
if length(k)~=0 Zz=pinv(Yz);
end
%preparation of pre-fault voltage vectors xxx=ones(nb);
vp0=xxx(:,1); yyy=zeros(nb); vn0=yyy(:,1); vz0=vn0;
%calculation of fault current fid=fopen('faultdata.txt','r'); b=textread('faultdata.txt'); fclose(fid);
bus=b(1);
imp=b(3); ifp=vp0(bus)/((Zp(bus,bus)+Zn(bus,bus)+imp)*j); ifn=-ifp;
ifz=0;
%post fault voltage calculations for i=1:nb
vpf(i)=vp0(i)-Zp(i,bus)*ifp*j; vnf(i)=vn0(i)-Zn(i,bus)*ifn*j; vzf(i)=vz0(i)-Zz(i,bus)*ifz*j;
end vpf_mag=abs(vpf); vpf_phase=angle(vpf); vnf_mag=abs(vnf); vnf_phase=angle(vnf);
vzf_mag=abs(vzf); vzf_phase=angle(vzf);

%calculation of post fault flows in generators, transformers and line


%reading of elements and classifying them as lines, transformers or
%generators
%the convention for the status of elements is as follows
% 1=lines
% 2=generators
% 3=transformers without phase shift
% 0=transformers with phase shift fid=fopen('linedata.txt','r'); bb=textread('linedata.txt');
fclose(fid);
x=bb(:,1);
y=bb(:,2);
zp=bb(:,3);
zz=bb(:,4); ele_num=1:length(x); start_bus=x; end_bus=y;

for i=1:length(x) status(i)=1;


end
for i=1:nl poslineflowp(i)=(vpf_mag(x(i))(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j)-
vpf_mag(y(i))(cos(vpf_phase(y(i)))+sin(vpf_phase(y(i)))*j))/(zp(i)*j);
poslineflown(i)=(vnf_mag(x(i))(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j)-
vnf_mag(y(i))(cos(vnf_phase(y(i)))+sin(vnf_phase(y(i)))*j))/(zp(i)*j);
poslineflowz(i)=(vzf_mag(x(i))(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j)-
vzf_mag(y(i))(cos(vzf_phase(y(i)))+sin(vzf_phase(y(i)))*j))/(zz(i)*j);
neglineflowp(i)=(vpf_mag(y(i))(cos(vpf_phase(y(i)))+sin(vpf_phase(y(i)))*j)-
vpf_mag(x(i))(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j))/(zp(i)*j);
neglineflown(i)=(vnf_mag(y(i))(cos(vnf_phase(y(i)))+sin(vnf_phase(y(i)))*j)-
vnf_mag(x(i))(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j))/(zp(i)*j);
neglineflowz(i)=(vzf_mag(y(i))(cos(vzf_phase(y(i)))+sin(vzf_phase(y(i)))*j)-
vzf_mag(x(i))(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j))/(zz(i)*j); end
poslineflowp_mag=abs(poslineflowp); poslineflowp_phase=angle(poslineflowp);
poslineflown_mag=abs(poslineflown); poslineflown_phase=angle(poslineflown);
poslineflowz_mag=abs(poslineflowz); poslineflowz_phase=angle(poslineflowz);
neglineflowp_mag=abs(neglineflowp); neglineflowp_phase=angle(neglineflowp);
neglineflown_mag=abs(neglineflown); neglineflown_phase=angle(neglineflown);
neglineflowz_mag=abs(neglineflowz); neglineflowz_phase=angle(neglineflowz)

fid=fopen('transformerdata.txt','r'); bb=textread('transformerdata.txt'); fclose(fid);


x=bb(:,1);
y=bb(:,3);
z=bb(:,5);
zz=bb(:,6);
hv=bb(:,2);
lv=bb(:,4); hvbuses=x;
ele_num=1:length([ele_num y']); start_bus=[start_bus' x']; end_bus=[end_bus' y'];
for i=1:length(x)
if (hv(i)==1 & lv(i)==2) | (hv(i)==2 & lv(i)==1) | (hv(i)==0 & lv(i)==2) | (hv(i)==2 & lv(i)==0)
status(i+nl)=0;
else
status(i+nl)=3; end
end s=length(status); for i=1:nt
postransflowp(i)=(vpf_mag(x(i))(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j)-
vpf_mag(y(i))(cos(vpf_phase(y(i)))+sin(vpf_phase(y(i)))*j))/(z(i)*j);
postransflown(i)=(vnf_mag(x(i))(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j)-
vnf_mag(y(i))(cos(vnf_phase(y(i)))+sin(vnf_phase(y(i)))*j))/(z(i)*j);

postransflowz(i)=(vzf_mag(x(i))(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j)-
vzf_mag(y(i))(cos(vzf_phase(y(i)))+sin(vzf_phase(y(i)))j))(-j)*Yz(x(i),y(i));
negtransflowp(i)=(vpf_mag(y(i))(cos(vpf_phase(y(i)))+sin(vpf_phase(y(i)))*j)-
vpf_mag(x(i))(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j))/(z(i)*j);
negtransflown(i)=(vnf_mag(y(i))(cos(vnf_phase(y(i)))+sin(vnf_phase(y(i)))*j)-
vnf_mag(x(i))(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j))/(z(i)*j);
negtransflowz(i)=(vzf_mag(y(i))(cos(vzf_phase(y(i)))+sin(vzf_phase(y(i)))*j)-
vzf_mag(x(i))(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))j))(-j)*Yz(x(i),y(i));
end postransflowp_mag=abs(postransflowp); postransflowp_phase=angle(postransflowp);
postransflown_mag=abs(postransflown); postransflown_phase=angle(postransflown);
postransflowz_mag=abs(postransflowz); postransflowz_phase=angle(postransflowz);
negtransflowp_mag=abs(negtransflowp); negtransflowp_phase=angle(negtransflowp);
negtransflown_mag=abs(negtransflown); negtransflown_phase=angle(negtransflown);
negtransflowz_mag=abs(negtransflowz); negtransflowz_phase=angle(negtransflowz);

fid=fopen('gendata.txt','r'); bb=textread('gendata.txt'); fclose(fid);


x=bb(:,1);
z=bb(:,2);
zz=bb(:,3);
gnd=bb(:,4); ele_num=1:length([ele_num y']);
%length(ele_num) start_bus=[start_bus x']; for i=1:length(x)
temp(i)=nb+1; %nb+1 is taken as the code for the reference bus i.e ground end
end_bus=[end_bus temp]; for i=1:ng
status(s+i)=2; end
for i=1:ng
posgenflowp(i)=(vpf_mag(x(i))*(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j)-1)/(z(i)*j);
posgenflown(i)=(vnf_mag(x(i))*(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j))/(z(i)*j);
posgenflowz(i)=(vzf_mag(x(i))*(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j))/((zz(i)+3*gnd(i))
*j);
neggenflowp(i)=-(vpf_mag(x(i))*(cos(vpf_phase(x(i)))+sin(vpf_phase(x(i)))*j)-1)/(z(i)*j);
neggenflown(i)=-(vnf_mag(x(i))*(cos(vnf_phase(x(i)))+sin(vnf_phase(x(i)))*j))/(z(i)*j);
neggenflowz(i)=-(vzf_mag(x(i))*(cos(vzf_phase(x(i)))+sin(vzf_phase(x(i)))*j))/((zz(i)+3*gnd(i)
)*j); end
posgenflowp_mag=abs(posgenflowp); posgenflowp_phase=angle(posgenflowp);
posgenflown_mag=abs(posgenflown); posgenflown_phase=angle(posgenflown);
posgenflowz_mag=abs(posgenflowz); posgenflowz_phase=angle(posgenflowz);
neggenflowz_mag=abs(neggenflowz); neggenflowz_phase=angle(neggenflowz);
neggenflowp_mag=abs(neggenflowp);

neggenflowp_phase=angle(neggenflowp); neggenflown_mag=abs(neggenflown);
neggenflown_phase=angle(neggenflown);

posflowp=[poslineflowp postransflowp posgenflowp]; posflown=[poslineflown postransflown


posgenflown]; posflowz=[poslineflowz postransflowz posgenflowz];

posflowp_phase=[poslineflowp_phase postransflowp_phase posgenflowp_phase];


posflown_phase=[poslineflown_phase postransflown_phase posgenflown_phase];
posflowz_phase=[poslineflowz_phase postransflowz_phase posgenflowz_phase];

negflowp=[neglineflowp negtransflowp neggenflowp]; negflown=[neglineflown negtransflown


neggenflown]; negflowz=[neglineflowz negtransflowz neggenflowz];

negflowp_phase=[neglineflowp_phase negtransflowp_phase neggenflowp_phase];


negflown_phase=[neglineflown_phase negtransflown_phase neggenflown_phase];
negflowz_phase=[neglineflowz_phase negtransflowz_phase neggenflowz_phase];

%correction for star-delta connected transformers for i=1:(nb+1)


M(i)=1000; %phase adjusting parameter of each bus end
M(1)=0;
k=1; %running variable for element number kk=find(M==1000);
while length(kk)~=0 flag=k; ib=start_bus(k); jb=end_bus(k);

if M(ib)==1000
if M(jb)==1000 k=k+1;
end
if M(jb)~=1000
if status(k)~=0 M(ib)=M(jb); k=k+1;
end temp=find(hvbuses==ib);
if k<=(length(ele_num)) & k==flag if status(k)==0
if (length(temp)==0) M(ib)=M(jb)-1;
k=k+1; else
M(ib)=M(jb)+1;
k=k+1; end
end end end
kk=find(M==1000);

if length(kk)==0
%M
break; end
if length(kk)~=0
if k<=length(ele_num)
%k=k+1;
%M
continue; end
if k>length(ele_num) k=1;
%M
continue; end
end end
if M(ib)~=1000
if M(jb)~=1000 k=k+1;
end
if M(jb)==1000
if status(k)~=0 M(jb)=M(ib); k=k+1;
end temp=find(hvbuses==jb);
if k<=(length(ele_num)) & k==flag if status(k)==0
if (length(temp)==0) M(jb)=M(ib)-1;
k=k+1; else
M(jb)=M(ib)+1;
k=k+1; end
end end end
kk=find(M==1000); if length(kk)==0
%M
break; end
if length(kk)~=0
if k<=length(ele_num)
%k=k+1;
%M
continue; end
if k>length(ele_num) k=1;
%M
continue; end

end end
end

fid=fopen('faultdata.txt','r'); b=textread('faultdata.txt'); fclose(fid);


FB=b(1);
flag=M(FB); for i=1:(nb+1)
M(i)=M(i)-flag; end
M;
%correction of voltage and flows
value=input('Enter the phase shift that you desire in degrees. The choice that you have are 30 and
90 degrees.');
for i=1:nb
if value==30 vpf_phase(i)=vpf_phase(i)+M(i)*(value*pi/180);
vnf_phase(i)=vnf_phase(i)-M(i)*(value*pi/180);
end
if value==90
vpf_phase(i)=vpf_phase(i)-M(i)*(value*pi/180);
vnf_phase(i)=vnf_phase(i)+M(i)*(value*pi/180);
end end
for i=1:length(posflowp_phase) if value==30
posflowp_phase(i)=posflowp_phase(i)+M(start_bus(i))*(value*pi/180);
posflown_phase(i)=posflown_phase(i)-M(start_bus(i))*(value*pi/180);
end
if value==90
posflowp_phase(i)=posflowp_phase(i)-M(start_bus(i))*(value*pi/180);
posflown_phase(i)=posflown_phase(i)+M(start_bus(i))*(value*pi/180);
end
if value==30 negflowp_phase(i)=negflowp_phase(i)+M(end_bus(i))*(value*pi/180);
negflown_phase(i)=negflown_phase(i)-M(end_bus(i))*(value*pi/180);
end
if value==90
negflowp_phase(i)=negflowp_phase(i)-M(end_bus(i))*(value*pi/180);
negflown_phase(i)=negflown_phase(i)+M(end_bus(i))*(value*pi/180);
end end

%conversion to phase quantities a=cos(2*pi/3)+sin(2*pi/3)*j; asq=cos(4*pi/3)+sin(4*pi/3)*j; for


i=1:nb
va(i)=vzf(i)+vpf(i)+vnf(i); vb(i)=vzf(i)+asq*vpf(i)+a*vnf(i); vc(i)=vzf(i)+a*vpf(i)+asq*vnf(i);
end
for i=1:length(ele_num) posflowa(i)=posflowz(i)+posflowp(i)+posflown(i);
posflowb(i)=posflowz(i)+asq*posflowp(i)+a*posflown(i);

posflowc(i)=posflowz(i)+a*posflowp(i)+asq*posflown(i); end
for i=1:length(ele_num) negflowa(i)=negflowz(i)+negflowp(i)+negflown(i);
negflowb(i)=negflowz(i)+asq*negflowp(i)+a*negflown(i);
negflowc(i)=negflowz(i)+a*negflowp(i)+asq*negflown(i);
end

%data entry into output file fid=fopen('output.txt','w');


fprintf(fid,'The positive sequence Ybus\n\n'); for i=1:nb
for k=1:nb
fprintf(fid,'%-10.3f\t',Yp(i,k)); end
fprintf(fid,'\n\n'); end
fprintf(fid,'The positive sequence Zbus\n\n'); for i=1:nb
for k=1:nb
fprintf(fid,'%-10.3f\t',Zp(i,k)); end
fprintf(fid,'\n\n'); end

fprintf(fid,'The zero sequence Ybus\n\n'); for i=1:nb


for k=1:nb
fprintf(fid,'%-10.3f\t',Yz(i,k)); end
fprintf(fid,'\n\n'); end
fprintf(fid,'The zero sequence Zbus\n\n'); for i=1:nb
for k=1:nb
fprintf(fid,'%-10.3f\t',Zz(i,k)); end
fprintf(fid,'\n\n'); end
fprintf(fid,'Fault current\n\n'); fprintf(fid,'Magnitude Angle(radians)\n'); fprintf(fid,'I1\n');
fprintf(fid,'%-10.3f %-10.3f\n\n',abs(ifp),angle(ifp)); fprintf(fid,'I2\n');
fprintf(fid,'%-10.3f %-10.3f\n\n',abs(ifn),angle(ifn)); fprintf(fid,'I0\n');
fprintf(fid,'%-10.3f %-10.3f\n\n',abs(ifz),angle(ifz)); fprintf(fid,'Printing of sequence
components\n'); fprintf(fid,'Post Fault Voltages\n\n');
fprintf(fid,'Bus-number Pos-seq-Magnitude Pos-seq-Angle(radians) Neg-seq-Magnitude
Neg-seq- Angle(radians) Zero-seq-Magnitude Zero-seq-Angle(radians)\n');
for i=1:length(vpf)
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',i,abs(vpf(i)),vpf_phase(i),abs(vnf(i)),vnf_phase(i),abs(vzf(i)),angle(vzf(i))); end

fprintf(fid,'\n\n');
fprintf(fid,'Post fault flows\n\n');
fprintf(fid,'The extra bus represents the reference bus\n');
fprintf(fid,'Start-bus End-bus pos-Magnitude pos-Phase neg-mag neg-phase zero-mag
zero-phase\n'); fprintf(fid,'This represents the I to J flows\n\n');
for i=1:length(posflowp)
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',start_bus(i),end_bus(i),abs(posflowp(i)),angle(posflowp(i)),abs(posflown(i)),angle(posfl
own(i)),abs (posflowz(i)),angle(posflowz(i)));
end fprintf(fid,'\n\n');
fprintf(fid,'This represents the J to I flows\n\n');
fprintf(fid,'Start-bus End-bus pos-Magnitude pos-Phase neg-mag neg-phase zero-mag
zero-phase\n'); kk=find(end_bus==(nb+1));
for i=1:length(negflowp) test=find(kk==i);
if length(test)==0
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',end_bus(i),start_bus(i),abs(negflowp(i)),angle(negflowp(i)),abs(negflown(i)),angle(negfl
own(i)),ab s(negflowz(i)),angle(negflowz(i)));
end end
fprintf(fid,'\n\n');

fprintf(fid,'Printing of phase quantities\n');


fprintf(fid,'Bus-number A-Magnitude A-Angle(radians) B-Magnitude B-Angle(radians)
C-Magnitude C- Angle(radians)\n');
for i=1:length(vpf)
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',i,abs(va(i)),angle(va(i)),abs(vb(i)),angle(vb(i)),abs(vc(i)),angle(vc(i))); end
fprintf(fid,'\n\n');
fprintf(fid,'Post fault flows\n\n');
fprintf(fid,'The extra bus represents the reference bus\n');
fprintf(fid,'Start-bus End-bus A-Magnitude A-Phase B-mag B-phase C-mag C-phase\n');
fprintf(fid,'This represents the I to J flows\n\n');
for i=1:length(posflowp)
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',start_bus(i),end_bus(i),abs(posflowa(i)),angle(posflowa(i)),abs(posflowb(i)),angle(posfl
owb(i)),abs (posflowc(i)),angle(posflowc(i)));
end fprintf(fid,'\n\n');
fprintf(fid,'This represents the J to I flows\n\n');
fprintf(fid,'Start-bus End-bus A-Magnitude A-Phase B-mag B-phase C-mag C-phase\n');
kk=find(end_bus==(nb+1));
for i=1:length(negflowp) test=find(kk==i);
if length(test)==0
fprintf(fid,'%-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-10.3f %-
10.3f\n',end_bus(i),start_bus(i),abs(negflowa(i)),angle(negflowa(i)),abs(negflowb(i)),angle(negfl
owb(i)),ab s(negflowc(i)),angle(negflowc(i)));
end end
fprintf(fid,'\n\n'); fclose(fid);
done=1;

INPUT:
Line data
1.00 2.00 0.10 0.30 1 0
2.00 3.00 0.10 0.30 1 0
3.00 1.00 0.10 0.30 1 0

Generator data
4.00 0.20 0.05 0.03
5.00 0.20 0.05 0.03

Transformer data
4.00 0.20 0.05 0.03
5.00 0.20 0.05 0.03

OUTPUT:
The positive sequence Ybus

40.000 -10.000 -10.000 -20.000 0.000


-10.000 40.000 -10.000 0.000 -20.000

-10.000 -10.000 20.000 0.000 0.000

-20.000 0.000 0.000 25.000 0.000

0.000 -20.000 0.000 0.000 25.000

The positive sequence Zbus


0.140 0.110 0.125 0.112 0.088
0.110 0.140 0.125 0.088 0.112

0.125 0.125 0.175 0.100 0.100

0.112 0.088 0.100 0.129 0.071

0.088 0.112 0.100 0.071 0.129

The zero sequence Ybus

26.667 -3.333 -3.333 -20.000 0.000

-3.333 26.667 -3.333 0.000 0.000


-3.333 -3.333 6.667 0.000 0.000

-20.000 0.000 0.000 27.143 0.000

0.000 0.000 0.000 0.000 7.143

The zero sequence Zbus

0.108 0.022 0.065 0.080 -0.000


0.022 0.044 0.033 0.016 -0.000

0.065 0.033 0.199 0.048 -0.000

0.080 0.016 0.048 0.095 -0.000

-0.000 -0.000 -0.000 -0.000 0.140

Fault
current

Magnitude Angle(radians) I1

3.730 -1.571
I2
1.984 3.142
I0
1.746 3.142

Printing of sequence components Post Fault Voltages


Bus-number Pos-seq-Magnitude Pos-seq-Angle(radians) Neg-seq-Magnitude
Neg-seq-Angle(radians) Zero-seq-Magnitude Zero-seq-Angle(radians)
1.000 0.534 0.000 0.248 1.571 0.113 1.571
2.000 0.534 0.000 0.248 1.571 0.058 1.571
3.000 0.347 0.000 0.347 1.571 0.347 1.571
4.000 0.627 0.000 0.198 1.571 0.083 1.571
5.000 0.627 1.571 0.198 0.000 0.000 0.000

Post fault flows

The extra bus represents the reference bus


Start-bus End-bus pos-Magnitude pos-Phase neg-mag neg-phase zero-mag zero-phase

This represents the I to J flows

1.000 2.000 0.000 0.000 0.000 0.000 0.185 -0.000


2.000 3.000 1.865 -1.571 0.992 3.142 0.966 3.142
3.000 1.000 1.865 1.571 0.992 -0.000 0.780 -0.000
1.000 4.000 1.865 1.571 0.992 -0.000 0.595 3.142
2.000 5.000 1.865 1.571 0.992 -0.000 0.000 0.000
4.000 6.000 1.865 1.571 0.992 -0.000 0.595 -0.000
5.000 6.000 1.865 1.571 0.992 -0.000 0.000 0.000

This represents the J to I flows

Start-bus End-bus pos-Magnitude pos-Phase neg-mag neg-phase zero-mag zero-phase


2.000 1.000 0.000 0.000 0.000 0.000 0.185 3.142
3.000 2.000 1.865 1.571 0.992 -0.000 0.966 -0.000
1.000 3.000 1.865 -1.571 0.992 3.142 0.780 3.142
4.000 1.000 1.865 -1.571 0.992 3.142 0.595 -0.000
5.000 2.000 1.865 -1.571 0.992 3.142 0.000 0.000

Printing of phase quantities


Bus-number A-Magnitude A-Angle(radians) B-Magnitude B-Angle(radians) C-Magnitude C-
Angle(radians)
1.000 0.644 0.595 0.675 -2.365 0.454 1.686
2.000 0.615 0.520 0.715 -2.310 0.399 1.702
3.000 0.776 1.107 0.491 -2.880 0.491 1.309
4.000 0.687 0.422 0.740 -2.286 0.546 1.833
5.000 0.658 0.306 0.805 -2.218 0.466 1.880
Post fault flows

The extra bus represents the reference bus


Start-bus End-bus A-Magnitude A-Phase B-mag B-phase C-mag C-phase This represents the I to
J flows

1.000 2.000 0.185 -0.000 0.185 -0.000 0.185 -0.000


2.000 3.000 2.704 -2.380 2.086 3.106 2.127 1.002
3.000 1.000 2.573 0.811 1.901 -0.039 2.232 -2.210
1.000 4.000 1.907 1.361 0.529 -0.139 3.246 -2.557
2.000 5.000 2.113 1.082 1.122 -0.065 2.769 -2.438
4.000 6.000 2.449 0.866 1.716 -0.043 2.347 -2.273
5.000 6.000 2.113 1.082 1.122 -0.065 2.769 -2.438

This represents the J to I flows

Start-bus End-bus A-Magnitude A-Phase B-mag B-phase C-mag C-phase

2.000 1.000 0.185 3.142 0.185 3.142 0.185 3.142


3.000 2.000 2.704 0.761 2.086 -0.035 2.127 -2.140
1.000 3.000 2.573 -2.331 1.901 3.103 2.232 0.932
4.000 1.000 1.907 -1.780 0.529 3.002 3.246 0.585
5.000 2.000 2.113 -2.060 1.122 3.076 2.769 0.704

RESULT: Symmetrical fault analysis in a given power system network was successfully carried
out using MATLAB.
Experiment-7
Siddartha Aggarwal (2K20/EE/266)

Aim: To compute the optimal dispatch in the three-generator system given below
(a) without and (b) with transmission losses.

Theory:

The fuel cost functions of the above three generators are given as : C1 = 0.001562Pg 2 + 7.92Pg +
561
C2 = 0.00194Pg 2 + 7.85Pg + 310 C3 = 0.00482Pg 2 + 7.92Pg + 78
The total active power demand is 850 MW. The transmission losses are given as
Ploss= 0.00003Pg12+0.00009Pg 2+ 0.00012Pg 2 MW

(a.) Without transmission loss, the optimal dispatch rule is given as:
(b.) With transmission losses, the optimal dispatch rule is given as:

The above equation pertains to generating unit 1. Similarly, the equations for generating units 2
and 3 can be written. After putting all three equations in the matrix form, we get,

It can be observed that with transmission losses included, the above equations have become
nonlinear, unlike in part (a). They have now to be iteratively solved after assuming an initial
value of This method is known as the lambda iteration method. As a good approximation, wecan
choose the value of which was obtained in part (a) i.e., without transmission losses.

MATLAB Code:
clc clear all
a=[0.003124 0 0 -1;
0 .00388 0 -1;
0 0 .00964 -1;
1 1 1 0];
pd=850; % pd stands for total demand b=[ -7.92; -7.85; -7.97; pd];

x=inv(a)*b;
fprintf("Without transmission losses (Pg1,Pg2,Pg3):\n"); disp( x(1:3,1) );
fprintf("Incremental cost = %0.4f \n\n", x(4,1));

lam=x(end); % lam stands for lambda err=100;


t=0;
while err>=1e-1 a1=[0.003124+0.00006*lam 0 0;
0 0.00388+0.00018*lam 0;
0 0 0.00964+0.00024*lam];
b1=[ lam-7.92; lam-7.85; lam-7.97]; pg=inv(a1)*b1;
ploss= 0.00003*(pg(1)^2) + 0.00009*(pg(2)^2) + 0.00012*(pg(3)^2);
if sum(pg)-ploss-pd>=0 lam=lam-0.01;
else
lam=lam+0.01; end
err=abs(sum(pg)-ploss-pd); t=t+1;
end
fprintf("WIth transmission Losses (Pg1,Pg2,Pg3):\n"); disp(pg);
fprintf("\n");

Output:

Conclusion
To compute the optimal dispatch in the three-generator system given below (a) without and (b)
with transmission losses.
Experiment-8
Siddartha Aggarwal (2K20/EE/266)

Aim: To determine the critical clearing time for the single machine infinite
bus system given below.

The above system comprises a thermal generating station consisting of four 555 MVA, 24 kV, 60
Hz generating units supplying power to an infinite bus through two transmission circuits. All
network reactances shown in the diagram are in per unit on 2220 MVA, 24 kV base.

The initial system operating condition is: P = 0.9 p.u, Q = 0.436,

|Et| = 1.0 p.u.


Generators are modeled as a single equivalent generator represented by the
classical model with parameters as:

x, = 0.3 p.u, H = 3.5 secs.

Circuit 2 (transmission line 2) experiences a bolted three-phase fault at ‘F’,


with the fault cleared by isolating Circuit 2.

Theory:

The dynamic equation relating the rotor angle and the accelerating power is given
by the nonlinear differential equation (swing equation) given as

For numerical solution of the swing equation, the above second order
differential equation is resolved into two first order differential
equations given as

At x=x0, t=t0 we can represent the curve representing the true solution by its tangent having slope
of

Therefore the value of x at t = t1=t0+Δt is given by,


After using the Euler technique for determining x=x1 corresponding to t=t1, we can take
another short step Δt and determine x2 corresponding to t2 = t1+Δt as follows

By applying the technique successively, values of x can be determined corresponding to


different values of t.

System modeling:

With the generator represented by the classical model, the system equivalent is given as

The reduced equivalent circuits representing the three system conditions (i) prefault (before
fault), (ii) during fault and (iii) postfault are shown below in diagrams (a), (b)and (c),
respectively.
Thus, we will be solving the two first order simultaneous differential equations
again given below

during the two time periods (i) t0 to t0+tc and (ii) t0+tc to tf where ‘t0’ is the time instant at
which the fault occurs, ‘tc’ is the time at which the fault is cleared (clearing time) and ‘tf’ is
the final time instant up to which we are interested in finding the solution.

MATLAB Code:
clc clear all p=0.9;
q=0.436;
et=1; pm=p;
xddash=0.3; xt=0.15; xl1=0.5; xl2=0.93;
xleq=(xl1*xl2)/(xl1+xl2); alpha=160*pi/180; i1=(p-1i*q)/conj(et); eb=et-1i*(xt+xleq)*i1;
edash=et+1i*xddash*i1;

% pre fault analysis

del0=angle(edash)-angle(eb); % intial or prefault value of delta xlbf=xleq+xt+xddash;

xldf=xt+xddash; xlaf=xl1+xt+xddash; tc=0.088;

t0=1;

tfinal=5; delt=0.001; n0=t0/delt;


arr1=linspace(t0,t0+tc,100); n1=length(arr1); delt1=tc/100;
arr2=linspace(t0+tc,tfinal,10000); n2=length(arr2);delt2=(tfinal-t0-tc)/10000;
%initial conditions for differential equations del1(1)=del0;
delw1(1)=0; H=3.5;

for i=1:n1-1 del1(i+1)=del1(i)+377*delw1(i)*delt1; delw1(i+1)=delw1(i)+(pm/(2*H))*delt1;


% during fault from t0 to t0+tc, pe=0 end del2(1)=del1(end);delw2(1)=delw1(end); for
i=1:n2-1 del2(i+1)=del2(i)+377*delw2(i)*delt2;

peaf=abs(edash)*abs(eb)*sin(del2(i+1))/xlaf; delw2(i+1)=delw2(i)+((pm-peaf)/(2*H))*delt2;
% during fault from t0+tc to t0+tc+5, pe≠0 end

delin=del0*ones(1,n0); del=[delin del1 del2]*180/pi;

t1=(0:n0-1)*delt;t2=t1(end)+(0:n1-1)*delt1; t3=t2(end)+(0:n2-1)*delt2;
t=[t1 t2 t3]; plot(t,del),grid xlabel('time') ylabel('del')
Output:

Conclusion
We determined the critical clearing time for the single machine infinite bus system given
to us.

You might also like