0% found this document useful (0 votes)
19 views25 pages

Tic

matlab bi newton

Uploaded by

mezgebumelkamu8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views25 pages

Tic

matlab bi newton

Uploaded by

mezgebumelkamu8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

tic

clc;

%clear all;

n=14;

nline=20;

maxiter=100;

a=[1 2 0.019380 0.059170 0.026400 0.026400 1.000000

2 3 0.046990 0.197970 0.021900 0.021900 1.000000

2 4 0.058110 0.176320 0.018700 0.018700 1.000000

1 5 0.054030 0.223040 0.024600 0.024600 1.000000

2 5 0.056950 0.173880 0.017000 0.017000 1.000000

3 4 0.067010 0.171030 0.017300 0.017300 1.000000

4 5 0.013350 0.042110 0.006400 0.006400 1.000000

5 6 0.000000 0.252020 0.000000 0.000000 0.932000

4 7 0.000000 0.209120 0.000000 0.000000 0.978000

7 8 0.000000 0.176150 0.000000 0.000000 1.000000

4 9 0.000000 0.556180 0.000000 0.000000 0.969000

7 9 0.000000 0.110010 0.000000 0.000000 1.000000

9 10 0.031810 0.084500 0.000000 0.000000 1.000000

6 11 0.094980 0.198900 0.000000 0.000000 1.000000

6 12 0.122910 0.255810 0.000000 0.000000 1.000000

6 13 0.066150 0.130270 0.000000 0.000000 1.000000

9 14 0.127110 0.270380 0.000000 0.000000 1.000000

10 11 0.082050 0.192070 0.000000 0.000000 1.000000

12 13 0.220920 0.199880 0.000000 0.000000 1.000000

13 14 0.170930 0.348020 0.000000 0.000000 1.000000];


0 0 0 0 0 0 0 0 0.19 0 0 0 0 0

