Skip to content

Commit 59d9a65

Browse files
authored
Uploading all the codes
1 parent d021324 commit 59d9a65

18 files changed

+2894
-0
lines changed

Comp_N2N_node_elim.m

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
%% Generate test values
2+
3+
clear all;
4+
%%inputs
5+
f = 50; %frequency 5 Hz
6+
N = [5 6;4 95;42 7;95 5;42 52; 94 54; 44 92; 96 94];
7+
N_wi = [1 0 0 1;1 0 1 0;1 1 0 1; 0 1 0 1;1 0 1 1; 0 1 1 1; 1 1 1 0; 0 1 1 0];
8+
R = [41 20 45 22; 93 21 97 24;63 51 68 55;39 68 45 72; 93 66 97 70 ];
9+
val_R = [3 2 1 5 4];
10+
C = [];
11+
val_C = [];
12+
V = [6 47 6];
13+
val_V = [3];
14+
L = [];
15+
val_L = [];
16+
%%
17+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
18+
%1-> right
19+
%2-> left
20+
%3-> up
21+
%4-> down
22+
% N -> contains (only orthogonal intersctions)nodes of the form (x,y)
23+
% R,C,L -> contains diagonal corners (x1,y1,x2,y2)
24+
% V -> contains centre of the voltage sources and radius (x,y,r)
25+
% N_wi -> contains 4 columns correspondng to each direction and they will
26+
% be 1 if there is a wire in that direction and will be 0 otherwise (right left up down)
27+
% val_R,val_C,val_L,val_V -> contains values of corresponding R,C,L,V
28+
% outputs
29+
% du_N -> a matrix containing duplicate nodes clubed together,number of
30+
% different nodes = size(du_N,1). The node numbers corresponding to the
31+
% input matrix are given from the 2nd colum of each row. The first column
32+
% of each row gives the number of duplicate nodes in each cluster
33+
% Z_N,V_N -> and n*n matrix where element (i,j) gives the impedence and voltage source connected
34+
% between node i & j
35+
du_N = ones(size(N,1),1);
36+
du_N(:,2) = 1:size(N,1);
37+
38+
% finding reactance of C and L
39+
for i = 1:size(val_C,2)
40+
val_C(i) = -(1/(2*pi*f*val_C(i)))*(i);
41+
end
42+
43+
for i = 1:size(val_L,2)
44+
val_L(i) = (2*pi*f*val_C(i))*(i);
45+
end
46+
47+
if(size(R,1)~=0)
48+
Rp = [(R(:,1)+R(:,3))/2 (R(:,2)+R(:,4))/2];
49+
end
50+
if(size(C,1)~=0)
51+
Cp = [(C(:,1)+C(:,3))/2 (C(:,2)+C(:,4))/2];
52+
end
53+
if(size(L,1)~=0)
54+
Lp = [(L(:,1)+L(:,3))/2 (L(:,2)+L(:,4))/2];
55+
end
56+
57+
Vp = V(:,1:2);
58+
r_m = mean(V(:,3));
59+
60+
Z_N = zeros(size(N,1));
61+
V_N = zeros(size(N,1));
62+
%making window for nodes
63+
for i=1:size(N,1)
64+
w_N(i,:) = [N(i,1)+r_m N(i,2)+r_m N(i,1)-r_m N(i,2)-r_m ];
65+
end
66+
67+
%making window for voltage source
68+
for i=1:size(V,1)
69+
w_V(i,:) = [V(i,1)+V(i,3) V(i,2)+V(i,3) V(i,1)-V(i,3) V(i,2)-V(i,3) ];
70+
end
71+
clear V;
72+
V = w_V;
73+
%matrix with centre points of everything in the circuit
74+
p_CO = N;
75+
if(size(R,1)~=0)
76+
p_CO(size(p_CO,1)+1:size(p_CO,1)+size(Rp,1),:) = Rp;
77+
end
78+
if(size(C,1)~=0)
79+
p_CO(size(p_CO,1)+1:size(p_CO,1)+size(Cp,1),:) = Cp;
80+
end
81+
if(size(L,1)~=0)
82+
p_CO(size(p_CO,1)+1:size(p_CO,1)+size(Lp,1),:) = Lp;
83+
end
84+
p_CO(size(p_CO,1)+1:size(p_CO,1)+size(V,1),:) = Vp;
85+
86+
%matrix with window of everything in the circuit
87+
w_CO = w_N;
88+
if(size(R,1)~=0)
89+
w_CO(size(w_CO,1)+1:size(w_CO,1)+size(R,1),:) = R;
90+
end
91+
if(size(C,1)~=0)
92+
w_CO(size(w_CO,1)+1:size(w_CO,1)+size(C,1),:) = C;
93+
end
94+
if(size(L,1)~=0)
95+
w_CO(size(w_CO,1)+1:size(w_CO,1)+size(L,1),:) = L;
96+
end
97+
w_CO(size(w_CO,1)+1:size(w_CO,1)+size(V,1),:) = V;
98+
99+
%generate matrix with all the values
100+
val_CO = zeros(size(p_CO,1));
101+
val_CO(size(N,1)+1:size(N,1)+size(R,1)) = val_R;
102+
val_CO(size(N,1)+size(R,1)+1:size(N,1)+size(R,1)+size(C,1)) = val_C;
103+
val_CO(size(N,1)+size(R,1)+size(C,1)+1:size(N,1)+size(R,1)+size(L,1)+size(C,1)) = val_L;
104+
val_CO(size(N,1)+size(R,1)+size(C,1)+size(L,1)+1:size(N,1)+size(R,1)+size(L,1)+size(C,1)+size(V,1)) = val_V;
105+
106+
%% compute distance b/w each components and nodes
107+
d_CO = 10000*ones(size(p_CO,1),size(p_CO,1));
108+
o_CO = zeros(size(p_CO,1),size(p_CO,1));
109+
for i =1:size(p_CO,1)
110+
for j =i+1:size(p_CO,1)
111+
112+
d1 = p_CO(i,1)-p_CO(j,1);
113+
d2 = p_CO(i,2)-p_CO(j,2);
114+
%define distance matrix ->distance between everything in the matrix
115+
d_CO(i,j) = sqrt(d1.^2 + d2.^2);
116+
d_CO(j,i) = d_CO(i,j);
117+
118+
%obtaining orientation
119+
%1-> right
120+
%2-> left
121+
%3-> up
122+
%4-> down
123+
if((p_CO(i,1)<max(w_CO(j,1),w_CO(j,3)) && (p_CO(i,1)>min(w_CO(j,1),w_CO(j,3)))))
124+
if(d2>0) %left
125+
o_CO(i,j) = 3;
126+
o_CO(j,i) = 4;
127+
else
128+
o_CO(i,j) = 4;
129+
o_CO(j,i) = 3;
130+
end
131+
elseif((p_CO(i,2)<max(w_CO(j,2),w_CO(j,4))&& (p_CO(i,2)>min(w_CO(j,4),w_CO(j,2)))))
132+
if(d1>0) %left
133+
o_CO(i,j) = 2;
134+
o_CO(j,i) = 1;
135+
else
136+
o_CO(i,j) = 1;
137+
o_CO(j,i) = 2;
138+
end
139+
end
140+
end
141+
end
142+
%%
143+
%we have distance and orientation between each components
144+
N_wi_d = ones(size(N,1),4);
145+
CO_d = zeros(size(p_CO,1),1);
146+
for i = 1:size(N,1) %checking each node
147+
for j = 1:4 %checking each direction
148+
149+
if(N_wi(i,j)==1 && N_wi_d(i,j)==1)
150+
Z = 0+0i;
151+
V_s = 0;
152+
flag_n = 0;
153+
i1 = i;
154+
while (flag_n ==0)
155+
t_min = 10000;
156+
for k = 1:size(p_CO,1)
157+
if(o_CO(i1,k)== j && d_CO(i1,k)<t_min && k~=i1 && CO_d(k)== 0)
158+
t_min = d_CO(i1,k);
159+
min_ind = k;
160+
end
161+
end
162+
if(min_ind > size(N,1))
163+
CO_d(min_ind) = 1;
164+
if(min_ind > size(p_CO,1)-size(V,1))
165+
V_s = V_s + val_CO(min_ind);
166+
else
167+
Z = Z+val_CO(min_ind);
168+
end
169+
i1 = min_ind;
170+
else
171+
flag_n = 1;
172+
if(Z == 0 && V_s ==0)
173+
Z_N(i,min_ind) = -1;
174+
Z_N(min_ind,i) = -1;
175+
else
176+
V_N(i,min_ind) = V_s;
177+
V_N(min_ind,i) = V_s;
178+
Z_N(i,min_ind) = Z;
179+
Z_N(min_ind,i) = Z;
180+
end
181+
N_wi_d(i,j) = 0;
182+
N_wi_d(min_ind,j+rem(j,2)-rem(rem(j,2)+1,2)) = 0;
183+
end
184+
end
185+
end
186+
end
187+
CO_d(i) = 1;
188+
end
189+
190+
%% eliminating similar nodes
191+
%Z1 = Z_N;
192+
for i = 1:size(Z_N)
193+
term=0;
194+
while(term == 0)
195+
if(size(Z_N,1)<i)
196+
term =1;
197+
else
198+
k = find(Z_N(i,:) == -1);
199+
if(size(k,2)==0)
200+
term =1;
201+
else
202+
for j=1:size(k)
203+
Z_N(i,k(j)) = 0;
204+
Z_N(k(j),i) = 0;
205+
Z_N(i,:) = Z_N(i,:) + Z_N(k(j),:);
206+
Z_N(:,i) = Z_N(:,i) + Z_N(:,k(j));
207+
208+
V_N(i,:) = V_N(i,:) + V_N(k(j),:);
209+
V_N(:,i) = V_N(:,i) + V_N(:,k(j));
210+
du_N(i,du_N(i,1)+2) = du_N(k(j),2);
211+
du_N(i,1) = du_N(i,1)+1;
212+
end
213+
214+
for j=1:size(k)
215+
Z2 = [Z_N(1:k(j)-1,:); Z_N(k(j)+1:size(Z_N,1),:)];
216+
clear Z_N;
217+
Z_N = [Z2(:,1:k(j)-1) Z2(:,k(j)+1:size(Z2,2))];
218+
219+
V2 = [V_N(1:k(j)-1,:); V_N(k(j)+1:size(V_N,1),:)];
220+
clear V_N;
221+
V_N = [V2(:,1:k(j)-1) V2(:,k(j)+1:size(V2,2))];
222+
223+
du_N2 = [du_N(1:k(j)-1,:); du_N(k(j)+1:end,:)];
224+
clear du_N;
225+
du_N = du_N2;
226+
clear du_N2;
227+
clear Z2;
228+
clear V2;
229+
230+
end
231+
end
232+
233+
end
234+
end
235+
end
236+

0 commit comments

Comments
 (0)