How do we use while loop with break command to avoid infinite loop?
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 28 Okt. 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
am 28 Okt. 2018
Bearbeitet: Chao-Zhen Liu
am 28 Okt. 2018
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!