{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Runge phenomenon"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider interpolating the following two functions on $[-1,1]$\n",
"$$\n",
"f_1(x) = \\exp(-5x^2), \\qquad f_2(x) = \\frac{1}{1 + 16 x^2}\n",
"$$\n",
"We will try uniformly spaced points and Chebyshev points."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'svg'\n",
"from numpy import exp,linspace,polyfit,polyval,cos,pi\n",
"from matplotlib.pyplot import figure,plot,legend,axis,text,subplot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us first plot the two functions."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"xmin, xmax = -1.0, +1.0\n",
"\n",
"f1 = lambda x: exp(-5.0*x**2)\n",
"f2 = lambda x: 1.0/(1.0+16.0*x**2)\n",
"\n",
"xx = linspace(xmin,xmax,100,True)\n",
"figure(figsize=(8,4))\n",
"plot(xx,f1(xx),xx,f2(xx))\n",
"legend((\"$1/(1+16x^2)$\", \"$\\exp(-5x^2)$\"));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The two functions look qualitatively similar and both are infinitely differentiable."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def interp(f,points):\n",
" xx = linspace(xmin,xmax,100,True);\n",
" ye = f(xx);\n",
"\n",
" figure(figsize=(9,8))\n",
" for i in range(1,7):\n",
" N = 2*i\n",
" subplot(3,2,i)\n",
" if points == 'uniform':\n",
" x = linspace(xmin,xmax,N+1,True)\n",
" else:\n",
" theta = linspace(0,pi,N+1, True)\n",
" x = cos(theta)\n",
" y = f(x);\n",
" P = polyfit(x,y,N);\n",
" yy = polyval(P,xx);\n",
" plot(x,y,'o',xx,ye,'--',xx,yy)\n",
" axis([xmin, xmax, -1.0, +1.1])\n",
" text(-0.1,0.0,'N = '+str(N))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Interpolate $f_1(x)$ on uniformly spaced points."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interp(f1,'uniform')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Interpolate $f_2(x)$ on uniformly spaced points."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interp(f2,'uniform')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above results are not good. Let us try $f_2(x)$ on Chebyshev points."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interp(f2,'chebyshev')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What about interpolating $f_1(x)$ on Chebyshev points ?"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interp(f1,'chebyshev')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This also seems fine. So uniform points sometimes works, Chebyshev points work all the time."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 1
}