{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Notebook for exercise sheet 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Important: run the code cell below once when you just opened this notebook! It will make sure that plotting is enabled." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# some setup\n", "%matplotlib inline\n", "import numpy as np # makes numpy routines and data types available as np.[name ouf routine or data type]\n", "import matplotlib.pyplot as plt # makes plotting command available as plot.[name of command]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## H 3 (Newton interpolation)\n", "\n", "a) Write a python subroutine which computes the coefficients $c_1,\\dots,c_n$ in Sheet 1, H2. Do this by implementing a suitable divided differences scheme. Make sure that your implemented subroutine behaves according to the documentation given below." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def divdiff(x,f):\n", " \"\"\"\n", " Computes the coefficient of the interpolation polynomial in Newton.\n", " The coefficients are return as numpy.array.\n", " \n", " Keyword arguments:\n", " x -- list or numpy.array of sampling points\n", " f -- list or numpy.array of function values\n", " \"\"\"\n", " # your code goes here\n", " \n", "divdiff([-5,-2,-1,0,1],[17,8,21,42, 35])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b) Implement a subroutine, which plots a function and the corresponding interpolation polynomial. For evaluating the interpolation polynomial you can use the subroutine evaluate_polynomial provided below. For the plot evaluate the the function and the interpolation polynomial at the points\n", "$$\n", " z_k = x_0 + (k-1) \\frac{x_n-x_0}{1000}, \\quad k = 1, \\dots, 1001,\n", "$$\n", "where $x_0$ and $x_n$ are the first and last interpolation point." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def evaluate_polynomial(x,coef,z):\n", " \"\"\"\n", " Evaluates the Newton interpolation polynomial at the points given by z.\n", " The values of the polynomial at the points z are returned as numpy.array.\n", " \n", " Keyword arguments:\n", " x -- list or numpy.array of sampling points\n", " coef -- list or numpy.array of coefficients\n", " z -- list or numpy.array of points\n", " \"\"\"\n", " n = np.size(coef)\n", " m = np.size(z)\n", " p = coef[n-1] * np.ones(m)\n", " for i in np.arange(n-1,-1,-1):\n", " p = p*(z - x[i]*np.ones(m)) + coef[i]*np.ones(m)\n", " return p\n", "\n", "def plotinterpol(x_sampl, f):\n", " # your code goes here\n", " return" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use your implementation to generate plots for the function\n", "$$\n", " f(x) = \\frac{1}{1+25x^2}\n", "$$\n", "with interpolation points\n", "\n", "* 0.0, 0.5, 1.0\n", "* 0.0, 0.3, 0.6, 0.9, 1.0\n", "* 0.0 - 1.0 with step size 0.2\n", "* 0.0 - 1.0 with step size 0.1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "f = lambda x: 1.0/(1+25*np.power(x,2))\n", "\n", "# your code goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## H4 (Plotting of kernels)\n", "\n", "Generate plots of the Gauss kernel and the inverse multiquadrics for various parameter values. Also generate a plot of Wendland's kernel" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# your code for generating plots of the Gauss kernel goes here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# your code for generating plots of the inverse multiquadrics goes here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# your code for generating plots of Wendland's kernel goes here" ] } ], "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 }