if statement executing even when condition is false

조회 수: 13 (최근 30일)
Pratik Sahoo
Pratik Sahoo 2023년 3월 1일
편집: Pratik Sahoo 2023년 3월 6일
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
Walter Roberson
Walter Roberson 2023년 3월 1일
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 2023년 3월 2일
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.

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

답변 (2개)

Arka
Arka 2023년 3월 6일
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 2023년 3월 6일
편집: Pratik Sahoo 2023년 3월 6일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by