Vous êtes sur la page 1sur 8

%%

%ENTRADA DE DADOS DO PROBLEMA

x = 1+matricula;

%de/para/resistencia/reat�ncia/suscep.
dados_circuito = [1 2 0.04 0.20 0.02;
1 4 0.04 0.30 0.02;
1 5 0.06 0.30 0.03;
2 3 0.05 0.25 0.03;
2 4 0.02 0.10 0.01;
2 5 0.06 0.30 0.03;
2 6 0.04 0.30 0.02;
3 5 0.05 0.26 0.03;
3 6 0.02 0.10 0.01;
4 5 0.08 0.40 0.04;
5 6 0.06 0.30 0.03;]

% barra/tipo/v/theta/Pgerada/Qgerada/Pcarga/Qcarga
dados_barra = [1 1 x 0 1 0 0 0;
2 3 1 0 0 1 3.5 0;
3 2 x 0 5 0 0 0;
4 3 1 0 0 1 3 0];
% 1 ref, 2 pv, 3 carga
%%
[nlin ,coluna]=size(dados_circuito); % numero de linhas e colunas dos circuitos
swing = 1 ; %referencia
numBar = 4 ; %numero de barras
Sb = 100; %pot base
%%
Ind_PQ= sort([2 4]);
Ind_PV=sort([3]);
Ind_PVPQ = sort([Ind_PQ Ind_PV]) ;
nPQ = length(Ind_PQ) ;
nPV = length(Ind_PV) ;
nPVnPQ = length(Ind_PVPQ) ;
%%
Pg=dados_barra(:,5); % Potencia Ativa Gerada
Qg=dados_barra(:,6); % Reativo Gerado
Pl=dados_barra(:,7); % Carga
Ql=dados_barra(:,8); % Carga
%%
%inicializa��o de var
theta=zeros(numBar,1);
Ybus = zeros(numBar);
Bx=zeros(numBar);
P_c=zeros(numBar);
Q_c=zeros(numBar);
Pf=zeros(numBar);
Qf=zeros(numBar);
V=dados_barra(:,3);
tempV=zeros(numBar,1);
toler = 1e-3 ; % tolerancia
delta_PQ = 100;
kp=1; %desacoplado rapido
kq=1; %desacoplado rapido
inter1=0; %desacoplado rapido
inter2=0; %desacoplado rapido
newton=0;
desacoplado=0;
%%
%Constru��o da matriz Ybus
for i = 1 : nlin
kl = dados_circuito(i,1) ;
ml = dados_circuito(i,2) ;
r = dados_circuito(i,3) ;
x =dados_circuito(i,4) ;
bx = - 1/ x ;
Y = 1 / (r + (1i)*x) ;
Ybus(kl,kl) = Ybus(kl,kl) + Y;
Ybus(ml,ml) = Ybus(ml,ml) + Y;
Ybus(kl,ml) = Ybus(kl,ml) - Y ;
Ybus(ml,kl) = Ybus(ml,kl) - Y ;
Bx(kl,kl) = Bx(kl,kl) + bx ;
Bx(ml,ml) = Bx(ml,ml) + bx ;
Bx(kl,ml) = Bx(kl,ml) - bx ;
Bx(ml,kl) = Bx(ml,kl) - bx ;
end
G = real(Ybus) ;
B = imag(Ybus) ;

%newton desacoplado rapido


Hdr = - Bx(Ind_PVPQ,Ind_PVPQ) ; % desacoplado rapido
Ldr = - B(Ind_PQ , Ind_PQ); % desacoplado rapido
%%

%Primeiro metodo NEWTON RAPHSON COMPLETO


% zerar variaveis
theta_c(1:nPVnPQ,1) = theta(Ind_PVPQ) ;
V_c(1:nPQ,1) = V(Ind_PQ);
tempV=V(:,1);
V(Ind_PQ) = cos(theta(Ind_PQ)) ; % Valor inicial para tens�o n�o especificada.
theta_V(1:2*nPQ+nPV,1) = [theta(Ind_PVPQ)' V(Ind_PQ)']' ;% vetor com os angulos
theta e tens�o V.

