elseif statement wont execute
2 ビュー (過去 30 日間)
古いコメントを表示
The code is to determine outflow from a tank with variable input (hydrograph). The method is usually called chainsaw routing. There are two outlets in the tank, one at 0 elevation, the other at 2 ft. Outlets have different sizes. Naturally I used if and elseif statements to calculate outflow from the two orifices, as the top one does not engage until at least 2 feet of head is reached. However, the elseif statement is not executing; it appears everything just gets calculated from the if statement. One might think that 2 feet of head is never reached, but plot(x,h) shows that head gets to be nearly 8 ft. I am relatively new to coding, and cannot see whats wrong. Please help me find the error
Z = load('routing data.txt') ;
x = Z(:,1) ;
I = Z(:,2) ;
r1 = 1.75;
r2 = 2.25;
Aclarifier= 1800;
Aoutlet1 = pi*(r1/12)^2;
Aoutlet2 = pi*(r2/12)^2;
for i = 2:length(x)
h(1) = 0;
O(1) = 0;
dh(1) = 0;
I(1) = 0;
O1(1) = 0;
O2(1) = 0;
dh(i) = (I(i-1)*60*2)/Aclarifier - (O(i-1)*60*2)/Aclarifier;
h(i) = dh(i-1)+h(i-1);
if ( 0 < (h(i)) <= 2)
O(i) = (.6)*Aoutlet1*sqrt(64.4*h(i)) ;
elseif (h(i) > 2)
O2(i) = (.6)*(Aoutlet2)*sqrt(64.4*(h(i)-2));
O1(i) = (.6)*(Aoutlet1)*sqrt(64.4*h(i));
O(i) = O1(i)+O2(i);
else
O(i) = 0 ;
dh(i) = 0;
end
end
0 件のコメント
採用された回答
Walter Roberson
2012 年 3 月 15 日
Recode
if ( 0 < (h(i)) <= 2)
to
if 0 < h(i) && h(i) <= 2
Otherwise the code is interpreted as ((0<h(i)) <= 2) and the first part of that returns a logical value (0 or 1), and 0 and 1 are always <= 2 so the condition was always true.
4 件のコメント
Walter Roberson
2012 年 3 月 16 日
put a breakpoint in at the "if" and examine h(i), and run the tests by hand such as putting
0 < h(i) && h(i) <= 2
in to the command window.
If your h(i) are for some reason negative, then the "else" branch would be the one activated.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!