In [101]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib
In [131]:
ctable= ['b','r']
lstyle = ['-.',':']
f0, [ax0,ax1,ax2] = plt.subplots(1,3)
f0.suptitle('Problem 1',fontsize = 'x-large')
ax0.set_title('A*rect(x/a)')
ax1.set_title('B*rect(x/b)')
ax2.set_title('convoluted signal')
for i, N in enumerate([250,500]):
print N
A = 1.
B = 1.
a = 4.
b = 1.
x1 = np.linspace(-10,10,N)
x2 = np.linspace(-10,10,N)
y1 = np.where((x1>-a/2) & (x1<=a/2), A*1., 0.)
y2 = np.where((x2>-b/2) & (x2<=b/2), B*1., 0.)
#y1 = y1/y1.sum()
y2_scaled = y2/y2.sum()
trapz = np.convolve(y1,y2_scaled,mode='same')
ax0.hold(True)
ax0.plot(x1,y1, color = ctable[i], linestyle = lstyle[i])
ax0.set_xlim(-3,3)
ax1.plot(x2,y2, color = ctable[i], linestyle = lstyle[i])
ax1.set_xlim(-3,3)
ax2.plot(x1,trapz, color = ctable[i], linestyle = lstyle[i])
ax2.set_xlim(-3,3)
ax2.legend(('N = 250','N = 500'),loc = 'lower right')
In [136]:
from math import pi
print pi
In [132]:
plt.close('all')
In [170]:
f0, ax = plt.subplots(1,2,sharex=True, sharey=True)
for i, N in enumerate([250,500]):
x = np.linspace(0,4,N)
y = np.cos(2*pi*x)
y_analytic = -2*pi*np.sin(2*pi*x)
der = [1,-1]
y_num = np.convolve(y,der,mode='same')
ax[i].plot(x,y_analytic, linestyle = '-.')
ax[i].hold(True)
ax[i].plot(x,y_num*N/4, c = 'r', linestyle = ':')
ax[i].legend(['Analytical derivative','Numerical derivative'])
ax[i].set_title('N = %s' %str(N))
In [160]:
ax
Out[160]:
In [161]:
ax[0]
Out[161]:
In [169]:
plt.close('all')
In [176]:
np.exp(a)
Out[176]:
In [175]:
a = np.complex(0,1)
In [267]:
f0, ax = plt.subplots(1,5)
for i, M in enumerate([10,20,30,40,50]):
m = np.array(range((2*M+1)))-M
k = np.linspace(-30,30,101)
dx = 0.1
m = m[:][np.newaxis]
k = k[:][np.newaxis]
print m.shape
print k.shape
matx = np.dot(m.T,k)*np.complex(0,1)
ft_matx = dx*np.exp(2*pi*matx*dx)
k = np.linspace(-30,30,61)
ax[i].plot(k,aaa.real, c= 'b')
ax[i].hold(True)
ax[i].plot(k,aaa.imag, c = 'r')
ax[i].legend(['real','imag'])
ax[i].set_title('M = %s' %M)
In [ ]:
ft_ele = dx*np.exp(np.complex(0,2*pi*k*m*dx))
In [238]:
k
Out[238]:
In [250]:
plt.close('all')
In [210]:
In [211]:
matx.shape
Out[211]:
In [216]:
In [214]:
matx = matx
In [275]:
#############
# Problem 4 #
#############
import numpy as np
import matplotlib.pyplot as plt
from math import pi
f0, ax = plt.subplots(1,2,sharex=True, sharey=True)
f0.suptitle('Problem 4')
N_k = 1001
for i, M in enumerate([10,50]):
m = np.array(range((2*M+1)))-M
k = np.linspace(-30,30,N_k)
dx = 0.1
m = m[:][np.newaxis]
k = k[:][np.newaxis]
matx = np.dot(m.T,k)*np.complex(0,1)
ft_matx = dx*np.exp(2*pi*matx*dx)
k = np.linspace(-30,30,N_k)
sigma = np.sum(ft_matx,axis = 0)
ax[i].plot(k,sigma.real, c= 'b')
ax[i].hold(True)
ax[i].plot(k,sigma.imag, c = 'r')
ax[i].legend(['real','imag'])
ax[i].set_title('M = %s' %M)
ax[i].set_xlabel('k (1/cm)')
ax[i].set_ylabel('Amplitude')
plt.show
Out[275]:
In [218]:
ft_matx.shape
Out[218]:
In [226]:
aaa = np.sum(ft_matx,axis = 0)
In [236]:
ax0.set_xl
Out[236]:
In [239]:
Out[239]:
In [270]:
plt.close('all')
In [223]:
numpy.sum?
In [ ]: