Index exceeds the number of array elements (8).

2 views (last 30 days)
Parth Trivedi
Parth Trivedi on 12 Apr 2021
Commented: DGM on 12 Apr 2021
% Col 1
Q = Q = [12 16 20 25 28 32 36 42];
Asc = 3.3807e+04
i = 1;
while i <= length(Q)
Qa = (3.14/4)*(Q(i).^2);
Long_col1_bar_req= Asc/Qa;
col1_bar_provide = round(Long_col1_bar_req)+1;
Long_col1_Ast_provide = col1_bar_provide*Qa;
Long_col1_spacing = (Qa/Long_col1_Ast_provide) *x1*1000;
if Long_col1_spacing > 100
if Long_col1_spacing > 300
Long_col1_spacing=300;
end
spacing_b = Long_col1_spacing;
break
end
i = i+1;
end
Long_col1_min_spacing = spacing_b ;
Long_col1_min_q = Q(i);
Long_col1_unit_wt = (Q(i).^2)/162;
Long_col1_bar_provide = col1_bar_provide;
Long_col1_Total_wt = Long_col1_unit_wt*col1_bar_provide;

Answers (1)

DGM
DGM on 12 Apr 2021
Your loop condition is
while i <= length(Q)
and the last thing that happens in the loop is
i = i+1;
so the moment the loop exits, i = 9. Then you try to index into Q
Long_col1_min_q = Q(i);
I try to recommend that people avoid while loops unless necessary. If you know how many iterations a loop needs to perform, use a for loop instead.
  2 Comments
Parth Trivedi
Parth Trivedi on 12 Apr 2021
what type of change i should do ? as per your recommendation?
DGM
DGM on 12 Apr 2021
I'm not sure why you're using Q(i) in the lines after the loop, since Q is not changed within the loop. I imagine you intend to do this instead
Long_col1_min_q = Q;
Long_col1_unit_wt = (Q.^2)/162;
Which will return vectors, but I don't know what this script is actually calculating in concept. Maybe you're looking for a specific element of Q instead? The minimum value, maybe?
If the question is "how would I use a for-loop here", then something like this:
Q = [12 16 20 25 28 32 36 42];
Asc = 3.3807e+04
for i=1:numel(Q) % i is constrained to array boundaries
Qa = (pi/4)*(Q(i).^2);
Long_col1_bar_req= Asc/Qa;
col1_bar_provide = round(Long_col1_bar_req)+1;
Long_col1_Ast_provide = col1_bar_provide*Qa;
Long_col1_spacing = (Qa/Long_col1_Ast_provide) *x1*1000;
if Long_col1_spacing > 100
if Long_col1_spacing > 300
Long_col1_spacing=300;
end
spacing_b = Long_col1_spacing;
break
end
end
Long_col1_min_spacing = spacing_b ;
Long_col1_min_q = Q(i); % decide what you want to do with these two lines
Long_col1_unit_wt = (Q(i).^2)/162;
Long_col1_bar_provide = col1_bar_provide;
Long_col1_Total_wt = Long_col1_unit_wt*col1_bar_provide;
But bear in mind that I'm making no recommendations about the correctness of calculations that are being made.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!