b=[2.324 -0.1601 0 0

0.4 0.4541 0.217 0.127

0 0.2528 0.942 0.19

0 0 0.478 -0.039

0 0 0.076 0.016

0 0.1362 0.112 0.075

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

% IMPLEMENTATION OF NEWTON RAPHSON METHOD IN MATLAB

% DESIGNED BY:

% HAFIZ KASHIF KHALEEL B-12588

% CREATED: 16-JAN-2010

% SEMESTER # 7, EE(POWER).

% SUBJECT: POWER SYSTEM OPERATION AND CONTROL.

% TEXT BOOK: POWER SYSTEM ANALYSIS BY WILLIAM D. STEVENSON, JR.

% CHAPTER # 9.3 - 9.4 (PAGE # 342 - 356).

% UNIVERSITY OF SOUTH ASIA. LAHORE. PAKISTAN.

%======================================================= DATA INPUT


=======================================================%

format short g

disp (' TABLE 9.2 PAGE # 337 LINE DATA FOR EXAMPLE 9.2 ')

linedata=[1 2 0.01008, 0.05040, 3.815629, -19.078144, 10.25, 0.05125;

1 3 0.00744, 0.03720, 5.169561, -25.847809, 7.75, 0.03875;

2 4 0.00744, 0.03720, 5.169561, -25.847809, 7.75, 0.03875;

3 4 0.01272, 0.06360, 3.023705, -15.118528, 12.75, 0.06375]

disp (' TABLE 9.3 PAGE # 338 BUS DATA FOR EXAMPLE 9.2 ')

busdata=[1 0, 0, 50, 30.99, 1.00, 0 1;

2 0, 0, 170, 105.35, 1.00, 0 2;

3 0, 0, 200, 123.94, 1.00, 0 2;


4 318, 0 , 80, 49.58, 1.02, 0 3]

% Last column shows Bus Type: 1.Slack Bus 2.PQ Bus 3.PV Bus

%=================================================== PROGRAM STARTS HERE


===================================================%

ss=i*linedata(:,8); % Y/2

y=linedata(:,5)+i*linedata(:,6);

totalbuses = max(max(linedata(:,1)),max(linedata(:,2))); % total buses

totalbranches = length(linedata(:,1)); % no. of branches

ybus = zeros(totalbuses,totalbuses);

w=0;

u=0;

for n=1:totalbuses % total no of PV busses

if busdata(n,2)>0

w=w+1;

end

end

for n=2:totalbuses % total no of PQ busses

if busdata(n,2)==0

u=u+1;

end

end

% ------ INITIALLIZING YBUS ------

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

ybus(c,c) = ybus(c,c) + y(d) + ss(d);

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;

J=zeros(s,s); % initiallizing empty jacobian matrix

MM=zeros(s,1);

AN=ANG;

P = (PG-PL)./100; % per unit active power at buses

Q = (QG-QL)./100; % per unit reactive power at buses

tol=1;

iter=0;
kk=input('Enter the tolerance for iteration ');

%------------------------------Rect to Polar--------------------------------

for b=1:totalbranches

% Real part of ybus

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;

while tol > kk

%------------------------ Delta P --------------------------------%

for i = 2:totalbuses

YVV = 0;

for n = 1:totalbuses

if i~=n

YVV = YVV + (ybusR(i,n)* V(n)*V(i))*cos((ybusA(i,n))+busdata(n,7)-busdata(i,7)); % multiplying


admittance & voltage

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

%------------------------ Delta Q -----------------------------%

for i = 2:totalbuses-w

if busdata(i,8)== 2

YVV = 0;

for n = 1:totalbuses

if i~=n

YVV = YVV + (ybusR(i,n)* V(n)*V(i))*sin((ybusA(i,n))+busdata(n,7)-busdata(i,7)); % multiplying


admittance & voltage

end

YVV;

end

Qc(i) = -((abs(V(i))^2)*imag(ybus(i,i)))-YVV; % Compute Calculated Q.

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

J11(i-1,j-1)= -abs(((ybusR(i,j)* V(i)*V(j)))*sin((ybusA(i,j))+busdata(j,7)-busdata(i,7)));

end

if i==j

for n=1:totalbuses

if n~=i

J11(i-1,i-1)=J11(i-1,i-1)+( abs(((ybusR(i,n)* V(i)*V(n)))*sin((ybusA(i,n))+busdata(n,7)-


busdata(i,7))));

end

end

end
end

end

J11;

%----------------------------J12-------------------------------%

for i=2:totalbuses

for j=2:totalbuses-w

if busdata(j,8)== 2

if i~=j

J12(i-1,j-1)= (abs(V(j))*abs((ybusR(i,j)* V(i))))*cos((ybusA(i,j))+busdata(j,7)-busdata(i,7));

end

yv12=0;

if i==j

for n=1:totalbuses

if n~=i

yv12=yv12 + abs((ybusR(i,n)* V(n)))*cos((ybusA(i,n))+busdata(n,7)-busdata(i,7));

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

J21(i-1,j-1)= abs(((ybusR(i,j)* V(i)*V(j)))*cos((ybusA(i,j))+busdata(j,7)-busdata(i,7)));

end

if i==j

for n=1:totalbuses

if n~=i

J21(i-1,j-1)=J21(i-1,j-1)+( -abs(((ybusR(i,n)* V(i)*V(n)))*cos((ybusA(i,n))+busdata(n,7)-


busdata(i,7))));

end

end

end

end

end

end

J21;

%----------------------------J22-------------------------------%

for i=2:totalbuses-w

for j=2:totalbuses-w

if busdata(i,8)== 2 && busdata(j,8)== 2

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;

tol=max(abs(abs(V) - abs(VV))) && max(abs(abs(ANG) - abs(AN)));

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;

a=[1 2 0.019380 0.059170 0.026400 0.026400 1.000000

2 3 0.046990 0.197970 0.021900 0.021900 1.000000

2 4 0.058110 0.176320 0.018700 0.018700 1.000000

1 5 0.054030 0.223040 0.024600 0.024600 1.000000

2 5 0.056950 0.173880 0.017000 0.017000 1.000000

3 4 0.067010 0.171030 0.017300 0.017300 1.000000

4 5 0.013350 0.042110 0.006400 0.006400 1.000000

5 6 0.000000 0.252020 0.000000 0.000000 0.932000

4 7 0.000000 0.209120 0.000000 0.000000 0.978000

7 8 0.000000 0.176150 0.000000 0.000000 1.000000

4 9 0.000000 0.556180 0.000000 0.000000 0.969000

7 9 0.000000 0.110010 0.000000 0.000000 1.000000

9 10 0.031810 0.084500 0.000000 0.000000 1.000000

6 11 0.094980 0.198900 0.000000 0.000000 1.000000

6 12 0.122910 0.255810 0.000000 0.000000 1.000000

6 13 0.066150 0.130270 0.000000 0.000000 1.000000

9 14 0.127110 0.270380 0.000000 0.000000 1.000000

10 11 0.082050 0.192070 0.000000 0.000000 1.000000

12 13 0.220920 0.199880 0.000000 0.000000 1.000000

13 14 0.170930 0.348020 0.000000 0.000000 1.000000];

b=[2.324 -0.1601 0 0 0

0.4 0.4541 0.217 0.127 0

0 0.2528 0.942 0.19 0

0 0 0.478 -0.039 0
0 0 0.076 0.016 0

0 0.1362 0.112 0.075 0

0 0 0 0 0

0 0.1824 0 0 0

0 0 0.295 0.166 0.19

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

0 0 0.149 0.05 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] ;

d=[0 0 0 0 0 0 0 0 0.19 0 0 0 0 0];

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

%formation of Ybus is over

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

%cal of injected power flows

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

%adjusted Limits setting

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

%adjusted Limits setting

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

%diagonal elements formation is over

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,'Slack Real power\n');

fprintf(op,'%f\n',real(Sslack));

fprintf(op,'Slack Reactive power\n');

fprintf(op,'%f\n',imag(Sslack));

melkie newton

You might also like