Homework 7#
Due on Tuesday Dec 5 by midnight.
1. Zero-padding the cross-correlation function [Solution]#
The cross-correlation of two functions
Similarly to the convolution, it is straightforward to show that this can be evaluated using Fourier transforms as
where FT indicates Fourier transform and the overbar indicates the complex conjugate.
(a) Imagine that we have the values of two functions
(b) Next implement the cross-correlation using the Fourier transform (using numpy.fft
). In order to return numpy.fft.fft
the parameter n
that specifies the length. Compare the answer with part (a) – as a specific example, take
(c) Try part (b) without zero-padding the DFTs. What difference does it make? What part of the cross-correlation function is computed in this case?
2. Diffusion again(!) but this time with Chebyshev polynomials [Solution]#
Write a code that solves the diffusion equation
for
where
Hints:
if you need a reminder of what the Chebyshev polynomials look like, you can look at our earlier discussion of orthogonal polynomials.
you can fit a Chebyshev series to your initial
usingnumpy.polynomial.chebyshev.chebfit
or use the code from the orthogonal polynomial solutions.you should use an even number of polynomials in your Chebyshev series.
once you have the initial coefficients
, you can evolve them in time with a first order explicit Euler update.to calculate
you can usenp.polynomial.chebyshev.chebder
to obtain the coefficients of a Chebyshev series representing the second derivative .to enforce the boundary conditions initially and after each timestep, you can use the last two
values to set the sum of the even coefficients and the odd coefficients to zero.you might also find it helpful to set the coefficients to zero if they become too small, e.g. set
if drops below . This helps to avoid accumulation of roundoff errors.the Green’s function in this case where we have zero boundary conditions can be constructed by adding mirror images outside the domain on the left and right: (same ideas as the method of images in electrostatics!)
def greens_unbounded(x, x0, t, D):
# Green's function for the diffusion equation
return np.exp(-(x-x0)**2/(4*D*t)) / np.sqrt(4*np.pi*D*t)
def greens_bounded(x, x0, t, D):
# Green's function for the diffusion equation with zero boundary conditions at x=-1,+1
f = gaussian(x, x0, t, D)
f -= gaussian(x, -1-(1+x0), t, D)
f -= gaussian(x, 1+(1-x0), t, D)
return f