Python Programs for class demonstrations. Generally these require python packages: numpy, scipy, Gnuplot. Also gnuplot.

Black Body Radiation Uncertainty Principle

Black-Body Radiation

#!/usr/bin/env python
from numpy import *
import Gnuplot as G
"""
Plots Blackbody Radiation Curves and amounts
at red, green, and violet
"""
red=650
org=590
grn=515
blu=475
vlt=400
ans='dummy'
while ans != 'q':
	T=float(raw_input('Enter Temp in Kelvin '))
	text = 'BB radiation at '+str(int(T))+' Kelvin'
	peakwave = (2.9e-3/T)*1e9
	print "peak wavelength ",peakwave
	x = linspace(100,5*peakwave,501)
	def f(x):
	    return(1/(x**5*exp(1.44e7/(x*T))-1))
	y = f(x)
	y_red=f(red)
	y_org=f(org)
	y_grn=f(grn)
	y_blu=f(blu)
	y_vlt=f(vlt)
	g = G.Gnuplot() #!! Won't be able to use 'with' in python 2.6?
	d = G.Data(x,y,title=text, with='l lt -1')
	d_red = G.Data([(red,0),(red,y_red)],with='l lt 1 lw 3')
	d_org = G.Data([(org,0),(org,y_org)],with='l lt 15 lw 3')
	d_grn = G.Data([(grn,0),(grn,y_grn)],with='l lt 2 lw 3')
	d_blu = G.Data([(blu,0),(blu,y_blu)],with='l lt 3 lw 3') 
	d_vlt = G.Data([(vlt,0),(vlt,y_vlt)],with='l lt 4 lw 3')
	g('set grid')
	g.xlabel('Wavelength (nm)')
	g.ylabel('y axis')
	g('set mouse')
	g.plot(d,d_red,d_org,d_grn,d_blu,d_vlt)
	ans = raw_input('Enter f to create .png file, or q to quit, enter to continue ')
	if ans == 'f':
	    g.hardcopy('Blackbody.png',terminal = 'png')
	    ans=raw_input('Enter q to quit, enter to continue ')    
g.reset()  

 

Uncertainty Principle

#! /usr/bin/env python
from __future__ import division 
from scipy import * 
import Gnuplot as G 
"""
Use fft to do uncertainty principle with wave packet 
Use spatial frequency f where k = (2pi)f
So wavelength is 1/f
"""
N=12       #  Use 2^N points
print 2**N,'points'
x=linspace(0,1,2**N+1)
# Use 2^N + 1 
dx = (x[-1]-x[0])/(len(x)-1)     # Maximum frequency is 1/dx ? 
fmax = 1/(2*dx) 
f0 = 100 
wn=float(raw_input('Enter wave packet width in # of wavelengths: '))
w=wn/f0 
sig = exp(-((x-0.4)/w)**2)*sin(2*pi*f0*x) 
gsig=G.Gnuplot()
dsig=G.Data(x,sig,title='Wave packet',with='l')   #Maybe plot probability density
gsig.xlabel('position')
gsig('set grid')
gsig('set mouse')
gsig.plot(dsig)

npts=2**N #Use some power of 2 
ft = fft(sig,n=npts) 
mgft=abs(ft)
df = fmax/float(npts/2)
f=linspace(0,fmax,npts/2+1)    
print 'fmax = ',fmax,' df = ',df,' ','\n f = ',f[0:5]
g=G.Gnuplot()
d=G.Data(f,mgft[0:npts/2+1],with='l')
g('set xrange [0:200]')
g.xlabel('Spatial frequency')
g.ylabel('fft magnitude')
g('set grid')
g('set mouse') 
g.plot(d) 
raw_input('Press Enter ') 
g.reset()
gsig.reset()