while (max(abs(delta_PQ)) > toler) % enquanto o maximo valor em m�dulo for maior
que a toler�ncia
for k = 1 : numBar
for m = 1 : numBar
P_c(k,m) = V(m)*(G(k,m)*cos(theta(k)-theta(m)) + B(k,m)*sin(theta(k)-
theta(m))) ;
Q_c(k,m) = V(m)*(G(k,m)*sin(theta(k)-theta(m)) - B(k,m)*cos(theta(k)-
theta(m))) ;
end
Pcal(k) = V(k)*sum(P_c(k,:)) ;
delta_P(k) = [(Pg(k) - Pl(k)) - Pcal(k)];
Qcal(k) = V(k)*sum(Q_c(k,:)) ;
delta_Q(k) = (Qg(k) - Ql(k) - Qcal(k));
end
delta_Pc(1:nPQ+nPV,1) = delta_P(Ind_PVPQ) ;
delta_Qc(1:nPQ,1) = delta_Q(Ind_PQ) ;

delta_PQ = [delta_Pc' delta_Qc']';

for k = 1 : numBar % para k


for m = 1 : numBar % para k
Pcal(k,m) = V(m)*(G(k,m)*cos(theta(k)-theta(m)) + B(k,m)*sin(theta(k)-
theta(m))) ;
Qcal(k,m) = V(m)*(G(k,m)*sin(theta(k)-theta(m)) - B(k,m)*cos(theta(k)-
theta(m))) ;
H(k,m) = V(k)*V(m)*(G(k,m)*sin(theta(k)-theta(m)) -
B(k,m)*cos(theta(k)-theta(m))) ;
N(k,m) = V(k)*(G(k,m)*cos(theta(k)-theta(m)) + B(k,m)*sin(theta(k)-
theta(m)) ) ;
M(k,m) = -V(k)*V(m)*(G(k,m)*cos(theta(k)-theta(m)) +
B(k,m)*sin(theta(k)-theta(m))) ;
L(k,m) = V(k)*(G(k,m)*sin(theta(k)-theta(m)) - B(k,m)*cos(theta(k)-
theta(m))) ;
end
Pc(k) = V(k)*sum(Pcal(k,:)) ; % Pot�ncia Ativa calculada com
theta e V atualizados.
Qc(k) = V(k)*sum(Qcal(k,:)) ; % Pot�ncia Reativa calculada com
theta e V atualizados.
deltaP(k) = Pg(k) - Pl(k) - Pc(k) ; % Varia��o da Pot�ncia Ativa em
fun��o de V e theta.
deltaQ(k) = Qg(k) - Ql(k) - Qc(k) ; % Varia��o da Pot�ncia Reativa
em fun��o de V e theta.
H(k,k) = - Qc(k) - B(k,k)*V(k)^2 ;
N(k,k) = V(k) \ (Pc(k) + G(k,k)*V(k)^2 ) ;
M(k,k) = Pc(k) - G(k,k)*V(k)^2 ;
L(k,k) = V(k) \ (Qc(k) - B(k,k)*V(k)^2) ;
end
% Construindo Matriz Jacobina
J(1:nPQ+nPV,1:nPQ+nPV) = H(Ind_PVPQ , Ind_PVPQ) ;
J(1:nPQ+nPV,1+nPQ+nPV:2*nPQ+nPV) = N(Ind_PVPQ ,Ind_PQ) ;
J(1+nPQ+nPV:2*nPQ+nPV,1:nPQ+nPV) = M(Ind_PQ , Ind_PVPQ) ;
J(1+nPQ+nPV:2*nPQ+nPV,1+nPQ+nPV:2*nPQ+nPV) = L(Ind_PQ , Ind_PQ) ;
delta_PQ(1:2*nPQ+nPV,1) = [deltaP(Ind_PVPQ) deltaQ(Ind_PQ)]' ;
delta_thetaV = J \ delta_PQ ;
theta_V = theta_V + delta_thetaV ;% atualizando a matriz com a tens�o[pu] e
theta
theta(Ind_PVPQ) = theta_V(1:nPQ+nPV) ;
V(Ind_PQ) = theta_V(1+nPQ+nPV:2*nPQ+nPV) ;
newton=newton+1;
end

