If文、for文を回​し、パラスタを実行し​たが、出てこない答え​がある。

2 visualizzazioni (ultimi 30 giorni)
実香
実香 il 18 Gen 2023
条件を満たすような5つの変数のパターンの組み合わせを全て出したいです。(パラスタを行いたい)
for文、if文を使用し、条件を満たさなければ、直ちに次のパラスタを実行するようなものが理想です。
自分で一回コードを組みましたが、(Za、Zb、Zc、Zd、Ze)=(11、11、34、12、38)も答えになるはずなのに、解析結果に出てきません。何がいけないのでしょうか…
▼やりたいこと
 任意の値:P5
 変数:ZaZbZcZdZe (全て整数で、値は5100の範囲)
 条件:U=Zc/Za1
    V=(ZbZe)/(ZcZd)>1
    W=(ZbZe)/(ZaZd)>1
    X=(ZaZc)/N (整数)
    Y=(ZaZdZbZe)/(P(ZbZdの最大公約数)) (整数)
    Z(ZcZdZbZe)/(P(ZbZdの最大公約数)) (整数)
    R=(Za+Zc)/P (整数)
     I=(V+W)/(V-1)=171
出力イメージとしては、
 (ZaZbZcZdZe)=(1111341238
                    ・
                    ・
                    ・
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

Risposta accettata

交感神経優位なあかべぇ
(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

Più risposte (0)

Categorie

Scopri di più su 数値型 in Help Center e File Exchange

Tag

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!