{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Gibbs oscillations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us try to approximate a discontinuous function using Chebyshev interpolation\n",
"$$\n",
"f(x) = \\begin{cases}\n",
"1 & x < 0 \\\\\n",
"0 & x > 0\n",
"\\end{cases}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'svg'\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"xmin, xmax = -1.0, +1.0\n",
"fun = lambda x: (x < 0)*1.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will interpolate this function at Chebyshev points."
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Plot function on a finer grid\n",
"xx = np.linspace(xmin,xmax,100);\n",
"ye = fun(xx);\n",
"plt.figure(figsize=(10,8))\n",
"plt.plot(xx,ye,'--')\n",
" \n",
"for N in range(10,15):\n",
" theta = np.linspace(0,pi,N+1)\n",
" x = np.cos(theta)\n",
" y = fun(x);\n",
" P = np.polyfit(x,y,N);\n",
" yy = np.polyval(P,xx);\n",
" plt.plot(xx,yy)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The interpolants are oscillatory and this phenomenon is known as Gibbs oscillations."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 1
}