{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# p09: Polynomial interpolation in equispaced and chebyshev points"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_format='svg'\n",
"from numpy import pi,inf,linspace,arange,cos\n",
"from numpy.linalg import norm\n",
"from scipy.interpolate import barycentric_interpolate\n",
"from matplotlib.pyplot import figure,subplot,plot,axis,title,text"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"N = 16\n",
"xx = linspace(-1.01,1.01,400,True)\n",
"figure(figsize=(10,5))\n",
"for i in range(2):\n",
" if i==0:\n",
" s = 'equispaced points'; x = -1.0 + 2.0*arange(0,N+1)/N\n",
" if i==1:\n",
" s = 'Chebyshev points'; x = -cos(pi*arange(0,N+1)/N)\n",
" subplot(1,2,i+1)\n",
" u = 1.0/(1.0 + 16.0*x**2)\n",
" uu = 1.0/(1.0 + 16.0*xx**2)\n",
" pp= barycentric_interpolate(x, u, xx)\n",
" plot(x,u,'o',xx,pp)\n",
" axis([-1.1, 1.1, -1.0, 1.5])\n",
" title(s+\", N=\"+str(N))\n",
" error = norm(uu-pp, inf)\n",
" text(-0.6,-0.5,'max error='+str(error));"
]
}
],
"metadata": {
"anaconda-cloud": {},
"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
}