{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "import sympy as sy" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Warm Up\n", "\n", "Below is a row from our heads tossing table. Additionally we have built a plot of each outcome. Please use the graph to answer the following questions.\n", "\n", "
\n", "\n", "1. How many ways are there to toss 0 heads?\n", "2. How many ways are there to toss 1 head?\n", "3. How many ways are there to toss 2 heads?\n", "4. How many total outcomes are possible?\n", "5. What is the probability of tossing 0 heads?\n", "6. What is the probability of tossing 1 head?\n", "7. What is the probability of tossing 2 heads?\n", "8. What is the probability of tossing 0 or 1 head?\n", "9. What is the probability of tossing 0, 1, or 2 heads?\n", "10. What does this have to do with area?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Riemann Review\n", "\n", "1. Below is a graph of the function $f(x) = -x^2 + 1$. You are asked to approximate the area under the curve using the four rectangles shown.\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "2. Now we have the function $f(x) = x^2 - 2$. Same problem using 8 rectangles.\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Getting Formal $\\Sigma$\n", "\n", "Understanding Summation Notation. \n", "\n", "1. $\\sum_{i = 1}^n i = 1 + 2 + 3 + 4 + \\dots + (n - 1) + n$\n", "\n", "2. $\\sum_{i = 1}^n i^3 = 1^3 + 2^3 + 3^3 + 4^3 + \\dots + (n - 1)^3 + n^3$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**PROBLEM**\n", "\n", "Recall from last class, we had the fact that:\n", "\n", "1. $$\\sum_{i = 1}^n i = 1/2(n^2 + n)$$\n", "\n", "2. $$\\sum_{i = 1}^n i^3 = (1/2(n^2 + n))^2$$\n", "\n", "What is $\\sum_{i = 1}^5 i^3 - i$?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### An Algorithm\n", "\n", "A Riemann sum $S$ of $f$ over $I$ with partition $P$ is defined as\n", "\n", "$$\\displaystyle S=\\sum _{i=1}^{n}f(x_{i}^{*})\\,\\Delta x_{i}$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Set up Riemann sum for $f(x) = x^3$ on $x \\in [0, 2]$. \n", "\n", "- What is $\\Delta x_i$?\n", "- What is $f(x_i^*)$?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A Definition\n", "\n", "\n", "\n", "The Definite Integral of $f(x)$ on $[a, b]$ is defined as:\n", "\n", "$$\\int_a^b f(x) dx = \\lim_{n \\to \\infty} \\sum_{i = 1}^n f(x_i^*)\\Delta x_i$$\n", "\n", "
\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Using the Definition\n", "\n", "There are important patterns that emerge as a result of summations that we are able to capitalize on. We've seen two, and will see how we extend these to general polynomials below. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "i, n = sy.symbols('i n')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 2 \n", "n n\n", "── + ─\n", "2 2\n", "=======================================\n", " 3 2 \n", "n n n\n", "── + ── + ─\n", "3 2 6\n", "=======================================\n", " 4 3 2\n", "n n n \n", "── + ── + ──\n", "4 2 4 \n", "=======================================\n", " 5 4 3 \n", "n n n n \n", "── + ── + ── - ──\n", "5 2 3 30\n", "=======================================\n", " 6 5 4 2\n", "n n 5⋅n n \n", "── + ── + ──── - ──\n", "6 2 12 12\n", "=======================================\n" ] } ], "source": [ "for exponent in np.arange(1, 6):\n", " sy.pprint(sy.summation(i**exponent, (i, 1, n)))\n", " print('=======================================')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### A General Rule\n", "\n", "$$\\int_a^b x^n = \\frac{x^{n+1}}{n+1} |_{a}^b$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Some Examples\n", "\n", "1. $\\displaystyle \\int_0^3 x^2 dx$\n", "\n", "2. $\\displaystyle \\int_1^5 x^3 - x dx $\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Net Change\n", "\n", "Suppose the velocity of a particle in motion is given by $v(t) = 3t - 5$ for $t \\in [0, 3]$. Draw a Riemann sum approximation with 3 rectangles. What would these areas mean?\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "import name_caller" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'What do you think Katrina'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name_caller.your_turn()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "# import gif\n", "\n", "# def f(x): return -x**2 + 2\n", "# x = np.linspace(-2, 2, 1000)\n", "\n", "# @gif.frame\n", "# def plot_riemann(n):\n", "# a = x[0]\n", "# b = x[-1]\n", "# width = (b-a)/n\n", "# plt.plot(x, f(x), color = 'black')\n", "# bases = np.array([a + width*i for i in range(n)])\n", "# plt.bar(bases, f(bases), width = width, align = 'edge', \n", "# color = 'orange', edgecolor = 'black')\n", "# areas = [width * height for height in f(bases)]\n", "# plt.title(f'Area: {sum(areas)}')\n", " \n", "# frames = []\n", "# for i in range(1, 1000,10):\n", "# frame = plot_riemann(i)\n", "# frames.append(frame)\n", " \n", "# gif.save(frames, 'images/rda2g1.gif', duration = 200)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "###code for figure\n", "# N = 6\n", "# ks = np.arange(N+1)\n", "# outcomes = []\n", "# heads = []\n", "# for k in ks:\n", "# outcomes.append(scipy.special.comb(N, k))\n", "# heads.append(f'{k} heads')\n", " \n", "# plt.figure(figsize = (15, 5))\n", "# plt.bar(ks, outcomes, color = '#EE340D', hatch = 'x', edgecolor = 'black', width = 1, alpha = 0.4)\n", "# plt.yticks(outcomes);\n", "# for count in outcomes:\n", "# plt.axhline(count, color = 'black', linestyle = '--')\n", "# plt.xticks(ks, heads, rotation = 90);\n", "# plt.title(f'Number of ways for getting $x$ heads tossing {N} coins', fontsize = 16, loc = 'left');\n", "# plt.tight_layout()\n", "# plt.savefig('images/wrmup_d6.png')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "# def f(x): return -x**2 + 1\n", "# x = np.linspace(-1.1, 1.1, 1000)\n", "\n", "# fig, ax = plt.subplots(figsize = (12, 8))\n", "# ax.plot(x, f(x), color = 'black', alpha = 0.6)\n", "# ax.set_aspect('equal')\n", "# ax.grid(True, which='both')\n", "\n", "# # set the x-spine (see below for more info on `set_position`)\n", "# ax.spines['left'].set_position('zero')\n", "\n", "# # turn off the right spine/ticks\n", "# ax.spines['right'].set_color('none')\n", "# ax.yaxis.tick_left()\n", "\n", "# # set the y-spine\n", "# ax.spines['bottom'].set_position('zero')\n", "\n", "# # turn off the top spine/ticks\n", "# ax.spines['top'].set_color('none')\n", "# ax.xaxis.tick_bottom()\n", "# bars = [-1, -0.5, 0, 0.5]\n", "# for bar in bars:\n", "# ax.bar(bar, f(bar), width = 0.5, align = 'edge', color = 'orange', edgecolor = 'black', alpha = 0.7)\n", "# ax.text(bar + 0.05, f(bar)/2, f'Rectangle {bars.index(bar)}\\nHeight = {f(bar)}', fontsize = 14)\n", "# ax.set_title('Approximating area under $f(x) = -x^2 + 1$ with 4 rectangles.', fontsize = 14, loc = 'left')\n", "# plt.savefig('images/p1d6.png')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "# def f(x): return x**2 - 2\n", "# x = np.linspace(0, 2, 1000)\n", "\n", "# fig, ax = plt.subplots(figsize = (20, 8))\n", "# ax.plot(x, f(x), color = 'black', alpha = 0.6)\n", "# #ax.set_aspect('equal')\n", "# ax.grid(True, which='both')\n", "\n", "# # set the x-spine (see below for more info on `set_position`)\n", "# ax.spines['left'].set_position('zero')\n", "\n", "# # turn off the right spine/ticks\n", "# ax.spines['right'].set_color('none')\n", "# ax.yaxis.tick_left()\n", "\n", "# # set the y-spine\n", "# ax.spines['bottom'].set_position('zero')\n", "\n", "# # turn off the top spine/ticks\n", "# ax.spines['top'].set_color('none')\n", "# ax.xaxis.tick_bottom()\n", "# width = 1/4\n", "# bars = [0 + i*width for i in range(8)]\n", "# for bar in bars:\n", "# ax.bar(bar, f(bar), width = 1/4, align = 'edge', color = 'orange', edgecolor = 'black', alpha = 0.7)\n", "# ax.text(bar, f(bar)/2, f'Rectangle {bars.index(bar) + 1}\\nHeight = {f(bar):.2f}', fontsize = 14)\n", "# ax.set_title(f'Approximating area under $f(x) = x^2 - 2$ with {len(bars)} rectangles.', fontsize = 20, loc = 'left')\n", "# plt.savefig('images/p2d6.png')" ] } ], "metadata": { "celltoolbar": "Slideshow", "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.4" } }, "nbformat": 4, "nbformat_minor": 4 }