{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1 - integration of infinite intervals\n", "\n", "Calculate the value of the following integral using Gaussian quadrature: $$I=\\int_0^\\infty e^{-t^2}dt$$ The value of this integral is known analytically: $I=\\frac{1}{2}\\sqrt{\\pi}=0.886226925453\\ldots$. \n", "\n", "To solve the integral numerically, make the change of variables $z=t/(1+t)$. The integral becomes $$I=\\int_0^1 \\frac{e^{-z^2/(1-z)^2}}{(1-z)^2}dz .$$ \n", "\n", "1. Plot the functions $ f(x)=e^{-t^2} $ and $i(z)=\\frac{e^{-z^2/(1-z)^2}}{(1-z)^2}$.\n", "2. Change your Gauss-Legendre integration program from last week to solve this integral. \n", "3. Plot the value of the integral and/or the integration error as a function of integration points.\n", "\n", "\n", "## Talking points\n", "\n", "1. What do you observe?\n", "2. How many points do you need for 0.1% accuracy and how many for $10^{-6}$% accuracy?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2 - multi dimensional integrals\n", "\n", "Calculate the double integral $\\DeclareMathOperator\\erf{erf} \\int_{-1}^1\\int_{-1}^1 e^{(-x^2-y^2)} dx dy = \\pi \\erf(1)^2$ numerically. \n", "\n", "1. Plot the functions $ e^{(-x^2-y^2)}$.\n", "2. Adapt your Gauss-Legendre integration program to solve this integral. \n", "3. Plot the value of the integral and/or the integration error as a function of integration points.\n", "\n", "## Talking points\n", "\n", "1. What do you observe?\n", "2. How many points do you need for 0.1% accuracy and how many for $10^{-6}$% accuracy?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3 - numeric differentiation\n", "\n", "Calculate the derivative of $f(x)=x^3-2x^2+x-1$ numerically with the forward $\\left( \\frac{df}{dx} \\approx \\frac{f(x+h)-f(x)}{h}\\right)$ or the backward difference $\\left( \\frac{df}{dx} \\approx \\frac{f(x)-f(x-h)}{h} \\right)$ method. \n", "\n", "1. Use the forward and backward difference rule to calculate $f'(x)$ for $h=0.1$ and $h=0.01$.\n", "2. Plot the analytic derivative and your numeric derivatives in the same graph. \n", "3. Plot the difference between the analytic and the numeric derivatives.\n", "\n", "## Talking points\n", "\n", "1. What do you observe?\n", "2. How does the error in the numeric derivatives behave with $x$ and $h$?\n", "3. How can we do better?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4 - central difference method\n", "\n", "Calculate the derivative of $f(x)=x^3-2x^2+x-1$ numerically with the central difference $\\left( \\frac{df}{dx}\\approx\\frac{f((x+h/2)-f(x-h/2)}{h}\\right) $ method. \n", "\n", "1. Use the central difference rule to calculate $f'(x)$ for $h=0.1$ and $h=0.01$.\n", "2. Plot the analytic and your numeric derivative in the same graph. \n", "3. Plot the difference between the analytic and the numeric derivative.\n", "\n", "## Talking points\n", "\n", "1. What do you observe?\n", "2. How does the error in the numeric derivatives now behave with $x$ and $h$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.7.8" } }, "nbformat": 4, "nbformat_minor": 4 }