.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/electrical_engineering/ac_circuits/plot_02_steinmetz_phasors.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_ac_circuits_plot_02_steinmetz_phasors.py: Steinmetz's phasors and complex impedance (1893) ================================================ AC circuits obey linear differential equations, and before 1893 engineers solved them with trigonometry and graphical constructions. At the International Electrical Congress in Chicago, Charles Proteus Steinmetz showed that a sinusoid :math:`A\cos(\omega t + \phi)` can be carried as the complex number :math:`Ae^{j\phi}`. Differentiation becomes multiplication by :math:`j\omega`, so an inductor becomes the impedance :math:`j\omega L` (:func:`~engineeringkit.electrical_engineering.inductor_impedance`), a capacitor :math:`1/j\omega C` (:func:`~engineeringkit.electrical_engineering.capacitor_impedance`), and AC circuits are solved by Ohm's law with complex numbers (:func:`~engineeringkit.electrical_engineering.series_rlc_impedance`). This example solves a series RLC circuit by phasors and checks the answer against a time-domain simulation. .. GENERATED FROM PYTHON SOURCE LINES 21-34 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.integrate import solve_ivp from engineeringkit.electrical_engineering import ( capacitor_impedance, inductor_impedance, phasor, polar, series_rlc_impedance, ) from engineeringkit.electrical_engineering.visualizers import plot_phasors .. GENERATED FROM PYTHON SOURCE LINES 35-37 Phasor solution --------------- .. GENERATED FROM PYTHON SOURCE LINES 37-47 .. code-block:: Python R, L, C, f, Vm = 30.0, 0.1, 50e-6, 50.0, 100.0 w = 2 * np.pi * f Z = series_rlc_impedance(R, L, C, w) I = phasor(Vm, 0.0) / Z magnitude, phase = polar(I) print(f"Z = {Z:.2f} ohm, I = {magnitude:.3f} A at {np.degrees(phase):.1f} degrees") VR, VL, VC = I * R, I * inductor_impedance(L, w), I * capacitor_impedance(C, w) assert np.isclose(VR + VL + VC, Vm) # Kirchhoff's voltage law holds for phasors .. rst-class:: sphx-glr-script-out .. code-block:: none Z = 30.00-32.25j ohm, I = 2.270 A at 47.1 degrees .. GENERATED FROM PYTHON SOURCE LINES 48-53 Time-domain check ----------------- Integrate :math:`L\,di/dt + Ri + q/C = V_m\cos\omega t` for 40 cycles and compare the steady state with :math:`|I|\cos(\omega t + \arg I)`. .. GENERATED FROM PYTHON SOURCE LINES 53-78 .. code-block:: Python def rhs(t, x): q, i = x return [i, (Vm * np.cos(w * t) - R * i - q / C) / L] t_end = 40 / f sol = solve_ivp(rhs, (0, t_end), [0, 0], rtol=1e-9, atol=1e-12, dense_output=True) t = np.linspace(t_end - 2 / f, t_end, 400) i_sim = sol.sol(t)[1] i_phasor = magnitude * np.cos(w * t + phase) print(f"largest difference after 40 cycles: {np.max(np.abs(i_sim - i_phasor)):.1e} A") assert np.allclose(i_sim, i_phasor, atol=1e-4 * magnitude) fig, axes = plt.subplots(1, 2, figsize=(10, 4)) plot_phasors([Vm, VR, VL, VC, I * 50], labels=["$V$", "$V_R$", "$V_L$", "$V_C$", "$I$ (x50)"], ax=axes[0]) axes[0].set_title("Phasor diagram") axes[1].plot(t * 1e3, i_sim, label="ODE simulation") axes[1].plot(t * 1e3, i_phasor, "--", label="phasor solution") axes[1].set(xlabel="time [ms]", ylabel="current [A]", title="Steady state") axes[1].legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/electrical_engineering/ac_circuits/images/sphx_glr_plot_02_steinmetz_phasors_001.png :alt: Phasor diagram, Steady state :srcset: /api/gallery/electrical_engineering/ac_circuits/images/sphx_glr_plot_02_steinmetz_phasors_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none largest difference after 40 cycles: 1.4e-09 A .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.155 seconds) .. _sphx_glr_download_api_gallery_electrical_engineering_ac_circuits_plot_02_steinmetz_phasors.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/ac_circuits/plot_02_steinmetz_phasors.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_steinmetz_phasors.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_steinmetz_phasors.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_steinmetz_phasors.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_