Multiple elseif command not working even when condition is true

%% 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

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 .

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

 채택된 답변

DGM
DGM 2021년 4월 29일
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

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

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

추가 답변 (1개)

VBBV
VBBV 2021년 4월 29일
편집: VBBV 2021년 4월 29일
%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

VBBV
VBBV 2021년 4월 30일
편집: VBBV 2021년 4월 30일
%if true
Bita=round(abs(1-Alpha-x),1);
if Bita == 0.1000
....
....
elseif Bita == 0.2000
....
....
elseif Bita == 0.3000
....
....
end
% so on for other input values
The other option is round the value of Bita to single decimal units as shown at beginning
Thank you for reply
Bita=round((1-Alpha-x)*10)/10; this worked for me suggested by DGM, I hope this also will serve same purpose as of this suggested by you, Bita=round(abs(1-Alpha-x),1);
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.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 4월 29일

댓글:

2021년 4월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by