Index in position 1 exceeds array bounds (must not exceed 28).

1 view (last 30 days)
Hello guys, I am trying to run the backward-forward sweep program for power flow and I keep getting the error: Index in position 1 exceeds array bounds (must not exceed 28). Error in Test4 (line 131 [c e]=find(ldata(:,2:3)==NLD(i,3)); Can anyone help me with this?
Here is the code:
clear
clc
%branch no sending reciving R(PU) X(pu)
linedata=[ 1 1 2 0.057525912 0.029324489
2 2 3 0.307595167 0.156667640
3 3 4 0.228356656 0.116299674
4 4 5 0.237777928 0.121103899
5 5 6 0.510994811 0.441115179
6 6 7 0.116798814 0.386084969
7 7 8 0.443860450 0.146684835
8 8 9 0.642643047 0.461704714
9 9 10 0.651378001 0.461704714
10 10 11 0.122663712 0.040555144
11 11 12 0.233597628 0.077241951
12 12 13 0.915922324 0.720633708
13 13 14 0.337917936 0.444796338
14 14 15 0.368739846 0.328184702
15 15 16 0.465635443 0.340039282
16 16 17 0.804239697 1.073775422
17 17 18 0.456713311 0.358133116
18 2 19 0.102323747 0.097644308
19 19 20 0.938508419 0.845668336
20 20 21 0.255497406 0.298485858
21 21 22 0.442300637 0.584805173
22 3 23 0.281515090 0.192356167
23 23 24 0.560284909 0.442425422
24 24 25 0.559037059 0.437434020
25 6 26 0.126656834 0.064513875
26 26 27 0.177319567 0.090281989
27 27 28 0.660736881 0.582559042
28 28 29 0.501760717 0.437122057
29 29 30 0.316642084 0.161284687
30 30 31 0.607952801 0.600840053
31 31 32 0.193728802 0.225798562
32 32 33 0.212758523 0.330805188];
tielines = [33 8 21 2.00000000 2.00000000
34 9 15 2.00000000 2.00000000
35 12 22 2.00000000 2.00000000
36 18 33 0.50000000 0.50000000
37 25 29 0.50000000 0.50000000];
% bus no activepower reactivepower
busdata = [1 0 0
2 0.001 0.0006
3 0.0009 0.0004
4 0.0012 0.0008
5 0.0006 0.0003
6 0.0006 0.0002
7 0.002 0.001
8 0.002 0.001
9 0.0006 0.0002
10 0.0006 0.0002
11 0.00045 0.0003
12 0.0006 0.00035
13 0.0006 0.00035
14 0.0012 0.0008
15 0.0006 0.0001
16 0.0006 0.0002
17 0.0006 0.0002
18 0.0009 0.0004
19 0.0009 0.0004
20 0.0009 0.0004
21 0.0009 0.0004
22 0.0009 0.0004
23 0.0009 0.0005
24 0.0042 0.002
25 0.0042 0.002
26 0.0006 0.00025
27 0.0006 0.00025
28 0.0006 0.0002
29 0.0012 0.0007
30 0.002 0.006
31 0.0015 0.0007
32 0.0021 0.001
33 0.0006 0.0004] ;
rand('state',0); %temporary (demo purpose)
%Loop 1
%backup data first
linedata2 = linedata;
%copy original data
linedata = linedata2;
x=randperm(32, 5);
linedata(x,:) = [];
% ori+tielines
linedata = [linedata;tielines];
nbus = 33;
bdata = busdata;
ldata = linedata;
Pl = bdata(:,2); % PLi..
Ql = bdata(:,3); % QLi.
Branch=ldata(:,2:3);
M=max(ldata(:,2:3));
Totalbus=max(M);
f=[1:Totalbus]';
for i=1:Totalbus
g=find(Branch(:,:)==i);
h(i)=length(g);
end
k(:,1)=f;
k(:,2)=h';
% cent=input('central bus');
cent = 1;
% this section of the code is to adjust line data to the standard
NLD=zeros(Totalbus,size(ldata,2));
c=find(ldata(:,2:3)==cent);
NLD=ldata(c,:);
ldata(c,:)=[];
t=find(k(:,1)==cent);
k(t,2)=k(t,2)-size(c,1);
j=size(c,1);
i=1;
while sum(k(:,2))>0
c=[];
b=[];
t=[];
[c, e]=find(ldata(:,2:3)==NLD(i,3));
if size(c,2)~=0
b=ldata(c,:);
ldata(c,:)=[];
t=find(k(:,1)==NLD(i,3));
k(t,2)=k(t,2)-(size(c,1)+1);
d=find(b(:,3)==NLD(i,3));
b(d,2:3)=[b(d,3),b(d,2)];
NLD(j+1:j+size(c,1),:)=b;
j=j+size(c,1);
end
i=i+1;
end

Accepted Answer

Cris LaPierre
Cris LaPierre on 18 Jan 2021
Edited: Cris LaPierre on 18 Jan 2021
I suspect the error is here: NLD(i,3)
Position 1 is indexed by i. Since you are using a while loop, check what the value of i is. I suspect it is 29. The error message says it can't be greater than 28. What is the size of NLD? It likely only has 28 rows.
The fix is likely to improve the criteria used on your while loop.
while sum(k(:,2))>0
  2 Comments
Desmond Loh Hou Yin
Desmond Loh Hou Yin on 18 Jan 2021
NLD is affected by the ldata and is supposed to stay at 33 however it kept dropping below 33. Is there any way to fix this?
Cris LaPierre
Cris LaPierre on 18 Jan 2021
Perhaps. Follow your code backwards. How do you create NLD? You actually do it three ways. The second way replaces the first way. The third adds to it.
NLD=zeros(Totalbus,size(ldata,2));
NLD=ldata(c,:);
NLD(j+1:j+size(c,1),:)=b;
You also define c three times.
c=find(ldata(:,2:3)==cent);
c=[];
[c, e]=find(ldata(:,2:3)==NLD(i,3));
Ultimately, it is j that is larger than you expect. I suggest cleaning up your code. Avoid reusing the same varilable names over and over. It will make the code much easier to follow. Perhaps in the process, you'll discover how NLD goes from 28x5 to 1x5 to 29x5.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!