{
"metadata": {
"name": "",
"signature": "sha256:df03c314719573ef9e0624980dc8a272e9e3c363c66f0d9bba3901f3ea5f9c8c"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Interpolate $\\cos(x)$ on uniform points"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us interpolate $\\cos(x)$ on uniformly spaced points in the interval $[0,2\\pi]$."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from numpy import pi,cos,linspace,polyfit,polyval\n",
"from matplotlib.pyplot import figure,subplot,plot,axis,text"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"xmin, xmax = 0.0, 2.0*pi\n",
"fun = lambda x: cos(x)\n",
"\n",
"xx = linspace(xmin,xmax,100);\n",
"ye = fun(xx);\n",
"\n",
"figure(figsize(10,8))\n",
"for i in range(1,7):\n",
" N = 2*i;\n",
" subplot(3,2,i)\n",
" x = linspace(xmin,xmax,N+1);\n",
" y = fun(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.1, +1.1])\n",
" text(3.0,0.5,'N='+str(N))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 2
}
],
"metadata": {}
}
]
}