"""
SCHÉMA D'EULER APPLIQUÉ AU PENDULE SIMPLE
Code proposé par Étienne Thibierge
https://www.etienne-thibierge.fr/
"""

import numpy as np
import matplotlib.pyplot as plt

g = 9.81
L = .2

w0 = np.sqrt(g/L)
T0 = 2 * np.pi / w0
dt = 1/(500*w0)

t_max = 100*T0

t = np.arange(0,t_max,dt)    # N points entre 0 et t_max par pas dt
theta = np.empty_like(t)
omega = np.empty_like(t)

theta[0] = 45 * np.pi/180
omega[0] = 0

for n in range(len(t)-1):
    theta[n+1] = theta[n] + dt * omega[n]
    omega[n+1] = omega[n] - dt * w0**2 * np.sin(theta[n])

plt.figure()
plt.plot(t,theta)
plt.xlabel(r'$t$ (s)')
plt.ylabel(r'$\theta$ (rad)')
plt.xlim(np.min(t),np.max(t))


E = w0**2 * (1-np.cos(theta)) + .5*omega**2 # numpy calcule terme à terme !

plt.figure()
plt.plot(t,E)
plt.xlabel(r'$t$ (s)')
plt.ylabel(r'$E/mL^2$ (s^{-2})')
plt.xlim(np.min(t),np.max(t))