can anyone help me please ?
조회 수: 2 (최근 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!