{
"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": [
"