{ "cells": [ { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Question 2: Changing \"sinwave.py\" so that it plots a moving, pulsating blob\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from matplotlib import animation\n", "fig = plt.figure()\n", "ax = plt.axes(xlim=(-2, 12), ylim=(0, 1)) ### changed this line\n", "line, = ax.plot([], [], lw=2)\n", "def init(): # Initialize with a blank plot\n", " line.set_data([], [])\n", " return line,\n", "def animate(i): # Plot a moving sine wave\n", " x = np.linspace(-2, 12, 1000) ### changed these lines\n", " x0 = 5*(1 + np.sin(0.01*i)) ###\n", " x1 = 0.05*i ###\n", " y = np.exp(-(x-x0)**2/(1.5+np.cos(x1))) ###\n", " line.set_data(x, y)\n", " return line,\n", "anim = animation.FuncAnimation(fig, animate, init_func=init,\n", " frames=1000, interval=20, blit=False)\n", "# Changed the number of frames from 200 to 1000 for a longer animation\n", "# On my Mac, I had to set blit=False. On the Managed Desktops you can set blit=True.\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Question 3: Animating the predator-prey system\n", "from scipy.integrate import odeint\n", "\n", "f, g, h = 8/3, 3/50, 3/20\n", "# predator-prey equations\n", "def dX_dt(X, t):\n", " x, y = X\n", " dxdt = x*(1-g*x) - x*y/(1+h*x)\n", " dydt = -f*y + x*y/(1+h*x)\n", " return [dxdt, dydt]\n", "\n", "def random_ic(): # generate initial condition\n", " return (5/2*(0.5+np.random.rand(2)))\n", "\n", "npts = 500; tmax = 100.0\n", "ts = np.linspace(0.0, tmax, npts)\n", "nlines = 20\n", "linedata = []\n", "for ic in [random_ic() for i in range(nlines)]:\n", " linedata.append( odeint(dX_dt, ic, ts) )\n", "\n", "fig = plt.figure()\n", "ax = plt.axes(xlim=(0,12), ylim=(0, 4))\n", "line, = ax.plot([], [], 'ro')\n", "npts = len(linedata[0][:,0])\n", "def init(): \n", " line.set_data([],[])\n", " return line,\n", "def animate(i): \n", " line.set_data([l[i,0] for l in linedata], [l[i,1] for l in linedata])\n", " return line,\n", "\n", "anim = animation.FuncAnimation(fig, animate, init_func=init,\n", " frames=npts, interval=50, blit=False)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Question 4: Building up the set of points in the complex plane which map onto\n", "# 0 under the iterative map z -> z - f(z)/f'(z), with f(z) = z^3 - 1.\n", "\n", "# First, generate the data.\n", "n = 9;\n", "l = [np.zeros(1)]\n", "for k in range(n):\n", " myroots = np.zeros(3**(k+1),dtype=np.complex)\n", " for j in range(len(l[k])):\n", " w = l[k][j]\n", " t0 = np.roots([2, -3*w, 0, 1])\n", " myroots[3*j:3*(j+1)] = t0\n", " l.append(myroots)\n", "\n", "def topoints(l, k):\n", " arr = np.array([item for sublist in l[:k] for item in sublist])\n", " return (arr.real, arr.imag)\n", "\n", "# For a static plot, uncomment these lines\n", "#pts = topoints(l,n)\n", "#plt.plot(pts[0], pts[1], 'r.')\n", "#plt.xlim([-5,5]); plt.ylim([-5,5])\n", "#plt.show()\n", "\n", "# Next, create the animation.\n", "fig = plt.figure()\n", "ax = plt.axes(xlim=(-5, 5), ylim=(-5, 5))\n", "line, = ax.plot([], [], 'b.', lw=0.2)\n", "def init(): # Initialize with just one point\n", " line.set_data([0], [0])\n", " return line,\n", "def animate(i): \n", " x, y = topoints(l, i)\n", " line.set_data(x, y)\n", " return line,\n", "anim = animation.FuncAnimation(fig, animate, init_func=init,\n", " frames=n, interval=1000, blit=False)\n", "# I set the time between frames to 1 second (1000 milliseconds)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }