"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",
"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",
"$$ a=1 \\text{, } b=\\gamma/2p \\text{ when } \\gamma>2ω_0 $$\n",
"\n",
"a=1 $b=\\gamma/2$ when $\\gamma$=$2ω_0$\n",
"$$ a=1 \\text{, } b=\\gamma/2 \\text{ when } \\gamma=2ω_0 $$\n",
"\n",
"a=1 $b=\\gamma/2ω$ when $\\gamma$<$2ω_0$\n",
"$$ a=1 \\text{, } b=\\gamma/2ω \\text{ when } \\gamma<2ω_0 $$\n",
"\n",
"### Task\n",
"\n",
...
...
%% Cell type:markdown id:ed4c83c4 tags:
## Checkpoint 3
### Aim
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.
From a computing viewpoint this checkpoint demonstrates the use of *functions*, *loops*, *lists*, and the `Matplotlib pyplot` library for simple graph plotting.
### Mathematical Background
The damped simple harmonic oscillator satisfies the second order differential equation
$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.
Defining new constants
$\gamma$=b/m and $\omega_0^2$=k/m
we can re-write the differential equation as:
$$\ddot{x} + \gamma \dot{x} + \omega_{0}^2x =0 $$
where $ω_0$ is known as the natural frequency of the undamped oscillator.
The solutions to this equation take the following forms:
$$ x=\exp(−γt/2)[a\cosh(pt)+b\sinh(pt)] \text{ when } γ>2ω_0 \text{ with } p^2=(γ^2/4)−ω^2_0 $$
$$ x=\exp(−γt/2)[a+bt] \text{ when } γ=2ω_0 $$
$$ x=\exp(−γt/2)[a\cos(ωt)+b\sin(ωt)] \text{ when } γ<2ω_0 \text{ with } ω^2=ω^2_0−(γ^2/4)$$
where these three conditions are known as over damped, critically damped and under damped respectively.
Note: Read these equations *very* carefully and note the location of the parentheses.
With the initial conditions that x=1 and $\dot{x}$ =0 at t=0 the above constants, after some manipulation, become,
With the initial conditions that x=1 and $\dot{x}$ =0 at t=0 the above constants, after some manipulation, become
a=1 $b=\gamma/2p$ when $\gamma$>$2ω_0$
$$ a=1 \text{, } b=\gamma/2p\text{ when } \gamma>2ω_0 $$
a=1 $b=\gamma/2$ when $\gamma$=$2ω_0$
$$ a=1 \text{, } b=\gamma/2\text{ when } \gamma=2ω_0 $$
a=1 $b=\gamma/2ω$ when $\gamma$<$2ω_0$
$$ a=1 \text{, } b=\gamma/2ω\text{ when } \gamma<2ω_0 $$
### Task
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:
- Ask for and read in the values of $ω_0$, $\gamma$ and the number of points to plot on the graph from the terminal.
- Use a function of form shm(omega_zero,gamma,t) to calculate the displacement.
- Calculate and plot the amplitude and time to lists
- Plot the output via pyplot with suitable title and labels to axis
- Look at the structure of the [FlightPlot](../CodeExamples/FlightPlot.ipynb) example for structure.
### Background
For this checkpoint you will need to have read and studied the following sections:
1.[Lists](../CourseNotes/simplelists.ipynb)
2.[Loops](../CourseNotes/loops.ipynb)
3.[Plotting](../CourseNotes/plotting.ipynb)
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 (there is some helpful code for this included below).
### Checkpoint
- Test your program using the following input paramters:
- $\gamma=0.5\ \quad\&\quad \omega_0=1.0\quad$ for an under damped condition.
- $\gamma=2.0\ \quad\&\quad \omega_0=1.0\quad$ for a critically damped condition.
- $\gamma=20.0\!\quad\&\quad \omega_0=1.0\quad$ for an over damped condition.
- To obtain a smooth plot you need to calculate the above expression for at least 200 evenly spaced values of t.
- 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.
### Assessment
To pass this checkpoint:
- The programme works correctly for at least two of the three cases and the output plotted via pyplot.
- The output graph has correct title and labels.
- The program uses a function of the form shm(omega_zero,gamma,t) to calculate the displacement.
- The program calculates and stores the amplitude and time in lists with a loop to iterate over the number of points for the plot.
### Next steps
Once you have completed this checkpoint, please continue with the [Week3](../WeeklyTasks/Week3.ipynb) tasklist.
%% Cell type:code id:6077b2ca tags:
``` python
"""
Damped harmonic oscillator
"""
importmath
importmatplotlib.pyplotasplt# Import the plot package as plt
# Somewhere in your code you will need to implement the 3 distinct cases presented in the text to deal with various damping scenarios