フィルターのクリア

try&erorr

2 ビュー (過去 30 日間)
mohamed saber
mohamed saber 2012 年 1 月 25 日
i have variables FS,di and others .. after calculation i get new value of FS by name FS1 .... FS1& FS must be closed together ... if FS1 > FS .. di must be decreased ... if FS1 < FS .. di must be increased ... so i used while loop toll=input( 'Enter desired tollerance : ');
while FS1~= FS
if FS1>FS+toll
di=di-.1;
elseif FS1<FS-toll
di=di+.1;
end end but it doesn't work
[Information from duplicate question]
hello .... i have a problem with try&error
the given is FS,di and other variables
from this variables i get a new value of FS named by (FS1)
FS1 must be very close to FS to end the problem
if FS1>FS ... di must be decreased ... until FS1& FS are very closed
if FS1<FS ... di must be increased ... until FS1& FS are very closed
i have a problem with my code ....
clear, clc
fprintf(' \n <<< this programe is used to determine the diameter of power screw >>> \n\n ')
Sy=325;
F=25000;
fth=.15;
fco=.15;
dco=40;
FS=2.5;
% calculate inner diameter depend on axial load only without torque
di_1=((4*F*FS)/(Sy*pi))^.5;
di=di_1+.5*(di_1); %increase diameter to compensate torque which neglected
P= 5; % pitch
do=di+P ; % outer diameter
dm=(di+do)/2; % mean diameter
n=1;
L=n*P ; % lead
alfa=atand((L/(pi*dm))); % lead angle
phi=atand(fth) ;
B=15; % assumed thread angle= 30
Tth= F*(dm/2) * ((tand(alfa)+(tand(phi)*secd(B)))/(1-(tand(alfa)*tand(phi)*secd(B)))) ; % torque of thread
Tc=(F*fco*.5*dco); % torque of collar
Ttot=(Tth+Tc); % total torque
taw=((16*Ttot)/(pi*di^3)); % shear stress
seg=(4*F)/(pi*di^2); % normal stress
FS1=(Sy)/((seg^2 + 3*taw^2)^.5); % calculate new f.o.s
dfs=(FS1-FS);
toll=input('Enter desired tollerance ');
while abs(dfs)~=toll
if dfs>toll
di=di-.1;
elseif dfs<toll
di=di+.1;
end
end

回答 (2 件)

David Young
David Young 2012 年 1 月 25 日
You need to carry out some computation that changes the value of FS or of FS1 inside the loop. Perhaps that computation depends on di. As your code stands, there is nothing to change FS or FS1. If they are different at the start of the loop they will remain different, and the loop will never terminate.
A side point: if the computation leads to non-integer values for FS and FS1, you should not compare them with ~=. Instead, use
while FS1 > FS+toll || FS1 < FS - toll
  2 件のコメント
mohamed saber
mohamed saber 2012 年 1 月 25 日
FS is contant which user suggest at the beginning of programe . and FS1 depend on the value of di
i use while FS1 > FS+toll || FS1 < FS - toll
but it doesn't work too
this is the full code ... i enter the value of all variables
clear, clc
fprintf(' \n <<< this programe is used to determine the diameter of power screw >>> \n\n ')
Sy=325;
F=25000;
fth=.15;
fco=.15;
dco=40;
FS=2.5;
% calculate inner diameter depend on axial load only without torque
di_1=((4*F*FS)/(Sy*pi))^.5;
di=di_1+.5*(di_1); %increase diameter to compensate torque which neglected
P= 5; % pitch
do=di+P ; % outer diameter
dm=(di+do)/2; % mean diameter
n=1;
L=n*P ; % lead
alfa=atand((L/(pi*dm))); % lead angle
phi=atand(fth) ;
B=15; % assumed thread angle= 30
Tth= F*(dm/2) * ((tand(alfa)+(tand(phi)*secd(B)))/(1-(tand(alfa)*tand(phi)*secd(B)))) ; % torque of thread
Tc=(F*fco*.5*dco); % torque of collar
Ttot=(Tth+Tc); % total torque
taw=((16*Ttot)/(pi*di^3)); % shear stress
seg=(4*F)/(pi*di^2); % normal stress
FS1=(Sy)/((seg^2 + 3*taw^2)^.5); % calculate new f.o.s
toll=.2; % tollerance
while FS1 > FS+toll || FS1 < FS - toll
if FS1>FS+toll
di=di-.1;
elseif FS1<FS-toll
di=di+.1;
end
end
David Young
David Young 2012 年 1 月 25 日
The problem is that there is nothing inside the while loop that changes the value of FS or of FS1. Changing the value of di does not automatically change anything else: you must calculate a new value for FS or FS1 inside the loop.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2012 年 1 月 27 日
Your code has
while abs(dfs)~=toll
if dfs>toll
di=di-.1;
elseif dfs<toll
di=di+.1;
end
end
You do not change dfs or toll in the loop, so the loop will not be executed at all, or will be executed infinitely.
Warning: two floating point numbers calculated different ways will seldom become exactly equal through calculation! This is a fundamental problem in computing. See http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
Important point
When you have a line of code such as
FS1=(Sy)/((seg^2 + 3*taw^2)^.5);
then that does not mean that at any time that FS1 is referenced, that the value should be calculated according to the values of all of the variables (such as Sy) at the time that FS1 is referenced!
Instead, a line of code such as that assignment means that the values of the variables are evaluated right when that line of code is executed, and the resulting value is to be stored in FS1, and then no matter how those other variables change, the value of FS1 will stay that calculated value until FS1 is specifically over-written.
So, when you change di within the loop, all that does is change the value for di, and it does not trigger a recalculation of "do" or "dm" or "alfa" or any of the other variables including FS1. If you want those variables to be updated and a new FS1 value calculated, then you have to execute those assignments again.

カテゴリ

Help Center および File ExchangeStress and Strain についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by