
% Probability density function: natural measue
% Ricardo Carretero April-2003

f = inline('4*x.*(1-x)','x');

show_p=0;
if(abs(f(0.5)-1)<1e-5) show_p=1;end

Nit=1e5;
Nshow=100;
Nbox=125;
Ntrans=1000;
Nstop=Nit/Nshow;

xmin=0;
xmax=1;
dx=(xmax-xmin)/Nbox;
x=linspace(xmin+dx/2,xmax-dx/2,Nbox);
box=zeros(1,Nbox);

fs=15;
lw=2;
figure(1)

x0=0.01;
xo=x0;

for i=1:Ntrans
 xn=f(xo);
 xo=xn;
end

for i=1:Nshow
 for j=1:Nstop
  xn=f(xo);
  ii = ceil(xn*Nbox);
  box(ii)=box(ii)+1;
  xo=xn;
 end
 clf
 set(gca,'FontSize',[fs]);
 bar(x,box/(dx*Nstop*i),1,'w')
 if(show_p)
 hold on
  plot(x,1./(pi*sqrt(x-x.^2)),'LineWidth',lw);
 end
 axis([0 1 0 5]);
 xlabel('x')
 ylabel('p(x)');
 title(['Nit=',num2str(i*j)])
 pause(0.001)
end


