.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/mechanical_engineering/plot_mechanical_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_mechanical_engineering_plot_mechanical_engineering_benchmark.py: Oscillator energy balance ========================= An unforced undamped oscillator conserves kinetic plus spring energy. Viscous damping removes that energy; compare both against the initial value. .. GENERATED FROM PYTHON SOURCE LINES 8-28 .. image-sg:: /api/gallery/mechanical_engineering/images/sphx_glr_plot_mechanical_engineering_benchmark_001.png :alt: plot mechanical engineering benchmark :srcset: /api/gallery/mechanical_engineering/images/sphx_glr_plot_mechanical_engineering_benchmark_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from engineeringkit.mechanical_engineering import simulate_sdof t = np.linspace(0, 10, 501) fig, ax = plt.subplots() for c in [0, 0.4]: result = simulate_sdof(1, c, 4, x0=0.1, t=t) E = 0.5 * result.velocity**2 + 2 * result.displacement**2 if c == 0: np.testing.assert_allclose(E, 0.02, rtol=1e-5) else: assert E[-1] < E[0] ax.plot(t, E, label=f"c={c} N s/m") ax.set(xlabel="time [s]", ylabel="mechanical energy [J]") ax.legend() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.133 seconds) .. _sphx_glr_download_api_gallery_mechanical_engineering_plot_mechanical_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/mechanical_engineering/plot_mechanical_engineering_benchmark.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_mechanical_engineering_benchmark.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mechanical_engineering_benchmark.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_mechanical_engineering_benchmark.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_