.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_04_radioactive_decay.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_04_radioactive_decay.py: Radioactive decay with background ================================= Counts from a short-lived isotope are recorded in 10-s intervals. The counts follow Poisson statistics, so the uncertainty of each value is √N. A constant background from the environment adds to the decay. .. GENERATED FROM PYTHON SOURCE LINES 11-23 .. code-block:: Python import numpy as np import fitwise as fw rng = np.random.default_rng(5) t = np.arange(0, 600, 10.0) # s expected = 850 * np.exp(-t / 140) + 25 counts = rng.poisson(expected).astype(float) result = fw.fit(fw.models.exponential + fw.models.constant, t, counts, yerr=np.sqrt(counts)) 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 (11 evals) Data: 60 points, yerr given (absolute) Start: amp, tau: log-linear fit · c: edge baseline Parameter Value ± Std.Err Rel. Bounds amp 851 12 1.4 % – tau 141.1 3.0 2.1 % – c 23.4 2.6 11.3 % – χ²/ndf = 0.972 (57 dof, p = 0.54) R² = 0.9930 AIC = 1.2 Correlations > 0.5: tau–c (-0.82), amp–tau (-0.61) Warnings: none .. GENERATED FROM PYTHON SOURCE LINES 24-25 The half-life follows from the decay time: T½ = τ · ln 2. .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: Python tau = result.params["tau"] print(f"T½ = {tau.value * np.log(2):.1f} ± {tau.stderr * np.log(2):.1f} s") .. rst-class:: sphx-glr-script-out .. code-block:: none T½ = 97.8 ± 2.1 s .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. code-block:: Python fig, axes = result.plot() axes[0].set_yscale("log") axes[0].set_ylabel("counts per 10 s") axes[1].set_xlabel("t (s)") .. image-sg:: /auto_examples/images/sphx_glr_plot_04_radioactive_decay_001.png :alt: plot 04 radioactive decay :srcset: /auto_examples/images/sphx_glr_plot_04_radioactive_decay_001.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.184 seconds) .. _sphx_glr_download_auto_examples_plot_04_radioactive_decay.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_radioactive_decay.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_radioactive_decay.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_radioactive_decay.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_