can anyone help me please ?
1 view (last 30 days)
Show older comments
can anyone solve me this problem, ican't detect the error ?
D=0;
d=0.3;
if th==7.5
(D=d/0.6); %&& (K==0.15);
else if th==7.8
(D=d/0.5); %&& (K==0.2)
else if th==8
(D=d/0.4); %&& (K==0.26);
else if th==8.5
(D=d/0.3); %&& (K==0.31);
else if th==9
(D=d/0.2); %&& (K==0.36);
else th==9.5
(D=d/0.1); %&& (K==0.41);
end
end
end
end
end
L=(D-d)/(2*tan((th/2)*(pi/180)));
2 Comments
Stephen23
on 28 Mar 2018
Edited: Stephen23
on 28 Mar 2018
That code tests for equivalence of floating point values, which very unreliable. Instead of == you should compare the absolute difference against a tolerance, like this:
abs(A-B)<tol
Read these to know more about floating point numbers and why testing for equivalence is leads to bugs:
David Fletcher
on 28 Mar 2018
It's a personal thing I suppose but I tend to find switch - case blocks easier to follow than nested if-elseif blocks
switch round(th*10)
case 75
D=d/0.6
case 78
D=d/0.5
case 80
D=d/0.4
case 85
D=d/0.3
case 90
D=d/0.2
case 95
D=d/0.1
end
Answers (1)
Star Strider
on 28 Mar 2018
First, remove the parentheses around the ‘D’ assignments in the if block,
Second, your if block elseif statements will only execute if ‘th’ is exactly equal to the values you test. If ‘th’ are not exactly equal to them, they will be evaluated with the else condition:
D = d/0.1;
If ‘th’ can only take certain discrete values, then your code will likely do what you want.
If ‘th’ is continuous, consider testing for ranges of ‘th’:
if th <= 7.5
D=d/0.6; %&& (K==0.15);
elseif (th > 7.5) & (th <= 7.8)
D=d/0.5; %&& (K==0.2)
... and so for the rest of the ‘elseif’ tests
end
0 Comments
See Also
Categories
Find more on Prepare and Analyze Model 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!