%calculo do fluxo de potencia


for k = 1 : numBar
for m = 1 : numBar
if (m~=k)
Pf(k,m) = -G(k,m)*V(k)^2 + G(k,m)*V(k)*V(m)*cos(theta(k)-theta(m)) +
B(k,m)*V(k)*V(m)*sin(theta(k)-theta(m));
Qf(k,m) = V(k)^2*(B(k,m)) - B(k,m)*V(k)*V(m)*cos(theta(k)-theta(m)) +
G(k,m)*V(k)*V(m)*sin(theta(k)-theta(m));
end
end
Qf(k,k)=sum(Qf(k,:));
Pf(k,k)=sum(Pf(k,:));
end
Sf=Pf+(1i)*Qf;
Qbarras=diag(Qf);
Sfluxo=abs(Sf*100);
carga_barra=dados_barra(:,7)+1i*dados_barra(:,8);
for i=1:4
SG(i)=Pf(i,i)+(1i)*Qf(i,i);
end
gerado=SG'+carga_barra;
fprintf('__________________________________________________________________________
_____________________________________________\n');
fprintf(' Primeiro m�todo:\n\n');
fprintf('***NEWTON COMPLETO***\n');
fprintf('Tens�o nas barras:\n');
disp(V);
fprintf('Angulo:\n');
disp(theta);
fprintf('Balan�o de Potencia:\n\n');
disp(SG);
fprintf('Fluxo de Potencia ativa:\n');
disp(Pf);
fprintf('Fluxo de Potencia reativa:\n');
disp(Qf);
fprintf('Fluxo de Potencia MVA:\n');
disp(Sfluxo);

%%
% segundo metodo: Newton Desacoplado
% zerar variaveis
delta_Pc(1:nPVnPQ,1) = toler + 1 ; % condi��o inicial arbitr�ria
para delta_P para iniciar o calculo.
delta_Qc(1:nPQ,1) = toler + 1 ; % condi��o inicial arbitr�ria
para delta_Q para iniciar o calculo.
theta=zeros(numBar,1);
theta_c(1:nPVnPQ,1) = theta(Ind_PVPQ) ;
V_c(1:nPQ,1) = V(Ind_PQ);
tempV=V(:,1);
V(Ind_PQ) = cos(theta(Ind_PQ)) ; % Valor inicial para tens�o n�o especificada.
theta_V(1:2*nPQ+nPV,1) = [theta(Ind_PVPQ)' V(Ind_PQ)']' ;% vetor com os angulos
theta e tens�o V.
while (max(abs(delta_Pc)) > toler)||(max(abs(delta_Qc)) > toler)
for k = 1 : numBar
for m = 1 : numBar
P_c(k,m) = V(m)*(G(k,m)*cos(theta(k)-theta(m)) +
B(k,m)*sin(theta(k)-theta(m))) ;
Hd(k,m) = V(k)*V(m)*(G(k,m)*sin(theta(k)-theta(m)) -
B(k,m)*cos(theta(k)-theta(m))) ;
Q_c(k,m) = V(m)*(G(k,m)*sin(theta(k)-theta(m)) -
B(k,m)*cos(theta(k)-theta(m))) ;
Ld(k,m) = V(k)*(G(k,m)*sin(theta(k)-theta(m)) -
B(k,m)*cos(theta(k)-theta(m))) ;

end
Hd(k,k) = - V(k)^2*B(k,k) - sum(Hd(k,:)) ;
Ld(k,k) = -V(k)*B(k,k) + sum(Ld(k,:)) ;
P_cal(k) = V(k)*sum(P_c(k,:)) ;
delta_P(k) = Pg(k) - Pl(k) - P_cal(k) ;
Q_cal(k) = V(k)*sum(Q_c(k,:)) ;
delta_Q(k) = Qg(k) - Ql(k) - Q_cal(k) ;

end
J1d(1:nPVnPQ,1:nPVnPQ) = - Hd(Ind_PVPQ , Ind_PVPQ) ;

delta_Pc(1:nPQ+nPV,1) = delta_P(Ind_PVPQ) ;
delta_theta = - inv(J1d)*delta_Pc ; % varia��o da pot�ncia ativa.(nas barras
PQ e PV)
theta_c = theta_c + delta_theta ;
theta(Ind_PVPQ) = theta_c(1:nPVnPQ) ;
J2d(1:nPQ,1:nPQ) = - Ld(Ind_PQ , Ind_PQ) ;

delta_Qc(1:nPQ,1) = delta_Q(Ind_PQ) ;
delta_V = - inv(J2d) * delta_Qc ;
V_c = V_c + delta_V ;
V(Ind_PQ) = V_c(1:nPQ) ;
desacoplado=desacoplado+1;

end
%fluxo de potencia
for k = 1 : numBar
for m = 1 : numBar
if (m~=k)
Pf(k,m) = -G(k,m)*V(k)^2 + G(k,m)*V(k)*V(m)*cos(theta(k)-theta(m)) +
B(k,m)*V(k)*V(m)*sin(theta(k)-theta(m));
Qf(k,m) = V(k)^2*(B(k,m)) - B(k,m)*V(k)*V(m)*cos(theta(k)-theta(m)) +
G(k,m)*V(k)*V(m)*sin(theta(k)-theta(m));
end
end
Qf(k,k)=sum(Qf(k,:));
Pf(k,k)=sum(Pf(k,:));
end
Sf=Pf+(1i)*Qf;
Qbarras=diag(Qf);
Sfluxo=abs(Sf*100);
carga_barra=dados_barra(:,7)+1i*dados_barra(:,8);
for i=1:4
SG(i)=Pf(i,i)+(1i)*Qf(i,i);
end
gerado=SG'+carga_barra;
fprintf('__________________________________________________________________________
_____________________________________________');
fprintf(' \n\n Segundo m�todo:\n\n');
fprintf('***NEWTON DESACOPLADO***\n');
fprintf('Tens�o nas barras:\n');
disp(V);
fprintf('Angulo:\n');
disp(theta);
fprintf('Balan�o de Potencia:\n\n');
disp(SG);
fprintf('Fluxo de Potencia ativa:\n');
disp(Pf);
fprintf('Fluxo de Potencia reativa:\n');
disp(Qf);
fprintf('Fluxo de Potencia MVA:\n');
disp(Sfluxo);
%%
% terceiro
% Newton desacoplado rapido
% zerar variaveis
theta=zeros(numBar,1);
theta_c(1:nPVnPQ,1) = theta(Ind_PVPQ) ;
V_c(1:nPQ,1) = V(Ind_PQ);
tempV=V(:,1);
V(Ind_PQ) = cos(theta(Ind_PQ)) ; % Valor inicial para tens�o n�o especificada.
theta_V(1:2*nPQ+nPV,1) = [theta(Ind_PVPQ)' V(Ind_PQ)']' ;% vetor com os angulos
theta e tens�o V.
while(kp~=0 && kq~=0)
if(kp==1)
for k = 1 : numBar
for m = 1 : numBar
P_c(k,m) = V(m)*(G(k,m)*cos(theta(k)-theta(m)) + B(k,m)*sin(theta(k)-
theta(m))) ;
end
P_cal(k) = V(k)*sum(P_c(k,:)) ;
delta_P(k) = [(Pg(k) - Pl(k)) - P_cal(k)]./tempV(k) ;
end
delta_Pc(1:nPQ+nPV,1) = delta_P(Ind_PVPQ) ;
end
if(max(abs(delta_Pc))<toler)
kp=0;
if(kq==0)
break;
else
for k=1:numBar
for m=1:numBar
Q_c(k,m) = V(m)*(G(k,m)*sin(theta(k)-theta(m)) - B(k,m)*cos(theta(k)-theta(m)))
;
end
Q_cal(k) = V(k)*sum(Q_c(k,:)) ;
delta_Q(k) = (Qg(k) - Ql(k) - Q_cal(k))./tempV(k);
end
delta_Qc(1:nPQ,1) = delta_Q(Ind_PQ) ;
if((max(abs(delta_Qc))<toler))
kq=0;
if kp==0
break;
end
else
J2(1:nPQ,1:nPQ) = - Ldr(:,:) ;
delta_V = - inv(J2) * delta_Qc ;
V_c = V_c + delta_V ;
V(Ind_PQ) = V_c(1:nPQ) ;
tempV=V(:,1);
inter1 = inter1 + 1 ;
kp=1;
end
end
else
J1(1:nPVnPQ,1:nPVnPQ) = - Hdr(:,:) ;
delta_theta = - inv(J1)*delta_Pc ;

theta_c = theta_c + delta_theta ;


theta(Ind_PVPQ) = theta_c(1:nPVnPQ) ;
inter2=inter2+1;
kq=1;
end
if(kq==1)
for k=1:numBar
for m=1:numBar
Q_c(k,m) = V(m)*(G(k,m)*sin(theta(k)-theta(m)) - B(k,m)*cos(theta(k)-theta(m))) ;
end
Q_cal(k) = V(k)*sum(Q_c(k,:)) ;
delta_Q(k) = (Qg(k) - Ql(k) - Q_cal(k))./tempV(k);
end
delta_Qc(1:nPQ,1) = delta_Q(Ind_PQ) ;
if((max(abs(delta_Qc))<toler))
kq=0;
if kp==0
break;
end
else
J2(1:nPQ,1:nPQ) = - Ldr(:,:) ;
delta_V = - inv(J2) * delta_Qc ;
V_c = V_c + delta_V ;
V(Ind_PQ) = V_c(1:nPQ) ;
tempV=V(:,1);
inter1 = inter1 + 1 ;
kp=1;
end
end
end

% fluxo
for k = 1 : numBar
for m = 1 : numBar
if (m~=k)
Pf(k,m) = -G(k,m)*V(k)^2 + G(k,m)*V(k)*V(m)*cos(theta(k)-theta(m)) +
B(k,m)*V(k)*V(m)*sin(theta(k)-theta(m));
Qf(k,m) = V(k)^2*(B(k,m)) - B(k,m)*V(k)*V(m)*cos(theta(k)-theta(m)) +
G(k,m)*V(k)*V(m)*sin(theta(k)-theta(m));
end
end
Qf(k,k)=sum(Qf(k,:));
Pf(k,k)=sum(Pf(k,:));
end
Sf=Pf+(1i)*Qf;
Qbarras=diag(Qf);
Sfluxo=abs(Sf*100);
carga_barra=dados_barra(:,7)+1i*dados_barra(:,8);
for i=1:4
SG(i)=Pf(i,i)+(1i)*Qf(i,i);
end
gerado=SG'+carga_barra;
fprintf('__________________________________________________________________________
_____________________________________________');
fprintf('\n\n Terceiro m�todo:\n\n');
fprintf('***NEWTON DESACOPLADO RAPIDO***\n');
fprintf('Tens�o nas barras:\n');
disp(V);
fprintf(' Angulo:\n');
disp(theta);
fprintf('Balan�o de Potencia:\n\n');
disp(SG);
fprintf('Fluxo de Potencia ativa:\n');
disp(Pf);
fprintf('Fluxo de Potencia reativa:\n');
disp(Qf);
fprintf('Fluxo de Potencia MVA:\n');
disp(Sfluxo);