How do we use while loop with break command to avoid infinite loop?
6 views (last 30 days)
Show older comments
Chao-Zhen Liu
on 28 Oct 2018
Commented: Chao-Zhen Liu
on 29 Oct 2018
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!
2 Comments
Stephen23
on 28 Oct 2018
Within the while loop it is not clear what you expect those continue and break statements to achieve: surely the break could just be replaced by the while condition itself... and continue is superfluous, because you have nothing following that if statement anyway.
It is not clear what you want from us, but if you are having problems then you will have to debug your code: start by printing the values on each iteration or using the debug mode, and use them to narrow down why the code does not behave as you expect.
Accepted Answer
Walter Roberson
on 28 Oct 2018
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.
5 Comments
Walter Roberson
on 28 Oct 2018
fzero() does not use randomness. Given the same inputs, it always calculates the same outputs. Your loop is not changing alpha, n, du, kursti_max, aLehat_rdata, or x0, so unless fun_max can return different values when called twice with the same parameters, your overall fzero calculation is invariant.
More Answers (0)
See Also
Categories
Find more on Interpolation of 2-D Selections in 3-D Grids in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!