can anyone help me please ?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2018-3-28
编辑:Stephen23
2018-3-28
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
2018-3-28
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
回答(1 个)
Star Strider
2018-3-28
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Prepare and Analyze Model 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!