{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Interpolate abs(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider interpolating $|x|$ on $[-1,1]$. We will try uniformly spaced points and Chebyshev points."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'svg'\n",
"from numpy import linspace,polyfit,polyval,cos,pi\n",
"from matplotlib.pyplot import figure,plot,subplot,legend,axis,text"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def interp(points):\n",
" xmin, xmax = -1.0, +1.0\n",
" xx = linspace(xmin,xmax,100);\n",
" ye = abs(xx);\n",
"\n",
" figure(figsize=(10,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)\n",
" else:\n",
" theta = linspace(0,pi,N+1)\n",
" x = cos(theta)\n",
" y = abs(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, -0.1, +1.1])\n",
" text(-0.1,0.5,'N = '+str(N))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interp('uniform')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interp('chebyshev')"
]
}
],
"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
}