def neglap2d(n): """ Returns the Chebyshev collocation approximation to -D_x^2-D_y^2 on the square [-1,1]^2 on the interior points only """ D, x = cheb(n + 1) D2 = np.dot(D, D) D2 = D2[1:-1, 1:-1] D2 = csr_matrix(D2) I = identity(n) L = -(kron(D2, I) + kron(I, D2)) # FD negative Laplacian return L
# p14 - solve nonlinear BVP U_xx = exp(u), u(-1)=u(1)=0 by iteration. import numpy as np from chebPy import cheb from scipy.linalg import solve from matplotlib import pyplot as plt N=16 D,x=cheb(N) D2=np.dot(D,D) D2=D2[1:N,1:N] u=np.zeros(N-1) err=np.zeros(N-1) change = 0.1 # arbitrary small number it = 0 while True: unew=solve(D2,np.exp(u)) err = np.abs(unew-u) change = err.max() u = unew it += 1 if(change < 1e-15): break s=np.zeros(N+1) s[0]=0.0 s[N]=0.0 s[1:N]=u xx=np.linspace(-1.,1.,100)
# p35.py - Allen-Cahn eq. # same problem as in p34.py, except BCs that are imposed explicitly from chebPy import cheb import numpy as np from scipy.linalg import solve import pylab as p import mpl_toolkits.mplot3d.axes3d as p3 from matplotlib import cm # Differentiation matrix and initial data: N = 20 D, x = cheb(N) D2 = np.dot(D, D) # Use full size matrix D2[0, :] = 0. # for convenience D2[N, :] = 0. eps = 0.01 dt = min(.01, 50. * N**(-4.) / eps) t = 0. v = .53 * x + .47 * np.sin(-1.5 * np.pi * x) # Solve PDE by Euler formula and plot results: tmax = 100. tplot = 2 nplots = int(round(tmax / tplot)) plotgap = int(round(tplot / dt)) dt = float(tplot) / float(plotgap) xx = np.linspace(-1., 1., 40) vv = np.polyval(np.polyfit(x, v, N), xx) plotdata = np.zeros((nplots + 1, xx.size))
# x variable in [-A,A], Fourier - trigonometric series: A = 3. Nx = 50 dx = 2 * A / float(Nx) x = -A + dx * np.arange(1, Nx + 1) # Create entries for Derivative matrix - a Toeplitz matrix with an entry from 'entries' on each diagonal. entries = np.zeros(Nx) entries[0] = -1 / (3 * (dx / A)**2) - 1 / 6. entries[1:Nx] = .5 * (-1)**np.arange(2, Nx + 1) / np.sin( (np.pi * dx / A) * np.arange(1, Nx) / 2)**2 # Form derivative matrix as a Toeplitz matrix using 'entries' D2x = (np.pi / A)**2 * toeplitz(entries) # y variable in [-1,1], Chebyshev: Ny = 15 Dy, y = cheb(Ny) D2y = np.dot(Dy, Dy) # Prepare boundary conditions Abc = -np.array([[Dy[0, 0], Dy[0, Ny]], [Dy[Ny, 0], Dy[Ny, Ny]]]) BC = np.zeros((2, Ny - 1)) Dybc = np.zeros((2, Ny - 1)) Dybc[0, 0:Ny - 1] = Dy[0, 1:Ny] Dybc[1, 0:Ny - 1] = Dy[Ny, 1:Ny] BC[:, 0:Ny - 1] = solve(Abc, Dybc[:, 0:Ny - 1]) # Grid and initial data: xx, yy = np.meshgrid(x, y) vv = np.exp(-8. * ((xx + 1.5)**2 + yy**2)) dt = 5. / float(Nx + Ny**2) vvold = np.exp(-8. * ((xx + dt + 1.5)**2 + yy**2))
# x variable in [-A,A], Fourier - trigonometric series: A = 3. Nx = 50 dx = 2*A/float(Nx) x = -A + dx*np.arange(1,Nx+1) # Create entries for Derivative matrix - a Toeplitz matrix with an entry from 'entries' on each diagonal. entries = np.zeros(Nx) entries[0] = -1/(3*(dx/A)**2)-1/6. entries[1:Nx] = .5*(-1)**np.arange(2,Nx+1)/np.sin((np.pi*dx/A)*np.arange(1,Nx)/2)**2 # Form derivative matrix as a Toeplitz matrix using 'entries' D2x = (np.pi/A)**2*toeplitz(entries) # y variable in [-1,1], Chebyshev: Ny =15 Dy,y = cheb(Ny) D2y = np.dot(Dy,Dy) # Prepare boundary conditions Abc = - np.array([ [Dy[0,0],Dy[0,Ny]], [Dy[Ny,0],Dy[Ny,Ny]] ]) BC = np.zeros((2,Ny-1)) Dybc = np.zeros((2,Ny-1)) Dybc[0,0:Ny-1] = Dy[0,1:Ny] Dybc[1,0:Ny-1] = Dy[Ny,1:Ny] BC[:,0:Ny-1] = solve(Abc,Dybc[:,0:Ny-1]) # Grid and initial data: xx,yy = np.meshgrid(x,y) vv = np.exp(-8.*((xx+1.5)**2+yy**2)) dt = 5./float(Nx+Ny**2)