.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_03_rc_discharge.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_03_rc_discharge.py: RC discharge ============ A capacitor discharges through a known resistor. The voltage decays exponentially towards a small offset of the measuring device. From the time constant τ = RC we obtain the capacitance. .. GENERATED FROM PYTHON SOURCE LINES 11-23 .. code-block:: Python import numpy as np import fitwise as fw rng = np.random.default_rng(3) t = np.linspace(0, 50, 60) # ms voltage = 4.8 * np.exp(-t / 12.5) + 0.12 + rng.normal(0, 0.02, t.size) # V model = fw.models.exponential + fw.models.constant result = fw.fit(model, t, voltage, yerr=0.02) print(result) .. rst-class:: sphx-glr-script-out .. code-block:: none Model: exponential(x; amp, tau) + constant(x; c) Method: Trust Region Reflective (scipy least_squares) (auto: nonlinear) Converged: yes (27 evals) Data: 60 points, yerr given (absolute) Start: amp, tau: log-linear fit · c: edge baseline Parameter Value ± Std.Err Rel. Bounds amp 4.794 0.010 0.2 % – tau 12.538 0.067 0.5 % – c 0.1180 0.0064 5.4 % – χ²/ndf = 1.2 (57 dof, p = 0.14) R² = 0.99971 AIC = 13.8 Correlations > 0.5: tau–c (-0.83) Warnings: none .. GENERATED FROM PYTHON SOURCE LINES 24-27 The ``Start:`` line of the report shows how the starting values were found: the offset ``c`` from the flat end of the curve (``edge baseline``), the decay from a log-linear fit of the rest. .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: Python fig, axes = result.plot() axes[0].set_ylabel("U (V)") axes[1].set_xlabel("t (ms)") .. image-sg:: /auto_examples/images/sphx_glr_plot_03_rc_discharge_001.png :alt: plot 03 rc discharge :srcset: /auto_examples/images/sphx_glr_plot_03_rc_discharge_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 18.503588541666666, 't (ms)') .. GENERATED FROM PYTHON SOURCE LINES 32-33 Capacitance with R = 10.0 kΩ (assumed exact). τ is in ms, so C comes out in µF. .. GENERATED FROM PYTHON SOURCE LINES 33-36 .. code-block:: Python R = 10.0 # kΩ tau = result.params["tau"] print(f"C = {tau.value / R:.4f} ± {tau.stderr / R:.4f} µF") .. rst-class:: sphx-glr-script-out .. code-block:: none C = 1.2538 ± 0.0067 µF .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.116 seconds) .. _sphx_glr_download_auto_examples_plot_03_rc_discharge.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_rc_discharge.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_rc_discharge.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_rc_discharge.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_