import math
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Paramètres de l'onde (vitesse: de propagation)
amplitude, periode, vitesse = 1.0, 2.0, 0.5
# Domaine spatial (x) et temporel (t)
x_min, x_max, t_min, t_max = 0, 10, 0, 10
pas_x, pas_t = 0.05, 0.1
# Génération des listes x et t
x = [i * pas_x for i in range(int(x_max / pas_x) + 1)]
t = [i * pas_t for i in range(int(t_max / pas_t) + 1)]
# Création de la figure
fig, ax = plt.subplots(figsize=(10, 5))
ax.set_title("Simulation de la propagation d'une onde sinusoïdale")
ax.set_xlabel("Position (x)")
ax.set_ylabel("Amplitude")
ax.set_xlim(x_min, x_max)
ax.set_ylim(-2, 2)
ax.grid(True)
# Initialisation de la ligne de l'onde
line, = ax.plot([], [], 'b-', lw=2)
# Fonction pour calculer l'onde à un instant t
def onde(x_list, t_val, amplitude, periode, vitesse):
    y = []
    for pos in x_list:
        y.append(amplitude * math.sin(2 * math.pi * (pos - vitesse * t_val) / periode))
    return y
# Initialisation de l'animation
def init():
    line.set_data([], [])
    return line,
# Mise à jour de l'animation à chaque frame
def update(frame):
    y = onde(x, frame, amplitude, periode, vitesse)
    line.set_data(x, y)
    ax.set_title(f"Propagation de l'onde (t = {frame:.1f} s)")
    return line,
# Création de l'animation
ani = FuncAnimation(fig, update, frames=t, init_func=init, blit=True,interval=50)
plt.show()