Index exceeds matrix dimensions.

1 view (last 30 days)
Sri Wahyuni
Sri Wahyuni on 3 Mar 2022
Commented: Sri Wahyuni on 4 Mar 2022
clc;
clear all;
format short;
tic
% bus no|activepower kW | reactivepower kVar
m=[ 1 0 0 0
2 100 60 0
3 90 40 0
4 120 80 0
5 60 30 0
6 -2522.6622 20 0
7 200 100 0
8 200 100 0
9 60 20 0
10 60 20 0
11 45 30 0
12 60 35 0
13 60 35 0
14 120 80 0
15 60 10 0
16 60 20 0
17 60 20 0
18 90 40 0
19 90 40 0
20 90 40 0
21 90 40 0
22 90 40 0
23 90 50 0
24 420 200 0
25 420 200 0
26 60 25 0
27 60 25 0
28 60 20 0
29 120 70 0
30 200 600 0
31 150 70 0
32 210 100 0
33 60 40 0
];
%branchno. INbus OUTbus Resistance(ohm) Reactance(ohm)
l=[ 1 1 2 0.0922 0.0470
2 2 3 0.4930 0.2511
3 3 4 0.3660 0.1864
4 4 5 0.3811 0.1941
4 5 6 0.8190 0.7070
6 6 7 0.1872 0.6188
7 7 8 0.7114 0.2351
8 8 9 1.0300 0.7400
9 9 10 1.0440 0.7400
10 10 11 0.1966 0.0650
11 11 12 0.3744 0.1238
12 12 13 1.4680 1.1550
13 13 14 0.5416 0.7129
14 14 15 0.5910 0.5260
15 15 16 0.7463 0.5450
16 16 17 1.2890 1.7210
17 17 18 0.7320 0.5740
18 2 19 0.1640 0.1565
19 19 20 1.5042 1.3554
20 20 21 0.4095 0.4784
21 21 22 0.7089 0.9373
22 3 23 0.4512 0.3083
23 23 24 0.8980 0.7091
24 24 25 0.8960 0.7011
25 6 26 0.2030 0.1034
26 26 27 0.2842 0.1447
27 27 28 1.0590 0.9337
28 28 29 0.8042 0.7006
29 29 30 0.5075 0.2585
30 30 31 0.9744 0.9630
31 31 32 0.3105 0.3619
32 32 33 0.3410 0.5302
33 8 21 2.0000 2.0000
34 9 15 2.0000 2.0000
35 12 22 2.0000 2.0000
36 18 33 0.5000 0.5000
37 25 29 0.5000 0.5000] ;
br=length(l); %data branch
no=length(m); %data bus
MVAb=100; %MVA base
KVb=12.66; %KV base
Zb=(KVb^2)/MVAb;
LF=1; %load factor
PF=1; %power factor
% Per unit Values
for i=1:br
R(i,1)=(l(i,4))/Zb;
X(i,1)=(l(i,5))/Zb;
end
for i=1:no
P(i,1)=(LF*(m(i,2))/(1000*MVAb));
Q(i,1)=(LF*(m(i,3))/(1000*MVAb));
end
% draw bus and line data to a distribution system
R
R = 37×1
0.0575 0.3076 0.2284 0.2378 0.5110 0.1168 0.4439 0.6426 0.6514 0.1227
X
X = 37×1
0.0293 0.1567 0.1163 0.1211 0.4411 0.3861 0.1467 0.4617 0.4617 0.0406
P
P = 33×1
0 0.0010 0.0009 0.0012 0.0006 -0.0252 0.0020 0.0020 0.0006 0.0006
Q
Q = 33×1
1.0e+00 * 0 0.0006 0.0004 0.0008 0.0003 0.0002 0.0010 0.0010 0.0002 0.0002
C=zeros(br,no);
for i=1:br
a=l(i,2);
b=l(i,3);
for j=1:no
if a==j
C(i,j)=-1;
end
if b==j
C(i,j)=1;
end
end
end
C
C = 37×33
-1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
e=1;
for i=1:no
d=0;
for j=1:br
if C(j,i)==-1
d=1;
end
end
if d==0
endnode(e,1)=i;
e=e+1;
end
end
endnode
endnode = 2×1
22 33
h=length(endnode);
for j=1:h
e=2;
f=endnode(j,1);
% while (f~=1)
for s=1:no
if (f~=1)
k=1;
for i=1:br
if ((C(i,f)==1)&&(k==1))
f=i;
k=2;
end
end
k=1;
for i=1:no
if ((C(f,i)==-1)&&(k==1));
f=i;
g(j,e)=i;
e=e+1;
k=3;
end
end
end
end
end
for i=1:h
g(i,1)=endnode(i,1);
end
g;
w=length(g(1,:))
w = 14
for i=1:h
j=1;
for k=1:no
for t=1:w
if g(i,t)==k
g(i,t)=g(i,j);
g(i,j)=k;
j=j+1;
end
end
end
end
g;
for k=1:br
e=1;
for i=1:h
for j=1:w-1
if (g(i,j)==k)
if g(i,j+1)~=0
adjb(k,e)=g(i,j+1);
e=e+1;
else
adjb(k,1)=0;
end
end
end
end
end
adjb;
for i=1:br-1
for j=h:-1:1
for k=j:-1:2
if adjb(i,j)== adjb(i,k-1)
adjb(i,j)=0;
end
end
end
how to change the position index can be more than 32 because the data I have to enter is more than 32
end
Index in position 1 exceeds array bounds. Index must not exceed 32.
  2 Comments
KSSV
KSSV on 3 Mar 2022
Buggy and lengthy code.
Sri Wahyuni
Sri Wahyuni on 3 Mar 2022
I don't understand what you mean. I'm confused, change the program so that it can load more 32 data. can you help me?

Sign in to comment.

Answers (1)

Simon Chan
Simon Chan on 3 Mar 2022
Edited: Simon Chan on 3 Mar 2022
Check the following first:
In the following loop, you assign something to variable adjb if g(i,j)==k. However, if g(i,j)~=k, there is nothing assigned to adjb.
So the number of rows on adjb may not reach 'br', which is 37 in your case. If, for example, after k=33, the above condition are all not satisfy, variable adjb would not reach your expected size.
It is better to initialize the variable with a known size, such as adjb = zeros(37,2) or NaN(37,2) depends on your requirement. This is also true for other variables.
for k=1:br
e=1;
for i=1:h
for j=1:w-1
if (g(i,j)==k) % Do something for g(i,j)==k, but do nothing for g(i,j)~=k
if g(i,j+1)~=0
adjb(k,e)=g(i,j+1);
e=e+1;
else
adjb(k,1)=0;
end
end
end
end
end
  4 Comments
Simon Chan
Simon Chan on 3 Mar 2022
I have no idea about any calculation in your code, so you need to decide the expected size of each newly generated variables, such as g and adjb.
Just have a quick look on the code again and you may define g = zeros(2,37) or NaN(2,37) and adjb = zeros(37,2) or NaN(37,2) before assigning any number on these two variables.

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!