{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Interpolation Error\n", "\n", "Copyright (C) 2020 Andreas Kloeckner\n", "\n", "
\n", "MIT License\n", "Permission is hereby granted, free of charge, to any person obtaining a copy\n", "of this software and associated documentation files (the \"Software\"), to deal\n", "in the Software without restriction, including without limitation the rights\n", "to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n", "copies of the Software, and to permit persons to whom the Software is\n", "furnished to do so, subject to the following conditions:\n", "\n", "The above copyright notice and this permission notice shall be included in\n", "all copies or substantial portions of the Software.\n", "\n", "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n", "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n", "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n", "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n", "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n", "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n", "THE SOFTWARE.\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "import numpy as np\n", "import numpy.linalg as la\n", "import matplotlib.pyplot as pt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's fix a function to interpolate:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "if 1:\n", " def f(x):\n", " return np.exp(1.5*x)\n", "elif 0:\n", " def f(x):\n", " return np.sin(20*x)\n", "else:\n", " def f(x):\n", " return (x>=0.5).astype(np.int).astype(np.float)\n", " " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "x_01 = np.linspace(0, 1, 1000)\n", "pt.plot(x_01, f(x_01))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And let's fix some parameters. Note that the interpolation interval is just $[0,h]$, not $[0,1]$!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "degree = 1\n", "h = 1\n", "\n", "nodes = 0.5 + np.linspace(-h/2, h/2, degree+1)\n", "nodes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now build the Vandermonde matrix:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "V = np.array([\n", " nodes**i\n", " for i in range(degree+1)\n", "]).T" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "V" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now find the interpolation coefficients as `coeffs`:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are some points. Evaluate the interpolant there:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "x_0h = 0.5+np.linspace(-h/2, h/2, 1000)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now plot the interpolant with the function:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "pt.plot(x_01, f(x_01), \"--\", color=\"gray\", label=\"$f$\")\n", "pt.plot(x_0h, interp_0h, color=\"red\", label=\"Interpolant\")\n", "pt.plot(nodes, f(nodes), \"or\")\n", "pt.legend(loc=\"best\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Also plot the error:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "error = interp_0h - f(x_0h)\n", "pt.plot(x_0h, error)\n", "print(\"Max error: %g\" % np.max(np.abs(error)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* What does the error look like? (Approximately)\n", "* How will the error react if we shrink the interval?\n", "* What will happen if we increase the polynomial degree?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true } }, "outputs": [], "source": [] } ], "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }