Professional Documents
Culture Documents
Power System
Power System
Rollno- 120EE0476
Q1. Write a MATLAB code to do load flow operation using Newton-Raphson. For the given data
below:
Feeder Data:
Transformer Data:
Solution:
%% General Information
no_bus = 9;
no_fd = 6;
no_tr = 3;
no_pv = 2;
Sb = 100;
%% Bus Data
bustype = [1 2 2 3 3 3 3 3 3]';
Vsp = [1 1.025 1.025 0 0 0 0 0 0]';
Pg = [0 163.0000 85.0000 0 0 0 0 0
0]'/Sb;
Qg = [0 0 0 0 0 0 0 0 0]'/Sb;
Pd = [0 0 0 0 125 90 0 100 0]'/Sb;
Qd = [ 0 0 0 0 50 30 0 35 0]'/Sb;
Bus = [1 2 3 4 5 6 7 8 9]';
slackbus = 1;
Busdata = table(Bus,Vsp,Pg,Qg,Pd,Qd,bustype);
%% Feeder Data
fd_frombus = [6 7 9 7 5 8]';
fd_tobus = [4 5 6 8 4 9]';
fd_ys = transpose(fd_ys);
tr_frombus = [2 4 3]';
tr_tobus = [7 1 9]';
tr_r = [0 0 0]';
tr_x = [0.0625 0.0576 0.0586]';
tr_alpha = [1.0 1.0204 1.0]';
TransformerBusdata = table(tr_frombus, tr_tobus, tr_r,tr_x,tr_alpha);
%% Generator Data
clear all
close all
clc
%%
%%
Y=zeros(no_bus,no_bus);
for k=1:no_fd
i=fd_frombus(k);
j=fd_tobus(k);
Y(i,i)=Y(i,i)+fd_y(k)+fd_ys(k)/2;
Y(i,j)=Y(i,j)-fd_y(k);
Y(j,i)=Y(i,j);
Y(j,j)=Y(j,j)+fd_y(k)+fd_ys(k)/2;
end
if no_tr>0
tr_z=complex(tr_r,tr_x);
tr_y=zeros(no_tr,1);
for i=1:no_tr
tr_y(i)=1/tr_z(i);
end
Y_tr=zeros(no_bus,no_bus);
for k=1:no_tr
i=tr_frombus(k);
j=tr_tobus(k);
Y_tr(i,i)=Y_tr(i,i)+tr_alpha(k)*conj(tr_alpha(k))*tr_y(k);
Y_tr(i,j)=Y_tr(i,j)-conj(tr_alpha(k))*tr_y(k);
Y_tr(j,i)=Y_tr(j,i)-tr_alpha(k)*tr_y(k);
Y_tr(j,j)=Y_tr(j,j)+tr_y(k);
end
Y=Y+Y_tr;
end
fprintf("\nY-bus matrix....\n");
disp(Y);
%%
%%Initialize voltage
V=zeros(no_bus,1);
Vm=zeros(no_bus,1);
delta=zeros(no_bus,1);
for i=1:no_bus
if bustype(i)==1 || bustype(i)==2
Vm(i)=Vsp(i);
else
Vm(i)=1;
end
end
Psp=Pg-Pd;
Qsp=Qg-Qd;
Pcal=zeros(no_bus,1);
Qcal=zeros(no_bus,1);
j=1;
for i=1:no_bus
if bustype(i) == 3
Npq(j) = i;
j = j + 1;
end
end
m=1;
for i=1:no_bus
if bustype(i)==2 || bustype(i)==3
Np(m)=i;
m=m+1;
end
end
delP=zeros(no_bus-1,1);
delQ=zeros(no_pq,1);
H=zeros(no_bus-1,no_bus-1);
N=zeros(no_bus-1,no_pq);
J=zeros(no_pq,no_bus-1);
L=zeros(no_pq,no_pq);
max_itr=10;
G=real(Y);
B=imag(Y);
for itr=1:max_itr
%% Calculate P
for i=1:no_bus
s=0;
for k=1:no_bus
s=s+Vm(i)*Vm(k)*(G(i,k)*cos(delta(i)-delta(k))
+B(i,k)*sin(delta(i)-delta(k)));
end
Pcal(i)=s;
end
%% Calculate Q
for i=1:no_bus
s=0;
for k=1:no_bus
s=s+Vm(i)*Vm(k)*(G(i,k)*sin(delta(i)-delta(k))-
B(i,k)*cos(delta(i)-delta(k)));
end
Qcal(i)=s;
end
%% Calculate delP
p=1;
for i=1:no_bus-1
j=Np(i);
delP(i)=Psp(j)-Pcal(j);
end
%% Calculate delQ
q=1;
for i=1:no_pq
j=Npq(i);
delQ(i)=Qsp(j)-Qcal(j);
end
%% H matrix
for m=1:no_bus-1
for n=1:no_bus-1
i=Np(m);
j=Np(n);
if i==j
H(m,n)=-Qcal(i)-Vm(i)*Vm(i)*B(i,i);
else
H(m,n)=Vm(i)*Vm(j)*(G(i,j)*sin(delta(i)-delta(j))-
B(i,j)*cos(delta(i)-delta(j)));
end
end
end
%% N Matrix
for m=1:no_bus-1
for n=1:no_pq
i=Np(m);
j=Npq(n);
if i==j
N(m,n)=Pcal(i)+Vm(i)*Vm(i)*G(i,i);
else
N(m,n)=Vm(i)*Vm(j)*(G(i,j)*cos(delta(i)-delta(j))
+B(i,j)*sin(delta(i)-delta(j)));
end
end
end
%% J matrix
for m=1:no_pq
for n=1:no_bus-1
i=Npq(m);
j=Np(n);
if i==j
J(m,n)=Pcal(i)-Vm(i)*Vm(i)*G(i,i);
else
J(m,n)=Vm(i)*Vm(j)*(-G(i,j)*cos(delta(i)-delta(j))-
B(i,j)*sin(delta(i)-delta(j)));
end
end
end
%% L matrix
for m=1:no_pq
for n=1:no_pq
i=Npq(m);
j=Npq(n);
if i==j
L(m,n)=Qcal(i)-Vm(i)*Vm(i)*B(i,i);
else
L(m,n)=Vm(i)*Vm(j)*(G(i,j)*sin(delta(i)-delta(j))-
B(i,j)*cos(delta(i)-delta(j)));
end
end
end
%% Jacobian matrix
Jacobian_matrix=[H N;J L];
z=[delP;delQ];
x=inv(Jacobian_matrix)*z;
%% update delta
del_delta=x(1:no_bus-1);
for i=1:no_bus-1
k = Np(i);
delta(k) = delta(k)+del_delta(i);
end
%% update voltage
del_V=x(no_bus-1+1:no_bus-1+no_pq);
for i=1:no_pq
k = Npq(i);
Vm(k) = Vm(k)+Vm(k)*del_V(i);
end
%% Calculating complex V
V=zeros(no_bus,1);
for i=1:no_bus
a=Vm(i)*cos(delta(i));
b=Vm(i)*sin(delta(i));
V(i)=complex(a,b);
end
%% convergence criteria
eps=10e-5;
if max(abs(z))<eps
fprintf("Load Flow converged in %d iterations\n",itr);
break;
end
end
delta_degree = delta*(180/pi);
%% Calculation of current injection
I_inj = Y*V;
%% Calculation of Power injection
%Apparent power injection
Si = V.*conj(I_inj);
% active Power injection
Pi = real(Si);
% reactive power injection
Qi = imag(Si);
% active power generation
Pg = Pi+Pd;
% reactive power generation
Qg = Qi+Qd;
activefd_S_from_to = real(fd_S(:,1));
reactivefd_S_from_to = imag(fd_S(:,1));
activefd_S_to_from = real(fd_S(:,2));
reactivefd_S_to_from = imag(fd_S(:,2));
activefd_loss=real(fd_loss);
reactivefd_loss=imag(fd_loss);
if no_tr>0
Feeder_code =[1;2;3;4;5;6];
fd_frombus = [6 ; 7 ; 9 ; 7 ; 5 ; 8];
fd_tobus = [4 ; 5 ; 6 ; 8 ; 4 ; 9];
disp('Feeder current and power (both ends) using obtained voltage')
Feeder_Data =
table(Feeder_code ,fd_frombus,fd_tobus,fd_I_from_to,angle_fd_from_to,fd_I_to_from,
angle_fd_to_from,activefd_S_from_to,reactivefd_S_from_to,activefd_S_to_from,reacti
vefd_S_to_from,activefd_loss,reactivefd_loss)
end
%% Transformer current calculation
if no_tr>0
tr_I=zeros(no_tr,2);
for k=1:no_tr
i=tr_frombus(k);
j=tr_tobus(k);
tr_I(k,1) = tr_alpha(k) *conj(tr_alpha(k))* tr_y(k) * V(i) -
conj(tr_alpha(k)) * V(j)*tr_y(k);
tr_I(k,2) = -tr_alpha(k) * tr_y(k) * V(i) + tr_y(k) * V(j);
end
end
%% Transformer power calculation
if no_tr>0
tr_S=zeros(no_tr,2);
for k=1:no_tr
i=tr_frombus(k);
j=tr_tobus(k);
tr_S(k,1) = V(i) * conj(tr_I(k,1));
tr_S(k,2) = V(j) * conj(tr_I(k,2));
end
end
%% Transformer loss calculation
if no_tr>0
tr_loss=zeros(no_tr,1);
for k=1:no_tr
i=tr_frombus(k);
j=tr_tobus(k);
tr_loss(k) = tr_S(k,1) + tr_S(k,2);
end
end
activetr_S_from_to = real(tr_S(:,1));
reactivetr_S_from_to = imag(tr_S(:,1));
activetr_S_to_from =real(tr_S(:,2));
reactivetr_S_to_from = imag(tr_S(:,2));
activetr_loss=real(tr_loss);
reactivetr_loss=imag(tr_loss);
transformer_code = [7;8;9];
tr_frombus = [2 ;4; 3];
tr_tobus = [7; 1; 9];
gen_I = zeros(no_bus,1);
Sd = Pd + 1i * Qd;
load_I = conj(Sd./V);
for k = 1:no_bus
if(bustype(k) ~= 3)
gen_I(k) = I_inj(k) + load_I(k);
end
end
gen_I = gen_I(gen_I ~= 0);
gen_I_injected = abs(gen_I);
angle_gen_I_inj= angle(gen_I)*(180/pi);
%% generator power
gen_loss = zeros(no_bus,1);
for k = 1:no_bus
if(bustype(k) ~= 3)
gen_loss(k) = V(k) * conj(gen_I(k));
end
end
gen_loss = gen_loss(gen_I~= 0);
Active_gen_injection=real(gen_loss);
Reactive_gen_injection=imag(gen_loss);
%% Total loss
if no_tr>0
total_loss = sum([load_loss ;gen_loss ;fd_loss(:); tr_loss(:)]);
else
total_loss = sum([load_loss ;gen_loss ;fd_loss(:)]);
end
%% Total power
total_power=sum(Si);
Output:
Y-Bus:
Feeder Data:
Transformer Data:
Generator Data: