try&erorr
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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 个评论
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Stress and Strain 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!