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 """ x,DM = chebdif(n+2,4) D2 = DM[1,:,:] # Python counting starts from zero... D3 = DM[2,:,:] D4 = DM[3,:,:] v = np.zeros(n+2) v[1:-1] = 1./(1.-x[1:-1]**2) S = np.diag(v) D4 = np.dot((np.dot(np.diag(1.-x**2),D4) - 8*np.dot(np.diag(x),D3) - 12*D2),S) D4 = D4[1:-1,1:-1] D2=D2[1:-1,1:-1] I = identity(n) L = csr_matrix(kron(I,D4)+kron(D4,I)+2*kron(D2,D2)) # Like this, or... #L=csr_matrix(kron(I,D4)+kron(D4,I)+2*np.dot(kron(D2,I),kron(I,D2))) # ...like this. return L
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 """ x, DM = chebdif(n + 2, 4) D2 = DM[1, :, :] # Python counting starts from zero... D3 = DM[2, :, :] D4 = DM[3, :, :] v = np.zeros(n + 2) v[1:-1] = 1. / (1. - x[1:-1]**2) S = np.diag(v) D4 = np.dot((np.dot(np.diag(1. - x**2), D4) - 8 * np.dot(np.diag(x), D3) - 12 * D2), S) D4 = D4[1:-1, 1:-1] D2 = D2[1:-1, 1:-1] I = identity(n) L = csr_matrix(kron(I, D4) + kron(D4, I) + 2 * kron(D2, D2)) # Like this, or... #L=csr_matrix(kron(I,D4)+kron(D4,I)+2*np.dot(kron(D2,I),kron(I,D2))) # ...like this. return L
# p38 - solve u_xxxx = exp(x), u(-1)=u(1)=u'(-1)=u'(1)=0 # compare with p13 # from chebdif import * import numpy as np from scipy.linalg import solve from matplotlib import pyplot as plt # Construct dscrete biharmonic operator: N = 15 x,DM = chebdif(N+1,4) # Weideman&Reddy function for differentiation matrix v = np.zeros(N+1) # Work array v[1:N] = 1./(1.-x[1:N]**2) S = np.diag(v) D2 = DM[1,:,:] # Python counting starts from zero... D3 = DM[2,:,:] D4 = DM[3,:,:] print D4.shape D4 = np.dot((np.dot(np.diag(1.-x**2),D4) - 8*np.dot(np.diag(x),D3) - 12*D2),S) D4 = D4[1:N,1:N] # Solve boundary-value problem and plot results: f=np.exp(x[1:N]) u=solve(D4,f) u1=np.zeros(N+1) u1[0]=0.0 u1[N]=0.0 u1[1:N]=u # Exact solution and max err:
from scipy.interpolate import interp2d from matplotlib import pyplot as plt from math import log10 #import pylab as p import mpl_toolkits.mplot3d.axes3d as p3 from matplotlib import cm from time import time start = time() N=18 x,DM = chebdif(N+1,4) D2 = DM[1,:,:] # Python counting starts from zero... D3 = DM[2,:,:] D4 = DM[3,:,:] y=x # tensor product grid xx,yy = np.meshgrid(x[1:N], y[1:N]) xx=xx.flatten(1) yy=yy.flatten(1) # RHS vector: pi=np.pi f=4.*np.cos(pi*xx)*np.cos(pi*yy)+np.cos(pi*xx)+np.cos(pi*yy) v = np.zeros(N+1) # Work array
# p38 - solve u_xxxx = exp(x), u(-1)=u(1)=u'(-1)=u'(1)=0 # compare with p13 # from chebdif import * import numpy as np from scipy.linalg import solve from matplotlib import pyplot as plt # Construct dscrete biharmonic operator: N = 15 x, DM = chebdif(N + 1, 4) # Weideman&Reddy function for differentiation matrix v = np.zeros(N + 1) # Work array v[1:N] = 1. / (1. - x[1:N]**2) S = np.diag(v) D2 = DM[1, :, :] # Python counting starts from zero... D3 = DM[2, :, :] D4 = DM[3, :, :] print D4.shape D4 = np.dot( (np.dot(np.diag(1. - x**2), D4) - 8 * np.dot(np.diag(x), D3) - 12 * D2), S) D4 = D4[1:N, 1:N] # Solve boundary-value problem and plot results: f = np.exp(x[1:N]) u = solve(D4, f) u1 = np.zeros(N + 1) u1[0] = 0.0 u1[N] = 0.0 u1[1:N] = u # Exact solution and max err:
from scipy.interpolate import interp2d from matplotlib import pyplot as plt from math import log10 #import pylab as p import mpl_toolkits.mplot3d.axes3d as p3 from matplotlib import cm from time import time start = time() N = 18 x, DM = chebdif(N + 1, 4) D2 = DM[1, :, :] # Python counting starts from zero... D3 = DM[2, :, :] D4 = DM[3, :, :] y = x # tensor product grid xx, yy = np.meshgrid(x[1:N], y[1:N]) xx = xx.flatten(1) yy = yy.flatten(1) # RHS vector: pi = np.pi f = 4. * np.cos(pi * xx) * np.cos(pi * yy) + np.cos(pi * xx) + np.cos(pi * yy) v = np.zeros(N + 1) # Work array v[1:N] = 1. / (1. - x[1:N]**2)