以下的代码是计算电力系统潮流分析的MATLAB实现,针对给出的数据计算结果却拉满了迭代次数,求各位帮忙找找原因
之前尝试使用其他方式的电力系统潮流分析的方法进行计算,使用的是同样的数据,也就是说这组数据是可以计算的,但是本代码最后却不收敛,也就是说代码应该是存在问题的,用别的方法算出来差不多是迭代6次
% (bus#)(volt)(ang)(p)(q)(bus type)
clear;
bus=[
1 1.00 0.00 -1.60 -0.80 1;
2 1.00 0.00 -2.00 -1.00 1;
3 1.00 0.00 -3.70 -1.30 1;
4 1.05 0.00 5.00 0.00 2;
5 1.05 0.00 0.00 0.00 3];
% b#1 b#2 ( R )( X )( G )( B ) ( K )
line = [
1 2 0.04 0.25 0.0 0.25 0;
1 3 0.10 0.35 0.0 0.0 0;
2 3 0.08 0.30 0.0 0.25 0;
5 3 0.00 0.03 0.0 0.0 1.05;
4 2 0.00 0.015 0.0 0.0 1.05];
function [bus,line,nPQ,nPV,nodenum]=Sort(bus,line)
[nb,mb]=size(bus);
[nl,ml]=size(line);
global nSW;
global nPV;
global nPQ;
nSW = 0; % number of swing bus counter
nPV = 0; % number of PV bus counter
nPQ = 0; % number of PQ bus counter
for i = 1:nb % nb为总节点数
type= bus(i,6);
if type == 3
nSW = nSW + 1; % increment swing bus counter
SW(nSW,:)=bus(i,:);
elseif type == 2
nPV = nPV +1; % increment PV bus counter
PV(nPV,:)=bus(i,:);
else
nPQ = nPQ + 1; % increment PQ bus counter
PQ(nPQ,:)=bus(i,:);
end
end
bus=[PQ;PV;SW];
newbus=[1:nb]';
nodenum=[newbus bus(:,1)];
bus(:,1)=newbus;
for i=1:nl
for j=1:2
for k=1:nb
if line(i,j)==nodenum(k,2)
line(i,j)=nodenum(k,1);
break
end
end
end
end
function Y= y(bus,line)
% Purpose: build admittance matrix Y from the line data
%
% Input: bus - bus data
% line - line data
%
% Output: Y - admittance matrix
[nb,mb]=size(bus);
[nl,ml]=size(line);
Y=zeros(nb,nb); % 对导纳矩阵赋初值0
for k=1:nl
I=line(k,1); %读入线路参数
J=line(k,2);
Zt=line(k,3)+j*line(k,4);
Yt=1/Zt;
Ym=line(k,5)+j*line(k,6);
K=line(k,7);
if (K==0)&(J~=0) % 普通线路: K=0;
Y(I,I)=Y(I,I)+Yt+Ym;
Y(J,J)=Y(J,J)+Yt+Ym;
Y(I,J)=Y(I,J)-Yt;
Y(J,I)=Y(I,J);
end
if (K==0)&(J==0) % 对地支路: K=0,J=0,R=X=0;
Y(I,I)=Y(I,I)+Ym;
end
if K>0 % 变压器线路: Zt和Ym为折算到i侧的值,K在j侧
Y(I,I)=Y(I,I)+Yt+Ym;
Y(J,J)=Y(J,J)+Yt/K/K;
Y(I,J)=Y(I,J)-Yt/K;
Y(J,I)=Y(I,J);
end
if K<0 % 变压器线路: Zt和Ym为折算到K侧的值,K在i侧
Y(I,I)=Y(I,I)+Yt+Ym;
Y(J,J)=Y(J,J)+K*K*Yt;
Y(I,J)=Y(I,J)+K*Yt;
Y(J,I)=Y(I,J);
end
end
function [delP,delQ] =dPQ(Y,bus,nPQ,nPV)
[n,m]=size(Y);
[nb,mb]=size(bus);
G=zeros(n,m);
B=zeros(n,m);
for i=1:n
for j=1:m
G(i,j)=real(Y(i,j));
B(i,j)=imag(Y(i,j));
end
end
temp_delP=0;
temp_delQ=0;
for i=1:nb-1
for j=1:m
temp_delP=temp_delP+bus(i,2)*bus(j,2)*(G(i,j)*cos(bus(i,3)-bus(j,3))+B(i,j)*sin(bus(i,3)-bus(j,3)));
end
delP(i,1)= bus(i,4)-temp_delP;
end
for i=1:nPQ
for j=1:m
temp_delQ=temp_delQ+bus(i,2)*bus(j,2)*(G(i,j)*sin(bus(i,3)-bus(j,3))-B(i,j)*cos(bus(i,3)-bus(j,3)));
end
delQ(i,1)=bus(i,5)-temp_delQ;
end
function J=form_jac(bus,Y,nPQ,nPV)
[n,m]=size(Y);
[nb,mb]=size(bus);
G=zeros(n,m);
B=zeros(n,m);
H=zeros(nb-1,nb-1);
N=zeros(nb-1,nPQ);
K=zeros(nPQ,nb-1);
L=zeros(nPQ,nPQ);
for i=1:n
for j=1:m
G(i,j)=real(Y(i,j));
B(i,j)=imag(Y(i,j));
end
end
for i=1:nb-1
for j=1:nb-1
if i~=j
H(i,j)=-bus(i,2)*bus(j,2)*(G(i,j)*sin(bus(i,3)-bus(j,3))-B(i,j)*cos(bus(i,3)-bus(j,3)));
elseif i==j
% H(i,j)=bus(i,2).^2*B(i,i)+bus(i,5);
for jj=1:nb
if jj~=i
H(i,j)= H(i,j)+bus(jj,2) *(G(i,jj)*sin(bus(i,3)-bus(jj,3))-B(i,jj)*cos(bus(i,3)-bus(jj,3)));
end
end
H(i,j)=bus(i,2)*H(i,j);
end
end
end
for i=1:nb-1
for j=1:nPQ
if i~=j
N(i,j)=-bus(i,2)*bus(j,2)*(G(i,j)*cos(bus(i,3)-bus(j,3))+B(i,j)*sin(bus(i,3)-bus(j,3)));
elseif i==j
% N(i,i)=-bus(i,2).^2*G(i,i)-bus(i,4);
for jj=1:nb
if jj~=i
N(i,i)= N(i,i)+bus(jj,2) *(G(i,jj)*cos(bus(i,3)-bus(jj,3))+B(i,jj)*sin(bus(i,3)-bus(jj,3)));
end
end
N(i,i)=-bus(i,2).*N(i,i)-2*bus(i,2).^2.*G(i,i);
end
end
end
for i=1:nPQ
for j=1:nb-1
if i~=j
K(i,j)=bus(i,2)*bus(j,2)*(G(i,j)*cos(bus(i,3)-bus(j,3))+B(i,j)*sin(bus(i,3)-bus(j,3)));
elseif i==j
% K(i,i)=bus(i,2).^2*G(i,i)-bus(i,4);
K(i,i)=N(i,i)+2*bus(i,2).^2.*G(i,i);
end
end
end
for i=1:nPQ
for j=1:nPQ
if i~=j
L(i,j)=-bus(i,2)*bus(j,2)*(G(i,j)*sin(bus(i,3)-bus(j,3))-B(i,j)*cos(bus(i,3)-bus(j,3)));
elseif i==j
%L(i,i)=bus(i,2).^2*B(i,i)-bus(i,5);
L(i,i)= -H(i,i)+2*bus(i,2).^2.*B(i,i);
end
end
end
%合成雅可比矩阵
J=[H N;K L];
data1;
Max=100;
%[bus,line]=Openfile;
%tic;
[nb,mb]=size(bus);
[nl,ml]=size(line);
[bus,line,nPQ,nPV,nodenum]=Sort(bus,line);
Y= y(bus,line);
myf = fopen('Output.m','w');
fclose(myf);
format long
eps = 1.0e-10;
for t=1:Max
[delP,delQ] =dPQ(Y,bus,nPQ,nPV);
J = form_jac(bus,Y,nPQ,nPV);
UD = zeros(nPQ,nPQ);
for i=1:nPQ
UD(i,i) = bus(i,2);
end
A = J;
b = [delP;delQ];
%x=Gauss(A,b);
x=A\b;
dx = x(1:(nb-1),1);
dU = UD*(x(nb:(nb+nPQ-1),1));
bus(1:nPQ,2) = bus(1:nPQ,2)-dU;
bus(1:nb-1,3) = bus(1:nb-1,3)-dx;
sprintf('第%d次的迭代结果:\n',t)
% disp(J);
%disp(dx);
%disp(dU);
disp(bus);
if (max(abs(dU))<eps)&&(max(abs(dx))<eps)
break
end
end