{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Mean-square displacements (MSD)\n\nGenerate a number of random walks and compute their MSD.\n\nCompute also the MSD for a constant velocity motion.\n\nFor a random walk, the MSD is linear: $MSD(\\tau) \\approx 2 D \\tau$\n\nFor a constant velocity motion, the MSD is quadratic:\n$MSD(\\tau) = v \\tau^2$\n\nWe show in the figures the numerical result computed by `tidynamics.msd`\n('num.') and the theoretical value ('theo.').\n\nFor the constant velocity case, we also display a \"pedestrian approach\" where\nthe loop for averaging the MSD is performed explicitly.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport tidynamics\nimport matplotlib.pyplot as plt\n\nplt.rcParams['figure.figsize'] = 5.12, 3.84\nplt.rcParams['figure.subplot.bottom'] = 0.18\nplt.rcParams['figure.subplot.left'] = 0.16\n\n# Generate 32 random walks and compute their mean-square\n# displacements\n\nN = 1000\nmean = np.zeros(N)\ncount = 0\nfor i in range(32):\n    # Generate steps of value +/- 1\n    steps = -1 + 2*np.random.randint(0, 2, size=(N, 2))\n    # Compute random walk position\n    data = np.cumsum(steps, axis=0)\n    mean += tidynamics.msd(data)\n    count += 1\n\nmean /= count\nmean = mean[1:N//2]\n\ntime = np.arange(N)[1:N//2]\n\nplt.plot(time, mean, label='Random walk (num.)')\n\nplt.plot(time, 2*time, label='Random walk (theo.)')\n\ntime = np.arange(N//2)\n\n# Display the mean-square displacement for a trajectory with\n# constant velocity. Here the trajectory is taken equal to the\n# numerical value of the time.\n\nplt.plot(time[1:], tidynamics.msd(time)[1:],\n         label='Constant velocity (num.)', ls='--')\nplt.plot(time[1:], time[1:]**2,\n         label='Constant velocity (theo.)', ls='--')\n\n# Compute the the mean-square displacement by explicitly\n# computing the displacements along shorter samples of the\n# trajectory.\n\nsum_size = N//10\npedestrian_msd = np.zeros(N//10)\nfor i in range(10):\n    for j in range(N//10):\n        pedestrian_msd[j] += (time[10*i]-time[10*i+j])**2\npedestrian_msd /= 10\nplt.plot(time[1:N//10], pedestrian_msd[1:], ls='--',\n         label=\"pedestrian\")\n\nplt.loglog()\nplt.legend()\nplt.xlabel('time')\nplt.ylabel('mean square displacement')\nplt.title('Examples for the mean-square displacement')\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}