# -*- coding: utf-8 -*-
"""
@author: Benjamin Langlois
"""

""" Etalement au cours du temps pour une marche aléatoire 1D."""

import matplotlib.pyplot as plt
import random

# Nb particules
N = 100
# L_x : liste des N positions
# Positions initiales au centre x = 0
L_x = [0]*N
# Nombre de collisions
coll = 10000
# Déplacement par pas de temps
libreparcours = 1
# pas de temps
tau = 1
# Liste des temps
temps = [k*tau for k in range(coll+1)]
# Liste contenant la moyenne des x**2 au cours du temps
L_x2moyen= [0]
# Theorie x2moyen = Dt = (libreparcours**2/tau)*t
theorie = [k*libreparcours**2/tau for k in temps]

# Calculer une trajectoire
for i in range(coll):
    x2 = 0
    for k in range(N):
        L_x[k] += random.choice([-1, 1]) * libreparcours
        x2 += L_x[k] **2
    L_x2moyen.append(x2/N)
   
    
plt.plot(temps,L_x2moyen,label='Simulation')
plt.plot(temps,theorie,label='Théorie')
plt.grid()
plt.legend()
plt.xlabel('t')
plt.ylabel('<x²>')
plt.title('Marche au hasard à une dimension: <x²>=f(t), 100 particules, 10000 pas')