can anyone help me please ?

2 次查看(过去 30 天)
diadalina
diadalina 2018-3-28
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 个评论
David Fletcher
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
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

类别

Help CenterFile Exchange 中查找有关 Prepare and Analyze Model 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by