clear all
figure(1)
clf

for r=3.5:.005:4.0
    %r=3.99;        %the rate parameter range 0-4  nice 3.7
    n=4000;       %the number of point to generate
    y(1)=0.55;    %initial value for x
    for i=1:n-1
        y(i+1)=r*y(i)*(1-y(i));
    end
    
    y=y(end-3000:end);
    sd = std(y);
    y = (y-mean(y))/sd;
    ly = length(y);
    
    ord = 4;
    permlist = perms(1:ord);
    c(1:length(permlist))=0;
    
    for j=1:ly-ord
        [a,iv]=sort(y(j:j+ord-1));
        for jj=1:length(permlist)
            if (abs(permlist(jj,:)-iv))==0
                c(jj) = c(jj) + 1 ;
            end
        end
    end
    
    p = max(1/ly,c/(ly-ord))
    e = -sum(p .* log(p))/(ord-1);
    
    np=1;
    subplot(np,1,1);
    plot(r,e,'x','markersize',3)
    set(gca,'ylim',[0 1], 'xlim', [3.5 4])
    title(['permutation entropy: order=',num2str(ord)])
    ylabel('permutation entropy')
    xlabel('logistic growth parameter')
    %legend(name)
    hold on 
    drawnow
end