{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Numerical Integration\n", "\n", "## Overview, Objectives, and Key Terms\n", " \n", "In [Numerical Differentiation](L19_NumericalDifferentiation.ipynb), finite-difference approximations were developed and used to evaluate derivatives numerically. In this lecture, we'll explore and implement our own numerical methods for evaluation of integrals. We'll also continue using SciPy, this time through its numerical integration tools.\n", " \n", "### Objectives\n", "\n", "By the end of this lesson, you should be able to\n", "\n", "- Evaluate definite integrals numerically using left- and right-sided Riemann sums, the mid-point rule, and the trapezoid rule.\n", "- Evaluate integrals of sampled data using `scipy.integrate.trapezoid`, `scipy.integrate.simpson`, and `scipy.integrate.cumulative_trapezoid`.\n", "- Interpret how sampling choices create overestimates and missed area.\n", "- Evaluate integrals of callable functions using `scipy.integrate.quad` and related functions.\n", "- Establish the order of an integration scheme using numerical, graphical, or symbolic means.\n", "- Recognize how numerical integration can be used to build probability and balance checks in deterministic response models.\n", "\n", "### Key Terms\n", "\n", "- Riemann sum\n", "- left-sided Riemann sum\n", "- right-sided Riemann sum\n", "- mid-point rule\n", "- trapezoid rule\n", "- Simpson's rule\n", "- sampled-data integration\n", "- adaptive quadrature\n", "- `scipy.integrate.trapezoid`\n", "- `scipy.integrate.simpson`\n", "- `scipy.integrate.cumulative_trapezoid`\n", "- `scipy.integrate.quad`\n", "- `scipy.integrate.dblquad`\n", "- $n$th-order approximation\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Riemann Sums in Practice\n", "\n", "For derivatives, we took a limit of a finite difference for an exact answer but stopped that limit for a sufficiently small $\\Delta$ to ensure a reasonable estimate. For the first derivative, we saw the first-order forward and backward schemes along with the second-order central difference scheme. \n", "\n", "For integrals, we have analogs to these choices. You may remember the following formal definition for a definite integral:\n", "\n", "$$\n", " \\int^b_a f(x) dx = \n", " \\lim_{n \\to \\infty} \\sum^{n}_{i=1} f(i (b-a)/n + a) (b-a)/n\n", "$$\n", "\n", "Here, the sum is a *Riemann sum* (specifically a right-sided sum). Integrals are areas under curves, and Riemann sums approximate those areas by rectangles. Just as for derivatives, we *don't take the limit* but let $n$ be a finite number. Moreover, we have different numerical options depending on how we define the rectangles of the Riemann sum. \n", "\n", "Let's look at this concretely for $f(x) = x^2$ over $x\\in[0, 1]$ with $n=5$:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "For this increasing function, the right-sided estimate has positive signed error: the estimate lies above the curve on each interval." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the height of a rectangle is equal to the function evaluated at the rectangle's right $x$ coordinate. We call such a sum a *right-sided* Riemann sum. For this particular function, this right-sided sum leads to some significant over estimation. \n", "\n", "If instead we defined our sum to be\n", "\n", "$$\n", " S =\\frac{b-a}{n} \\sum^{n}_{i=1} f \\Bigg ( \\frac{ (i-1)(b-a) }{n} + a \\Bigg ) \n", "$$\n", "\n", "the picture becomes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "The left-sided estimate has negative signed error here: the estimate misses area under the curve on each interval." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, the rectangle height is based on the function evaluated at its \n", "left side, making this a *left-sided* Riemann sum. Just as the right-sided\n", "sum *overestimates* the integral, the left-sided sum *underestimates* \n", "the integral." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "