.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_acf_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_acf_1.py: ===================== Correlation functions ===================== Generate the velocity for a Ornstein-Uhlenbeck process and compute its autocorrelation function. We also display the force velocity correlation as an example of using the routine `correlation`. .. GENERATED FROM PYTHON SOURCE LINES 12-81 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_acf_1_001.png :alt: Velocity autocorrelation :srcset: /auto_examples/images/sphx_glr_plot_acf_1_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_acf_1_002.png :alt: Force-velocity correlation :srcset: /auto_examples/images/sphx_glr_plot_acf_1_002.png :class: sphx-glr-multi-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 data for a Ornstein-Uhlenbeck process gamma = 2.7 T = 0.1 dt = 0.02 v_factor = np.sqrt(2*T*gamma*dt) N = 32768 v = 0 for i in range(100): noise_force = v_factor*np.random.normal() v = v - gamma*v*dt + noise_force v_data = [] noise_data = [] for i in range(N): noise_force = v_factor*np.random.normal() v = v - gamma*v*dt + noise_force v_data.append(v) noise_data.append(noise_force) v_data = np.array(v_data) noise_data = np.array(noise_data)/np.sqrt(dt) # Compute the autocorrelation function and plot the result acf = tidynamics.acf(v_data)[:N//64] time = np.arange(N//64)*dt plt.plot(time, acf, label='VACF (num.)') plt.plot(time, T*np.exp(-gamma*time), label='VACF (theo.)') plt.legend() plt.title('Velocity autocorrelation') plt.xlabel(r'$\tau$') plt.ylabel(r'$\langle v(t) v(t+\tau) \rangle$') # Compute the force velocity correlation and plot the result plt.figure() time = np.arange(N)*dt twotimes = np.concatenate((-time[1:][::-1], time)) plt.plot(twotimes, tidynamics.correlation(noise_data, v_data), label='num.') plt.plot(time, 2*T/gamma*np.exp(-gamma*time), label='theo.') plt.xlim(-5/gamma, 5/gamma) plt.ylim(-2*T/gamma/10, 1.1*2*T/gamma) plt.legend() plt.title('Force-velocity correlation') plt.xlabel(r'$\tau$') plt.ylabel(r'$\langle F(t) v(t+\tau) \rangle$') plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.373 seconds) .. _sphx_glr_download_auto_examples_plot_acf_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_acf_1.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_acf_1.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_