% convergence example 3. Barnett 10/12/21.
clear
Ns = 10.^(2:0.25:8);      % takes < 1 min
for i=1:numel(Ns), N=Ns(i), tic;
    t=0; for j=1:N, t=t-log(rand()); end, toc
    ys(i) = t/N;
end
ytrue = 1;
figure; loglog(Ns,abs(ys-ytrue),'+-');
xlabel('N (samples averaged)'); ylabel('est err $\varepsilon$','interpreter','latex');
hold on; plot(Ns,3*Ns.^-.5,'r--');
axis tight;
h=text(3e3,.1,'negative slope \approx 1/2 = order','rotation',-35);
set(gcf,'paperposition',[0 0 3.6 3.6]);
print -dpng stoch.png
