{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# p15: Solve eigenvalue BVP \n",
"\n",
"$$\n",
"u_{xx} = \\lambda u, \\qquad u(-1)=u(1)=0\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_format='svg'\n",
"from pylab import *\n",
"from chebPy import cheb\n",
"from scipy.linalg import solve,eig\n",
"from scipy.interpolate import barycentric_interpolate"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"N = 36\n",
"D,x = cheb(N)\n",
"D2 = dot(D,D)\n",
"D2 = D2[1:N,1:N]\n",
"\n",
"lam,V = eig(D2) \n",
"ii = argsort(-lam)\n",
"lam = real(lam[ii])\n",
"V = V[:,ii]\n",
"\n",
"fig = figure(figsize=(10,15))\n",
"for j in range(5,35,5): \n",
" lv = shape(V)[0]+2\n",
" u = zeros(lv)\n",
" u[1:lv-1] = V[:,int(j)] \n",
" subplot(6,1,j//5)\n",
" plot(x,u,'bo')\n",
" xx = linspace(-1.0,1.0,501)\n",
" uu = barycentric_interpolate(x,u,xx)\n",
" s = 'eig %d = %20.13f * $\\\\pi^2/4$' %(j,lam[j-1]*4/pi**2)\n",
" s = s + '\\t\\t %4.1f ppw' % (4*N/(pi*j))\n",
" title(s)\n",
" plot(xx,uu,'b')\n",
" axis('off')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"lexact = - pi**2/4 * arange(1,N)**2\n",
"error = abs((lam - lexact)/lexact)\n",
"semilogy(error,'o')\n",
"grid(True), xlabel('n'), ylabel('Error in $\\\\lambda_n$');"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}