-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zbus.m
56 lines (55 loc) · 1.68 KB
/
Zbus.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
function [Z,N] = Zbus(branches, shunts)
%Zbus creates the Zbus matrix with direct method
% [Z,N] = Zbus(branches, shunts):
% Z is Zbus
% N is node names
% branches are lines connecting 2 nodes
% shunts are lines connecting nodes to ground(ref)
%% new bus to ref
N = shunts(:,1);
Z = zeros(numel(N));
for i = 1:length(shunts)
Z(i,i) = shunts(i,2);
end
%% new bus to existing
all_nodes = unique(branches(:,1:2));
last_node=length(N);
added_branches=[];
while last_node < length(all_nodes)
for i = 1:length(branches)
if ~ismember(i,added_branches)
if ismember(branches(i,1),N) && ~ismember(branches(i,2),N)
added_branches(end+1) = i;
j=find(branches(i,1)==N,1,'first');
last_node=last_node+1;
N(last_node)=branches(i,2);
Z = [Z Z(:,j);Z(j,:) Z(j,j)+branches(i,3)];
elseif ismember(branches(i,2),N) && ~ismember(branches(i,1),N)
added_branches(end+1) = i;
j=find(branches(i,2)==N,1,'first');
last_node=last_node+1;
N(last_node)=branches(i,1);
Z = [Z Z(:,j);Z(j,:) Z(j,j)+branches(i,3)];
end
end
end
end
%% existing to existing
branches(added_branches,:)=[];
for branch = branches.'
i = find(branch(1)==N,1,'first');
j = find(branch(2)==N,1,'first');
a=Z(:,i)-Z(:,j);
b=Z(i,:)-Z(j,:);
y=1./(branch(3)+Z(i,i)-2*Z(i,j)+Z(j,j));
Z=Z-y*a*b;
end
%% sort nodes
for i =1:length(N)
if i ~= N(i)
j=N(i);
N([i,j])=N([j,i]);
Z([i,j],:)=Z([j,i],:);
Z(:,[i,j])=Z(:,[j,i]);
end
end