Newton Raphson Matlab Code
Newton Raphson Matlab Code
clc
clear all;
fb=data(:,1);
tb=data(:,2);
r=data(:,3);
x=data(:,4);
y2=data(:,5);
z=r+(1i*x);
y=1./z;
nbus=max(max(fb),max(tb));
nbranch=length(fb);
Y=zeros(nbus,nbus);
pq=find(type==2);
npq=length(pq);
pv=find(type==3);
npv=length(pv);
nou=(2*npq)+npv;
for iter=1:n
V=Vb.*exp(1i*Del);
Pcal=zeros(nbus,1);
Qcal=zeros(nbus,1);
for k=2:nbus
soi=0;
for j=1:nbus;
if j~=k
soi=soi+(V(j,1)*Y(k,j));
end
soi;
if type(k)==2 || type(k)==3
Pcal(k,1) = real(conj(V(k,1))*(soi + Y(k,k)*V(k,1)));
end
if type(k)==2
Qcal(k,1) = -imag(conj(V(k,1))*(soi + Y(k,k)*V(k,1)));
end
end
end
Pcal;
Qcal;
for x=1:npq
y=pq(x);
QschM(x,1)=Qsch(y);
QcalM(x,1)=Qcal(y);
end
delP=Psch(2:nbus)-Pcal(2:nbus);
delQ=QschM-QcalM;
MM=[delP;delQ]; %Mismatch Matrix
%to find J1
J1=zeros(nbus-1);
for a=1:(nbus-1)
m=a+1;
for b=1:(nbus-1)
l=b+1;
if m==l
J1(a,b)=-Qcal(m)-imag(conj(V(m))*V(l)*Y(m,l));
else
J1(a,b)=imag(conj(V(m)*V(l)*Y(m,l)));
end
end
end
J1;
%to find J2
j2=zeros(nbus-1,nbus-1-npv);
for a=1:(nbus-1)
m=a+1;
for b=1:(nbus-1-npv)
l=pq(b);
jjj=0;
J2(a,b)=real(V(m)*V(l)*(Y(m,l)))/abs(V(l));
if l==m
J2(a,b)=(2*J2(a,b))+real(soi);
end
end
end
J2;
%to find J3
J3=zeros(nbus-1-npv,nbus-1);
for a=1:(nbus-1-npv)
m=pq(a);
for b=1:nbus-1
l=b+1;
if l==m
for l=1:nbus
J3(a,b)=J3(a,b)+real(conj(V(m))*V(l)*Y(m,l));
end
J3(a,b)=J3(a,b)-real(conj(V(m))*V(m)*Y(m,m));
else
J3(a,b)=-real(conj(V(m))*V(l)*Y(m,l));
end
end
end
J3;
%to find J4
J4=zeros(nbus-1-npv,nbus-1-npv);
for a=1:(nbus-1-npv)
m=pq(a);
for b=1:(nbus-1-npv)
l=pq(b);
if l==m
J4(a,b)=Qcal(m)-((abs(V(m)))^2)*imag(Y(m,m));
else
J4(a,b)=(-imag(conj(V(m))*V(l)*Y(m,l)))/V(l);
%(J1(m,l))/V(l);
end
end
end
J4;
J=[J1,J2;J3,J4];
IM=(inv(J))*MM; %Improvement Matrix
delV=zeros(nbus,1);
delD=zeros(nbus,1);
delD(2:nbus)=IM(1:(nbus-1));
for t=1:npq
r=pq(t);
delV(r)=IM((nbus-1)+t);
end
Vb=Vb+delV;
Del=Del+(delD.*(180/pi));
fprintf('iteration no. = ')
disp(iter)
disp('jacobian matrix = ')
disp(J)
disp('Mismatch Matrix = ')
disp(MM)
disp('Improvement Matrix = ')
disp(IM)
disp('bus voltages = ')
disp(Vb)
disp('voltage angle del (in degree) = ')
disp(Del)
end
OUTPUT OF NR METHOD :-
Y-bus matrix of given system =
8.9852 -44.8360i -3.8156 +19.0781i -5.1696 +25.8478i 0.0000 + 0.0000i
-3.8156 +19.0781i 8.9852 -44.8360i 0.0000 + 0.0000i -5.1696 +25.8478i
-5.1696 +25.8478i 0.0000 + 0.0000i 8.1933 -40.8638i -3.0237 +15.1185i
0.0000 + 0.0000i -5.1696 +25.8478i -3.0237 +15.1185i 8.1933 -40.8638i
jacobian matrix =
45.4429 0 -26.3648 9.7771 0
0 41.2687 -15.4209 0 8.1933
-26.3648 -15.4209 42.5147 -5.2730 -3.0842
-9.0886 0 5.2730 44.2290 0
0 -8.2537 3.0842 0 40.4590
bus voltages =
1.0000
0.9834
0.9710
1.0200
iteration no. = 2
jacobian matrix =
-14.9906 0 -18.7126 -57.2964 0
0 -24.7429 -15.2179 0 52.4889
-18.7126 -15.2179 42.5147 -18.9957 1.2937
-29.9292 0 12.6543 101.7131 0
0 -17.8524 -5.4912 0 101.7930
bus voltages =
1.0000
0.7630
-0.0428
1.0200