.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_dynamics/plot_24_sindy_lorenz.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 or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_06_dynamics_plot_24_sindy_lorenz.py: Sparse identification of the Lorenz system ========================================== SINDy (Brunton, Proctor and Kutz, 2016) regresses the time derivatives of the states on a library of candidate terms and keeps few of them by thresholded least squares. On the chaotic Lorenz system it recovers the three equations :math:`\dot x=\sigma(y-x)`, :math:`\dot y=x(\rho-z)-y`, :math:`\dot z=xy-\beta z` from a single trajectory. The identified model is then simulated from the same initial state: the trajectories agree for a while and diverge later, as two runs of any chaotic system do, but stay on the same attractor. .. GENERATED FROM PYTHON SOURCE LINES 13-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq from eqdisco.datasets import lorenz t, Z = lorenz(T=10, dt=0.002) .. GENERATED FROM PYTHON SOURCE LINES 23-25 Identification -------------- .. GENERATED FROM PYTHON SOURCE LINES 25-50 .. code-block:: Python sindy = eq.SINDy(degree=2, threshold=0.05, feature_names=["x", "y", "z"]).fit(Z, t) print(sindy.model_) Zs = sindy.simulate(Z[0], t) fig = plt.figure(figsize=(11, 3.6)) ax0 = fig.add_subplot(1, 3, 1) terms = [str(tt) for tt in sindy.terms_] im = ax0.imshow(np.abs(sindy.coef_), cmap="Blues", aspect="auto") ax0.set_yticks(range(3)) ax0.set_yticklabels(["dx/dt", "dy/dt", "dz/dt"]) ax0.set_xticks(range(len(terms))) ax0.set_xticklabels(terms, rotation=70, fontsize=7) for i in range(3): for j in range(len(terms)): if sindy.coef_[i, j]: ax0.text(j, i, f"{sindy.coef_[i, j]:.2f}", ha="center", va="center", fontsize=6, color="white" if abs(sindy.coef_[i, j]) > 10 else "black") ax1 = fig.add_subplot(1, 3, 2) eq.viz.plot_phase_portrait(Z, Zs, ax=ax1, dims=(0, 2), names=["x", "y", "z"]) ax2 = fig.add_subplot(1, 3, 3) ax2.plot(t, Z[:, 0], color="#6B7785", lw=1, label="data") ax2.plot(t, Zs[:, 0], color="#D55E00", lw=1, ls="--", label="model") ax2.set_xlabel("time") ax2.set_ylabel("x") ax2.legend(frameon=False, fontsize=8) fig.tight_layout() .. image-sg:: /auto_examples/06_dynamics/images/sphx_glr_plot_24_sindy_lorenz_001.png :alt: plot 24 sindy lorenz :srcset: /auto_examples/06_dynamics/images/sphx_glr_plot_24_sindy_lorenz_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none dx/dt = 0.0001678 - 9.999 * x + 9.999 * y dy/dt = 0.0007748 + 27.99 * x - 0.9985 * y - 0.9998 * x * z dz/dt = -0.0003198 - 2.666 * z + 0.9999 * x * y .. GENERATED FROM PYTHON SOURCE LINES 51-53 Parameter study: measurement noise and the derivative estimate -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 53-68 .. code-block:: Python noises = [0.0, 0.002, 0.01, 0.03] fig, ax = plt.subplots(figsize=(4.8, 3.2)) for k, method in enumerate(("finite_difference", "savgol")): errs = [] for nz in noises: tn, Zn = lorenz(T=10, dt=0.002, noise=nz, random_state=1) s = eq.SINDy(threshold=0.05, derivative_method=method, window=41).fit(Zn, tn) errs.append(abs(s.coef_[0, [str(x) for x in s.terms_].index("z0")] + 10) / 10) print(method, "noise", noises, "relative error of the -10 x coefficient", np.round(errs, 4).tolist()) ax.plot(noises, errs, "o-", color=eq.viz.OKABE_ITO[k], label=method) ax.set_yscale("log") ax.set_xlabel("noise (relative to the signal std)") ax.set_ylabel("relative error of $\\sigma$") ax.legend(frameon=False, fontsize=8) plt.show() .. image-sg:: /auto_examples/06_dynamics/images/sphx_glr_plot_24_sindy_lorenz_002.png :alt: plot 24 sindy lorenz :srcset: /auto_examples/06_dynamics/images/sphx_glr_plot_24_sindy_lorenz_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none finite_difference noise [0.0, 0.002, 0.01, 0.03] relative error of the -10 x coefficient [0.0001, 0.0005, 0.0026, 0.0125] savgol noise [0.0, 0.002, 0.01, 0.03] relative error of the -10 x coefficient [0.0002, 0.0001, 0.0006, 0.0763] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.861 seconds) .. _sphx_glr_download_auto_examples_06_dynamics_plot_24_sindy_lorenz.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/eqdisco/main?urlpath=lab/tree/notebooks/auto_examples/06_dynamics/plot_24_sindy_lorenz.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_24_sindy_lorenz.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_24_sindy_lorenz.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_24_sindy_lorenz.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_