Azzera filtri
Azzera filtri

elseif statement wont execute

2 visualizzazioni (ultimi 30 giorni)
Gleb Skljar
Gleb Skljar il 15 Mar 2012
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

Risposta accettata

Walter Roberson
Walter Roberson il 15 Mar 2012
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 Commenti
Walter Roberson
Walter Roberson il 16 Mar 2012
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 il 16 Mar 2012
problem solved, thanks for the help

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by