{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\" From: \"A SURVEY OF COMPUTATIONAL PHYSICS\" \n", " by RH Landau, MJ Paez, and CC BORDEIANU \n", " Copyright Princeton University Press, Princeton, 2008.\n", " Electronic Materials copyright: R Landau, Oregon State Univ, 2008;\n", " MJ Paez, Univ Antioquia, 2008; and CC BORDEIANU, Univ Bucharest, 2008.\n", " Support by National Science Foundation\n", "\"\"\"\n", "# Version in which the real part is updated, than the imaginary part\n", "#is updated using the new values\n", "# Harmonic oscillator using MatPlotLib\n", "# runs very slowly\n", "from numpy import *\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import matplotlib.animation as animation\n", "%matplotlib notebook\n", "#psr=np.zeros(())\n", "#initialize wave function, probability, potential\n", "dx = 0.02; dx2 = dx*dx; k0 = 3.0*pi; dt = dx2/4.0; xmax = 7.5\n", "xs = np.arange(-xmax,xmax+dx/2,dx) # array of x positions\n", "nmax=751\n", "R=np.zeros((nmax,2),float)\n", "I=np.zeros((nmax,2),float)\n", "v=np.zeros((nmax),float)\n", "#psr = exp(-0.5*(xs/0.5)**2) * cos(k0*xs) # Re wave function Psi\n", "#psi = exp(-0.5*(xs/0.5)**2) * sin(k0*xs) # Im wave function Psi\n", "R[:,0]= exp(-0.5*(xs/0.5)**2) * cos(k0*xs) \n", "I[:,0]= exp(-0.5*(xs/0.5)**2) * sin(k0*xs) \n", " \n", "#print(R[750,0],I[750,0])\n", "v[:] = 5.0*xs**2\n", "fig=plt.figure() # figure to plot (a changing line)\n", "# select axis; 111: only one plot, x,y, scales given\n", "ax = fig.add_subplot(111, autoscale_on=False, xlim=(-xmax,xmax), ylim=(0, 1.1))\n", "ax.grid() # plot a grid\n", "plt.title(\"Harmonic Oscillator\")\n", "line, = ax.plot(xs, R[:,0]**2+I[:,0]**2, lw=2) # x axis, y values, linewidth=2\n", "\n", "def animate(dum):\n", " R[1:-1,1] = R[1:-1,0] - (dt)*(I[2:,0]+I[:-2,0]-2*I[1:-1,0])/dx2 +dt*v[1:-1]*I[1:-1,0]\n", " line.set_data(xs,R[:,1]*R[:,0]+I[:,0]**2+0.001*v[:])\n", " I[1:-1,1] = I[1:-1,0] + (dt)*(R[2:,1]+R[:-2,1]-2*R[1:-1,1])/dx2 -dt*v[1:-1]*R[1:-1,1]\n", " \n", " #R[:,0]=R[:,1]\n", " #I[:,0]=I[:,1]\n", " for i in range(0,nmax):\n", " R[i,0]=R[i,1]\n", " I[i,0]=I[i,1]\n", " \n", " return line,\n", " \n", "ani = animation.FuncAnimation(fig, animate,frames=1000,interval=1) \n", "\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }