can anyone help me please ?

2 ビュー (過去 30 日間)
diadalina
diadalina 2018 年 3 月 28 日
コメント済み: David Fletcher 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 Center および File ExchangePrepare and Analyze Model についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by