{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# Integrate the Damped spring-mass oscillator\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28468 characters]",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import scipy as sp\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "m = 0.5  # kg\n",
    "k = 4  # N/m\n",
    "c = 0.4  # N s/m\n",
    "\n",
    "zeta = c / (2 * m * np.sqrt(k / m))\n",
    "omega = np.sqrt(k / m)\n",
    "\n",
    "\n",
    "def f(t, z, zeta, omega):\n",
    "    return (z[1], -zeta * omega * z[1] - omega**2 * z[0])\n",
    "\n",
    "\n",
    "t_span = (0, 10)\n",
    "t_eval = np.linspace(*t_span, 100)\n",
    "z0 = [1, 0]\n",
    "res = sp.integrate.solve_ivp(\n",
    "    f, t_span, z0, t_eval=t_eval, args=(zeta, omega), method=\"LSODA\"\n",
    ")\n",
    "\n",
    "plt.figure(figsize=(4, 3))\n",
    "plt.plot(res.t, res.y[0], label=\"y\")\n",
    "plt.plot(res.t, res.y[1], label=\"dy/dt\")\n",
    "plt.legend(loc=\"best\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
