-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWLS_SE.m
230 lines (223 loc) · 8.95 KB
/
WLS_SE.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
function [V,k]=WLS_SE(Y,z,zType,R,iter_max,threshold,Vtrue)
% SE
n=length(Y);%number of nodes
G=real(Y);%real part of Y
B=imag(Y);%imaginary part of Y
%initial guess
% x=[.01*randn(1,n-1),ones(1,n)+.03*randn(1,n)];
% x=[zeros(1,n-1),ones(1,n)];
% x=[[-2*pi/3,-4*pi/3,repmat([0,-2*pi/3,-4*pi/3],1,n/3-1)],ones(1,n)];
x=[angle(Vtrue(2:end)),abs(Vtrue)]; %for testing the accuracy with correct solution as starting point
for k=1:iter_max
V=x(n:end);%voltage magnitudes
Th=[0,x(1:n-1)];%voltage angles (Theta(1)=0 reference)
%calculating measurement function given the states h(x)
h=zeros(length(z),1);
for m=1:length(z)
if zType(m,1)==1 %Pij
i=zType(m,2);
j=zType(m,3);
h(m)=V(i)^2*(-G(i,j))-V(i)*V(j)*(-G(i,j)*cos(Th(i)-Th(j))-B(i,j)*sin(Th(i)-Th(j)));
elseif zType(m,1)==2 %Pi
i=zType(m,2);
h(m)=0;
for j=1:length(Y)
h(m)=h(m)+V(i)*V(j)*(G(i,j)*cos(Th(i)-Th(j))+B(i,j)*sin(Th(i)-Th(j)));
end
elseif zType(m,1)==3 %Qij
i=zType(m,2);
j=zType(m,3);
h(m)=-V(i)^2*(-B(i,j))-V(i)*V(j)*(-G(i,j)*sin(Th(i)-Th(j))+B(i,j)*cos(Th(i)-Th(j)));
elseif zType(m,1)==4 %Qi
i=zType(m,2);
h(m)=0;
for j=1:length(Y)
h(m)=h(m)+V(i)*V(j)*(G(i,j)*sin(Th(i)-Th(j))-B(i,j)*cos(Th(i)-Th(j)));
end
elseif zType(m,1)==5 %|Vi|
i=zType(m,2);
h(m)=V(i);
elseif zType(m,1)==6 %Theta Vi
i=zType(m,2);
h(m)=Th(i);
elseif zType(m,1)==7 %real Iij
i=zType(m,2);%sending end
j=zType(m,3);%receiving end
ph=zType(m,4);%phase
a1=3*(i-1)+1;
b1=3*(i-1)+2;
c1=3*(i-1)+3;
a2=3*(j-1)+1;
b2=3*(j-1)+2;
c2=3*(j-1)+3;
y=-Y([a1,b1,c1],[a2,b2,c2]);
g=real(y);
b=imag(y);
h(m)=g(ph,1)*(V(a1)*cos(Th(a1))-V(a2)*cos(Th(a2)))-b(ph,1)*(V(a1)*sin(Th(a1))-V(a2)*sin(Th(a2)))+...
g(ph,2)*(V(b1)*cos(Th(b1))-V(b2)*cos(Th(b2)))-b(ph,2)*(V(b1)*sin(Th(b1))-V(b2)*sin(Th(b2)))+...
g(ph,3)*(V(c1)*cos(Th(c1))-V(c2)*cos(Th(c2)))-b(ph,3)*(V(c1)*sin(Th(c1))-V(c2)*sin(Th(c2)));
elseif zType(m,1)==8 %imag Iij
i=zType(m,2);%sending end
j=zType(m,3);%receiving end
ph=zType(m,4);%phase
a1=3*(i-1)+1;
b1=3*(i-1)+2;
c1=3*(i-1)+3;
a2=3*(j-1)+1;
b2=3*(j-1)+2;
c2=3*(j-1)+3;
y=-Y([a1,b1,c1],[a2,b2,c2]);
g=real(y);
b=imag(y);
h(m)=g(ph,1)*(V(a1)*sin(Th(a1))-V(a2)*sin(Th(a2)))+b(ph,1)*(V(a1)*cos(Th(a1))-V(a2)*cos(Th(a2)))+...
g(ph,2)*(V(b1)*sin(Th(b1))-V(b2)*sin(Th(b2)))+b(ph,2)*(V(b1)*cos(Th(b1))-V(b2)*cos(Th(b2)))+...
g(ph,3)*(V(c1)*sin(Th(c1))-V(c2)*sin(Th(c2)))+b(ph,3)*(V(c1)*cos(Th(c1))-V(c2)*cos(Th(c2)));
end
end
%building the jacobian H
H=zeros(length(z),length(x));
for m=1:length(z)
if zType(m,1)==1 %Pij
i=zType(m,2);
j=zType(m,3);
if i~=1
H(m,i-1)=V(i)*V(j)*(-G(i,j)*sin(Th(i)-Th(j))+B(i,j)*cos(Th(i)-Th(j)));
end
if j~=1
H(m,j-1)=-V(i)*V(j)*(-G(i,j)*sin(Th(i)-Th(j))+B(i,j)*cos(Th(i)-Th(j)));
end
H(m,i+n-1)=-V(j)*(-G(i,j)*cos(Th(i)-Th(j))-B(i,j)*sin(Th(i)-Th(j)))+2*(-G(i,j))*V(i);
H(m,j+n-1)=-V(i)*(-G(i,j)*cos(Th(i)-Th(j))-B(i,j)*sin(Th(i)-Th(j)));
elseif zType(m,1)==2 %Pi
i=zType(m,2);
for j=1:n
if i~=j
if j>1
H(m,j-1)=V(i)*V(j)*(G(i,j)*sin(Th(i)-Th(j))-B(i,j)*cos(Th(i)-Th(j)));
end
H(m,j+n-1)=V(i)*(G(i,j)*cos(Th(i)-Th(j))+B(i,j)*sin(Th(i)-Th(j)));
end
end
if i>1
H(m,i-1)=-V(i)^2*B(i,i);
for j=1:n
H(m,i-1)=H(m,i-1)+V(i)*V(j)*(-G(i,j)*sin(Th(i)-Th(j))+B(i,j)*cos(Th(i)-Th(j)));
end
end
H(m,i+n-1)=+V(i)*G(i,i);
for j=1:n
H(m,i+n-1)=H(m,i+n-1)+V(j)*(G(i,j)*cos(Th(i)-Th(j))+B(i,j)*sin(Th(i)-Th(j)));
end
elseif zType(m,1)==3 %Qij
i=zType(m,2);
j=zType(m,3);
if i~=1
H(m,i-1)=-V(i)*V(j)*(-G(i,j)*cos(Th(i)-Th(j))-B(i,j)*sin(Th(i)-Th(j)));
end
if j~=1
H(m,j-1)=V(i)*V(j)*(-G(i,j)*cos(Th(i)-Th(j))-B(i,j)*sin(Th(i)-Th(j)));
end
H(m,i+n-1)=-V(j)*(-G(i,j)*sin(Th(i)-Th(j))+B(i,j)*cos(Th(i)-Th(j)))-2*(-B(i,j))*V(i);
H(m,j+n-1)=-V(i)*(-G(i,j)*sin(Th(i)-Th(j))+B(i,j)*cos(Th(i)-Th(j)));
elseif zType(m,1)==4 %Qi
i=zType(m,2);
for j=1:n
if i~=j
if j>1
H(m,j-1)=V(i)*V(j)*(-G(i,j)*cos(Th(i)-Th(j))-B(i,j)*sin(Th(i)-Th(j)));
end
H(m,j+n-1)=V(i)*(G(i,j)*sin(Th(i)-Th(j))-B(i,j)*cos(Th(i)-Th(j)));
end
end
if i>1
H(m,i-1)=-V(i)^2*G(i,i);
for j=1:n
H(m,i-1)=H(m,i-1)+V(i)*V(j)*(G(i,j)*cos(Th(i)-Th(j))+B(i,j)*sin(Th(i)-Th(j)));
end
end
H(m,i+n-1)=-V(i)*B(i,i);
for j=1:n
H(m,i+n-1)=H(m,i+n-1)+V(j)*(G(i,j)*sin(Th(i)-Th(j))-B(i,j)*cos(Th(i)-Th(j)));
end
elseif zType(m,1)==5 %|Vi|
i=zType(m,2);
H(m,i+n-1)=1;
elseif zType(m,1)==6 %Theta Vi
i=zType(m,2);
H(m,i-1)=1;
elseif zType(m,1)==7 %real Iij
i=zType(m,2);
j=zType(m,3);
ph=zType(m,4);%phase
a1=3*(i-1)+1;
b1=3*(i-1)+2;
c1=3*(i-1)+3;
a2=3*(j-1)+1;
b2=3*(j-1)+2;
c2=3*(j-1)+3;
y=-Y([a1,b1,c1],[a2,b2,c2]);
g=real(y);
b=imag(y);
%angles
if a1>1
H(m,a1-1)=-g(ph,1)*V(a1)*sin(Th(a1))-b(ph,1)*V(a1)*cos(Th(a1));
end
H(m,b1-1)=-g(ph,2)*V(b1)*sin(Th(b1))-b(ph,2)*V(b1)*cos(Th(b1));
H(m,c1-1)=-g(ph,3)*V(c1)*sin(Th(c1))-b(ph,3)*V(c1)*cos(Th(c1));
H(m,a2-1)= g(ph,1)*V(a2)*sin(Th(a2))+b(ph,1)*V(a2)*cos(Th(a2));
H(m,b2-1)= g(ph,2)*V(b2)*sin(Th(b2))+b(ph,2)*V(b2)*cos(Th(b2));
H(m,c2-1)= g(ph,3)*V(c2)*sin(Th(c2))+b(ph,3)*V(c2)*cos(Th(c2));
%magnitudes
H(m,a1+n-1)= g(ph,1)*cos(Th(a1))-b(ph,1)*sin(Th(a1));
H(m,b1+n-1)= g(ph,2)*cos(Th(b1))-b(ph,2)*sin(Th(b1));
H(m,c1+n-1)= g(ph,3)*cos(Th(c1))-b(ph,3)*sin(Th(c1));
H(m,a2+n-1)=-g(ph,1)*cos(Th(a2))+b(ph,1)*sin(Th(a2));
H(m,b2+n-1)=-g(ph,2)*cos(Th(b2))+b(ph,2)*sin(Th(b2));
H(m,c2+n-1)=-g(ph,3)*cos(Th(c2))+b(ph,3)*sin(Th(c2));
elseif zType(m,1)==8 %imag Iij
i=zType(m,2);
j=zType(m,3);
ph=zType(m,4);%phase
a1=3*(i-1)+1;
b1=3*(i-1)+2;
c1=3*(i-1)+3;
a2=3*(j-1)+1;
b2=3*(j-1)+2;
c2=3*(j-1)+3;
y=-Y([a1,b1,c1],[a2,b2,c2]);
g=real(y);
b=imag(y);
%angles
if a1>1
H(m,a1-1)= g(ph,1)*V(a1)*cos(Th(a1))-b(ph,1)*V(a1)*sin(Th(a1));
end
H(m,b1-1)= g(ph,2)*V(b1)*cos(Th(b1))-b(ph,2)*V(b1)*sin(Th(b1));
H(m,c1-1)= g(ph,3)*V(c1)*cos(Th(c1))-b(ph,3)*V(c1)*sin(Th(c1));
H(m,a2-1)=-g(ph,1)*V(a2)*cos(Th(a2))+b(ph,1)*V(a2)*sin(Th(a2));
H(m,b2-1)=-g(ph,2)*V(b2)*cos(Th(b2))+b(ph,2)*V(b2)*sin(Th(b2));
H(m,c2-1)=-g(ph,3)*V(c2)*cos(Th(c2))+b(ph,3)*V(c2)*sin(Th(c2));
%magnitudes
H(m,a1+n-1)= g(ph,1)*sin(Th(a1))+b(ph,1)*cos(Th(a1));
H(m,b1+n-1)= g(ph,2)*sin(Th(b1))+b(ph,2)*cos(Th(b1));
H(m,c1+n-1)= g(ph,3)*sin(Th(c1))+b(ph,3)*cos(Th(c1));
H(m,a2+n-1)=-g(ph,1)*sin(Th(a2))-b(ph,1)*cos(Th(a2));
H(m,b2+n-1)=-g(ph,2)*sin(Th(b2))-b(ph,2)*cos(Th(b2));
H(m,c2+n-1)=-g(ph,3)*sin(Th(c2))-b(ph,3)*cos(Th(c2));
end
end
%The right hand side of the equation
RHS=H.'*R^-1*(z-h);
%The gain matrix
GM=H.'*R^-1*H;
%Cholesky decomposition
% L = chol(G,'lower');
delta_x=GM\RHS;
if abs(delta_x)<threshold
break
end
x=x+(delta_x).';
end
Vmag=x(length(Y):end);%voltage magnitudes
Theta=[0,x(1:length(Y)-1)];%voltage angles (Theta(1)=0 reference)
V=Vmag.*(cos(Theta)+1i*sin(Theta));
end