clear
format short
tole=1E-9;
deg=180/pi;
rad=1/deg;
A=1.5; Sbs=100; Ubs=220; Zbs=Ubs^2/Sbs;
r=0.0529; x=0.529; bc=3.3081*1E-6;
L12=150; L13=170; L23=180; L34=100; L45=200; L56=100;
nbus=6;
U1=220/Ubs; the1=(-1.5)*rad; PL1=50/Sbs; QL1=5/Sbs;
PG2=100/Sbs; QG2=10/Sbs; PL2=(200+0.1*A)/Sbs; QL2=20/Sbs;
PG3=150/Sbs; QG3=15/Sbs; PL3=300/Sbs; QL3=30/Sbs;
PG4=0/Sbs; QG4=0/Sbs; PL4=0/Sbs; QL4=0/Sbs;
Qsh4 = (25-(0.2*A)); Qsh4_pu = j*Qsh4*Zbs/Ubs^2;
PG5=200/Sbs; QG5=20/Sbs; PL5=350/Sbs; QL5=(40-0.1*A)/Sbs;
PG6=200/Sbs; QG6=10/Sbs; PL6=100/Sbs; QL6=20/Sbs;
Z12=(r+j*x)*L12/Zbs; Y12=(L12*(j*bc))*Zbs/2;
Z13=(r+j*x)*L13/Zbs; Y13=(L13*(j*bc))*Zbs/2;
Z23=(r+j*x)*L23/Zbs; Y23=(L23*(j*bc))*Zbs/2;
Z34=(r+j*x)*L34/Zbs; Y34=(L34*(j*bc))*Zbs/2;
Z45=(r+j*x)*L45/Zbs; Y45=(L45*(j*bc))*Zbs/2;
Z56=(r+j*x)*L56/Zbs; Y56=(L56*(j*bc))*Zbs/2;
Z21=Z12; Y21=Y12;
Z31=Z13; Y31=Y13;
Z32=Z23; Y32=Y23;
Z43=Z34; Y43=Y34;
Z54=Z45; Y54=Y45;
Z65=Z56; Y65=Y56;
y11=(1/Z12)+Y12+(1/Z13)+Y13;
y22=(1/Z12)+Y12+(1/Z23)+Y23;
y33=(1/Z13)+Y13+(1/Z23)+Y23+(1/Z34)+Y34;
y44=(1/Z34)+Y34+(1/Z45)+Y45+Qsh4_pu;
y55=(1/Z45)+(1/Z56)+Y45+Y56;
y66=(1/Z56)+Y56;
y12=-1/Z12; y13=-1/Z13; y14=0; y15=0; y16=0;
y21=y12; y23=-1/Z23; y24=0; y25=0; y26=0;
y31=y13; y32=Y23; y34=-1/Z34; y35=0; y36=0;
y41=y14; y42=y24; y43=y34; y45=-1/Z45; y46=0;
y51=y15; y52=y25; y53=y35; y54=y45; y56=-1/Z56;
y61=y16; y62=y26; y63=y36; y64=y46; y65=y56;
Y=[ y11 y12 y13 y14 y15 y16
y21 y22 y23 y24 y25 y26
y31 y32 y33 y34 y35 y36
y41 y42 y43 y44 y45 y46
y51 y52 y53 y54 y55 y56
y61 y62 y63 y64 y65 y66 ];
G=real(Y); B=imag(Y);
PGD2=PG2-PL2; QGD2=QG2-QL2;
PGD3=PG3-PL3; QGD3=QG3-QL3;
PGD4=PG4-PL4; QGD4=QG4-QL4;
PGD5=PG5-PL5; QGD5=QG5-QL5;
PGD6=PG6-PL6; QGD6=QG6-QL6;
PGD=[PGD2; PGD3; PGD4; PGD5; PGD6];
QGD=[QGD2; QGD3; QGD4; QGD5; QGD6];
X0 = [0 0 0 0 0 1 1 1 1 1]';
s_z = size(X0);
nx = s_z(1,1);
options_solve = optimset('Display', 'off', 'TolX', tole, 'TolFun', tole);
PAR = [nx ; nbus ; U1 ; the1];
[X_X, FVAL, EXITFLAG, OUTPUT] = fsolve('solve_LF_A18', X0, options_solve, G, B, PGD, QGD, PAR);
if EXITFLAG~=1,
disp('No Solution'),
EXITFLAG=EXITFLAG,
return
end,
the=[the1 X_X(1) X_X(2) X_X(3) X_X(4) X_X(5)];
U=[U1 X_X(6) X_X(7) X_X(8) X_X(9) X_X(10)];
the_deg=the*deg;
U_kv=U*Ubs;
g=-G; b=-B;
P12=g(1,2)*U(1)^2-U(1)*U(2)*(g(1,2)*cos(the(1)-the(2))+b(1,2)*sin(the(1)-the(2)));
P13=g(1,3)*U(1)^2-U(1)*U(3)*(g(1,3)*cos(the(1)-the(3))+b(1,3)*sin(the(1)-the(3)));
Q12=(-abs(Y12)-b(1,2))*U(1)^2-U(1)*U(2)*(g(1,2)*sin(the(1)-the(2))-b(1,2)*cos(the(1)-the(2)));
Q13=(-abs(Y13)-b(1,3))*U(1)^2-U(1)*U(3)*(g(1,3)*sin(the(1)-the(3))-b(1,3)*cos(the(1)-the(3)));
P1=P12+P13;
Q1=Q12+Q13;
PG1=P12+P13+PL1;
QG1=Q12+Q13+QL1;
PG1_MW=PG1*Sbs;
QG1_MVAr=QG1*Sbs;
P21=g(2,1)*volt(2)^2-volt(2)*volt(1)*(g(2,1)*cos(ang(2)-ang(1))+b(2,1)*sin(ang(2)-ang(1)));
P23=g(2,3)*volt(2)^2-volt(2)*volt(3)*(g(2,3)*cos(ang(2)-ang(3))+b(2,3)*sin(ang(2)-ang(3)));
Q21=(-abs(Y21)-b(2,1))*volt(2)^2-volt(2)*volt(1)*(g(2,1)*sin(ang(2)-ang(1))-b(2,1)*cos(ang(2)-ang(1)));
Q23=(-abs(Y23)-b(2,3))*volt(2)^2-volt(2)*volt(3)*(g(2,3)*sin(ang(2)-ang(3))-b(2,3)*cos(ang(2)-ang(3)));
PG2=P21+P23+PL2;
QG2=Q21+Q23+QL2;
P31=g(3,1)*volt(3)^2-volt(3)*volt(1)*(g(3,1)*cos(ang(3)-ang(1))+b(3,1)*sin(ang(3)-ang(1)));
P32=g(3,2)*volt(3)^2-volt(3)*volt(2)*(g(3,2)*cos(ang(3)-ang(2))+b(3,2)*sin(ang(3)-ang(2)));
P34=g(3,4)*U(3)^2-U(3)*U(4)*(g(3,4)*cos(the(3)-the(4))+b(3,4)*sin(the(3)-the(4)));
Q31=(-abs(Y31)-b(3,1))*volt(3)^2-volt(3)*volt(1)*(g(3,1)*sin(ang(3)-ang(1))-b(3,1)*cos(ang(3)-ang(1)));
Q32=(-abs(Y32)-b(3,2))*volt(3)^2-volt(3)*volt(2)*(g(3,2)*sin(ang(3)-ang(2))-b(3,2)*cos(ang(3)-ang(2)));
Q34=(-abs(Y34)-b(3,4))*volt(3)^2-volt(3)*volt(4)*(g(3,4)*sin(ang(3)-ang(4))-b(3,4)*cos(ang(3)-ang(4)));
PG3=P31+P32+P34+PL3;
QG3=Q31+Q32+Q34+QL3;
P43=g(4,3)*volt(4)^2-volt(4)*volt(3)*(g(4,3)*cos(ang(4)-ang(3))+b(4,3)*sin(ang(4)-ang(3)));
P45=g(4,5)*volt(4)^2-volt(4)*volt(5)*(g(4,5)*cos(ang(4)-ang(5))+b(4,5)*sin(ang(4)-ang(5)));
Q43=(-abs(Y43)-b(4,3))*volt(4)^2-volt(4)*volt(3)*(g(4,3)*sin(ang(4)-ang(3))-b(4,3)*cos(ang(4)-ang(3)));
Q45=(-abs(Y45)-b(4,5))*volt(4)^2-volt(4)*volt(5)*(g(4,5)*sin(ang(4)-ang(5))-b(4,5)*cos(ang(4)-ang(5)));
PG4=P43+P45+PL4;
QG4=Q43+Q45+QL4;
P54=g(5,4)*volt(5)^2-volt(5)*volt(4)*(g(5,4)*cos(ang(5)-ang(4))+b(5,4)*sin(ang(5)-ang(4)));
P56=g(5,6)*volt(5)^2-volt(5)*volt(6)*(g(5,6)*cos(ang(5)-ang(6))+b(5,6)*sin(ang(5)-ang(6)));
Q54=(-abs(Y54)-b(5,4))*volt(5)^2-volt(5)*volt(4)*(g(5,4)*sin(ang(5)-ang(4))-b(5,4)*cos(ang(5)-ang(4)));
Q56=(-abs(Y56)-b(5,6))*volt(5)^2-volt(5)*volt(6)*(g(5,6)*sin(ang(5)-ang(6))-b(5,6)*cos(ang(5)-ang(6)));
PG5=P54+P56+PL5;
QG5=Q54+Q56+QL5;
P65=g(6,5)*volt(6)^2-volt(6)*volt(5)*(g(6,5)*cos(ang(6)-ang(5))+b(6,5)*sin(ang(6)-ang(5)));
Q65=(-abs(Y65)-b(6,5))*volt(6)^2-volt(6)*volt(5)*(g(6,5)*sin(ang(6)-ang(5))-b(6,5)*cos(ang(6)-ang(5)));
PG6=P65+PL6;
QG6=Q65+QL6;
Ploss_tot=(PG1+PG2+PG3+PG4+PG5+PG6)-(PL1+PL2+PL3+PL4+PL5+PL6);
Ploss_tot_MW=Ploss_tot*Sbs
Ploss_sys1=(PG1+PG2+PG3)-(PL1+PL2+PL3+P34);
Ploss_sys1_MW=Ploss_sys1*Sbs
Ploss_sys2=(PG4+PG5+PG6+P34)-(PL4+PL5+PL6);
Ploss_sys2_MW=Ploss_sys2*Sbs
function [g_x] = solve_LF_A18(X,G,B,PGD,QGD,PAR);
nx=PAR(1); nbus=PAR(2); U1=PAR(3); the1=PAR(4);
PGD2=PGD(1); PGD3=PGD(2); PGD4=PGD(3); PGD5=PGD(4); PGD6=PGD(5);
QGD2=QGD(1); QGD3=QGD(2); QGD4=QGD(3); QGD5=QGD(4); QGD6=QGD(5);
the2=X(1); the3=X(2); the4=X(3); the5=X(4); the6=X(5);
U2=X(6); U3=X(7); U4=X(8); U5=X(9); U6=X(10);
the = [the1 the2 the3 the4 the5 the6]';
U = [U1 U2 U3 U4 U5 U6]';
g_x=zeros(nx,1);
P2= U(2)*(U(1)*(G(2,1)*cos(the(2)-the(1))+B(2,1)*sin(the(2)-the(1)))...
+U(3)*(G(2,3)*cos(the(2)-the(3))+B(2,3)*sin(the(2)-the(3)))...
+U(2)*(G(2,2)*cos(the(2)-the(2))+B(2,2)*sin(the(2)-the(2))));
P3= U(3)*(U(1)*(G(3,1)*cos(the(3)-the(1))+B(3,1)*sin(the(3)-the(1)))...
+U(2)*(G(3,2)*cos(the(3)-the(2))+B(3,2)*sin(the(3)-the(2)))...
+U(4)*(G(3,4)*cos(the(3)-the(4))+B(3,4)*sin(the(3)-the(4)))...
+U(3)*(G(3,3)*cos(the(3)-the(3))+B(3,3)*sin(the(3)-the(3))));
P4= U(4)*(U(3)*(G(4,3)*cos(the(4)-the(3))+B(4,3)*sin(the(4)-the(3)))...
+U(5)*(G(4,5)*cos(the(4)-the(5))+B(4,5)*sin(the(4)-the(5)))...
+U(4)*(G(4,4)*cos(the(4)-the(4))+B(4,4)*sin(the(4)-the(4))));
P5= U(5)*(U(4)*(G(5,4)*cos(the(5)-the(4))+B(5,4)*sin(the(5)-the(4)))...
+U(6)*(G(5,6)*cos(the(5)-the(6))+B(5,6)*sin(the(5)-the(6)))...
+U(5)*(G(5,5)*cos(the(5)-the(5))+B(5,5)*sin(the(5)-the(5))));
P6= U(6)*(U(5)*(G(6,5)*cos(the(6)-the(5))+B(6,5)*sin(the(6)-the(5)))...
+U(6)*(G(6,6)*cos(the(6)-the(6))+B(6,6)*sin(the(6)-the(6))));
Q2= U(2)*(U(1)*(G(2,1)*sin(the(2)-the(1))-B(2,1)*cos(the(2)-the(1)))...
+U(3)*(G(2,3)*sin(the(2)-the(3))-B(2,3)*cos(the(2)-the(3)))...
+U(2)*(G(2,2)*sin(the(2)-the(2))-B(2,2)*cos(the(2)-the(2))));
Q3= U(3)*(U(1)*(G(3,1)*sin(the(3)-the(1))-B(3,1)*cos(the(3)-the(1)))...
+U(2)*(G(3,2)*sin(the(3)-the(2))-B(3,2)*cos(the(3)-the(2)))...
+U(4)*(G(3,4)*sin(the(3)-the(4))-B(3,4)*cos(the(3)-the(4)))...
+U(3)*(G(3,3)*sin(the(3)-the(3))-B(3,3)*cos(the(3)-the(3))));
Q4= U(4)*(U(3)*(G(4,3)*sin(the(4)-the(3))-B(4,3)*cos(the(4)-the(3)))...
+U(5)*(G(4,5)*sin(the(4)-the(5))-B(4,5)*cos(the(4)-the(5)))...
+U(4)*(G(4,4)*sin(the(4)-the(4))-B(4,4)*cos(the(4)-the(4))));
Q5= U(5)*(U(4)*(G(5,4)*sin(the(5)-the(4))-B(5,4)*cos(the(5)-the(4)))...
+U(6)*(G(5,6)*sin(the(5)-the(6))-B(5,6)*cos(the(5)-the(6)))...
+U(5)*(G(5,5)*sin(the(5)-the(5))-B(5,5)*cos(the(5)-the(5))));
Q6= U(6)*(U(5)*(G(6,5)*sin(the(6)-the(5))-B(6,5)*cos(the(6)-the(5)))...
+U(6)*(G(6,6)*sin(the(6)-the(6))-B(6,6)*cos(the(6)-the(6))));
g_x(1) = P2-PGD2;
g_x(2) = P3-PGD3;
g_x(3) = P4-PGD4;
g_x(4) = P5-PGD5;
g_x(5) = P6-PGD6;
g_x(6) = Q2-QGD2;
g_x(7) = Q3-QGD3;
g_x(8) = Q4-QGD4;
g_x(9) = Q5-QGD5;
g_x(10) = Q6-QGD6;
end