Monday, December 12, 2011

Modeling Wave Packets Using Python

 In this project we wrote a computer code in Python to help visualize wave packets. We also used Pylab to show the code as a series of graphs. In order to make our graphs we had to write our code in a series of steps. The first step was to create a sine graph. Next we manipulated that graph so that we could show several harmonics or several sine graphs of differing periodicity. After that was completed we then generated a Gaussian. After that was completed we then had to combine the sine functions and the Gaussian to generate a wave packet. This was very complicated to do and took us the most amount of time to resolve. When this was all completed we were able to generate normalized graphs that visually depicted wave packets. The code we used to do this is bellow. The code is "messy" so many lines were canceled out with ## so that everything would run like we wanted. However is is possible to obtain the sine functions and the Gaussian separately by canceling out the appropriate line and removing the ##s where necessary.

from pylab import*
harmonics= 20
center=harmonics/2
sigma=1
coeff=1/((sqrt(2*pi))*sigma)
gauss_list=[]
domain_list2=[]
L=10
knot= 2*pi/L
b_list=[]
kdomain=[]
##
##for k in arange(0,200,0.1):
##    bofk=(1/(knot))
##    b_list.append(bofk)
##    kdomain.append(k)
####plot(kdomain,b_list)
##




for x in range(1,harmonics):
    gauss=coeff*exp(-(x-center)**2/(2.*sigma**2))
    gauss_list.append(gauss)
    domain_list2.append(x)
  
##A_coeff_1=1
##wave_constant_1=1
##sine1_list=[]
##domain_list=[]
##for x in arange(-pi,pi,0.1):
##    sine1=A_coeff_1*sin(wave_constant_1*x)
##    sine1_list.append(sine1)
##    domain_list.append(x)
w=1
Fourier_Series=[]

for i in range(1,harmonics):
    sine_function=[]
    x=[]
    for t in arange(-pi,pi,0.01):
        sine_f=gauss_list[i-1]*sin(i*w*t)
        sine_function.append(sine_f)
        x.append(t)
    ##plot(x,sine_function)
    Fourier_Series.append(sine_function)

superposition=zeros(len(sine_function))
for function in Fourier_Series:
    for i in range(len(function)):
        superposition[i]+=function[i]
print kdomain
plot(x,superposition)
##plot(domain_list2,gauss_list)
##plot(domain_list,sine1_list)
show()

No comments:

Post a Comment