#############################
######### Z - score #########
############ J.B ############
########### 2021  ###########
#############################
#############################


import matplotlib.pyplot as plt
import numpy as np
from statistics import stdev, mean
plt.rcParams['font.size'] = 12
plt.close('all')

## mesures

m1 = 1 # kg
m2 = 0.5 #kg
# distances
h = .28 #m
u_h = .1  # incertitude
d = 0.26 #m
u_d = .1  # incertitude

#pour affichage
nom_resultat = "$f$" # coefficient de frottement
unite = "" # sans unité


## Méthode de Monté-Carlo
N =  10000 # nombre de tirages
h_alea = h + np.random.uniform(-u_h,u_h,N)
d_alea = d + np.random.uniform(-u_d,u_d,N)
f_alea = m2*h_alea / ( (m1+m2)*d_alea + h_alea*m1)

## représentation graphique
plt.figure(figsize=(10,6))

_,bins,_ = plt.hist(f_alea, 50,
                            density = True,
                            color ='green',
                            alpha = 0.3)
# Répartition gaussienne
sigma, mu = stdev(f_alea), mean(f_alea)
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
     np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
plt.plot(bins, y, '--', color ='black')
# Affichage moyenne
plt.plot([mu,mu],[0,max(y)],'--r')
texte = "$\mu$ = {:.2e} ".format(mu) +  unite
plt.text(mu+sigma/10,max(y)/20,texte,color='r')
# Affichage écart type
plt.arrow(mu,max(y)*.55,sigma,0, head_width = max(y)/50,
            head_length = sigma/20,fc='k', ec='k')
texte = "$\sigma =  {:.0e} $".format(sigma) + unite
plt.text(mu+sigma/10,max(y)*.56,texte)
# noms des axes et titre
plt.xlabel(nom_resultat + ' ('+unite + ')')
plt.ylabel('Fréquence')
plt.title('Méthode de Monté-Carlo')
plt.show()




