{ "cells": [ { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sympy import init_printing;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 1: Analytical Integration" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider the functions\n", "\n", "$f(x) = -4 \\; x \\; (x - 1) \\label{ffunc}$\n", "\n", "and\n", "\n", "$g(x) = \\frac{8}{9} \\cdot (-16\\;x^4 + 40\\;x^3 -35\\;x^2 + 11\\;x)$\n", "\n", "Compute the antiderivatives and evaluate the integrals.\n" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from __future__ import division\n", "from sympy import *\n", "x, y = symbols('x y')\n", "l, i = symbols('l, i', integer=True)\n", "g, h = symbols('f g', cls=Function)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 2: Composite Trapezoidal Rule" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function that approximates the integral via the Composite Trapezoidal Rule.\n", "Complete the function template, with $a$, $b$ and $f$ according to the definition above and $n$ being the number of trapezoids used." ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def trapezoidalRule(f, a, b, n):\n", " '''\n", " Implements CTR to approximate the integral of a given function.\n", " '''\n", " return 0.0\n", " " ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trapezoidalRule(lambda t: f.subs(x, t), 0.0, 1.0, 1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 3: Composite Simpson Rule" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do the same as in Exercise 2 for the Composite Simpson Rule." ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def simpsonRule(f, a, b, n):\n", " '''\n", " Implements SR to approximate the integral of given function.\n", " '''\n", " return 0.0" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simpsonRule(lambda t: g.subs(x, t), 0.0, 1.0, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 4: Archimedes' Hierarchical Approach" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this exercise we will use Archimedes' approach to approximate the integral.\n", "\n", "Let $\\vec{u} = [u_0,\\dots,u_{n-1}]^T \\in {\\mathbb R}^n, n = 2^l - 1, l \\in {\\mathbb N}$ a vector of function values with\n", "$u_i = f(x_i = \\frac{i + 1}{2^l})$.\n", "\n", "a)\n", "Write a function that transforms a given vector $\\vec{u} \\in {\\mathbb R}^n$ to a similar vector \n", "$\\vec{v} \\in {\\mathbb R}^n$ containing the hierarchical coefficients needed for Archimedes' quadrature approach.\n", "Use the function template below.\n", "\n", "b)\n", "Having computed the vector $\\vec{v}$ with the hierarchical coefficients, implement a function evaluating\n", "the integral.\n", "\n", "c)\n", "Write a function dehierarchize1d similar to hierarchize1d that computes the inverse of the\n", "transformation above.\n", "\n", "NOTE:\n", "We always assume a ZERO boundary case. And the domain is always $[0,1]$." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Task a)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def hierarchize1d(u, maxl):\n", " ''' \n", " Basic algorithm:\n", " -----------------\n", " for l = maxlevel..1 :\n", " delta = 2**(maxl - l)\n", " for j = index of every point of level l:\n", " v[j] = u[j] - 0.5*(u[j-delta] + u[j+delta])\n", " '''\n", " \n", " v = list(u) # OUTPUT vector\n", " \n", " # TODO\n", " \n", " return v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task b)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def computeIntegral(v, maxl): \n", " integ = 0.0\n", "\n", " N = len(v)\n", " \n", " # TODO\n", " \n", " return integ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task c)" ] }, { "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.2" } }, "nbformat": 4, "nbformat_minor": 0 }