{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Taylor Series\n", "\n", "This is an exposition on Taylor series, loosely following § 1.4 of *Applied Scientific Computing With Python* by Peter R. Turner, Thomas Arildsen, and Kathleen Kavanagh.\n", "\n", "The goal here is to stress that we can do rigorous computations in Python even with infinite quantities. However, to do so, we need to combine mathematical arguments, with careful coding.\n", "\n", "**Mathematics reviewed:**\n", "* Taylor series\n", "* Remainder formulas\n", "\n", "**Programming topics introduced:**\n", "* Alternate assignment operators\n", "* Working with lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Programming\n", "### Standard imports" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import math as m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment operators\n", "\n", "We will use some new assignment operators, which make some code easier to read here.\n", "\n", "The operators \n", "* `x += y` is short hand for `x = x+y`. \n", "* `x -= y` is short hand for `x = x-y`. \n", "* `x *= y` is short hand for `x = x*y`. \n", "* `x /= y` is short hand for `x = x/y`.\n", "* `x //= y` is short hand for `x = x//y`.\n", "* `x %= y` is short hand for `x = x%y`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 2\n", "x += 1\n", "x" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-7" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x -= 10\n", "x" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x //= -7\n", "x" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.14285714285714285" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x /= -7\n", "x" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 13\n", "x %= 3\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Working with lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists can be changed. This gives tools that makes them easy to construct.\n", "\n", "You can build a short list using square brackets." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'dog']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1, 2, \"dog\"]\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can add an object to the end of a list with the `append` method." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'dog', 'cat']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append(\"cat\")\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following creates a new list `ll` whose entries are given by `2*` the entries in `l`." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 4, 'dogdog', 'catcat']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ll = []\n", "for x in l:\n", " ll.append(2*x)\n", "ll" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python has a shorthand syntax which does the same thing as the loop above:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 4, 'dogdog', 'catcat']" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ll2 = [2*x for x in l]\n", "ll2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An item can be removed with the `del` keyword." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 'dogdog', 'catcat']" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del ll2[1]\n", "ll2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to actually use the item you remove, you should use the `pop` method instead." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Currently ll2 is [2, 'dogdog', 'catcat'].\n", "After popping off dogdog, it has become [2, 'catcat'].\n" ] } ], "source": [ "print(\"Currently ll2 is {}.\".format(ll2))\n", "item = ll2.pop(1)\n", "print(\"After popping off {}, it has become {}.\".format(item, ll2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Power Series \n", "\n", "A *power series* centered at a point $a \\in {\\mathbb R}$ has the form \n", "$$\\sum_{n=0}^\\infty C_n(x-a)^n = C_0 + C_1 (x-a) + C_2 (x-a)^2+\\ldots.$$\n", "\n", "If a real-valued function $f$ is infinitely differentiable at $a \\in {\\mathbb R}$, its *Taylor series* is\n", "$$\\sum_{n=0}^\\infty \\frac{f^{(n)}(a)}{n!}(x-a)^n = f(a) + f'(a) (x-a) + \\frac{f''(a)}{2} (x-a)^2+\\ldots.$$\n", "A function $f$ is called *analytic at $a$* if equals its Taylor series in a neighborhood of $a$ (i.e., for values $x \\in {\\mathbb R}$ with $|x-a|<\\epsilon$ for some $\\epsilon>0$). A function is called *analytic* if it is analytic at every point in its domain.\n", "\n", "Most of our favorite functions are analytic including exponential functions, sine, cosine, and logarithms." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Taylor's Theorem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Taylor's Theorem deals with the question of how closely a truncated Taylor series of a function approximates the function.\n", "\n", "The degree $N$ Taylor polynomial of $f$ at $a$ is\n", "$$P_N(x) = \\sum_{n=0}^N \\frac{f^{(n)}(a)}{n!}(x-a)^n = f(a) + f'(a) (x-a) +\\ldots+\\frac{f^{(N)}(a)}{N!}(x-a)^N.$$\n", "Then $N$th remainder is \n", "$$R_N(x) = f(x)-P_N(x).$$\n", "The various Taylor's Theorems give bounds on $|R_N(x)|$, which then can guarantee that $P_N(x)$ is very close to $f(x)$.\n", "\n", "The following is the a version of Taylor's Theorem giving the *Lagrange form* of the remainder." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "