120 lines
4.3 KiB
Matlab
120 lines
4.3 KiB
Matlab
clear
|
|
clc
|
|
yalmip('clear')
|
|
addpath('.\Powerflow')
|
|
[~, ~, ~, ~,Volt,Vangle,Y,Yangle,r,c,newwordParameter,PG,QG,PD,QD,Balance]=pf('ieee30.dat', '0');
|
|
%% 量测量
|
|
% 电压 节点电流 支路电流 节点功率 支路功率
|
|
%%
|
|
%% 状态量
|
|
% 电压 相角
|
|
%%
|
|
%% 开始生成量测量
|
|
sigma=0.05;% 标准差
|
|
%% 电压
|
|
%电压幅值
|
|
rVolt=Volt; %幅值
|
|
mVolt=rVolt.*(normrnd(0,sigma,length(Volt),1)+1);%电压量测量
|
|
rVAngel=Vangle;
|
|
%% 电流
|
|
%注入电流
|
|
cmpY=Y.*exp(1j*sparse(r,c,Yangle,length(Y),length(Y)));%复数导纳矩阵
|
|
cmpV=Volt.*exp(1j*Vangle); %复数电压
|
|
cmpI=cmpY*cmpV;% 注入电流
|
|
rI=abs(cmpI); %注入电流量测量要的是电流幅值
|
|
mI=rI.*(normrnd(0,sigma,length(rI),1)+1);%电流量测量
|
|
%% 支路电流
|
|
% 支路电流
|
|
lineI=newwordParameter.line.lineI;
|
|
lineJ=newwordParameter.line.lineJ;
|
|
lineR=newwordParameter.line.lineR;
|
|
lineX=newwordParameter.line.lineX;
|
|
lineB2=newwordParameter.line;
|
|
cmpBranchI=(cmpV(lineI)-cmpV(lineJ))./(lineR+1j*lineX);%复数支路电流
|
|
rBranchI=abs(cmpBranchI);% 支路电流幅值
|
|
mBranchI=rBranchI.*(normrnd(0,sigma,length(rBranchI),1)+1);%支路电流量测量
|
|
%% 支路功率
|
|
rBranchP=real((cmpV(lineI)-cmpV(lineJ)).*conj(cmpBranchI));
|
|
mBranchP=rBranchP.*(normrnd(0,sigma,length(rBranchP),1)+1);%支路功率量测量
|
|
rBranchQ=imag((cmpV(lineI)-cmpV(lineJ)).*conj(cmpBranchI));
|
|
mBranchQ=rBranchQ.*(normrnd(0,sigma,length(rBranchQ),1)+1);%支路功率量测量
|
|
%% 注入功率
|
|
rPD=PD;
|
|
PDi=find(PD~=0);
|
|
rQD=QD;
|
|
QDi=find(QD~=0);
|
|
rPG=PG;
|
|
PGi=find(PG~=0);
|
|
rQG=QG;
|
|
QGi=find(QG~=0);
|
|
mPD=rPD.*(normrnd(0,sigma,length(rPD),1)+1);
|
|
mQD=rQD.*(normrnd(0,sigma,length(rQD),1)+1);
|
|
mPG=rPG.*(normrnd(0,sigma,length(rPG),1)+1);
|
|
mQG=rQG.*(normrnd(0,sigma,length(rQG),1)+1);
|
|
%% 冗余度计算
|
|
stateVarCount=2*length(Volt);
|
|
measurements=length(mVolt)+length(mBranchP)+length(mBranchQ)+length(mPG)+length(mQG);
|
|
fprintf('冗余度 %f\n',measurements/stateVarCount);
|
|
%% 进入状态估计计算
|
|
% SEVolt=sdpvar(length(Volt),1,'full');
|
|
% SEVAngel=sdpvar(length(Vangle),1,'full');
|
|
% Objective=(SEVolt-mVolt)'*(1./sigma^2*eye(length(mVolt)))*(SEVolt-mVolt);%电压
|
|
%% 支路电流
|
|
% cmpSEV=SEVolt.*exp(1j*SEVAngel); %复数电压
|
|
% cmpSEBranchI=(cmpSEV(lineI)-cmpSEV(lineJ))./(lineR+1j*lineX);%复数支路电流
|
|
% SEBranchI=abs(cmpSEBranchI);% 支路电流幅值
|
|
% Objective=Objective+(SEBranchI-mBranchI)'*(1./sigma^2*eye(length(mBranchI)))*(SEBranchI-mBranchI);%%电流
|
|
%% 支路功率
|
|
% SEBranchP=real((cmpSEV(lineI)-cmpSEV(lineJ)).*conj(SEBranchI));
|
|
% SEBranchQ=imag((cmpSEV(lineI)-cmpSEV(lineJ)).*conj(SEBranchI));
|
|
% Objective=Objective+(SEBranchP-mBranchP)'*(1./sigma^2*eye(length(mBranchP)))*(SEBranchP-mBranchP);
|
|
% Objective=Objective+(SEBranchQ-mBranchQ)'*(1./sigma^2*eye(length(mBranchQ)))*(SEBranchQ-mBranchQ);
|
|
%% 0注入节点
|
|
zerosInjectionIndex=1:length(Volt);
|
|
zerosInjectionIndex=zerosInjectionIndex( ~(PD~=0|QD~=0|PG~=0|QG~=0) );
|
|
% PQ=diag(cmpSEV)*cmpY*cmpSEV;
|
|
% zeroInjP=real(PQ(zerosInjectionIndex));%% 0注入节点
|
|
% zeroInjQ=imag(PQ(zerosInjectionIndex));%% 0注入节点
|
|
%% 发电机注入功率
|
|
% 先找到只有发电机的节点
|
|
PDQDi=union(PDi,QDi);
|
|
onlyPG=setdiff(PGi,PDQDi);
|
|
onlyQG=setdiff(QGi,PDQDi);
|
|
% Objective=Objective+(PG(onlyPG)-real(PQ(onlyPG)))'*(1./sigma^2*eye(length(PG(onlyPG))))*(PG(onlyPG)-real(PQ(onlyPG)));
|
|
% Objective=Objective+(QG(onlyQG)-imag(PQ(onlyQG)))'*(1./sigma^2*eye(length(QG(onlyQG))))*(QG(onlyQG)-imag(PQ(onlyQG)));
|
|
%% YALMIP求解
|
|
% assign(SEVolt,1);
|
|
% assign(SEVAngel,1);
|
|
% Constraints=[SEVAngel(Balance)==0,zeroInjP==0,zeroInjP==0];
|
|
% Constraints=[SEVAngel(Balance)==0,PQ(zerosInjectionIndex)==0];
|
|
% options = sdpsettings('verbose',2,'solver','filtersd','usex0',1);
|
|
% solvesdp([],Objective,options)
|
|
% double(Objective)
|
|
% double(SEVolt)
|
|
% double(SEVAngel)
|
|
%% Opti ToolBox
|
|
Busnum=length(Volt);
|
|
seOpti=SEOpti();
|
|
seOpti=seOpti.init(Busnum, mVolt,sigma,newwordParameter,zerosInjectionIndex,cmpY,onlyPG,onlyQG,mPG,mQG,Balance,mBranchI,mBranchP,mBranchQ,Y,Yangle,r,c );
|
|
opts = optiset('solver','ipopt');
|
|
opts.maxiter=2500;
|
|
opts.tolrfun=1e-3;
|
|
opts.tolafun=1e-3;
|
|
opts.warnings='warnings';
|
|
opts.display='iter';
|
|
x0=[ones(Busnum,1);zeros(Busnum,1)];
|
|
[~,seOpti]=seOpti.equ(x0);
|
|
nlrhs=seOpti.nlrhs();
|
|
nle=seOpti.nle();
|
|
Opt = opti('fun',@seOpti.fun,'ndec',length(Volt)*2,'nlmix',@seOpti.equ,nlrhs,nle,'options',opts)
|
|
% Opt = opti('fun',@seOpti.fun,'ndec',length(Volt)*2,'options',opts)
|
|
[x,fval,exitflag,info] = solve(Opt,x0);
|
|
info
|
|
%% 输出结果
|
|
SEVolt=x(1:length(Volt));
|
|
SEVAngel=x(length(Volt)+Balance:2*length(Volt));
|
|
fprintf('目标函数为:%f\n',fval);
|
|
fprintf('相对误差\n');
|
|
(abs(rVolt-double(SEVolt)))./(rVolt)
|
|
MaxDeviation(rVolt,SEVolt,rVAngel,SEVAngel)
|