{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "# Interactive plots: Undamped oscillator\n", "\n", "---\n", "\n", "\n", "\n", "This notebook is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load packages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will need to have the following packages installed in order to run this notebook:\n", "\n", "* `ipython`\n", "* `ipywidgets`\n", "* `matplotlib`\n", "* `numpy`\n", "* `pandas`\n", "\n", "The interactive dashboard will run fine in regular Jupyter notebook mode, launch it by running,\n", "\n", "```bash\n", "jupyter notebook\n", "```\n", "\n", "**To get this to work in Jupyter Lab**, you need to first run the following on the command line,\n", "\n", "```bash\n", "jupyter labextension install @jupyter-widgets/jupyterlab-manager\n", "```\n", "\n", "After successfully installing the `@jupyter-widgets/jupyterlab-manager` JupyterLab extension, launch JupyterLab:\n", "\n", "```bash\n", "jupyter lab\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "from IPython.display import clear_output, display\n", "from ipywidgets import interact, interactive, fixed, FloatSlider, FloatText, RadioButtons\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "\n", "import undamped_oscillator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define interactive function" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def sim_plot(\n", " sim_time,\n", " delta_t,\n", " method,\n", "):\n", " simulation_df = undamped_oscillator.simulation(\n", " sim_time=sim_time,\n", " delta_t=delta_t,\n", " method=method,\n", " )\n", " \n", " figwidth = 5 # inches\n", " figheight = 0.618 * figwidth # golden ratio\n", " figsize = (figwidth, 1.5 * figheight)\n", " \n", " fig, ax = plt.subplots(nrows=2, ncols=1, figsize=figsize, dpi=120, sharex=True);\n", " \n", " ax[0].plot(simulation_df[\"time\"], simulation_df[\"length\"], \"-\")\n", " ax[1].plot(simulation_df[\"time\"], simulation_df[\"velocity\"], \"-\")\n", " ax[0].set_xlabel(\"time (s)\")\n", " ax[0].set_ylabel(\"length (m)\")\n", " ax[1].set_xlabel(\"time (s)\")\n", " ax[1].set_ylabel(\"velocity (m/s)\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup dashboard" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "integration_methods = [\"forward\", \"midpoint\", \"leapfrog\"]\n", "\n", "sim_time = FloatSlider(\n", " description=\"sim_time\",\n", " value=5.0,\n", " min=1,\n", " max=10,\n", " step=1,\n", ")\n", "delta_t = FloatText(\n", " description=\"delta_t\",\n", " value=0.01,\n", ")\n", "method = RadioButtons(\n", " options=integration_methods,\n", " description=\"integration method\",\n", ")\n", "\n", "interactive_window = interactive(\n", " sim_plot,\n", " sim_time=sim_time,\n", " delta_t=delta_t,\n", " method=method,\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c30914ab484542908279ec7c34fb91ab", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=5.0, description='sim_time', max=10.0, min=1.0, step=1.0), FloatText(v…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(interactive_window)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }