Error: Array indices must be positive integers or logical values

조회 수: 2 (최근 30일)
Trine Høy Oddershede
Trine Høy Oddershede 2021년 3월 5일
댓글: Trine Høy Oddershede 2021년 3월 5일
%% Inputs
Q = 0.16; %[m^3/s] flowraten
r_2 = 0.25; %[m]Ydre radius
D_2 = 0.50 %[m]Ydre diameter
b_2 = 0.5 %[m]Blad højden
beta_2 = 75 %[grader] vinklen
U_2 = (r_2 * omega) * 2*pi * 1/60
%Head
H_plot = zeros(50,1);
for Q = 1:1:50;
H = (U_2^2*Q/g - U_2/(pi*D_2*b_2*g*tand(beta_2))); % [m] Beregning af head
H_plot(i)= H;
i = i+1;
end
why do i get this error message: Array indices must be positive integers or logical values
  댓글 수: 2
Stephen23
Stephen23 2021년 3월 5일
"why do i get this error message: Array indices must be positive integers or logical values"
Because you do not define i, and so it defaults to the imaginary unit (which is not a valid index).
Note that it is usually much clearer to loop over an index variable rather than looping over data (e.g. Q). Note that you define Q twice in your code, which is unlikely to be intentional.
Most likely you can replace that loop with vectorized code:

댓글을 달려면 로그인하십시오.

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 3월 5일
Q = 0.16; %[m^3/s] flowraten
r_2 = 0.25; %[m]Ydre radius
D_2 = 0.50 %[m]Ydre diameter
b_2 = 0.5 %[m]Blad højden
beta_2 = 75 %[grader] vinklen
%Define Omega and g
omega=
g=
Remaining code (Loop can be avoided here)
U_2=(r_2 * omega) * 2*pi * 1/60
Q = 1:1:50;
H = (U_2^2*Q./g - U_2/(pi*D_2*b_2*g*tand(beta_2)));
plot(H,Q);

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by