If文、for文を回し、パラスタを実行したが、出てこない答えがある。
3 views (last 30 days)
Show older comments
条件を満たすような5つの変数のパターンの組み合わせを全て出したいです。(パラスタを行いたい)
for文、if文を使用し、条件を満たさなければ、直ちに次のパラスタを実行するようなものが理想です。
自分で一回コードを組みましたが、(Za、Zb、Zc、Zd、Ze)=(11、11、34、12、38)も答えになるはずなのに、解析結果に出てきません。何がいけないのでしょうか…
▼やりたいこと
任意の値:P=5
変数:Za、Zb、Zc、Zd、Ze (全て整数で、値は5~100の範囲)
条件:U=Zc/Za>1
V=(Zb・Ze)/(Zc・Zd)>1
W=(Zb・Ze)/(Za・Zd)>1
X=(Za+Zc)/N (整数)
Y=(Za・Zd+Zb・Ze)/(P・(ZbとZdの最大公約数)) (整数)
Z=(Zc・ZdーZb・Ze)/(P・(ZbとZdの最大公約数)) (整数)
R=(Za+Zc)/P (整数)
I=(V+W)/(V-1)=171
出力イメージとしては、
(Za、Zb、Zc、Zd、Ze)=(11、11、34、12、38)
・
・
・
clear
N=100;%%歯数範囲指定
P=5; %%ピニオンの数
K=1;
for Za=5:N %探索歯数範囲
for Zb=5:N %探索歯数範囲
for Zc=5:N %探索歯数範囲
for Zd=5:N %探索範囲
for Ze=5:N %探索範囲
U=Zc/Za; %i0
V=(Zb*Ze)/(Zc*Zd); %i0'
W=(Zb*Ze)/(Za*Zd); %i0"
if U<=1
continue
end
if V<=1
continue
end
if W<=1
continue
end
if W<=U
continue
end
if W<=V
continue
end
I=(V+W)/(V-1); %減速比計算
if I~=171 %希望の減速比を入れてね…!!
continue
end
Q=gcd(Zb,Zd); %ZbとZdの最大公約数
X=(Za+Zc)/P;
Y=(Za*Zd+Zb*Ze)/(P*Q);
Z=(Zc*Zd-Zb*Ze)/(P*Q);
R=(Za+Zc)/P;
if X~=floor(X)
continue
end
if Y~=floor(Y)
continue
end
if Z~=floor(Z)
continue
end
if R~=floor(R)
continue
end
A(K,:)=[Za Zb Zc Zd Ze];
K=K+1;
end
end
end
end
end
0 Comments
Accepted Answer
交感神経優位なあかべぇ
on 18 Jan 2023
(Za、Zb、Zc、Zd、Ze)=(11、11、34、12、38)の時、
I=(V+W)/(V-1)の計算結果は171ですが、if I ~= 171の判定でtrueになり、continue;に流れています。
これは、浮動小数点誤差が生じ、厳密に171にならなかったためです。(浮動小数点型の等号、不等号判定を禁止にするルールを設ける事例をよく聞きます。)
浮動小数点誤差の対策として、誤差1e-5まで許容させる例を記述しました。
clear
N=100;%%歯数範囲指定
P=5; %%ピニオンの数
K=1;
for Za=5:N %探索歯数範囲
for Zb=5:N %探索歯数範囲
for Zc=5:N %探索歯数範囲
for Zd=5:N %探索範囲
for Ze=5:N %探索範囲
U=Zc/Za; %i0
V=(Zb*Ze)/(Zc*Zd); %i0'
W=(Zb*Ze)/(Za*Zd); %i0"
if U<=1
continue
end
if V<=1
continue
end
if W<=1
continue
end
if W<=U
continue
end
if W<=V
continue
end
I=(V+W)/(V-1); %減速比計算
% 1e-5の誤差まで許容(浮動小数点誤差の対策)
if abs(I - 171) > 1e-5 %希望の減速比を入れてね…!!
continue
end
Q=gcd(Zb,Zd); %ZbとZdの最大公約数
X=(Za+Zc)/P;
Y=(Za*Zd+Zb*Ze)/(P*Q);
Z=(Zc*Zd-Zb*Ze)/(P*Q);
R=(Za+Zc)/P;
if X~=floor(X)
continue
end
if Y~=floor(Y)
continue
end
if Z~=floor(Z)
continue
end
if R~=floor(R)
continue
end
A(K,:)=[Za Zb Zc Zd Ze];
K=K+1;
end
end
end
end
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!