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

6 visualizaciones (últimos 30 días)
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 comentarios
Stephen23
Stephen23 el 28 de Oct. de 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.
Chao-Zhen Liu
Chao-Zhen Liu el 28 de Oct. de 2018
Editada: Chao-Zhen Liu el 28 de Oct. de 2018
Hi Stephen,
I want to get some solution about how to make all the element in exact_UCB_max are positive.
To be honest, you are right, I have to debug my code, but I have studied it for a long time and I think it is initial value of fzero causing the problem finally. Under the same simulating environment, when N is small, my code could be work, however, when N is a little bigger, MATLAB will return Warning: Infinite or Not-a-Number value encountered.
I have try to increase the initial value with very small increment, hope I can find a suitable initial value that MATLAB will not return error message, but it is helpless.
In the end, I think it is reasonable to drop the value is negative or NaN and resolve it until get the positive value but I have encountered the problem above and hope you can give me some advice.
The following is that I use this simple example to test my logic for original problem:
for m = 1:10
a(m) = rand();
test_max = a(m);
while test_max < 0.1 || test_max > 0.5
new_a = rand();
if new_a <0.1
continue
elseif new_a > 0.5
continue
else
a(m) = new_a;
break
end
end
b = 1;
end
and I find it can return what I expected that a vector a which all the element in it are 0.1<= a(m) <=0.5, m = 1,2,...,10 in the same logic, why can't I applied it to my original problem? Thanks!

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de Oct. de 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 comentarios
Walter Roberson
Walter Roberson el 28 de Oct. de 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.
Chao-Zhen Liu
Chao-Zhen Liu el 29 de Oct. de 2018
Hi Walter,
Thanks! I will try it !

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interpolation of 2-D Selections in 3-D Grids en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by