# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Ex 1.3

# - **Simulate** the Buffon’s experiment (see LSN_Lecture_00, supplementary material):  A needle of length $L$ is thrown at random onto a horizontal plane ruled with straight lines a distance $d$ (must be $d > L$, but do not use $d\gg L$ otherwise $P\ll 1$) apart. The probability $P$ that the needle will intersect one of these lines is: $P = 2L/\pi d$. This could be used to evaluate $\pi$ from throws of the needle: if the needle is thrown down $N_{thr}$ times and is observed to land on a line $N_{hit}$ of those times, we can make an estimate of $\pi$ from
# $$\pi = \frac{2L}{Pd} = \lim_{N_{thr} \to \infty}\frac{2LN_{thr}}{N_{hit}d}$$
# <font color="red">Make a picture of the estimation of $\pi$ and its uncertainty (Standard Deviation of the mean) with a large number of *throws* $M$ as a function of the number of blocks, $N$</font> (see below: Computing statistical uncertainties). If possible, do not use $\pi$ to evaluate $\pi$.

import matplotlib 
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import figure
from time import time

start = time()

f = np.loadtxt("pies.txt")
x = np.arange(len(f))
x *= 10000

# +
figure(figsize=(8,6), dpi=200)

plt.errorbar(x,f[:,0],yerr=f[:,1])
plt.axhline(3.1415926535, color='red', label="π")
plt.legend(loc="upper right")
plt.xlabel("# throw")
plt.ylabel("<π>")
plt.grid(True)
plt.show()

# +
end = time()

print("Total computing time:", int((end-start)*100)/100., "sec.")

StarcoderdataJupyterScriptsDedupFiltered