{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The Hanging Chain\n",
"\n",
"For mathematical details, see https://scipython.com/blog/the-hanging-chain/\n",
"\n",
"Set the variable `mode` in cell 2 to visualize different modes of the motion."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"from scipy.special import j0, jn_zeros\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib import animation\n",
"from IPython.display import HTML"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Acceleration due to gravity, m.s-2\n",
"g = 9.81\n",
"# Chain length, m\n",
"L = 1\n",
"# Maximum amplitude, m\n",
"A = 0.05\n",
"# Vertical axis (m)\n",
"z = np.linspace(0, L, 201)\n",
"# Scaled vertical axis\n",
"u = 2 * np.sqrt(z/g)\n",
"\n",
"mode = 3\n",
"# jn_zeros calculates the first n zeros of the zero-th order Bessel function of the first kind\n",
"w = jn_zeros(0, mode)[mode-1] * np.sqrt(g/L) / 2"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Set up the Figure and Axes\n",
"fig, ax = plt.subplots(figsize=(4,6))\n",
"\n",
"ax.axis('off')\n",
"ax.set_xlim((-2*A, 2*A))\n",
"ax.set_ylim((0, L))\n",
"ax.set_title('m = {}'.format(mode))\n",
"\n",
"line, = ax.plot([], [], 'k', dashes=[5,2], lw=3)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Initialization function for the animation\n",
"def init():\n",
" line.set_data([], [])\n",
" return (line,)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Delay between frames (ms)\n",
"interval = 10\n",
"# Total number of frames in the animation so that the animation is for one period of oscillation\n",
"nframes = int(2 * np.pi / w * 1000 / interval)\n",
"\n",
"# The animation function, to be called sequentially\n",
"def animate(i):\n",
" t = i * interval / 1000\n",
" x = A * j0(w*u) * np.cos(w*t)\n",
" line.set_data(x, z)\n",
" return (line,)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Set up the animation\n",
"anim = animation.FuncAnimation(fig, animate, init_func=init,\n",
" frames=nframes, interval=interval, blit=True)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# To save the animation as a gif, run this cell.\n",
"anim.save('chain_{}.gif'.format(mode), writer='imagemagick', fps=1000/interval)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# To show the animation in a Jupyter Notebook cell, run this cell.\n",
"HTML(anim.to_html5_video())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:py35]",
"language": "python",
"name": "conda-env-py35-py"
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}