# Python 2.7 file for Numerical Methods II, Spring 2016 # http://www.math.nyu.edu/faculty/goodman/teaching/NumericalMethodsII2016/index.html # File: FourierAnalysis.py # For assignment 2 # Solve the heat equation and make a movie of the solution # Type: python FourierAnalysis.py ... and hope for the best import numpy as np import scipy as sc import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import copy as cp # The difference between copy (simple =) and deep copy print "Warning about the meaning of = in Python\n" n = 5 print "Change u by changing v, which is bound to the same object" u = np.ndarray(n) u[0] = 1. u[1:n] = 0. print "before changing v, u is: " + str(u) v = u v[1] = 1. print "after changing v, u is: " + str(u) + " (u[1] is different!)" print "deepcopy creates a distinct object, not just a new name" u = np.ndarray(n) u[0] = 1. u[1:n] = 0. print "before changing v, u is: " + str(u) v = cp.deepcopy(u) v[1] = 1. print "after changing v, u is: " + str(u) + " (u[1] is hasn't changed!)" print "\nHello Fourier analysis\n" n = 6 u = np.ndarray(n) u[0] = 1. u[1:n] = 0. v = cp.deepcopy(u) # Now doing it the right way v[1] = 1. uHat = np.fft.fft(u) print " uHat is: " + str(uHat) vHat = np.fft.fft(v) print " vHat is: " + str(vHat) uNsq = np.dot(u,u) print "uNsq = " + str(uNsq) uHNsq = (np.vdot(uHat,uHat)).real uHNsq = abs(np.vdot(uHat,uHat)) # should get the same answer either way print "uHNsq = " + str(uHNsq) udv = np.dot(u,v) print "u dot v is " + str(udv) uHdvH = np.vdot(uHat,vHat) print "uHat dot vHat is " + str(uHdvH) + ", Plancharel worked?" n = 10000 L = 1. dx = L/n xm = L/2. r = .001 u = np.ndarray(n) for j in range(n): xj = dx*j u[j] = np.exp( -( (xj-xm)*(xj-xm)) / ( 2.* r*r)) uHat = np.fft.fft(u) plt.figure(1) plt.plot(uHat.real) plt.ylabel("fft") plt.title("Beautiful Gaussian in the center of the plot with n=50") plt.grid() plt.savefig("fft.pdf")