{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# p12: Accuracy of Chebyshev spectral differentiation"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_format='svg'\n",
"from numpy import zeros,pi,inf,linspace,arange,abs,dot,exp\n",
"from scipy.linalg import toeplitz,norm\n",
"from matplotlib.pyplot import figure,subplot,semilogy,loglog,title,xlabel,ylabel,axis,grid\n",
"from chebPy import *"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"Nmax = 50\n",
"E = zeros((4,Nmax))\n",
"for N in range(1,Nmax+1):\n",
" D,x = cheb(N)\n",
" \n",
" v = abs(x)**3 # 3rd deriv in BV\n",
" vprime = 3.0*x*abs(x)\n",
" E[0][N-1] = norm(dot(D,v)-vprime,inf)\n",
" \n",
" v = exp(-(x+1.0e-15)**(-2)) # C-infinity\n",
" vprime = 2.0*v/(x+1.0e-15)**3\n",
" E[1][N-1] = norm(dot(D,v)-vprime,inf)\n",
" \n",
" v = 1.0/(1.0+x**2) # analytic in a [-1,1]\n",
" vprime = -2.0*x*v**2\n",
" E[2][N-1] = norm(dot(D,v)-vprime,inf)\n",
" \n",
" v = x**10\n",
" vprime = 10.0*x**9 # polynomial\n",
" E[3][N-1] = norm(dot(D,v)-vprime,inf)\n",
"\n",
"\n",
"titles = [\"$|x|^3$\", \"$\\\\exp(-x^{-2})$\", \\\n",
" \"$1/(1+x^2)$\", \"$x^{10}$\"]\n",
"figure(figsize=(10,10))\n",
"for iplot in range(4):\n",
" subplot(4,2,2*iplot+1)\n",
" semilogy(arange(1,Nmax+1,),E[iplot][:],'.-')\n",
" title(titles[iplot]+\", semilogy\")\n",
" xlabel('N'), ylabel('error'), grid(True)\n",
" subplot(4,2,2*iplot+2)\n",
" loglog(arange(1,Nmax+1,),E[iplot][:],'.-')\n",
" title(titles[iplot]+\", loglog\")\n",
" xlabel('N'), ylabel('error'), grid(True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* $|x|^3$: loglog plot is straight line indicating algebraic decay of the form $N^{-p}$.\n",
"* $\\exp(-x^{-2})$: loglog plot goes down faster than a straight line, indicating faster than algebraic decay.\n",
"* $1/(1+x^2)$: semilogy plot is straight line, indicating exponential decay of the form $\\exp(-\\alpha N)$.\n",
"* $x^{10}$: for $N \\ge 10$ we get exact answer."
]
}
],
"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
}