{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Choice of Nodes for Polynomial Interpolation" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib qt4" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.5/dist-packages/IPython/external/qt_loaders.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='/usr/lib/python3/dist-packages/PyQt4/QtCore.cpython-35m-x86_64-linux-gnu.so'>\n", " imp.find_module('QtCore', mod.__path__)\n", "/usr/local/lib/python3.5/dist-packages/IPython/external/qt_loaders.py:119: ResourceWarning: unclosed file <_io.BufferedReader name='/usr/lib/python3/dist-packages/PyQt4/QtGui.cpython-35m-x86_64-linux-gnu.so'>\n", " imp.find_module('QtGui', mod.__path__)\n", "/usr/local/lib/python3.5/dist-packages/IPython/external/qt_loaders.py:120: ResourceWarning: unclosed file <_io.BufferedReader name='/usr/lib/python3/dist-packages/PyQt4/QtSvg.cpython-35m-x86_64-linux-gnu.so'>\n", " imp.find_module('QtSvg', mod.__path__)\n" ] } ], "source": [ "import numpy as np\n", "import numpy.linalg as la\n", "\n", "from matplotlib.pyplot import (\n", " clf, plot, show, xlim, ylim,\n", " get_current_fig_manager, gca, draw, connect)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Choose a function below:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "func = \"sin\"\n", "\n", "if func == \"sin\":\n", " def f(x):\n", " return np.sin(5*x)\n", "elif func == \"jump\":\n", " def f(x):\n", " result = 0*x\n", " result.fill(-1)\n", " result[x > 0] = 1\n", " return result\n", "elif func == \"runge\":\n", " def f(x):\n", " return 1/(1+25*x**2)\n", "else:\n", " raise RuntimeError(\"unknown function '%s'\" % func)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run this cell to play with the node placement toy:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/lib/python3/dist-packages/numpy/lib/polynomial.py:594: RankWarning: Polyfit may be poorly conditioned\n", " warnings.warn(msg, RankWarning)\n", "/usr/lib/python3/dist-packages/numpy/lib/polynomial.py:594: RankWarning: Polyfit may be poorly conditioned\n", " warnings.warn(msg, RankWarning)\n", "/usr/lib/python3/dist-packages/numpy/lib/polynomial.py:594: RankWarning: Polyfit may be poorly conditioned\n", " warnings.warn(msg, RankWarning)\n", "/usr/lib/python3/dist-packages/numpy/lib/polynomial.py:594: RankWarning: Polyfit may be poorly conditioned\n", " warnings.warn(msg, RankWarning)\n" ] } ], "source": [ "#keep\n", "x_points = []\n", "y_points = []\n", "deg = [1]\n", "\n", "def update_plot():\n", " clf()\n", " xlim([-1, 1])\n", " ylim([-1.5, 1.5])\n", " gca().set_autoscale_on(False)\n", " plot(x_points, y_points, 'o')\n", "\n", " x = np.linspace(-1, 1, 500)\n", " plot(x, f(x), \"--\")\n", "\n", " if len(x_points) >= deg[0]+1:\n", " eval_points = np.linspace(-1, 1, 500)\n", " poly = np.poly1d(np.polyfit(\n", " np.array(x_points),\n", " np.array(y_points), deg[0]))\n", " plot(eval_points, poly(eval_points), \"-\")\n", "\n", "\n", "def click(event):\n", " \"\"\"If the left mouse button is pressed: draw a little square. \"\"\"\n", " tb = get_current_fig_manager().toolbar\n", " if event.button == 1 and event.inaxes and tb.mode == '':\n", " x_points.append(event.xdata)\n", " x_ary = np.array([event.xdata])\n", " y_ary = f(x_ary)\n", " y_points.append(y_ary[0])\n", "\n", " if event.button == 2 and event.inaxes and tb.mode == '':\n", " if len(x_points) >= deg[0]+2:\n", " deg[0] += 1\n", "\n", " update_plot()\n", " draw()\n", "\n", "update_plot()\n", "connect('button_press_event', click)\n", "show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": 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.5.1+" } }, "nbformat": 4, "nbformat_minor": 0 }