################################
#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 random de numpy
import matplotlib.pyplot as plt ##module matplotlib.pyplot renommé plt


x = np.array([6,7,8,9,10, 11])             #tableau des x
y = np.array([1.9,6.1,7.8,7.5,12.1,15.3])  #tableau des y
u_y = np.array([5, 1.3, 1.1, 1.2, 1.1, 5]) #tableau des incertitudes-types


#########################################
#PAS DE PRISE EN COMPTE DES INCERTITUDES
#########################################
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

plt.figure()
plt.plot(xfit,yfit,color='red',label="y = " + str("%.2f" % a)+" x " + " + " + str("%.2f" % abs(b)))
plt.grid()    #affiche la grille
plt.xlabel('Axe des x')
plt.ylabel('Axe des y')
plt.plot(x, y, 'o', color='blue', label="points expérimentaux") #bo : blue circle markers
plt.legend(loc='best')
plt.show()



#########################################
#PRISE EN COMPTE DES INCERTITUDES
#########################################
Residus=y - (a*x+b)    #tableau des résidus
print("a = {:.2e}".format(a))
print("b = {:.2e}".format(b))


plt.figure()   #nouvelle fenêtre graphique
plt.xlabel('x')
plt.ylabel('y')
plt.plot(xfit, yfit, color='r')
plt.errorbar(x, y, yerr=u_y, fmt='o', color='blue')
plt.legend(['y=ax+b', "(barres d'inc = inc. type)"])
plt.tick_params(axis = 'both', labelsize = taille_cara)
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
#########################################
N = 1000  #nombre de tirages à simuler
tab_a=np.zeros(N) #tableau de N valeurs nulles
tab_b=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
    tab_a[i]=p[0]           #valeur de a pour la simulation i
    tab_b[i]=p[1]           #valeur de b pour la simulation i

u_a = np.std(tab_a, ddof=1) #écart-type pour tab_a avec N-1 au dénominateur
u_b = np.std(tab_b, ddof=1) #écart-type pour tab_b avec N-1 au dénominateur




print("a = {:.1e} +/- {:.1e}".format(a,u_a)+" (incertitude-type)")
print("b = {:.1e} +/- {:.1e}".format(b,u_b)+" (incertitude-type)")



