# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import math

##### À COMPLÉTER PAR LE CANDIDAT  #####
L = []
a = []
##### FIN DE LA PARTIE À COMPLÉTER #####

# Hypothèse : incertitude-type sur L constante (en m)
sigma_L = 0.0005  # à adapter selon les conditions expérimentales

def round_up_sig(x, sig=1):
    if x == 0:
        return 0.0
    exp = math.floor(math.log10(abs(x)))
    factor = 10 ** (exp - sig + 1)
    return math.ceil(x / factor) * factor

# Vérification des données
if not L or not a or len(L) != len(a):
    raise ValueError("Les listes L et a doivent être non vides et de même longueur.")

inv_a = np.array([1.0 / x for x in a])
L = np.array(L)

# Régression linéaire 
k = np.sum(a*L)/len(a)
k_tab = [a*L]

# Formule pour u(k)
std_err = np.std(k_tab, ddof = 1)

# Arrondis
k_str = "{:.3g}".format(k)
u_k = round_up_sig(std_err, 1)
u_k_str = "{:.1e}".format(u_k)

# Tracé
plt.figure()
x_vals = np.linspace(min(inv_a), max(inv_a), 100)
y_vals = k* x_vals

plt.scatter(inv_a, L, color='blue', label='Données expérimentales - k en m²')
plt.plot(x_vals, y_vals, color='red',
         label="L = {} · (1/a)\nincertitude type u(k) = {} m²".format(k_str, u_k_str))
plt.xlabel("inverse des fils calibrés 1/a en m⁻¹")
plt.ylabel("largeur des tâches centrales en mètre")
plt.title("Modélisation linéaire L = k · (1/a)")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()

# Affichage final
print("OUVRIR LA FENÊTRE GRAPHIQUE")