Multiple elseif command not working even when condition is true

9 次查看(过去 30 天)
%% 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 个评论
Dhananjay Hiwase
Dhananjay Hiwase 2021-4-29
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 个评论
Dhananjay Hiwase
Dhananjay Hiwase 2021-4-30
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 个评论
Dhananjay Hiwase
Dhananjay Hiwase 2021-4-30
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);
Dhananjay Hiwase
Dhananjay Hiwase 2021-4-30
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by