% 1. mu    - initial mean
% 2. sigma - initial standard deviation
% 3. rho   - rarity parameter
% 4. alpha - smoothing parameter
% 5. N     - sample size
% 6. eps   - error tolerance
% ceros([-6 3],[10 5],0.1,0.7,100,0.01)

function ceros(mu,B,rho,alpha,N,eps,beta,q)

xx=linspace(-2,2,100);
[XX,YY]=meshgrid(xx,xx);
ZZ=100*(XX - YY.^2).^2 + (1 - XX).^2;

VV(1:5)=linspace(0,10,5);
VV(6:10)=linspace(10,100,5);
VV(11:15)=linspace(100,1000,5);
VV(16:20)=linspace(1000,max(max(ZZ)),5);

tic
t=0; % iteration counter
n=size(mu,2);
while max(diag(B)) > eps
    t=t+1;
  B_Mod = beta - beta*(1-1/t)^q;
		   contour(XX,YY,ZZ,VV)
		   hold on;
plot(1,1,'bx')
plotce(mu,B*B,floor(rho*N));
drawnow;
hold off;
    x=tinormrand(mu,B,2,N);
    SX=rosenbrock(x);
    sortSX=sortrows([x SX],n+1);
    mu=alpha.*mean(sortSX(1:rho*N,1:n))+(1-alpha).*mu; % smoothed updating of mu
[U,S,V]=svd(cov(sortSX(1:rho*N,1:n)));
B=B_Mod*U*sqrtm(S)*V'+(1-B_Mod)*B; % smoothed updating of covariance matrix

    fprintf('%g  %3.4f  %3.4f  %3.4f  %3.4f  %3.4f  \n', t, sortSX(1,n+1),sortSX(rho*N,n+1),mu)
  fprintf('\n')
statuspress=waitforbuttonpress;
end
