{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# p23a: Eigenvalues of perturbed Laplacian in 2-D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We solve the following eigenvalue problem\n",
"\n",
"$$\n",
"-(u_{xx} + u_{yy}) + f(x,y) u = \\lambda u, \\qquad -1 < x,y < 1, \\qquad u=0 \\quad \\mbox{on boundary}\n",
"$$\n",
"\n",
"where\n",
"\n",
"$$\n",
"f(x,y) = \\exp(20(y-x-1))\n",
"$$\n",
"\n",
"The code is almost same as p23, with only one extra line."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_format='svg'\n",
"from chebPy import cheb\n",
"from numpy import meshgrid,dot,eye,kron,zeros,reshape,pi,real,imag\n",
"from numpy import diag,exp,argsort,linspace,inf\n",
"from matplotlib.pyplot import figure,subplot,plot,title,contour\n",
"from scipy.linalg import eig,norm\n",
"from scipy.interpolate import RegularGridInterpolator"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Set up tensor product Laplacian and compute 4 eigenmodes\n",
"N = 16; D,x = cheb(N); y = x;\n",
"xx,yy = meshgrid(x[1:N],y[1:N],indexing='ij')\n",
"xx = reshape(xx,(N-1)**2, order='F')\n",
"yy = reshape(yy,(N-1)**2, order='F')\n",
"D2 = dot(D,D); D2 = D2[1:N,1:N]; I = eye(N-1)\n",
"L = -kron(I,D2) - kron(D2,I)\n",
"L = L + diag(exp(20*(yy-xx-1))) # Only this line extra compared to p23\n",
"D,V = eig(L); D = real(D); V = real(V)\n",
"ii = argsort(D); ii = ii[0:4]; D = D[ii]; V = V[:,ii]\n",
"\n",
"# Reshape them to 2D grid, interpolate to finer grid, and plot\n",
"fine = linspace(-1.0,1.0,100,True);\n",
"X,Y = meshgrid(fine,fine,indexing='ij')\n",
"uu = zeros((N+1,N+1));\n",
"\n",
"figure(figsize=(10,10))\n",
"for i in range(4):\n",
" uu[1:N,1:N] = reshape(V[:,i],(N-1,N-1),order='F')\n",
" uu = uu/norm(uu,inf)\n",
" f = RegularGridInterpolator((x,y),uu,method='cubic')\n",
" uuu = f((X,Y))\n",
" subplot(2,2,i+1)\n",
" contour(X,Y,uuu,10)\n",
" title(\"eig = %18.12f $\\pi^2/4$\"%(D[i]/(pi**2/4)))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}