from scipy.integrate import quad
import numpy as np
import matplotlib.pyplot as plt

## Initialisation des paramètres physiques à compléter

L = 1.0  # longueur du pendule
g = 9.81
T0 = 2*np.pi*np.sqrt(L/g)   # période du pendule aux petites osccillations

## Calcul exact de la période

def periode(theta0) :

    def f(theta):
        return 1/np.sqrt( 2*(np.cos(theta)-np.cos(theta0)) )

    resultat, precision = quad(f,0,theta0)
    return 2*T0/np.pi*resultat

## Paramètres numériques utiles et représentation de T en fonction de theta0

N_points = 1000
theta0_liste = np.linspace(0,np.pi/2,N_points)
T = []

for theta0 in theta0_liste :
    T.append(periode(theta0))

T[0] = T0  # nécessaire car periode(0) ne renvoie pas le bon résultat...

plt.close()
plt.figure()
plt.plot(theta0_liste,T)
plt.xlabel("$theta_0$ (rad)")
plt.ylabel("T(s)")
plt.title("Période pendule")
plt.show()


## Ecart à la période T0 à compléter

i = 0
while abs(T[i]-T0) <= 0.01*T0 and i < N_points :
    i += 1
if i == N_points :
    print("Pas d'écart significatif entre T et T0'")
else :
    print(theta0_liste[i]*180/np.pi)

## Ecart à la formule de Borda à compléter

i = 0
while abs(T[i]-T0-T0*(theta0_liste[i])**2/16) <= 0.01*T0 and i < N_points :
    i += 1
if i == N_points :
    print("Pas d'écart significatif entre T et T0'")
else :
    print(theta0_liste[i]*180/np.pi)