MATLAB Answers

Multiple elseif command not working even when condition is true

1 view (last 30 days)
Dhananjay Hiwase
Dhananjay Hiwase on 29 Apr 2021
Commented: Dhananjay Hiwase on 30 Apr 2021
%% in below code for Bita =0.3 and 0.1 and 0.2 and 0.6 it is not executing inside statement however for Bita=0.4 and 0.5 only it is working please reply
Alpha = input ('Enter the value of alpha layer from 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 = ');
x= input ('Enter the value of gamma layer from 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 = ');
Bita=1-Alpha-x;
if Bita == 0.1
Fx =0.2065*(x^2)-1.0425*(x)+0.9486;
Fxt =Fx*2326.2; %in Mpa IM7/8552 table 1.4 barbero pg.33
Fxc =-0.1508*(x^3) + 0.1658*(x^2) - 0.8379*(x) + 0.922;
Fxc =Fxc*1200.1; %in Mpa IM7/8552 table 1.4 barbero pg.33
Fxy=0.3524*(x)+.0884;
Fxy=Fxy*1200.1; %in Mpa IM7/8552 table 1.4 barbero pg.33
elseif Bita == 0.2
Fx =-0.2797*(x^2) - .607*(x)+ 0.8112;
Fxt =Fx*2326.2; %in Mpa
Fxc =44.032*(x^6) - 103.87*(x^5) + 94.683*(x^4) - 41.999*(x^3) + 9.3574*(x^2) - 1.716*(x)+ 0.8608;
Fxc =Fxc*1200.1; %in Mpa
Fxy=0.3524*(x)+.0884;Fxy=Fxy*1200.1; %in Mpa
elseif Bita == 0.3
disp(Bita)
Fx =-.8*(x)+.74; Fxc=Fx;
Fxt = Fx*2326.2; %in Mpa
Fxc = Fxc*1200.1; %in Mpa
Fxy=.3524*(x)+.0884;Fxy=Fxy*1200.1; %in Mpa
elseif Bita == 0.4
Fx =-.6*(x-.1)+.565; Fxc=Fx;
Fxt = Fx*2326.2; %in Mpa
Fxc = Fxc*1200.1; %in Mpa
Fxy=.3524*(x)+.0884;Fxy=Fxy*1200.1; %in Mpa
elseif Bita == 0.5
Fx = -.8*(x-.1)+.475; Fxc=Fx;
Fxt = Fx*2326.2; %in Mpa
Fxc = Fxc*1200.1; %in Mpa
Fxy=.3524*(x)+.0884;Fxy=Fxy*1200.1; %in Mpa
elseif Bita == 0.6
Fx =-.8*(x-.1)+.38; Fxc=Fx;
Fxt = Fx*2326.2; %in Mpa
Fxc = Fxc*1200.1; %in Mpa
Fxy=.3524*(x)+.0884;Fxy=Fxy*1200.1; %in Mpa
elseif Bita == 0.7
Fx =-.8*(x-.1)+.284; Fxc=Fx;
Fxt = Fx*2326.2; %in Mpa
Fxc = Fxc*1200.1; %in Mpa
Fxy=.3524*(x)+.0884;Fxy=Fxy*1200.1; %in Mpa
end
  1 Comment
Dhananjay Hiwase
Dhananjay Hiwase on 29 Apr 2021
Please reply to above problem even when condition is true i.e Beta value is 0.3 , then also inside statements i.e formulas of Fxc ,Fx Fxy are not executed .

Sign in to comment.

Accepted Answer

DGM
DGM on 29 Apr 2021
Beware strict equality tests when using floating point numbers. There are a few things you could try:
You could try to clean up the result in the hopes that it matches using an equality test:
Bita=round((1-Alpha-x)*10)/10;
Or the more robust approach is probably to test against a tolerance:
tol=1E-12;
if abs(Bita-0.1)<tol
% ...
elseif abs(Bita-0.7)<tol
% ...
else
% figure out how to handle all other cases
end
And add a case to catch all other inputs
  1 Comment
Dhananjay Hiwase
Dhananjay Hiwase on 30 Apr 2021
Instead of this Bita=1-Alpha-x;
using below
Bita=round((1-Alpha-x)*10)/10; suggested by you
solved my problem
thank you so much for reply
I gave much time on it earlier but was not able to figure it out
"Beware strict equality tests when using floating point numbers." will remember this

Sign in to comment.

More Answers (1)

VBBV
VBBV on 29 Apr 2021
Edited: VBBV on 29 Apr 2021
%f
if Bita <= 0.1
....
....
elseif Bita > 0.1 & Bita <= 0.2
....
....
elseif Bita > 0.2 & Bita <= 0.3
....
....
end
% so on for other input values
Try this option
  3 Comments
Dhananjay Hiwase
Dhananjay Hiwase on 30 Apr 2021
VBBV thank you I have accepted your answer also, as it has absolute command also, so in case if value goes negative it eill still work, but in my case value will not go negative.

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by