Tic
Tic
clc;
%clear all;
n=14;
nline=20;
maxiter=100;
b=[2.324 -0.1601 0 0
0 0 0.478 -0.039
0 0 0.076 0.016
0 0 0 0
0 0.1824 0 0
0 0 0.295 0.166
0 0 0.090 0.058
0 0 0.035 0.018
0 0 0.061 0.016
0 0 0.135 0.058
0 0 0.149 0.05 ];
c=[2 1.06 0 10
2 1 0 10
2 1 0 10
1 1 0 10
1 1 0 10
2 1 0 10
1 1 0 10
2 1 0 10
1 1 0 10
1 1 0 10
1 1 0 10
1 1 0 10
1 1 0 10
1 1 0 10] ;
Melkie newton
% DESIGNED BY:
% CREATED: 16-JAN-2010
% SEMESTER # 7, EE(POWER).
format short g
disp (' TABLE 9.2 PAGE # 337 LINE DATA FOR EXAMPLE 9.2 ')
disp (' TABLE 9.3 PAGE # 338 BUS DATA FOR EXAMPLE 9.2 ')
% Last column shows Bus Type: 1.Slack Bus 2.PQ Bus 3.PV Bus
ss=i*linedata(:,8); % Y/2
y=linedata(:,5)+i*linedata(:,6);
ybus = zeros(totalbuses,totalbuses);
w=0;
u=0;
if busdata(n,2)>0
w=w+1;
end
end
if busdata(n,2)==0
u=u+1;
end
end
for b=1:totalbranches
ybus((linedata(b,1)),(linedata(b,2)))=-y(b);
ybus((linedata(b,2)),(linedata(b,1))) =ybus((linedata(b,1)),(linedata(b,2)));
end
for c=1:totalbuses
for d=1:totalbranches
if linedata(d,1) == c || linedata(d,2) == c
end
end
end
disp('TABLE 9.3 PAGE # 338 BUS ADMITTANCE MATRIX FOR EXAMPLE 9.2')
ybus
ybusR=zeros(totalbuses,totalbuses);
ybusA=zeros(totalbuses,totalbuses);
z=zeros(totalbuses,8);
busnumber=busdata(:,1);
PG=busdata(:,2);
QG=busdata(:,3);
PL=busdata(:,4);
QL=busdata(:,5);
V=busdata(:,6);
VV=V;
ANG=busdata(:,7);
type = busdata(:,8);
s=(2*totalbuses)-w-2;
MM=zeros(s,1);
AN=ANG;
tol=1;
iter=0;
kk=input('Enter the tolerance for iteration ');
%------------------------------Rect to Polar--------------------------------
for b=1:totalbranches
ybusR((linedata(b,1)),(linedata(b,2)))=abs(-y(b));
ybusR((linedata(b,2)),(linedata(b,1))) =ybusR((linedata(b,1)),(linedata(b,2)));
ybusR(b,b)=abs(-ybus(b,b));
% Angle of ybus
ybusA((linedata(b,1)),(linedata(b,2)))=angle(-y(b));
ybusA((linedata(b,2)),(linedata(b,1))) =ybusA((linedata(b,1)),(linedata(b,2)));
ybusA(b,b)=angle(-ybus(b,b));
end
ybusR;
ybusA;
for i = 2:totalbuses
YVV = 0;
for n = 1:totalbuses
if i~=n
end
YVV;
end
Pc(i) = ((abs(V(i))^2)*real(ybus(i,i)))+YVV; % Compute Calculated P.
deltaP(i)=P(i)-Pc(i);
end
Pc;
dp=deltaP';
for i=2:totalbuses
MM(i-1,1)=dp(i);
end
for i = 2:totalbuses-w
if busdata(i,8)== 2
YVV = 0;
for n = 1:totalbuses
if i~=n
end
YVV;
end
deltaQ(i)=Q(i)-Qc(i);
end
end
Qc;
dq=deltaQ';
for i=2:totalbuses-w
if busdata(i,8)== 2
MM(totalbuses-2+i,1)=dq(i);
else
i=i-1;
end
end
J11=zeros(totalbuses-1,totalbuses-1);
J12=zeros(totalbuses-1,u);
J21=zeros(u,totalbuses-1);
J22=zeros(u,u);
%----------------------------J11-------------------------------%
for i=2:totalbuses
for j=2:totalbuses
if i~=j
end
if i==j
for n=1:totalbuses
if n~=i
end
end
end
end
end
J11;
%----------------------------J12-------------------------------%
for i=2:totalbuses
for j=2:totalbuses-w
if busdata(j,8)== 2
if i~=j
end
yv12=0;
if i==j
for n=1:totalbuses
if n~=i
end
end
J12(i-1,j-1) = abs(V(i))*(2*abs(V(i))*real(ybus(i,i))+yv12);
end
end
end
end
J12;
%----------------------------J21-------------------------------%
for i=2:totalbuses-w
if busdata(i,8)== 2
for j=2:totalbuses
if i~=j
end
if i==j
for n=1:totalbuses
if n~=i
end
end
end
end
end
end
J21;
%----------------------------J22-------------------------------%
for i=2:totalbuses-w
for j=2:totalbuses-w
if i~=j
J22(i-1,j-1)= -(abs(V(j))*abs((ybusR(i,j)* V(i))))*sin((ybusA(i,j))+busdata(j,7)-busdata(i,7));
end
yv12=0;
if i==j
J22(i-1,j-1)=-(J11(i-1,i-1)) - (2*(abs(V(i))^2)*imag(ybus(i,i)));
end
end
end
end
J22;
%---------------------------------------------------------------------------------%
for k=1:totalbuses-1
for l=1:totalbuses-1
J(k,l)=J11(k,l);
end
end
for k=1:u
for l=1:totalbuses-1
J(l,k+totalbuses-1)=J12(l,k);
end
end
for k=1:u
for l=1:totalbuses-1
J(k+totalbuses-1,l)=J21(k,l);
end
end
for k=1:u
for l=1:u
J(l+totalbuses-1,k+totalbuses-1)=J22(l,k);
end
end
MM;
J;
format short g
k=inv(J)*MM;
for n=1:totalbuses-1
S(n)=(ANG(n+1)+k(n));
sa=radtodeg(S);
%-------------
busdata(n+1,7)=S(n);
%-------------
end
ANG=busdata(:,7);
sa;
for m=2:u+1
V(m)=abs(busdata(m,6)+k(totalbuses-2+m));
%abs(VV)
busdata(m,6)=V(m);
end
V;
iter=iter+1;
VV=V;
AN=ANG;
end
iter
%-------------------------DISPLAYING OUTPUTS---------------------------%
%real(VACC')
z(1:totalbuses,1)=busdata(:,1);
z(1:totalbuses,2)=busdata(:,8);
z(1:totalbuses,3)=abs(busdata(:,6));
z(1:totalbuses,4)=radtodeg(busdata(:,7));
z(1:totalbuses,5)=PG;
z(1:totalbuses,7)=busdata(:,4);
z(1:totalbuses,8)=busdata(:,5);
disp(' |Bus No.| |Bus Type| |Voltage| |Angle| |MW(G)| |Mvar(G)| |MW(L)| |
Mvar(L)| ');
format short g
melkie newton
% function [R]=NR()
% global a;
% global b;
% global c;
% global d;
b=[2.324 -0.1601 0 0 0
0 0 0.478 -0.039 0
0 0 0.076 0.016 0
0 0 0 0 0
0 0.1824 0 0 0
0 0 0.090 0.058 0
0 0 0.035 0.018 0
0 0 0.061 0.016 0
0 0 0.135 0.058 0
c=[2 1.06 -2 20
2 1 -6 25
2 1 -25 10
1 1 0 10
1 1 0 10
2 1 0 10
1 1 0 10
2 1 0 10
1 1 0 10
1 1 0 10
1 1 0 10
1 1 0 10
1 1 0 10
1 1 0 10] ;
op=fopen('NROP.m','w++')
n=14;
nline=20;
itermax=100;
nslack=1;
epsilon=0.0001;
lp=a(:,1);
lq=a(:,2);
r=a(:,3);
x=a(:,4);
ycp=a(:,5);
ycp=complex(0,ycp);
ycq=a(:,6);
ycq=complex(0,ycq);
tap=a(:,7);
pgen=b(:,1);
qgen=b(:,2);
pload=b(:,3);
qload=b(:,4);
itype=c(:,1);
ntype=itype;
vsp=c(:,2);
qmin=c(:,3);
qmax=c(:,4);
%yshunt=d(1,[1:n]);
%yshunt=complex(0,yshunt);
for k=1:nline
yline(k)=1/(complex(r(k),x(k)));
if (tap(k)==1)
t1=1-(1/tap(k));
t2=-t1/tap(k);
ycp(k)=t2*yline(k);
ycq(k)=t1*yline(k);
yline(k)=yline(k)/tap(k);
end
end
for i=1:n
nlcont(i)=0;
end
for k=1:nline
p=lp(k);
q=lq(k);
nlcont(p)=nlcont(p)+1;
nlcont(q)=nlcont(q)+1;
end
ITAGF(1)=1;
ITAGTO(1)=nlcont(1);
for i=2:n
ITAGF(i)=ITAGTO(i-1)+1;
ITAGTO(i)=ITAGTO(i-1)+nlcont(i);
end
for i=1:n
Ypp(i)=0;
end
for k=1:nline
p=lp(k);
q=lq(k);
Ypp(p)=Ypp(p)+yline(k)+ycp(k);
Ypp(q)=Ypp(q)+yline(k)+ycq(k);
end
%for i=1:n
%Ypp(i)=Ypp(i)+yshunt(i);
%end
for i=1:n
ICOUNT(i)=0;
end
for k=1:nline
p=lp(k);
q=lq(k);
lpq=ITAGF(p)+ICOUNT(p);
Ypq(lpq)=-yline(k);
ADJQ(lpq)=q;
ADJL(lpq)=k;
ICOUNT(p)=ICOUNT(p)+1;
lqp=ITAGF(q)+ICOUNT(q);
Ypq(lqp)=-yline(k);
ADJQ(lqp)=p;
ADJL(lqp)=k;
ICOUNT(q)=ICOUNT(q)+1;
end
for i=1:n
pshed(i)=pgen(i)-pload(i);
qshed(i)=qgen(i)-qload(i);
end
for i=1:n
qmininj(i)=qmin(i)-qload(i);
qmaxinj(i)=qmax(i)-qload(i);
end
for i=1:n
vmag(i)=vsp(i);
delta(i)=0;
E(i)=complex(vsp(i),0);
end
% start of iterations
for iter=0:itermax
delpmax=0;
delqmax=0;
for i=1:n
if(i~=nslack)
if(itype(i)==2)
e=real(E(i));
f=imag(E(i));
delnew=atan(f/e);
enew=vsp(i)*cos(delnew);
fnew=vsp(i)*sin(delnew);
Enew=complex(enew,fnew);
Inew=Ypp(i)*Enew;
jstart=ITAGF(i);
jstop=ITAGTO(i);
for j=jstart:jstop
q=ADJQ(j);
Inew=Inew+Ypq(j)*E(q);
end
Qnew=imag(Enew*conj(Inew));
qshed(i)=Qnew;
if(Qnew>qmaxinj(i))
qshed(i)=qmaxinj(i);
ntype(i)=1;
end
if(Qnew<qmininj(i))
qshed(i)=qmininj(i);
ntype(i)=1;
end
if(qshed(i)==Qnew)
E(i)=Enew;
ntype(i)=2;
end
end
end
sum=Ypp(i)*E(i);
jstart=ITAGF(i);
jstop=ITAGTO(i);
for j=jstart:jstop
q=ADJQ(j);
sum=sum+Ypq(j)*E(q);
s=E(i)*conj(sum);
pcal(i)=real(s);
qcal(i)=imag(s);
end
delp(i)=pshed(i)-pcal(i);
delq(i)=qshed(i)-qcal(i);
delp(nslack)=0;
delq(nslack)=0;
if(ntype(i)==2)
delq(i)=0;
end
if(abs(delp(i))>abs(delpmax))
delpmax=abs(delp(i));
end
if(abs(delq(i))>abs(delqmax))
delqmax=abs(delq(i));
end
end
if(abs(delpmax)<epsilon)
if(abs(delqmax)<epsilon)
break
end
end
for i=1:2*n
for j=1:2*n
A(i,j)=0;
end
end
for i=1:n
A(i,i)=A(i,i)-qcal(i)-imag(Ypp(i))*vmag(i)*vmag(i);
A(i+n,i+n)=A(i+n,i+n)+qcal(i)-imag(Ypp(i))*vmag(i)*vmag(i);
A(i+n,i)=A(i+n,i)+pcal(i)-real(Ypp(i))*vmag(i)*vmag(i);
A(i,i+n)=A(i,i+n)+pcal(i)+real(Ypp(i))*vmag(i)*vmag(i);
end
A(nslack,nslack)=10^20;
A(nslack+n,nslack+n)=10^20;
for i=1:n
ep=real(E(i));
fp=imag(E(i));
jstart=ITAGF(i);
jstop=ITAGTO(i);
for j=jstart:jstop
q=ADJQ(j);
eq=real(E(q));
fq=imag(E(q));
Gpq=real(Ypq(j));
Bpq=imag(Ypq(j));
term1=ep*eq+fp*fq;
term2=fp*eq-ep*fq;
A(i,q)=A(i,q)+Gpq*term2-Bpq*term1;
A(i+n,q+n)=A(i,q);
A(i,q+n)=A(i,q+n)+Gpq*term1+Bpq*term2;
A(i+n,q)=-A(i,q+n);
end
if(ntype(i)==2)
A(i+n,i+n)=10^20;
end
end
for i=1:n
delpq(i)=delp(i);
delpq(i+n)=delq(i);
end
B=delpq;
%GAUSS ELIMINATION
for i=1:2*n
Fact=1/A(i,i);
A(i,i)=1;
for j=i+1:2*n
A(i,j)=A(i,j)*Fact;
end
B(i)=B(i)*Fact;
for k=i+1:2*n
Fact=A(k,i);
A(k,i)=0;
for j=i+1:2*n
A(k,j)=A(k,j)-A(i,j)*Fact;
end
B(k)=B(k)-Fact*B(i);
end
end
x=B;
for i=(2*n)-1:-1:1
sum=0;
for j=i+1:2*n
sum=sum+A(i,j)*x(j);
end
x(i)=(B(i)-sum);
end
for i=1:n
delta(i)=delta(i)+x(i);
vmag(i)=vmag(i)+x(i+n)*vmag(i);
e(i)=vmag(i)*cos(delta(i));
f(i)=vmag(i)*sin(delta(i));
E(i)=complex(e(i),f(i));
end
end
for k=1:nline
p=lp(k);
q=lq(k);
Ipq=E(p)*ycp(k)+(E(p)-E(q))*yline(k);
spq(k)=E(p)*conj(Ipq);
Iqp=E(q)*ycq(k)+(E(q)-E(p))*yline(k);
sqp(k)=E(q)*conj(Iqp);
sloss(k)=spq(k)+sqp(k);
end
Sslack=0;
jstart=ITAGF(nslack);
jstop=ITAGTO(nslack);
for j=jstart:jstop
k=ADJL(j);
Sslack=Sslack+sloss(k);
end
pgen(nslack)=real(Sslack)+pload(nslack);
qgen(nslack)=imag(Sslack)+qload(nslack);
toc;
fprintf(op,'Magnitude of voltages\n');
fprintf(op,'%f\n',abs(E));
fprintf(op,'Line losses\n');
fprintf(op,'%f\n',sloss);
fprintf(op,'%f\n',real(Sslack));
fprintf(op,'%f\n',imag(Sslack));
melkie newton