{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Weekly exercise 12: Choosing interpolation method\n", "\n", "In this exercise you will check how different interpolation methods\n", "work with different functions, after writing some diagnostic functions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Choosing the right interpolation method\n", "\n", "Review the code in the lecture notebook to refresh your memory about\n", "\n", "1. Linear interpolation \n", "1. Quadratic and cubic splines \n", "1. Polynomial interpolation \n", "\n", "\n", "Fix the interpolation window $ x \\in [0,25] $, the interpolation nodes\n", "as in the code below, and only change the interpolated function and\n", "interpolation scheme." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "from scipy import interpolate # Interpolation routines\n", "\n", "# fixed parts of the problem\n", "a,b = 0,25 # interval\n", "xd = np.linspace(a,b,1000) # dense grid for plotting\n", "np.random.seed(21234) # fix random number sequences\n", "nodes = np.sort(np.random.uniform(a,b,15)) # sorted random points" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 1. Measuring the accuracy\n", "\n", "Write a function to plot the true function and the interpolation function,\n", "and let it also return the measure of accuracy equal to the average square deviation\n", "between the two.\n", "\n", "More precisely, let two measures of accuracy be calculated: one calculated off\n", "all points within the initial interval, and the second only between the min and max\n", "nodes (so, the second one excludes the extrapolated points)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "# write your code here\n", "# come up with a test of your own" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 2. Interpolating smooth function\n", "\n", "Compare the accuracy of all three schemes above to interpolate\n", "\n", "$$\n", "f(x) = \\exp(-x/10)\\sin(x/2)\n", "$$\n", "\n", "Hint: use *fill_value=”extrapolate”* option in *interp1d()* to allow for extrapolation\n", "\n", "Which interpolation scheme is most accurate?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "# write your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 3. Interpolating complex periodic function\n", "\n", "Compare the accuracy of all three schemes above to interpolate\n", "\n", "$$\n", "f(x) = x + \\exp(x/10)\\sin(x)\n", "$$\n", "\n", "Which interpolation scheme is most accurate?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "# write your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 4. Interpolating function with kinks\n", "\n", "Compare the accuracy of all three schemes above to interpolate\n", "\n", "$$\n", "f(x) = \\max \\big( x + \\exp(x/10)\\sin(3x/4); x + 5 + \\exp([x+5]/10)\\sin(3[x+5]/4) \\big)\n", "$$\n", "\n", "Which interpolation scheme is most accurate?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "# write your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 5. Interpolating discontinuous function\n", "\n", "Compare the accuracy of all three schemes above to interpolate\n", "\n", "$$\n", "f(x) = \\exp(-x/10)\\sin(x/2) + \\mathbb{1}\\{\\cos(x)>1/2\\}\n", "$$\n", "\n", "Which interpolation scheme is most accurate?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "# write your code here" ] } ], "metadata": { "date": 1627475014.595328, "filename": "exercise12.rst", "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.7.6" }, "title": "Weekly exercise 12: Choosing interpolation method" }, "nbformat": 4, "nbformat_minor": 4 }