Hello, please help me with and advice about conditional statement

2 visualizaciones (últimos 30 días)
Hello, I encountered the following issue: I was defined a vector with a specific length. I want to insert a conditional statement: if value of z<than a specific value then flow to be calculate with a specific formula, else (z>than a specific value) then a flow to be calculate with another formula. Bellow you should view the code:
clc,clf,close all
% Calcul GF ca si canal deschis
b=2, % b -latimea conductei rectangulare
zr=116.50; %zr- cota radier
zc=133.10,
zg=117.50; %zg- cota ax golire de fund
minval=117.50,
z=zr:0.01:zc;
A=z(z<minval);
t=length(A)
B=z(z>=minval)
n=0.014; % n- coeficient de rugozitate
i=(116.50-116.40)/54.76; % i - panta hidraulica
if z(z<minval)
z=A;
A=b*(z-zr).*(z>=zr); %aria sectiunii transversale
C=(1/n)*((b*(z-zr).*(z>=zr))/(b+2*(z-zr).*(z>=zr)))^(1/6);
R=((b*(z-zr).*(z>=zr))/(b+2*(z-zr).*(z>=zr)));
disp('1')
Q=A*C*R^0.5*sqrt(i)
elseif z(z>=minval)
z=B;
% ===================
zz=zg:0.01:zc;
zt=zr:0.01:zc;
lambda=0.025; % lambda- coef. de rezistenta liniara
L=54.76; % L=72.25; L- lungimea conductei
omega=2*2; % A- aria sectiunii rectangulare a conductei
D=2*sqrt(omega/pi); % D- diametrul efectiv al conductei rectangulare
beta=2.42;s=0.001;b=0.005;alfa=90;
Zita_g=beta*(s/b)^4/3*sin(alfa) %Zita_g- pierdere de sarcina gratar
Zita_v=0.10; % Zita_v- pierdere de sarcina vana
Zita_in=0.5; % Zita_in- pierdere de sarcina orif. de intrare
SumZ=Zita_g+Zita_v+Zita_in
miu=1/sqrt(1+SumZ+lambda*L/D)
kg=miu*omega*sqrt(2*9.81);
disp('2')
Q=kg.*(z-zg).^0.5
end
h=figure;plot(Q,z,'linewidth',3);grid; ;xlabel('Q [mc/s)');ylabel('Cota [mdMN]');title('Cheie limnimetrica golire de fund acumulare Pungesti')
disp('test 1')
for k=1:length(z)
disp(sprintf('%10.2f',z(k),Q(k)))
end

Respuesta aceptada

Joost
Joost el 8 de Sept. de 2016
There is indeed a problem with the if-elseif-end statement.
As soon as the if condition is evaluated true, the elseif is not considered anymore. The if condition is in your case not a boolean expression, but a vector with doubles: all values of z lower than minval. Matlab interprets this as true, as long as there is no zero present in the vector. This seems to be the case in your data.
A potential solution is to make a for loop over all values in z. Inside the for loop you can create an if-else-end statement where the if condition only looks at 1 element in z. Based on the value ( < minval or not) the appropriate action is to be taken, and the results vector can be constructed.
In general it is better to avoid for loops, and use Matlab in a vectorized way. However, the for loop can be a good starting point to get your algorithm to work, and to get the job done. At a later stage, vectorization can be considered.
  1 comentario
Cristian Paduraru
Cristian Paduraru el 8 de Sept. de 2016
Hello Joost, I agree with the fact that if statement is complicated to setting out. I try to insert a loop with for instruction followed bt if conditional statement and I've noticed that the first condition is considered if I compress length of z, other solution didn't work. Or the second eq. is considered despite the first. I don't know what could I try else...

Iniciar sesión para comentar.

Más respuestas (1)

Thorsten
Thorsten el 8 de Sept. de 2016
I would program it like this
% compute Q1 with formula 1
...
Q1 = ...
% compute Q2 with formula 2
...
Q2 =
% compute Q based on condition
Q = Q1;
Q(z>=minval) = Q2(z>=minval);
  1 comentario
Cristian Paduraru
Cristian Paduraru el 8 de Sept. de 2016
Hello Thorsten, I cannot put the equal sign on those two eqs. because they aren't same length. If you could be more specific I'l appreciate. Thank You.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing 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