In [101]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib


Using matplotlib backend: MacOSX

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')


250
500

In [136]:
from math import pi
print pi


3.14159265359

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]:
array([<matplotlib.axes.AxesSubplot object at 0x11079d410>,
       <matplotlib.axes.AxesSubplot object at 0x10d7eb8d0>], dtype=object)

In [161]:
ax[0]


Out[161]:
<matplotlib.axes.AxesSubplot at 0x11079d410>

In [169]:
plt.close('all')

In [176]:
np.exp(a)


Out[176]:
(0.54030230586813977+0.8414709848078965j)

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)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-267-083db939d2cf> in <module>()
     11     ft_matx = dx*np.exp(2*pi*matx*dx)
     12     k = np.linspace(-30,30,61)
---> 13     ax[i].plot(k,aaa.real, c= 'b')
     14     ax[i].hold(True)
     15     ax[i].plot(k,aaa.imag, c = 'r')

/Users/Yigong/anaconda/lib/python2.7/site-packages/matplotlib/axes.pyc in plot(self, *args, **kwargs)
   3994         lines = []
   3995 
-> 3996         for line in self._get_lines(*args, **kwargs):
   3997             self.add_line(line)
   3998             lines.append(line)

/Users/Yigong/anaconda/lib/python2.7/site-packages/matplotlib/axes.pyc in _grab_next_args(self, *args, **kwargs)
    328                 return
    329             if len(remaining) <= 3:
--> 330                 for seg in self._plot_args(remaining, kwargs):
    331                     yield seg
    332                 return

/Users/Yigong/anaconda/lib/python2.7/site-packages/matplotlib/axes.pyc in _plot_args(self, tup, kwargs)
    306             x = np.arange(y.shape[0], dtype=float)
    307 
--> 308         x, y = self._xy_from_xy(x, y)
    309 
    310         if self.command == 'plot':

/Users/Yigong/anaconda/lib/python2.7/site-packages/matplotlib/axes.pyc in _xy_from_xy(self, x, y)
    246         y = np.atleast_1d(y)
    247         if x.shape[0] != y.shape[0]:
--> 248             raise ValueError("x and y must have same first dimension")
    249         if x.ndim > 2 or y.ndim > 2:
    250             raise ValueError("x and y can be no greater than 2-D")

ValueError: x and y must have same first dimension
(1, 21)
(1, 101)

In [ ]:
ft_ele = dx*np.exp(np.complex(0,2*pi*k*m*dx))

In [238]:
k


Out[238]:
array([-30., -29., -28., -27., -26., -25., -24., -23., -22., -21., -20.,
       -19., -18., -17., -16., -15., -14., -13., -12., -11., -10.,  -9.,
        -8.,  -7.,  -6.,  -5.,  -4.,  -3.,  -2.,  -1.,   0.,   1.,   2.,
         3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.,  13.,
        14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,  22.,  23.,  24.,
        25.,  26.,  27.,  28.,  29.,  30.])

In [250]:
plt.close('all')

In [210]:


In [211]:
matx.shape


Out[211]:
(21, 61)

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]:
<function matplotlib.pyplot.show>

In [218]:
ft_matx.shape


Out[218]:
(21, 61)

In [226]:
aaa = np.sum(ft_matx,axis = 0)

In [236]:
ax0.set_xl


Out[236]:
array([-29., -28., -27., -26., -25., -24., -23., -22., -21., -20., -19.,
       -18., -17., -16., -15., -14., -13., -12., -11., -10.,  -9.,  -8.,
        -7.,  -6.,  -5.,  -4.,  -3.,  -2.,  -1.,   0.,   1.,   2.,   3.,
         4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.,  13.,  14.,
        15.,  16.,  17.,  18.,  19.,  20.,  21.,  22.,  23.,  24.,  25.,
        26.,  27.,  28.,  29.,  30.])

In [239]:



Out[239]:
[<matplotlib.lines.Line2D at 0x110721710>]

In [270]:
plt.close('all')

In [223]:
numpy.sum?


Object `numpy.sum` not found.

In [ ]: