MATLAB: How do we use while loop with break command to avoid infinite loop

break commandif statementwhile loop

Hi everyone,
I am now using while loop to repeat one of my code that I want to get the positive value, but it seem like causing infinite loop. Hope you can give me some advice, the following is my code:
clc; clear
tic
U = 14; L = 7; d = (U-L)/2;
T = ( 3*U+L )/(3+1);
Du = U-T; Dl = T - L; d_star = min(Du,Dl);
du = d/Du; dl = d/Dl;
alpha = 0.10;
aLe = 0.11;
N = 3; % simmulation times
k = 1;
for ksi_given = [-1,-0.5,0, 0.5,1]
if ksi_given <= 0
sigma = fzero(@(x) aLe - (x^2)*( (dl^2*ksi_given^2+1)/d_star^2 ), aLe) ;
mu = ksi_given*sigma + T;
else
sigma = fzero(@(x) aLe - (x^2)*( (du^2*ksi_given^2+1)/d_star^2 ), aLe) ;
mu = ksi_given*sigma + T;
end
every_sigma(k) = sigma;
every_mu(k) = mu;
j = 1;
for n = [30,40,50,100,150,200]
i = 1;
for m = 1:1:N
rdata = normrnd(mu,sigma,1,n);
xbar = mean(rdata);
sd = std(rdata);
aLehat_rdata = ( max( (xbar-T)*d/Du,(T-xbar)*d/Dl )/d_star )^2 + var(rdata)/d_star^2;
kursi_max = 0.5;
kursi_hat = (xbar-T)/sd; % kursi = kursi_hat
%x0 = [0.15-0.07 0.15+0.07];
x0 = 0.15;
fun_max = @(y,C) chi2cdf( (n*( (du.*kursi_max).^2+1 )./C).*aLehat_rdata-y, n-1).* ( 1./ (2.*sqrt(y)) ).* ( (dl^-1).*normpdf( (dl^-1).*sqrt(y) + sqrt(n)*(kursi_max) ) + (du^-1).*normpdf( (du^-1).*sqrt(y) - sqrt(n)*(kursi_max) ) );
exact_UCB_max(i) = fzero(@(C) alpha - integral(@(y) fun_max(y,C),0.00001,(n.*( (du.*kursi_max).^2+1 )./C).*aLehat_rdata), x0 );
test_max = exact_UCB_max(i);
while test_max < 0 || isnan(test_max) == 1
new_exact_UCB_max = fzero(@(C) alpha - integral(@(y) fun_max(y,C),0.00001,(n.*( (du.*kursi_max).^2+1 )./C).*aLehat_rdata), x0 );
if new_exact_UCB_max <0
continue
elseif isnan(new_exact_UCB_max) == 1
continue
else
exact_UCB_max(i) = new_exact_UCB_max;
break
end
end
if kursi_hat <= 0
%B = n*( (dl.*a).^2+1 )./C;
fun2 = @(y,C) chi2cdf( (n*( (dl.*kursi_hat).^2+1 )./C).*aLehat_rdata-y, n-1).* ( 1./ (2.*sqrt(y)) ).* ( (dl^-1).*normpdf( (dl^-1).*sqrt(y) + sqrt(n)*(kursi_hat) ) + (du^-1).*normpdf( (du^-1).*sqrt(y) - sqrt(n)*(kursi_hat) ) );
exact_UCB_hat(i) = fzero(@(C) alpha - integral(@(y) fun2(y,C),0.00001,(n.*( (dl.*kursi_hat).^2+1 )./C).*aLehat_rdata), x0 );
else
%B = n*( (du.*a).^2+1 )/C;
fun2 = @(y,C) chi2cdf( (n*( (du.*kursi_hat).^2+1 )./C).*aLehat_rdata-y, n-1).* ( 1./ (2.*sqrt(y)) ).* ( (dl^-1).*normpdf( (dl^-1).*sqrt(y) + sqrt(n)*(kursi_hat) ) + (du^-1).*normpdf( (du^-1).*sqrt(y) - sqrt(n)*(kursi_hat) ) );
exact_UCB_hat(i) = fzero(@(C) alpha - integral(@(y) fun2(y,C),0.00001,(n.*( (du.*kursi_hat).^2+1 )./C).*aLehat_rdata), x0 );
end
each_aLehat(i) = aLehat_rdata;
i = i + 1;
end
every_UCB_max(j,1:N,k) = exact_UCB_max;
every_UCB_hat(j,1:N,k) = exact_UCB_hat;
CR_1(j,k) = mean(aLe<exact_UCB_max); % kursi = 0.5
CR_2(j,k) = mean(aLe<exact_UCB_hat); %kursi = kursi_hat
average_aLehat(j,k) = mean(each_aLehat);
sd_aLehat(j,k) = std(each_aLehat);
average_exact_UCB_max(j,k) = mean(exact_UCB_max);
sd_exact_UCB_max(j,k) = std(exact_UCB_max);
average_exact_UB_hat(j,k) = mean(exact_UCB_hat);
sd_exact_UB_hat(j,k) = std(exact_UCB_hat);
j = j + 1;
end
k = k + 1;
end
About the while loop part
exact_UCB_max(i) = fzero(@(C) alpha - integral(@(y) fun_max(y,C),0.00001,(n.*( (du.*kursi_max).^2+1 )./C).*aLehat_rdata), x0 );
test_max = exact_UCB_max(i);
while test_max < 0 || isnan(test_max) == 1
new_exact_UCB_max = fzero(@(C) alpha - integral(@(y) fun_max(y,C),0.00001,(n.*( (du.*kursi_max).^2+1 )./C).*aLehat_rdata), x0 );
if new_exact_UCB_max <0
continue
elseif isnan(new_exact_UCB_max) == 1
continue
else
exact_UCB_max(i) = new_exact_UCB_max;
break
end
end
I want to modify the value of exact_UCB_max(i) until its value is positive but when I do this, it will run a long time than I expected, what's wrong with my logic? Hope you can give me some advice, thanks!

Best Answer

In order for a while loop to end, something in the body of the loop has to change the value that is being tested by the loop conditions, or a break has to be reached.
Your loop tests a value that is changed conditionally instead of the loop after which there is a break. Because of the break, if that variable ever does get changed, the loop test will not be done again, so does it make sense to test the value? Perhaps it does, but it could be confusing.
Now the condition under which the test can succeed: if it does succeed after the first iteration of the code, then in order for it to be possible to succeed on a later iteration, it is necessary that the loop changes some value that is used in the computation.
If you examine your code you will see that you are not changing any of the inputs to the fzero. So if the fzero does not give you the result you want the first time, it will give you exactly the same unwanted result every other time.
In other words you have an infinite loop.