%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         8. practice         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Nonlinear dynamics systems  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         2016.11.09.         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Spiderweb diagram of Logistic mapping %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Parameters:
figure;
%mu=3.83; %3 periods, end of chaos in this parameter window
kezdet=0.2;
iteraciok=2000; %200;%

for mu=4%0.1:0.05:4 %default
%for mu=3:0.01:4 %smaller stepsize, you can se period duplications: 2->4->8 periodus,
%for mu=3.828:0.001:3.86 %the chaos temporarely disappears, than starts again with the duplication of 3 periods: 3->6->12
egyenes=linspace(0,1,iteraciok);
gorbe=mu*egyenes.*(1-egyenes);

subplot(2,2,1);
%draw the x=y line and the function
plot(egyenes,gorbe, egyenes,egyenes);
hold on;

%do the iterations
Population=zeros(iteraciok,1);
Population(1)=kezdet;

for i=1:(iteraciok-1)
    Population(i+1)=mu*Population(i)*(1-Population(i));
end

% spiderweb diagram:
%{
%from the beginning:
    plot([Population(1),Population(2)],[Population(2),Population(2)],'red');
    plot([Population(1),Population(1)],[0,Population(2)],'red');
for i=2:(iteraciok-1)
    %}
for i=100:(iteraciok-1) %only the end after stabilisation
    plot([Population(i),Population(i+1)],[Population(i+1),Population(i+1)],'red');
    plot([Population(i),Population(i)],[Population(i),Population(i+1)],'red');
end
title(['mu=', num2str(mu)]);
hold off;

%population size
subplot(2,2,2);
plot(Population);
ylim([0,1]);
xlabel('iteration');
ylabel('population');

%histogram of the distribution of the population sizes
subplot(2,2,3);
gorbe2=1./(pi*sqrt(egyenes-egyenes.^2));
plot(egyenes, gorbe2,'g');
hold on;
histsteps=0:0.01:1;
norm_hist=100*hist(Population, histsteps)/iteraciok;
bar(histsteps, norm_hist, 'b');
xlim([-0.1,1.1]);
ylim([0,100]);
xlabel('population');
ylabel('% of steps');
title('distribution of the population sizes');
legend('1/(pi*sqrt(x-x^2))')
hold off;

%
%Ljapunov exponent:
Population_derivalt = mu-2*mu*Population;
Ljapunov = (log(abs(prod(Population_derivalt))))/iteraciok;
%[mu, Ljapunov] %write to the consol

subplot(2,2,4);
plot(0:4, zeros(1,5), 'g')
hold on;
plot(mu,Ljapunov, 'r.', 'MarkerSize', 10);
xlabel('mu');
xlim([0 4]);
ylabel('Ljapunov exponent');
ylim([-3,1]);
%}

%waitforbuttonpress;
pause(0.1);
end;