필터 지우기
필터 지우기

elseif statement wont execute

조회 수: 2 (최근 30일)
Gleb Skljar
Gleb Skljar 2012년 3월 15일
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

채택된 답변

Walter Roberson
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
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.
Gleb Skljar
Gleb Skljar 2012년 3월 16일
problem solved, thanks for the help

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by