{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13704 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# create a decaying sinewave, 8 cycles with decaying amplitude\n",
    "t = np.linspace(0, 8, 1000)\n",
    "decaying_sine = np.sin(t * np.pi) * 0.8 * np.exp(-t * np.pi / 4) + np.log(1 + t**0.25)\n",
    "\n",
    "# graph the decaying sinewave \n",
    "plt.plot(t, decaying_sine)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "1.0\n",
      "1.0\n",
      "1.0\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 10752 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "def plot_graph_tangent_at_time(graph, t, t_index, label='slope'):\n",
    "    # Get the slope of graph at that location\n",
    "    slope = np.gradient(graph, t)[t_index]\n",
    "    tangent = np.arctan(1/slope)\n",
    "    line_length = 1\n",
    "    line_w = np.sin(tangent) * line_length\n",
    "    line_h = np.cos(tangent) * line_length\n",
    "    # recalculate the length of the line\n",
    "    line_length = np.sqrt(line_w**2 + line_h**2)\n",
    "    print(line_length)\n",
    "\n",
    "    # Add a line to the graph\n",
    "    plt.scatter(t[t_index], graph[t_index], color='C1', linewidth=2)\n",
    "    plt.plot([t[t_index] - line_w, t[t_index] + line_w], [graph[t_index] - line_h, graph[t_index] + line_h], 'C1--', label=label)\n",
    "\n",
    "# PLot the last graph again\n",
    "plt.plot(t, decaying_sine, label='decaying sine wave')\n",
    "\n",
    "plot_graph_tangent_at_time(decaying_sine, t, 100)\n",
    "plot_graph_tangent_at_time(decaying_sine, t, 165)\n",
    "plot_graph_tangent_at_time(decaying_sine, t, 500)\n",
    "plot_graph_tangent_at_time(decaying_sine, t, 700)\n",
    "# plt.legend()\n",
    "plt.axis('equal')\n",
    "# set y axis range from 2 to 0\n",
    "plt.ylim(0, 2)\n",
    "\n",
    "plt.savefig(\"slopes_on_wave.png\", bbox_inches=\"tight\")\n",
    "plt.savefig(\"slopes_on_wave.svg\", bbox_inches=\"tight\", format=\"svg\")\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.4 ('.venv': venv)",
   "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.7.4"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "97190208b4a6552dc9493965a1caf8d39c9d1897fe9495f2fe88af90c5a07bd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
