function  [ztrata u y b P]=rizeni(H,mi,system,apriori)

sigma=system.sigma;
%%chce to mzslet na PYB

y=zeros(size(H,1));
b=zeros(size(H,1));
P=zeros(size(H,1));
u=zeros(size(H,1));
ztrata=0;

y(1)=apriori(1);
b(1)=apriori(2);
P(1)=apriori(3);


    for i=1:size(H,1)-1   
        eta_0=abs(y(i))/sigma;
        beta_0=abs(b(i))/sqrt(P(i));
        
        state(1)=eta_0/(1+eta_0);
        state(2)=beta_0^2/(1+beta_0^2);
        
        index=find_in_hyperstate5(state, H(i,:,:));
        
        %plot(H(i,:,1),H(i,:,2),'g.');    hold on
       %plot(H(i,index,1), H(i,index,2),'r+'); plot(state(1), state(2),'b.');  hold off
        
        u(i)=-mi(i,index)*eta_0/beta_0*sigma/sqrt(P(i))*sign(y(i)*b(i));
        
        y(i+1)=y(i)+system.b*u(i)+sigma*randn;
        K=u(i)*P(i)/(P(i)*u(i)^2+sigma^2);
        b(i+1)=b(i)+K*(y(i+1)-y(i)-b(i)*u(i));
        P(i+1)=(1-K*u(i))*P(i);   
        
        ztrata=ztrata+(y(i+1)-system.yr(i))^2;
        
      end
 end
