################################
#PARAMETRES POUR UTILISATION NORMALE
###################################
fen_abs=6.5
fen_ord=4.5
taille_cara=10
larg_trait=1
taille_mark=4

########################
#PARAMETRES POUR LIVRE DUNOD
##############################
fen_abs=18  #18 
fen_ord=12  #12
taille_cara=30   #30
larg_trait=4      #4
taille_mark=15    #15




import numpy as np              #bibliothèque numpy renommée np
import numpy.random as rd       #module numpy.random renommé rd


tab_t=np.array([0.18, 0.31,0.40,0.48,0.53,0.59,0.65])*1e-3
delta_t=[0.02e-3 for i in range(len(tab_t))]
y=tab_t**2
u_y=np.zeros(len(y))            #valeurs nulles pour u_y[n]

#########################################
#Incertitude-type pour chaque valeur de y[n]
#########################################
for n in range(len(y)):
    #méthode de Monte-Carlo pour y[n]
    N=10000  #nombre de tirages
    tab_tn=rd.uniform(tab_t[n]-delta_t[n], tab_t[n]+delta_t[n], N) 
    tab_yn=tab_tn**2
    u_y[n]=np.std(tab_yn, ddof=1) #écart-type du tableau tab_y
                                  #avec N-1 au dénominateur
#calcul théorique de l'incertitude-type de y[n]
#u_y_calcul=2*y/tab_t*delta_t/np.sqrt(3)*1e9




import matplotlib.pyplot as plt #module matplotlib.pyplot renommé plt
tab_n=np.array([0,1,2,3,4,5,6])
x=tab_n

#########################################
#REGRESSION LINEAIRE y=ax+b
#########################################
a, b = np.polyfit(x, y, 1)  #Régression linéaire de y en fonction de x : y=ax+b
xfit = np.linspace(np.min(x), np.max(x),2) #tableau de deux points
yfit=a*xfit+b  #tableau de deux points

#########################################
#PRISE EN COMPTE DES INCERTITUDES
#########################################
Residus=y - (a*x+b)    #tableau des résidus
plt.figure()   #nouvelle fenêtre graphique
plt.xlabel('x')
plt.ylabel('y*1e7')
plt.plot(xfit, yfit*1e7, color='r')
plt.errorbar(x, y*1e7, yerr=u_y, fmt='o', color='blue')
plt.legend(['y=ax+b', "(barres d'inc = inc. type)"])
plt.grid()    #affiche la grille
plt.show()    #affiche la figure à l'écran



#########################################
#figure des résidus normalisés
#########################################

z=Residus/u_y    #tableau des écarts normalisés
plt.figure()   #nouvelle fenêtre graphique
plt.title('Résidus normalisés')
plt.plot(x, z, 'o', color='Blue')
plt.xlabel('x')
plt.ylabel('Résidus normalisés')
plt.grid()   #affiche la grille
plt.show()    #affiche la figure à l'écran



#########################################
#Simulation Monte-Carlo pour estimer l'incertitude-type
#########################################
lambda0=633e-9
delta_lambda0=2e-9

N = 10000  #nombre de tirages à simuler
tab_g=np.zeros(N) #tableau de N valeurs nulles

for i in range(0, N):  #i varie entre 0 inclus et N exclu
    my = rd.normal(y, u_y)  #variables aléatoires suivant une loi normale
                            #de valeur centrale y[i] et d'écart-type u_y[i]
    p=np.polyfit(x, my, 1)  #régression linéaire de my en fonction de x
    val_a=p[0]              #valeur de a pour la simulation i
    val_lambda0=rd.uniform(lambda0-delta_lambda0,\
               lambda0+delta_lambda0)
    tab_g[i]=val_lambda0/val_a
    
g=np.mean(tab_g)
u_g = np.std(tab_g, ddof=1) #écart-type pour tab_g avec N-1 au dénominateur

print("g = {:.2e} +/- {:.1e}".format(g, u_g)+" (incertitude-type)")

g1=9.88
u_g1=0.32
g2=9.76
u_g2=0.25
z=abs(g2-g1)/np.sqrt(u_g1**2+u_g2**2)
print('z= ',z)
if z<2:
    print("Les deux mesures sont compatibles.")
else:
    print("Les deux mesures ne sont pas compatibles.")

