m = mean(X) ; S = cov(X,1) ;
[NumDias,p] = size(Y) ;
alpha = 0.05 ;
LC = ((n-1)*p/(n-p)) * finv(1-alpha,p,n-p) ;
T_i = 0;
T = [ ] ;
i = 1 ; 
while (T_i <= LC) & (i <= NumDias)
    T_i = (Y(i,:)-m)*inv(S)*(Y(i,:)-m)' ;
    T = [T ; T_i] ;
    i = i+1 ;
end   
plot([1:i-1]',T,'ko-','MarkerFaceColor','k')
hold on
plot([1:i-1],LC*ones(1,i-1),'k--','LineWidth',1.5)
hold on
text(1.5,LC+1.2,'UCL','FontSize',14)
if (i <= NumDias) | (T_i>UCL)
   plot(i-1,T_i,'ko','MarkerSize',12) 
end    
xlabel('Dia')
ylabel('T_i^2')
