.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_05_damped_pendulum.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_05_damped_pendulum.py: Damped pendulum =============== The angle of a pendulum is recorded with a rotary encoder. The oscillation decays exponentially; the encoder has a small zero offset. Frequency, phase and decay time are estimated automatically from a periodogram. .. GENERATED FROM PYTHON SOURCE LINES 11-22 .. code-block:: Python import numpy as np import fitwise as fw rng = np.random.default_rng(5) t = np.linspace(0, 20, 400) # s angle = 12 * np.exp(-t / 8) * np.sin(2 * np.pi * 0.62 * t + 0.4) + 0.3 + rng.normal(0, 0.2, t.size) result = fw.fit(fw.models.damped_sine + fw.models.constant, t, angle, yerr=0.2) print(result) .. rst-class:: sphx-glr-script-out .. code-block:: none Model: damped_sine(x; amp, freq, phase, tau) + constant(x; c) Method: Trust Region Reflective (scipy least_squares) (auto: nonlinear) Converged: yes (16 evals) Data: 400 points, yerr given (absolute) Start: amp, freq, phase, tau: periodogram · c: edge baseline Parameter Value ± Std.Err Rel. Bounds amp 12.007 0.045 0.4 % – freq 0.61991 0.00012 0.0 % – phase 0.4023 0.0039 1.0 % – tau 7.986 0.046 0.6 % – c 0.290 0.010 3.5 % – χ²/ndf = 0.938 (395 dof, p = 0.81) R² = 0.9975 AIC = -20.6 Correlations > 0.5: freq–phase (-0.73), amp–tau (-0.72) Warnings: none .. GENERATED FROM PYTHON SOURCE LINES 23-27 .. code-block:: Python fig, axes = result.plot() axes[0].set_ylabel("angle (°)") axes[1].set_xlabel("t (s)") .. image-sg:: /auto_examples/images/sphx_glr_plot_05_damped_pendulum_001.png :alt: plot 05 damped pendulum :srcset: /auto_examples/images/sphx_glr_plot_05_damped_pendulum_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 18.503588541666666, 't (s)') .. GENERATED FROM PYTHON SOURCE LINES 28-31 The quality factor of the pendulum is Q = π f τ. ``as_ufloat()`` returns the parameters as correlated values of the `uncertainties `_ package, so the error propagation automatically includes the correlation between f and τ: .. GENERATED FROM PYTHON SOURCE LINES 31-35 .. code-block:: Python u = result.params.as_ufloat() Q = np.pi * u["freq"] * u["tau"] print(f"Q = {Q:.1uS}") .. rst-class:: sphx-glr-script-out .. code-block:: none Q = 15.55(9) .. GENERATED FROM PYTHON SOURCE LINES 36-37 The 95 % prediction band shows where new readings of the encoder are expected: .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: Python fig, axes = result.plot(band="prediction", residuals=False) axes[0].set_xlim(0, 5) axes[0].set_ylabel("angle (°)") axes[0].set_xlabel("t (s)") .. image-sg:: /auto_examples/images/sphx_glr_plot_05_damped_pendulum_002.png :alt: plot 05 damped pendulum :srcset: /auto_examples/images/sphx_glr_plot_05_damped_pendulum_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 18.503588541666666, 't (s)') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.508 seconds) .. _sphx_glr_download_auto_examples_plot_05_damped_pendulum.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_damped_pendulum.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_damped_pendulum.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_damped_pendulum.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_