Tuesday, December 13, 2011

Python Lab #2

from pylab import*
r=50
center = r/2
sigma=1
coeff=1/sqrt(2*pi)*sigma
gauss_list=[]

k=1
pii=3.14
for x in arange(0,r):
    gauss=coeff*exp(-(x-center)**2/(2*sigma**2))
    gauss_list.append(gauss)
fourier_series=[]
A=gauss_list

for i in range (1,r+1):
    sin_function_list=[]
    domain_list=[]
    for x in arange (-pii,pii,0.01):
        sin_function=A[i-1]*sin(i*k*x)
        
        sin_function_list.append(sin_function)
        domain_list.append(x)
    fourier_series.append(sin_function_list)

superposition = zeros(len(sin_function_list))
for function in fourier_series:
    for i in range (len(function)):
        superposition[i]=superposition[i]+function[i]
plot(domain_list,superposition)
show()

this python lab was a lot less about how to program and a lot more on how can programming help us understand waves better
the code above displaces a graph of different waves functions adding and subtracting forming one wave, the superposition of them.

we can see that we defined a gauss function which represents the graph and have a while loop making a few gauss functions and plotting the total of their superposition.

No comments:

Post a Comment