 % Bertrand paradoxon szimulációja

clear all
% a minták száma
N = 50000000;

% a húr módszer
alpha = rand(N,1)*pi;
theta = rand(N,1)*2*pi;

p = [-cos(alpha) sin(alpha)];
p = repmat(-p(:,1),[1,2]) .* p;

% p2 az eredmény a HÚR módszerrel
cTh = cos(theta);
sTh = sin(theta);
p1(:,1) = p(:,1).* cTh + p(:,2).*sTh;
p1(:,2) =-p(:,1).* sTh + p(:,2).*cTh;

% az IRÁNY modell szerint az alpha csak az x-et jelöli

p2(:,1) = alpha/pi;
p2(:,2) = -p2(:,1).*sTh;
p2(:,1) =  p2(:,1).*cTh;

% a KÖZÉPPONT modell szerint
N2 = round(N * 4 / pi); % ennyivel kell több
p3 = (rand([N2,2])-.5)*2;
ll = sum(p3.^2,2);
ind = ll<1;
p3 = p3(ind,:);

%
% figure(1);
% 
% plot(p1(:,1),p1(:,2),'k.');
% subplot 132;
% plot(p2(:,1),p2(:,2),'k.');
% subplot 133;
% plot(p3(:,1),p3(:,2),'k.');

figure(2);clf;
d  = 500;
range = linspace(-1,1,d+1);

h1 = hist2d(p1(:,1),p1(:,2),range,range);
subplot 131;
surf(h1(1:d,1:d));
h2 = hist2d(p2(:,1),p2(:,2),range,range);
subplot 132;
surf(h2(1:d,1:d));
h3 = hist2d(p3(:,1),p3(:,2),range,range);
subplot 133;
surf(h3(1:d,1:d));

% parancsok, melyeket hasznaltunk a simitashoz
figure(1);
h11 = h1(1:5:500,:)+h1(2:5:500,:)+h1(3:5:500,:)+h1(4:5:500,:)+h1(5:5:500,:);
h111= h11(:,1:5:500)+h11(:,2:5:500)+h11(:,3:5:500)+h11(:,4:5:500)+h11(:,5:5:500);
h111(h111<3000)=NaN;
r = linspace(-1,1,100);
surf(r,r,h111);
view(155,55);
set(gca,'ztick',[]);
zlim([1000,100000]);
grid off;
print -depsc2 bertrand_hur.eps
figure(2);
h22 = h2(1:5:500,:)+h2(2:5:500,:)+h2(3:5:500,:)+h2(4:5:500,:)+h2(5:5:500,:);
h222= h22(:,1:5:500)+h22(:,2:5:500)+h22(:,3:5:500)+h22(:,4:5:500)+h22(:,5:5:500);
h222(h222<3000)=NaN;
r = linspace(-1,1,100);
clf;
surf(r,r,h222);
view(155,55);
set(gca,'ztick',[]);
zlim([1000,100000]);
grid off;
print -depsc2 bertrand_irany.eps
figure(3);
h33 = h3(1:5:500,:)+h3(2:5:500,:)+h3(3:5:500,:)+h3(4:5:500,:)+h3(5:5:500,:);
h333= h33(:,1:5:500)+h33(:,2:5:500)+h33(:,3:5:500)+h33(:,4:5:500)+h33(:,5:5:500);
h333(h333<500)=NaN;
r = linspace(-1,1,100);
clf;
surf(r,r,h333);
view(155,55);
set(gca,'ztick',[]);
zlim([1000,40000]);
grid off;
print -depsc2 bertrand_normal.eps
