Index exceeds matrix dimensions
Show older comments
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 Comments
Geoff Hayes
on 1 May 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
on 1 May 2018
Accepted Answer
More Answers (0)
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!