how to write if Loop (or while loop) in this kind of problem
1 view (last 30 days)
Show older comments
Asif Rashid
on 13 Mar 2021
Commented: Star Strider
on 13 Mar 2021
Dear Colleagues and teachers
i want to write
if NL is between 0.1 and 0.3 then output is NL (eactly at 0.2)
else if
NS is between 0.31 and 0.5 then output is NS (eactly at 0.4)
i just want to know the structure of this kind of if loop
---------------------------------------------
i have these limits and i want to write if loop to execute these statements.
NL [0.1 0.2 0.3]
NS [0.31 0.4 0.5]
ZE [0.51 0.6 0.7]
PS [0.71 0.8 0.9]
PL [0.91 1 1.1]
Looking forward for your kind help please. thank you
0 Comments
Accepted Answer
Star Strider
on 13 Mar 2021
2 Comments
Star Strider
on 13 Mar 2021
Thjat appears to me to be correct, however it may be necessary first to define:
NL = 0.2;
NS = 0.4;
ZE = 0.6;
PS = 0.8;
PL = 1;
Then the complete code is:
f = @(x,NS,NL,ZE,PS,PL) ((x>=0.1) & (x<=0.3)).*NL + ((x>0.3) & (x<=0.5)).*NS + ((x>0.51) & (x<=0.7)).*ZE + ((x>0.71) & (x<=0.9)).*PS + ((x>0.9) & (x<=1.1)).*PL;
NL = 0.2;
NS = 0.4;
ZE = 0.6;
PS = 0.8;
PL = 1;
x = linspace(-1, 2);
figure
plot(x, f(x,NS,NL,ZE,PS,PL), 'LineWidth',1.5)
grid
and it appears to produce the correct result.
Check the plot to see if it does what you want it to do.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!