{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial 02 - Error, accuracy, stability" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Floating point representation of numbers, roundoff error, truncation error, numerical stability and condition number." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Main sources of numerical errors: \n", "- rounding and cancellation (due to the use of finite-precision arithmetic)\n", "- truncation or approximation errors (due to approximation of infinite sequences or continuous functions by a finite number of samples)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic definitions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Absolute and relative error:**\n", "\n", "If $ \\tilde{x} $ is an approximation for $ x $, then:\n", "\n", " * absolute error $ A(x) = | \\tilde{x} - x | $\n", "\n", " * relative error $ R(x) = | \\tilde{x} - x | \\, / \\, | x | $\n", " \n", "**Decimal precision:**\n", "\n", "Given a relative error $ R $, the decimal precision $ p $ is the largest integer such that $ R \\leq 5 \\times 10^{-p} $\n", "\n", "**Big-O notation:**\n", "\n", "The error term in an approximation to a mathematical function can be described by the [big-O](https://en.wikipedia.org/wiki/Big_O_notation) notation:\n", "\n", "$$\n", "f(x) = O(g(x)) \\quad \\text{as} \\quad x \\rightarrow a\n", "$$ \n", "\n", "if and only if \n", "\n", "$$\n", "|f(x)| \\leq M |g(x)| \\quad \\text{as}\\quad |x - a| < \\delta \\quad \\text{where} \\quad M, a > 0.\n", "$$ " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Roundoff error" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Representation of real numbers in computer\n", "\n", "Most widely used representation of real numbers in computers is the floating-point representation. Floating-point representations have a base $ \\beta $, exponent $ E $ and precision $ p $. In general, a floating-point number is represented as \n", "\n", "$$ f = \\pm \\, d_1.d_2d_3 \\dots d_p \\times \\beta^E, $$\n", "\n", "where $ d_1.d_2d_3 \\dots d_p $ is called significand (also mantissa)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Properties of floating-point systems:**\n", "\n", "- Smallest positive number ([underflow](https://en.wikipedia.org/wiki/Arithmetic_underflow) if below)\n", "- Largest number ([overflow](https://en.wikipedia.org/wiki/Integer_overflow) if above)\n", "- [Machine epsilon](https://en.wikipedia.org/wiki/Machine_epsilon), $ \\varepsilon $, defined as the difference between 1 and the next larger floating point number (upper bound on the relative error due to rounding in floating point arithmetic)\n", "- Special values: zero (`0`), infinities (`+Inf`, `-Inf`), [not a number](https://en.wikipedia.org/wiki/NaN) (`NaN`)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "