.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/electrical_engineering/dc_circuits/plot_01_ohm_law.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 or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_electrical_engineering_dc_circuits_plot_01_ohm_law.py: Ohm's law (1827) ================ In 1826 Georg Simon Ohm drove current from a thermocouple, a steady source, through brass wires of different lengths :math:`x` and read the current on a torsion galvanometer. The readings fitted :math:`X = a/(b + x)`: a fixed "exciting force" :math:`a` divided by the total resistance of the wire plus the rest of the circuit, :math:`b`. In modern form that is :math:`V = IR` (:func:`~engineeringkit.electrical_engineering.ohms_law`). This example rebuilds Ohm's experiment, recovers the source voltage and internal resistance from the readings, and checks the series and parallel rules that follow from the law. .. GENERATED FROM PYTHON SOURCE LINES 17-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from engineeringkit.electrical_engineering import ohms_law, parallel_resistance, series_resistance .. GENERATED FROM PYTHON SOURCE LINES 23-29 Ohm's wires ----------- A thermocouple of EMF 2 mV and internal resistance 0.05 ohm drives copper wire of 0.017 ohm per metre. The current falls as the wire lengthens, exactly as :math:`a/(b + x)`, with measurement noise of 1%. .. GENERATED FROM PYTHON SOURCE LINES 29-42 .. code-block:: Python rng = np.random.default_rng(1827) emf, r_internal, r_per_m = 2e-3, 0.05, 0.017 lengths = np.array([0.5, 1, 2, 4, 8, 16, 32]) total = np.array([series_resistance(r_internal, r_per_m * x) for x in lengths]) current = np.array([ohms_law(voltage=emf, resistance=R) for R in total]) * (1 + 0.01 * rng.standard_normal(lengths.size)) # 1/I is linear in x: 1/I = (r_internal + r_per_m x) / emf. slope, intercept = np.polyfit(lengths, 1 / current, 1) emf_fit = r_per_m / slope r_fit = intercept * emf_fit print(f"fitted EMF {emf_fit * 1e3:.3f} mV (true 2.000), internal resistance {r_fit:.4f} ohm (true 0.0500)") .. rst-class:: sphx-glr-script-out .. code-block:: none fitted EMF 2.026 mV (true 2.000), internal resistance 0.0515 ohm (true 0.0500) .. GENERATED FROM PYTHON SOURCE LINES 43-49 Series and parallel ------------------- Equal current through resistors in series means their voltages add, so resistances add. Equal voltage across resistors in parallel means their currents add, so conductances add. .. GENERATED FROM PYTHON SOURCE LINES 49-74 .. code-block:: Python R = [100.0, 220.0, 470.0] V = 10.0 i_series = ohms_law(voltage=V, resistance=series_resistance(*R)) assert np.isclose(sum(i_series * r for r in R), V) i_parallel = sum(ohms_law(voltage=V, resistance=r) for r in R) assert np.isclose(i_parallel, ohms_law(voltage=V, resistance=parallel_resistance(*R))) print(f"series {series_resistance(*R):.0f} ohm, parallel {parallel_resistance(*R):.2f} ohm") fig, axes = plt.subplots(1, 2, figsize=(10, 3.6)) x = np.linspace(0, 35, 200) axes[0].plot(lengths, current * 1e3, "o", label="readings") axes[0].plot(x, emf / (r_internal + r_per_m * x) * 1e3, label="$a/(b + x)$") axes[0].set(xlabel="wire length x [m]", ylabel="current [mA]", title="Ohm's experiment") axes[0].legend() v = np.linspace(-5, 5, 11) for r in R: axes[1].plot(v, [ohms_law(voltage=vk, resistance=r) * 1e3 for vk in v], label=f"{r:.0f} ohm") axes[1].set(xlabel="voltage [V]", ylabel="current [mA]", title="Linear V-I characteristics") axes[1].legend() fig.tight_layout() assert abs(emf_fit - emf) / emf < 0.03 and abs(r_fit - r_internal) / r_internal < 0.1 plt.show() .. image-sg:: /api/gallery/electrical_engineering/dc_circuits/images/sphx_glr_plot_01_ohm_law_001.png :alt: Ohm's experiment, Linear V-I characteristics :srcset: /api/gallery/electrical_engineering/dc_circuits/images/sphx_glr_plot_01_ohm_law_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none series 790 ohm, parallel 59.98 ohm .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.056 seconds) .. _sphx_glr_download_api_gallery_electrical_engineering_dc_circuits_plot_01_ohm_law.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/electrical_engineering/dc_circuits/plot_01_ohm_law.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_ohm_law.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_ohm_law.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_ohm_law.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_