{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ed4c83c4",
   "metadata": {},
   "source": [
    "## Checkpoint 3\n",
    "\n",
    "### Aim\n",
    "\n",
    "To write a Python program to explore the behaviour of a damped simple harmonic oscillator for a range of damping coefficients. This involves writing a program to trace out the amplitude against time for a damped simple harmonic oscillator under a range of conditions.\n",
    "\n",
    "From a computing viewpoint this checkpoint demonstrates the use of *functions*, *loops*, *lists*, and the `Matplotlib pyplot` library for simple graph plotting.\n",
    "\n",
    "### Mathematical Background\n",
    "\n",
    "The damped simple harmonic oscillator satisfies the second order differential equation\n",
    "$m\\ddot{x} +b \\dot{x} + kx=0$ where m is the mass of the oscillator, b is the coefficient of damping, and k is the spring constant.\n",
    "\n",
    "Defining new constants\n",
    "$\\gamma$=b/m and $\\omega_0^2$=k/m\n",
    "we can re-write the differential equation as:\n",
    "\n",
    "$\\ddot{x} + \\gamma \\dot{x} + \\omega_{0}^2x =0 $\n",
    "\n",
    "where ω$_0$ is known as the natural frequency of the undamped oscillator.\n",
    "\n",
    "The solutions to this equation take the following forms:\n",
    "\n",
    "$ x=\\exp(−γt/2)[a\\cosh(pt)+b\\sinh(pt)] $ when $ γ>2ω_0 $ with $ p^2=(γ^2/4)−ω^2_0 $\n",
    "\n",
    "$ x=\\exp(−γt/2)[a+bt] $ when $ γ=2ω_0 $\n",
    "\n",
    "$ x=\\exp(−γt/2)[a\\cos(ωt)+b\\sin(ωt)] $ when $ γ<2ω_0 $ with $ ω^2=ω^2_0−(γ^2/4)$\n",
    "\n",
    "where these three conditions are known as over damped, critically damped and under damped respectively.\n",
    "\n",
    "Note: Read these equations *very* carefully and note the location of the parentheses.\n",
    "\n",
    "With the initial conditions that x=1 and $\\dot{x}$ =0 at t=0 the above constants, after some manipulation, become,\n",
    "\n",
    "a=1 b=$\\gamma$/2p when $\\gamma$>2ω$_0$\n",
    "\n",
    "a=1 b=$\\gamma$/2 when $\\gamma$=2ω$_0$\n",
    "\n",
    "a=1 b=$\\gamma$/2ω when $\\gamma$<2ω$_0$\n",
    "\n",
    "### Task\n",
    "\n",
    "Write an interactive Python program to compute and display, using the pyplot function from Matplotlib, the solution for x against t for t in the range 0$\\to 5\\pi/ω_0$. Your program should:\n",
    "\n",
    "- Ask for and read in the values of ω$_0$, $\\gamma$ and the number of points to plot on the graph from the terminal.\n",
    "- Use a function of form shm(omega_zero,gamma,t) to calculate the displacement.\n",
    "- Calculate and plot the amplitude and time to lists\n",
    "- Plot the output via pyplot with suitable title and labels to axis\n",
    "- Look at the structure of the [FlightPlot](../CodeExamples/FlightPlot.ipynb) example for structure.\n",
    "\n",
    "### Background\n",
    "\n",
    "For this checkpoint you will need to have read and studied the following sections:\n",
    "\n",
    "1. [Lists](../CourseNotes/simplelists.ipynb)\n",
    "2. [Loops](../CourseNotes/loops.ipynb)\n",
    "3. [Plotting](../CourseNotes/plotting.ipynb)\n",
    "\n",
    "Note: The structure of this program is reasonably complex. Think carefully how you are going to structure it before you start. Also be very careful when coding the expression for the amplitude of the oscillation; in particular make sure it is performing the correct arithmetic.\n",
    "\n",
    "\n",
    "### Checkpoint\n",
    "\n",
    "- Test your program using the following input paramters:\n",
    "    - $\\gamma=0.5\\ \\quad\\&\\quad \\omega_0=1.0\\quad$ for an under damped condition.  \n",
    "    - $\\gamma=2.0\\ \\quad\\&\\quad \\omega_0=1.0\\quad$ for an critically damped condition.  \n",
    "    - $\\gamma=20.0\\! \\quad\\&\\quad \\omega_0=1.0\\quad$ for an over damped condition.  \n",
    "- To obtain a smooth plot you need to calculate the above expression for at least 200 evenly spaced values of t.\n",
    "- Call a demonstrator, show them your code and run your program with the three sets of inputs above and any additional ones requested by the demonstrator.\n",
    "\n",
    "### Assessment\n",
    "\n",
    "To pass this checkpoint:\n",
    "\n",
    "- The programme works correctly for at least two of the three cases and the output plotted via pyplot.\n",
    "- The output graph has correct title and labels.\n",
    "- The program uses a function of the form shm(omega_zero,gamma,t) to calculate the displacement.\n",
    "- The program calculates and stores the amplitude and time in lists with a loop to iterate over the number of points for the plot.\n",
    "\n",
    "### Next steps\n",
    "Once you have completed this checkpoint, please continue with the [Week3](../WeeklyTasks/week3.ipynb) tasklist."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6077b2ca",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}