.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/electrical_engineering/plot_electrical_engineering_benchmark.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_plot_electrical_engineering_benchmark.py: RC charge and energy accounting =============================== Charging an initially empty ideal capacitor from a constant voltage source dissipates half the eventual source energy in the resistor. Numerical integration checks energy conservation over a finite time interval. .. GENERATED FROM PYTHON SOURCE LINES 9-32 .. image-sg:: /api/gallery/electrical_engineering/images/sphx_glr_plot_electrical_engineering_benchmark_001.png :alt: plot electrical engineering benchmark :srcset: /api/gallery/electrical_engineering/images/sphx_glr_plot_electrical_engineering_benchmark_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.integrate import trapezoid from engineeringkit.electrical_engineering import rc_charging_voltage, rc_time_constant R, C, Vs = 1000, 1e-6, 5 tau = rc_time_constant(R, C) t = np.linspace(0, 10 * tau, 10001) v = rc_charging_voltage(Vs, R, C, t) i = (Vs - v) / R source = trapezoid(Vs * i, t) loss = trapezoid(R * i**2, t) stored = 0.5 * C * v[-1] ** 2 assert np.isclose(source, loss + stored, rtol=1e-6) assert np.isclose(rc_charging_voltage(Vs, R, C, tau), Vs * (1 - np.exp(-1))) fig, ax = plt.subplots() ax.plot(t / tau, v, label="capacitor voltage") ax.set(xlabel="time / RC", ylabel="voltage [V]") ax.legend() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.063 seconds) .. _sphx_glr_download_api_gallery_electrical_engineering_plot_electrical_engineering_benchmark.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/plot_electrical_engineering_benchmark.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_electrical_engineering_benchmark.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_electrical_engineering_benchmark.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_electrical_engineering_benchmark.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_