if statement executing even when condition is false

4 visualizaciones (últimos 30 días)
Pratik Sahoo
Pratik Sahoo el 1 de Mzo. de 2023
Editada: Pratik Sahoo el 6 de Mzo. de 2023
The if statement executes regardless of the condition (Axialloadcalculated < res). Axialloadcalculated is a 1x1 double, Axialloads(r) is the rth element of 1xn array. How do I make the elseif statement trigger?
res = Axialloads(r)
for i = 0:1:round(eccu/strain_step_Moment) % Change curvature, from maximum to minimum
Axialloadcalculated = 0;
if Axialloadcalculated < res
for j = 1:n_layers
epsilon(1, j) = (eccu - i*strain_step_Moment)*(NA - (j-0.5)*layer_thickness)/(NA - cc - dt);
Load_depth(1, j) = areas_cc(1, j)*fc_unconfined(fck_unconfined, epsilon(1, j), ecp, ecu) + areas_core(1, j)*fc_confined(fck_confined, epsilon(1, j), eccp, eccu) + areas_longitudinal(1, j)*fs(fy, epsilon(1, j), eu, Es);
end
Axialloadcalculated = sum(Load_depth)
NA = NA + 1
elseif Axialloadcalculated > res
break
end
Load(r+5, i+1) = Axialloadcalculated;
for l = 1:n_layers
leverarm(1,l) = NA - (l - 0.5)*layer_thickness; % Taking moment about NA
Moment_depth = (areas_cc(1, l)*fc_unconfined(fck_unconfined, epsilon(1, l), ecp, ecu) + areas_core(1, l)*fc_confined(fck_confined, epsilon(1, l), eccp, eccu) + areas_longitudinal(1, l)*fs(fy, epsilon(1, l), eu, Es))*leverarm(1, l);
end
Moment(r+5,i+1) = sum(Moment_depth);
phi(r+5,i+1) = -(epsilon(1,1) - epsilon(1,n_layers))/(H - 5);
end
  2 comentarios
Walter Roberson
Walter Roberson el 1 de Mzo. de 2023
If you put in a breakpoint at
for j = 1:n_layers
and you display
Axialloadcalculated - res
what do you observe? You should only get negative results; a non-negative result should imply that the elseif would be tested.
CAM
CAM el 2 de Mzo. de 2023
The variable, res, must be a positive number, because for every iteration of "i", you reset Axialloadcalculated = 0. In the code posted above, res is constant, so the code will always execute the IF block (res>0) and never get to the ELSEIF block.
Should "Axialloadcalculated = 0" be moved above the for loop? It looks like an initialization statement to me.

Iniciar sesión para comentar.

Respuestas (2)

Arka
Arka el 6 de Mzo. de 2023
Hi,
Axialloadcalculated = 0; is written inside the for loop. In every iteration, the value of Axialloadcalculated is getting set to 0. The value of res is getting set just outside the loop. So, if res is greater than 0, in every iteration of the loop, the condition if Axialloadcalculated < res is getting evaluated to true, and the code enters the first if block.
If Axialloadcalculated = 0; is an initialization statement, you can move it outside the loop, so that the value of it does not get set to 0 on each iteration.
The updated code is as follows:
res = Axialloads(r)
Axialloadcalculated = 0;
for i = 0:1:round(eccu/strain_step_Moment) % Change curvature, from maximum to minimum
if Axialloadcalculated < res
for j = 1:n_layers
epsilon(1, j) = (eccu - i*strain_step_Moment)*(NA - (j-0.5)*layer_thickness)/(NA - cc - dt);
Load_depth(1, j) = areas_cc(1, j)*fc_unconfined(fck_unconfined, epsilon(1, j), ecp, ecu) + areas_core(1, j)*fc_confined(fck_confined, epsilon(1, j), eccp, eccu) + areas_longitudinal(1, j)*fs(fy, epsilon(1, j), eu, Es);
end
Axialloadcalculated = sum(Load_depth)
NA = NA + 1
elseif Axialloadcalculated > res
break
end
Load(r+5, i+1) = Axialloadcalculated;
for l = 1:n_layers
leverarm(1,l) = NA - (l - 0.5)*layer_thickness; % Taking moment about NA
Moment_depth = (areas_cc(1, l)*fc_unconfined(fck_unconfined, epsilon(1, l), ecp, ecu) + areas_core(1, l)*fc_confined(fck_confined, epsilon(1, l), eccp, eccu) + areas_longitudinal(1, l)*fs(fy, epsilon(1, l), eu, Es))*leverarm(1, l);
end
Moment(r+5,i+1) = sum(Moment_depth);
phi(r+5,i+1) = -(epsilon(1,1) - epsilon(1,n_layers))/(H - 5);
end

Pratik Sahoo
Pratik Sahoo el 6 de Mzo. de 2023
Editada: Pratik Sahoo el 6 de Mzo. de 2023
Thanks for the answers. I figured out the problem.
Axialloadcalculated is not an integer but res is an integer, hence if statement is always becoming true.
Putting round(Axialloadcalculated) < res fixed the issue

Categorías

Más información sobre General Applications en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by