.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/control_systems/feedback/plot_04_ziegler_nichols_tuning.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_control_systems_feedback_plot_04_ziegler_nichols_tuning.py: Ziegler and Nichols tune a PID from the edge of instability =========================================================== John Ziegler and Nathaniel Nichols of Taylor Instruments (1942) gave plant engineers a recipe: turn up a proportional gain until the loop oscillates steadily, note that *ultimate gain* :math:`K_u` and period :math:`T_u`, and read P, PI or PID settings from a table (:func:`~engineeringkit.control_systems.ziegler_nichols`). The ultimate point is exactly the gain margin and phase-crossover frequency of the loop (:func:`~engineeringkit.control_systems.stability_margins`). For :math:`1/(s+1)^3`, :math:`K_u = 8` and :math:`\omega_u = \sqrt3` rad/s. .. GENERATED FROM PYTHON SOURCE LINES 16-27 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from engineeringkit.control_systems import poles, stability_margins, transfer_function, ziegler_nichols plant = transfer_function([1], [1, 3, 3, 1]) margins = stability_margins(plant) ku, tu = margins["gain_margin"], 2 * np.pi / margins["phase_cross_frequency"] print(f"ultimate gain {ku:.4f}, ultimate period {tu:.4f} s") assert np.isclose(ku, 8.0, rtol=1e-4) and np.isclose(tu, 2 * np.pi / np.sqrt(3), rtol=1e-4) .. rst-class:: sphx-glr-script-out .. code-block:: none ultimate gain 8.0000, ultimate period 3.6276 s .. GENERATED FROM PYTHON SOURCE LINES 28-30 Tuned step responses -------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-46 .. code-block:: Python t = np.linspace(0, 60, 3001) fig, ax = plt.subplots() for kind in ["P", "PI", "PID"]: pid = ziegler_nichols(ku, tu, kind) assert np.all(poles(pid.closed_loop(plant)).real < 0) r = pid.simulate(plant, t) ax.plot(r.t, r.y, label=f"{kind}: kp={pid.kp:.2f}, ki={pid.ki:.2f}, kd={pid.kd:.2f}") print(kind, {k: round(v, 3) for k, v in r.summary().items()}) if kind != "P": assert abs(r.y[-1] - 1) < 0.01 ax.axhline(1, color="k", ls="--", lw=0.8) ax.set(xlabel="time [s]", ylabel="output", title=r"Ziegler-Nichols settings for $1/(s+1)^3$") ax.legend() plt.show() .. image-sg:: /api/gallery/control_systems/feedback/images/sphx_glr_plot_04_ziegler_nichols_tuning_001.png :alt: Ziegler-Nichols settings for $1/(s+1)^3$ :srcset: /api/gallery/control_systems/feedback/images/sphx_glr_plot_04_ziegler_nichols_tuning_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none P {'final': 0.8, 'peak': 1.234} PI {'final': 0.999, 'peak': 1.561} PID {'final': 1.0, 'peak': 1.406} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.189 seconds) .. _sphx_glr_download_api_gallery_control_systems_feedback_plot_04_ziegler_nichols_tuning.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/control_systems/feedback/plot_04_ziegler_nichols_tuning.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_ziegler_nichols_tuning.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_ziegler_nichols_tuning.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_ziegler_nichols_tuning.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_