Looping for a specified number of iteration

조회 수: 4 (최근 30일)
SITI AISHAH
SITI AISHAH 2019년 10월 6일
편집: ag 2024년 12월 29일
clc
clear all
%-------------------------------------------------------------------------%
% Specify the variable %
oil_SAE = 10;
Temp_in = 85; % Sump Temperature (°C)
d = 0.15; % Shaft Journal Diameter (Meter, m)
r = (d/2); % Shaft Journal Radius (Meter, m)
N = (166.69/60); % Shaft Speed (revolution per second,rps)
W = 9000; % Radial Load (Newton, N)
L = 0.3; % Bearing Length (Meter, m)
c = 0.000026; % Bearing clearance (Meter,m)
%-------------------------------------------------------------------------%
% Calculate the variable %
C_ratio = r/c; % Clearance ratio
P = W/(d*L); % Nominal Pressure (Pascal)
Slenderness = L/d ; % Bearing Aspect Ratio
%-------------------------------------------------------------------------%
% Viscosity - Temperature Chart
Temperature = xlsread ('MFT','viscosity-temperature chart','A4:A30');
if oil_SAE == 10
V0 = 0.1089*10^(-3);
b = 1157.5 ;
elseif oil_SAE == 20
V0 = 0.0937*10^(-3);
b = 1271.6;
elseif oil_SAE == 30
V0 = 0.0971*10^(-3);
b = 1360.0;
elseif oil_SAE == 40
V0 = 0.0827*10^(-3);
b = 1474.4;
elseif oil_SAE == 50
V0 = 0.1171*10^(-3);
b = 1509.6;
elseif oil_SAE == 60
V0 = 0.1288*10^(-3);
b = 1564.0;
end
%-------------------------------------------------------------------------%
% Calculation to find average temperature and viscosity %
Tf = Temp_in + 3; % Assumed Average Temperature (°C)
V = V0*exp (b/((1.8*Tf)+127)); % Viscosity (Pa.s)
S = (C_ratio)^2*((V*N)/P); % Sommerfeld Number
if Slenderness == 1
delta_T = (0.349109 + (6.00940*S)+(0.047467*(S^2)))*(P*(10^-6))/0.120; % Temperature Rise(°C) at l/d=1
elseif Slenderness == 1/2
delta_T = (0.394552 + (6.392527*S)-(0.036013*(S^2)))*(P*(10^-6))/0.120; % Temperature Rise(°C) at l/d=1/2
elseif Slenderness == 1/4
delta_T = 0.933828 + (6.437512*S)- (0.011048*(S^2))*(P*(10^-6))/0.120; % Temperature Rise(°C) at l/d=1/4
else
delta_T = ((33405*(S)^6)-(51557*(S)^5)+(30669*(S)^4)-(8871.9*(S)^3)+(1297.4*(S)^2)-(85.088*(S))+2.6111)*(P*(10^-6))/0.120; % Temperature Rise(°C) at l/d=Infinity
end
T_average = Temp_in + (delta_T/2); % Average temperature (°C)
iter=0;
while iter <= 5 && abs (Tf-T_average)>0.001
iter = iter + 1;
delta = (Tf-T_average)/2;
Tf = Tf-delta;
V = V0*exp (b/((1.8*Tf)+127)); % Viscosity (Pa.s)
S = (C_ratio)^2*((V*N)/P); % Sommerfeld Number
if Slenderness == 1
delta_T = (0.349109 + (6.00940*S)+(0.047467*(S^2)))*(P*(10^-6))/0.120; % Temperature Rise(°C) at l/d=1
elseif Slenderness == 1/2
delta_T = (0.394552 + (6.392527*S)-(0.036013*(S^2)))*(P*(10^-6))/0.120; % Temperature Rise(°C) at l/d=1/2
elseif Slenderness == 1/4
delta_T = 0.933828 + (6.437512*S)- (0.011048*(S^2))*(P*(10^-6))/0.120; % Temperature Rise(°C) at l/d=1/4
else
delta_T = ((33405*(S)^6)-(51557*(S)^5)+(30669*(S)^4)-(8871.9*(S)^3)+(1297.4*(S)^2)-(85.088*(S))+2.6111)*(P*(10^-6))/0.120; % Temperature Rise(°C) at l/d=Infinity
end
T_average = Temp_in + (delta_T/2); % Average temperature (°C)
disp ('Table for iteration');disp (' c Tf V S deltaT T average ');
format shortG
disp ([c',Tf', V',S',delta_T', T_average'])
end
** I want to loop the iteration as long as Tf-T_average is not in negative value and also not more than 0.001 which is 0 for 5th iteration. If the 5th iteration this is still not satisfied, then I want to increase the c where c=c+0.000001 for the next iteration until Tf-T_average=0. Please help me.Thanks

답변 (1개)

ag
ag 2024년 12월 29일
편집: ag 2024년 12월 29일
Hi Siti,
To incorporate the described logic, you can modify the code as shown below:
% Initialize the iteration counter
iter = 0;
% Start a loop that continues until the difference between Tf and T_average is small enough
while abs(Tf - T_average) > 0.001
% Increment the iteration counter
iter = iter + 1;
% Place your main calculation logic here
% Check every 5 iterations
if (mod(iter, 5) == 0)
if(Tf - T_average) != 0)
% if the convergence condition is not met increament c
c = c + 0.000001;
else
% if the convergence condition is met, break out of the loop
break
end
end
end
Please note, that if the breaking condition is not met the loop will continue running forever, so please ensure that the convergence condition is met after certain iterations.
Hope this helps!

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by