Index exceeds matrix dimensions

3 visualizaciones (últimos 30 días)
sheng  deng
sheng deng el 1 de Mayo de 2018
Comentada: sheng deng el 1 de Mayo de 2018
I guess this probably the most asked question. I trying to run a for loop which listed below
for i=1:1:288
if H_tank1(i)<14.5
if H_tank1(i)>H_limit1
if H_tank1(i)>H(i) %%%ebb
H01(i)=H_tank1(i)-H(i);
Q1(i)=A_inlet*sqrt(2*9.81*H01(i));
if Q1(i)<150;
Q1(i)=Q1(i);
else
Q1(i)=150;
end
P1(i)=0.9*9.81*1030*(Q1(i))*H01(i)/1000;
if H_tank2(i)<14.5
if H_tank2(i)>H_limit2
if H_tank2(i)>H_tank1(i)
H02(i)=H_tank2(i)-H_tank1(i);
Q_ex(i)=A_inlet_ex*sqrt(2*9.81*H02(i));
Sum2(i+1)=Sum2(i)-Q_ex(i)*n;
H_tank2(i)=Sum2(i)/A_tank2;
else
H02(i)=H_tank1(i)-H_tank2(i);
Q_ex(i)=0;
Sum2(i+1)=Sum2(i);
H_tank2(i)=Sum2(i)/A_tank2(i);
end
else
if H_tank2(i)>H_tank1(i)
H02(i)=H_tank2(i)-H_tank1(i);
Q_ex(i)=0;
Sum2(i+1)=Sum2(i);
H_tank2(i)=Sum2(i)/A_tank2(i);
else
H02(i)=H_tank1(i)-H_tank2(i);
Q_ex(i)=A_inlet_ex*sqrt(2*9.81*H02(i));
Sum2(i+1)=Sum2(i)+Q_ex(i)*n;
H_tank2(i)=Sum2(i)/A_tank2;
end
end
end
Sum1(i+1)=Sum1(i)-(Q1(i)-Q_ex(i))*n;
H_tank1(i)=Sum1(i)/A_tank1;
else
H01(i)=-H_tank(i)+H(i);
Q1(i)=A_inlet*sqrt(2*9.81*H01(i));
if Q1(i)<150;
Q1(i)=Q1(i);
else
Q1(i)=150;
end
P1(i)=0.9*9.81*1030*(Q1(i))*H01(i)/1000;
if H_tank2(i)<14.5
if H_tank2(i)>H_limit2
if H_tank2(i)>H_tank1(i)
H02(i)=H_tank2(i)-H_tank1(i);
Q_ex(i)=0;
Sum2(i+1)=Sum2(i);
H_tank2(i)=Sum2(i)/A_tank2(i);
else
H02(i)=H_tank1(i)-H_tank2(i);
Q_ex(i)=A_inlet_ex*sqrt(2*9.81*H02(i));
Sum2(i+1)=Sum2(i)-Q_ex(i)*n;
H_tank2(i)=Sum2(i)/A_tank2;
end
else
if H_tank2(i)>H_tank1(i)
H02(i)=H_tank2(i)-H_tank1(i);
Q_ex(i)=0;
Sum2(i+1)=Sum2(i);
H_tank2(i)=Sum2(i)/A_tank2(i);
else
H02(i)=H_tank1(i)-H_tank2(i);
Q_ex(i)=A_inlet_ex*sqrt(2*9.81*H02(i));
Sum2(i+1)=Sum2(i)+Q_ex(i)*n;
H_tank2(i)=Sum2(i)/A_tank2;
end
end
end
Sum1(i+1)=Sum1(i)+(Q1(i)-Q_ex(i))*n;
H_tank1(i)=Sum1(i)/A_tank1;
end
end
end
end
I have set the starting values for i=1;
H_tank1(1)=13.5;
H_tank2(1)=13.5;
Sum1(1)=H_tank1(1)*A_tank1;
Sum2(1)=H_tank2(1)*A_tank2;
and
H is a vector which has 288 values. Every time I ran the code, I got a message
Index exceeds matrix dimensions.
Error in test (line 22)
if H_tank1(i)<14.5
and that is for the first loop. which it got stuck. Any suggestion? I don't know why this is happening. Thanks for any helps.
  2 comentarios
Geoff Hayes
Geoff Hayes el 1 de Mayo de 2018
Sheng - you say that H is a vector which has 288 values. But is H the same as H_tank1. What are the dimensions of H_tank1?
sheng  deng
sheng deng el 1 de Mayo de 2018
Hi Geoff I want to calculate all the variables based on H, so for each loop, there would be a new H_tank1. like H_tank1(2) would be calculated from H(2) . Thanks

Iniciar sesión para comentar.

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 1 de Mayo de 2018
sheng - so initially, H_tank1 is a scalar or 1x1 as initialized with
H_tank1(1)=13.5;
If that is the case, then your code will always succeed on the first iteration of the loop but fail on the second because you have never added any new elements to H_tank1
if H_tank1(i)<14.5
will always fail for i > 1. Is this really what you want to do? Or do you want to use H instead for this condition?
  1 comentario
sheng  deng
sheng deng el 1 de Mayo de 2018
Thanks for your help. I have change the code, I changed H_tank(i) in the calculation for H_tank(i+1). which solved the problem.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by