.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_12_errors_in_x_and_y.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_12_errors_in_x_and_y.py: Errors in x and y: Ohm's law ============================ A resistor is characterized with a voltmeter and an ammeter. Both readings have uncertainties, the current even more than the voltage. An ordinary fit treats x as exact and underestimates the uncertainty of the resistance. With ``xerr``, fitwise uses orthogonal distance regression, which accounts for the errors in both variables. .. GENERATED FROM PYTHON SOURCE LINES 12-25 .. code-block:: Python import numpy as np import fitwise as fw rng = np.random.default_rng(12) current_true = np.linspace(2, 20, 12) # mA R_true = 0.47 # kΩ, so U = R·I comes out in V u_I, u_U = 0.4, 0.05 # instrument uncertainties: mA and V current = current_true + rng.normal(0, u_I, current_true.size) voltage = R_true * current_true + rng.normal(0, u_U, current_true.size) ohm = "R*x" .. GENERATED FROM PYTHON SOURCE LINES 26-28 Ordinary fit, current treated as exact. The χ² warning says that the voltage errors alone cannot explain the scatter: .. GENERATED FROM PYTHON SOURCE LINES 28-34 .. code-block:: Python import warnings with warnings.catch_warnings(record=True): ordinary = fw.fit(ohm, current, voltage, yerr=u_U) print(ordinary) .. rst-class:: sphx-glr-script-out .. code-block:: none Model: y = R*x Method: Linear least squares (direct) (auto: linear in the parameters) Converged: yes (2 evals) Data: 12 points, yerr given (absolute) Start: R: not needed (linear model) Parameter Value ± Std.Err Rel. Bounds R 0.4688 0.0012 0.2 % – χ²/ndf = 13.4 (11 dof, p = 6.4e-26) R² = 0.9957 AIC = 32.1 Warnings: ! χ²/ndf = 13.4 is too large for 11 degrees of freedom (p = 6.4e-26): the model does not describe the data, or yerr is underestimated .. GENERATED FROM PYTHON SOURCE LINES 35-36 With ``xerr`` the fit is consistent, and the uncertainty of R is realistic: .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python odr = fw.fit(ohm, current, voltage, yerr=u_U, xerr=u_I) print(odr) .. rst-class:: sphx-glr-script-out .. code-block:: none Model: y = R*x Method: Orthogonal distance regression (odrpack) (auto: xerr given) Converged: yes (31 evals) Data: 12 points, xerr and yerr given (absolute) Start: R: from ordinary fit (linear model) Parameter Value ± Std.Err Rel. Bounds R 0.4692 0.0045 1.0 % – χ²/ndf = 0.886 (11 dof, p = 0.55) R² = 0.9957 AIC = -0.5 Warnings: none .. GENERATED FROM PYTHON SOURCE LINES 40-42 The plot shows error bars in both directions. The residuals are normalized by the effective uncertainty √(yerr² + (R·xerr)²). .. GENERATED FROM PYTHON SOURCE LINES 42-46 .. code-block:: Python fig, axes = odr.plot() axes[0].set_ylabel("U (V)") axes[1].set_xlabel("I (mA)") .. image-sg:: /auto_examples/images/sphx_glr_plot_12_errors_in_x_and_y_001.png :alt: plot 12 errors in x and y :srcset: /auto_examples/images/sphx_glr_plot_12_errors_in_x_and_y_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 18.503588541666666, 'I (mA)') .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: Python for label, r in [("ordinary", ordinary), ("ODR", odr)]: R = r.params["R"] print(f"{label:8s} R = {1000 * R.value:.1f} ± {1000 * R.stderr:.1f} Ω (true: 470 Ω)") .. rst-class:: sphx-glr-script-out .. code-block:: none ordinary R = 468.8 ± 1.2 Ω (true: 470 Ω) ODR R = 469.2 ± 4.5 Ω (true: 470 Ω) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.137 seconds) .. _sphx_glr_download_auto_examples_plot_12_errors_in_x_and_y.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_12_errors_in_x_and_y.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_12_errors_in_x_and_y.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_12_errors_in_x_and_y.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_