.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "intro/scipy/auto_examples/plot_solve_ivp_damped_spring_mass.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_intro_scipy_auto_examples_plot_solve_ivp_damped_spring_mass.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_intro_scipy_auto_examples_plot_solve_ivp_damped_spring_mass.py:


============================================
Integrate the Damped spring-mass oscillator
============================================

.. GENERATED FROM PYTHON SOURCE LINES 8-37



.. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_solve_ivp_damped_spring_mass_001.png
   :alt: plot solve ivp damped spring mass
   :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_solve_ivp_damped_spring_mass_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import numpy as np
    import scipy as sp
    import matplotlib.pyplot as plt

    m = 0.5  # kg
    k = 4  # N/m
    c = 0.4  # N s/m

    zeta = c / (2 * m * np.sqrt(k / m))
    omega = np.sqrt(k / m)


    def f(t, z, zeta, omega):
        return (z[1], -zeta * omega * z[1] - omega**2 * z[0])


    t_span = (0, 10)
    t_eval = np.linspace(*t_span, 100)
    z0 = [1, 0]
    res = sp.integrate.solve_ivp(
        f, t_span, z0, t_eval=t_eval, args=(zeta, omega), method="LSODA"
    )

    plt.figure(figsize=(4, 3))
    plt.plot(res.t, res.y[0], label="y")
    plt.plot(res.t, res.y[1], label="dy/dt")
    plt.legend(loc="best")
    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.049 seconds)


.. _sphx_glr_download_intro_scipy_auto_examples_plot_solve_ivp_damped_spring_mass.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_solve_ivp_damped_spring_mass.ipynb <plot_solve_ivp_damped_spring_mass.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_solve_ivp_damped_spring_mass.py <plot_solve_ivp_damped_spring_mass.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_solve_ivp_damped_spring_mass.zip <plot_solve_ivp_damped_spring_mass.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_