.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_09_resonance.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_09_resonance.py: Resonance curve =============== The amplitude of a driven oscillator near resonance is approximately Lorentzian. The quality factor follows from the resonance frequency and the width: Q = f₀ / (2γ), where γ is the half width at half maximum. The detector has a small constant offset. .. GENERATED FROM PYTHON SOURCE LINES 11-22 .. code-block:: Python import numpy as np import fitwise as fw rng = np.random.default_rng(9) f = np.linspace(950, 1050, 101) # Hz amplitude = 2.4 * 4.0**2 / ((f - 1002.5) ** 2 + 4.0**2) + 0.05 + rng.normal(0, 0.02, f.size) result = fw.fit(fw.models.lorentzian + fw.models.constant, f, amplitude, yerr=0.02) print(result) .. rst-class:: sphx-glr-script-out .. code-block:: none Model: lorentzian(x; amp, cen, gamma) + constant(x; c) Method: Trust Region Reflective (scipy least_squares) (auto: nonlinear) Converged: yes (24 evals) Data: 101 points, yerr given (absolute) Start: amp, cen, gamma: peak heuristic · c: edge baseline Parameter Value ± Std.Err Rel. Bounds amp 2.392 0.011 0.5 % – cen 1002.499 0.019 0.0 % – gamma 3.984 0.030 0.8 % – c 0.0521 0.0026 4.9 % – χ²/ndf = 1.03 (97 dof, p = 0.41) R² = 0.9986 AIC = 6.4 Correlations > 0.5: amp–gamma (-0.60) Warnings: none .. GENERATED FROM PYTHON SOURCE LINES 23-29 .. code-block:: Python f0, gamma = result.params["cen"], result.params["gamma"] Q = f0.value / (2 * gamma.value) print( f"f0 = {f0.value:.2f} ± {f0.stderr:.2f} Hz, Q = {Q:.0f} ± {Q * gamma.stderr / gamma.value:.0f}" ) .. rst-class:: sphx-glr-script-out .. code-block:: none f0 = 1002.50 ± 0.02 Hz, Q = 126 ± 1 .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: Python fig, axes = result.plot() axes[0].set_ylabel("amplitude (V)") axes[1].set_xlabel("f (Hz)") .. image-sg:: /auto_examples/images/sphx_glr_plot_09_resonance_001.png :alt: plot 09 resonance :srcset: /auto_examples/images/sphx_glr_plot_09_resonance_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 18.503588541666666, 'f (Hz)') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.120 seconds) .. _sphx_glr_download_auto_examples_plot_09_resonance.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_09_resonance.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_09_resonance.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_09_resonance.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_