{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ae7e2851",
   "metadata": {},
   "source": [
    "# Integration methods\n",
    "- Börge Göbel"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efe2a841",
   "metadata": {},
   "source": [
    "![Integral](figure_05_integral.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a527df57",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "988683ad",
   "metadata": {},
   "source": [
    "### Define and plot function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4855ad1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "68683ccc",
   "metadata": {},
   "source": [
    "### Analytical solution"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7884b29",
   "metadata": {},
   "source": [
    "\\\\( f(x) = \\frac{1}{2} + \\frac{1}{10}x + \\frac{1}{5}x^2 + \\frac{3}{100}x^3 \\\\)\n",
    "\n",
    "\\\\( A = \\int_{-3}^3 f(x)\\,\\mathrm{d}x = \\int_{-3}^3\\left(\\frac{1}{2} + \\frac{1}{10}x + \\frac{1}{5}x^2 + \\frac{3}{100}x^3\\right)\\,\\mathrm{d}x = \\left[\\frac{1}{2}x + \\frac{1}{20}x^2 + \\frac{1}{15}x^3 + \\frac{3}{400}x^4\\right]_{-3}^3\\\\)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cf69ae54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.6"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "( 1/2*(3) + 1/20*(3)**2 + 1/15*(3)**3 + 3/400*(3)**4 ) - ( 1/2*(-3) + 1/20*(-3)**2 + 1/15*(-3)**3 + 3/400*(-3)**4 )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ec28ee8",
   "metadata": {},
   "source": [
    "### Create data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd0607bb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c577a8d8",
   "metadata": {},
   "source": [
    "## 1. Weighted sum"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a514cd55",
   "metadata": {},
   "source": [
    "\\\\( A = \\int_{a}^b f(x)\\,\\mathrm{d}x \\approx \\frac{b-a}{n-1}\\sum_{i=1}^n f(x_i)\\\\)\n",
    "\n",
    "Edges are problematic, as they are overrepresented. Furthermore, this only really works if the data is equidistant."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f516346d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "7b9ca0b4",
   "metadata": {},
   "source": [
    "## 2. Trapezoidal method "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d30bc808",
   "metadata": {},
   "source": [
    "Corresponds to integrating a linear spline through the data points. It is now possible to properly deal with non-equidistant data.\n",
    "\n",
    "\\\\( A = \\int_{a}^b f(x)\\,\\mathrm{d}x \\approx \\sum_{i=1}^{n-1} \\frac{f(x_{i+1})+f(x_{i})}{2}(x_{i+1}-x_i)\\\\)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77f26c8a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "01ba6e7d",
   "metadata": {},
   "source": [
    "For equidistant data this simplfies to \n",
    "\n",
    "\\\\( A \\approx \\frac{b-a}{n-1}\\left[\\frac{1}{2}f(x_1) + \\sum_{i=2}^{n-1} f(x_i) + \\frac{1}{2}f(x_n)\\right] \\\\)\n",
    "\n",
    "Therefore the edge issue is resolved."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d22b56ec",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "02671520",
   "metadata": {},
   "source": [
    "## 3. Simpson rule & Newton-Cortes equations"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92cb777a",
   "metadata": {},
   "source": [
    "This method corresponds to integrating a polynomial interpolation function through the data points. The coefficients have been optimized accordingly."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c32f84f7",
   "metadata": {},
   "source": [
    "### Simpson rule"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "593b3861",
   "metadata": {},
   "source": [
    "Careful! This method works only if there are an odd number of data points.\n",
    "\n",
    "\\\\( A = \\int_{a}^b f(x)\\,\\mathrm{d}x \\approx \\frac{b-a}{n-1} \\left[ \\frac{1}{3}f(x_1) + \\frac{4}{3}f(x_2) + \\frac{2}{3}f(x_3) + \\frac{4}{3}f(x_4) + \\dots + \\frac{4}{3}f(x_{n-3}) + \\frac{2}{3}f(x_{n-2}) + \\frac{4}{3}f(x_{n-1}) + \\frac{1}{3}f(x_n) \\right]\\\\)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce363749",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a48aebdc",
   "metadata": {},
   "source": [
    "We get the perfect result, because our data has been generated using a 3rd-order polynomial. This is also what the Simpson rule considers for the interpolation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44ecdd9f",
   "metadata": {},
   "source": [
    "### Higher-order Newton-Cortes equations "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d64d27b",
   "metadata": {},
   "source": [
    "There exist many more optimized methods, e.g. Boole's rule:\n",
    "\n",
    "\\\\( A = \\int_{a}^b f(x)\\,\\mathrm{d}x \\\\\\approx \\frac{b-a}{n-1} \\left[ \\frac{14}{45}f(x_1) + \\frac{64}{45}f(x_2) + \\frac{24}{45}f(x_3) + \\frac{64}{45}f(x_4) + \\frac{28}{45}f(x_5) + \\frac{64}{45}f(x_6) + \\frac{24}{45}f(x_7) \\dots + \\frac{64}{45}f(x_{n-3}) + \\frac{24}{45}f(x_{n-2}) + \\frac{64}{45}f(x_{n-1}) + \\frac{14}{45}f(x_n) \\right]\\\\)\n",
    "\n",
    "For this rule, the number of data points has to be a multiple of 5.\n",
    "\n",
    "More information: https://en.wikipedia.org/wiki/Newton%E2%80%93Cotes_formulas"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
