# How to terminate the loop

2 views (last 30 days)
Jaydeep Kansara on 25 Nov 2019
Commented: Jaydeep Kansara on 30 Nov 2019
I want to terminate the loop. Condition is satisfying at i = 96, but loop is still running till the end.
after the termination, i want to know the value of i.
for i = 1 : 100
PL_BM(i) = point_load(i) * beam_length / 4;
PL_BM = repelem(PL_BM(i),n);
failure_domain_BM = ( RV_BM - PL_BM(i) ) < 0;
p_f_BM = sum(failure_domain_BM (:) == 1) / n;
if p_f_BM == 1
break
end
end

the cyclist on 25 Nov 2019
Due to floating-point error, it may be that the condition
if p_f_BM == 1
is not exactly met. With floating-point numbers, it's better to check for equality within some tolerance. Try something like
if abs(p_f_BM-1) < tol
for some appropriately small value of tol.

Show 1 older comment
Jaydeep Kansara on 25 Nov 2019
This is the entire code, if it helps.
b18 = 3+0+0+7+6+0+4+9; % b18 = 29
b58 = 6+0+4+9; % b58 = 19
n = 100000000; % No of samples = 10,000,000
rng default
beam_length = 5; % Beam length = 5 m
% Bending Moment Capacity follows a log-normal distribution
mean_BM = 2 * b18; % mean of BM = 58 kNm
COV_BM = 0.01 * b58; % COV of BM = 0.19
variance_BM = (mean_BM * COV_BM)^2; % Variance of BM = 121.4404
mu_BM = log(mean_BM^2 / sqrt(mean_BM^2 + variance_BM)); % Location parameter of BM = 4.0427
sigma_BM = sqrt(log(1 + (variance_BM / mean_BM^2))); % Scale paramaeter of BM = 0.1883
RV_BM = lognrnd(mu_BM, sigma_BM, 1, n); % Random variables of BM
breaking_point = 10^-10;
for i = 1 : 100
PL_BM(i) = point_load(i) * beam_length / 4;
PL_BM = repelem(PL_BM(i),n);
failure_domain_BM = ( RV_BM - PL_BM(i) ) < 0;
p_f_BM = sum(failure_domain_BM (:) == 1) / n;
if abs(p_f_BM - 1) < breaking_point
break
end
end
Ridwan Alam on 25 Nov 2019
n is too big
you need a larger breaking point, maybe 1e-5?
Jaydeep Kansara on 30 Nov 2019
Hello Mr. Cyclist,

Ridwan Alam on 25 Nov 2019
Edited: Ridwan Alam on 25 Nov 2019
b18 = 3+0+0+7+6+0+4+9; % b18 = 29
b58 = 6+0+4+9; % b58 = 19
n = 10000000; % No of samples = 10,000,000
rng default
beam_length = 5; % Beam length = 5 m
% Bending Moment Capacity follows a log-normal distribution
mean_BM = 2 * b18; % mean of BM = 58 kNm
COV_BM = 0.01 * b58; % COV of BM = 0.19
variance_BM = (mean_BM * COV_BM)^2; % Variance of BM = 121.4404
mu_BM = log(mean_BM^2 / sqrt(mean_BM^2 + variance_BM)); % Location parameter of BM = 4.0427
sigma_BM = sqrt(log(1 + (variance_BM / mean_BM^2))); % Scale paramaeter of BM = 0.1883
RV_BM = lognrnd(mu_BM, sigma_BM, 1, n);
for i = 1 : 100
PL_BM(i) = point_load(i) * beam_length / 4;
%PL_BM = repelem(PL_BM(i),n);
failure_domain_BM = sum( RV_BM < PL_BM(i) );
p_f_BM = failure_domain_BM / n
if p_f_BM >= 0.9999
disp(['i=' num2str(i)])
break
end
end
this gave me i=93