.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_msd_1.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_msd_1.py: =============================== Mean-square displacements (MSD) =============================== Generate a number of random walks and compute their MSD. Compute also the MSD for a constant velocity motion. For a random walk, the MSD is linear: :math:`MSD(\tau) \approx 2 D \tau` For a constant velocity motion, the MSD is quadratic: :math:`MSD(\tau) = v \tau^2` We show in the figures the numerical result computed by `tidynamics.msd` ('num.') and the theoretical value ('theo.'). For the constant velocity case, we also display a "pedestrian approach" where the loop for averaging the MSD is performed explicitly. .. GENERATED FROM PYTHON SOURCE LINES 21-83 .. image-sg:: /auto_examples/images/sphx_glr_plot_msd_1_001.png :alt: Examples for the mean-square displacement :srcset: /auto_examples/images/sphx_glr_plot_msd_1_001.png :class: sphx-glr-single-img .. code-block:: default import numpy as np import tidynamics import matplotlib.pyplot as plt plt.rcParams['figure.figsize'] = 5.12, 3.84 plt.rcParams['figure.subplot.bottom'] = 0.18 plt.rcParams['figure.subplot.left'] = 0.16 # Generate 32 random walks and compute their mean-square # displacements N = 1000 mean = np.zeros(N) count = 0 for i in range(32): # Generate steps of value +/- 1 steps = -1 + 2*np.random.randint(0, 2, size=(N, 2)) # Compute random walk position data = np.cumsum(steps, axis=0) mean += tidynamics.msd(data) count += 1 mean /= count mean = mean[1:N//2] time = np.arange(N)[1:N//2] plt.plot(time, mean, label='Random walk (num.)') plt.plot(time, 2*time, label='Random walk (theo.)') time = np.arange(N//2) # Display the mean-square displacement for a trajectory with # constant velocity. Here the trajectory is taken equal to the # numerical value of the time. plt.plot(time[1:], tidynamics.msd(time)[1:], label='Constant velocity (num.)', ls='--') plt.plot(time[1:], time[1:]**2, label='Constant velocity (theo.)', ls='--') # Compute the the mean-square displacement by explicitly # computing the displacements along shorter samples of the # trajectory. sum_size = N//10 pedestrian_msd = np.zeros(N//10) for i in range(10): for j in range(N//10): pedestrian_msd[j] += (time[10*i]-time[10*i+j])**2 pedestrian_msd /= 10 plt.plot(time[1:N//10], pedestrian_msd[1:], ls='--', label="pedestrian") plt.loglog() plt.legend() plt.xlabel('time') plt.ylabel('mean square displacement') plt.title('Examples for the mean-square displacement') plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.373 seconds) .. _sphx_glr_download_auto_examples_plot_msd_1.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_msd_1.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_msd_1.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_