[1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sympy as sy
Warm Up¶
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.

- How many ways are there to toss 0 heads?
- How many ways are there to toss 1 head?
- How many ways are there to toss 2 heads?
- How many total outcomes are possible?
- What is the probability of tossing 0 heads?
- What is the probability of tossing 1 head?
- What is the probability of tossing 2 heads?
- What is the probability of tossing 0 or 1 head?
- What is the probability of tossing 0, 1, or 2 heads?
- What does this have to do with area?
Riemann Review¶
- 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.

- Now we have the function \(f(x) = x^2 - 2\). Same problem using 8 rectangles.

Getting Formal \(\Sigma\)¶
Understanding Summation Notation.
- \(\sum_{i = 1}^n i = 1 + 2 + 3 + 4 + \dots + (n - 1) + n\)
- \(\sum_{i = 1}^n i^3 = 1^3 + 2^3 + 3^3 + 4^3 + \dots + (n - 1)^3 + n^3\)
PROBLEM
Recall from last class, we had the fact that:
- \[\sum_{i = 1}^n i = 1/2(n^2 + n)\]
- \[\sum_{i = 1}^n i^3 = (1/2(n^2 + n))^2\]
What is \(\sum_{i = 1}^5 i^3 - i\)?
An Algorithm¶
A Riemann sum \(S\) of \(f\) over \(I\) with partition \(P\) is defined as
Set up Riemann sum for \(f(x) = x^3\) on \(x \in [0, 2]\).
- What is \(\Delta x_i\)?
- What is \(f(x_i^*)\)?
A Definition¶
The Definite Integral of \(f(x)\) on \([a, b]\) is defined as:

Using the Definition¶
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.
[2]:
i, n = sy.symbols('i n')
[3]:
for exponent in np.arange(1, 6):
sy.pprint(sy.summation(i**exponent, (i, 1, n)))
print('=======================================')
2
n n
── + ─
2 2
=======================================
3 2
n n n
── + ── + ─
3 2 6
=======================================
4 3 2
n n n
── + ── + ──
4 2 4
=======================================
5 4 3
n n n n
── + ── + ── - ──
5 2 3 30
=======================================
6 5 4 2
n n 5⋅n n
── + ── + ──── - ──
6 2 12 12
=======================================
A General Rule¶
Some Examples¶
- \(\displaystyle \int_0^3 x^2 dx\)
- $:nbsphinx-math:displaystyle `:nbsphinx-math:int`_1^5 x^3 - x dx $
Net Change¶
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?
[9]:
import name_caller
[17]:
name_caller.your_turn()
[17]:
'What do you think Katrina'
[3]:
# import gif
# def f(x): return -x**2 + 2
# x = np.linspace(-2, 2, 1000)
# @gif.frame
# def plot_riemann(n):
# a = x[0]
# b = x[-1]
# width = (b-a)/n
# plt.plot(x, f(x), color = 'black')
# bases = np.array([a + width*i for i in range(n)])
# plt.bar(bases, f(bases), width = width, align = 'edge',
# color = 'orange', edgecolor = 'black')
# areas = [width * height for height in f(bases)]
# plt.title(f'Area: {sum(areas)}')
# frames = []
# for i in range(1, 1000,10):
# frame = plot_riemann(i)
# frames.append(frame)
# gif.save(frames, 'images/rda2g1.gif', duration = 200)
[4]:
###code for figure
# N = 6
# ks = np.arange(N+1)
# outcomes = []
# heads = []
# for k in ks:
# outcomes.append(scipy.special.comb(N, k))
# heads.append(f'{k} heads')
# plt.figure(figsize = (15, 5))
# plt.bar(ks, outcomes, color = '#EE340D', hatch = 'x', edgecolor = 'black', width = 1, alpha = 0.4)
# plt.yticks(outcomes);
# for count in outcomes:
# plt.axhline(count, color = 'black', linestyle = '--')
# plt.xticks(ks, heads, rotation = 90);
# plt.title(f'Number of ways for getting $x$ heads tossing {N} coins', fontsize = 16, loc = 'left');
# plt.tight_layout()
# plt.savefig('images/wrmup_d6.png')
[5]:
# def f(x): return -x**2 + 1
# x = np.linspace(-1.1, 1.1, 1000)
# fig, ax = plt.subplots(figsize = (12, 8))
# ax.plot(x, f(x), color = 'black', alpha = 0.6)
# ax.set_aspect('equal')
# ax.grid(True, which='both')
# # set the x-spine (see below for more info on `set_position`)
# ax.spines['left'].set_position('zero')
# # turn off the right spine/ticks
# ax.spines['right'].set_color('none')
# ax.yaxis.tick_left()
# # set the y-spine
# ax.spines['bottom'].set_position('zero')
# # turn off the top spine/ticks
# ax.spines['top'].set_color('none')
# ax.xaxis.tick_bottom()
# bars = [-1, -0.5, 0, 0.5]
# for bar in bars:
# ax.bar(bar, f(bar), width = 0.5, align = 'edge', color = 'orange', edgecolor = 'black', alpha = 0.7)
# ax.text(bar + 0.05, f(bar)/2, f'Rectangle {bars.index(bar)}\nHeight = {f(bar)}', fontsize = 14)
# ax.set_title('Approximating area under $f(x) = -x^2 + 1$ with 4 rectangles.', fontsize = 14, loc = 'left')
# plt.savefig('images/p1d6.png')
[4]:
# def f(x): return x**2 - 2
# x = np.linspace(0, 2, 1000)
# fig, ax = plt.subplots(figsize = (20, 8))
# ax.plot(x, f(x), color = 'black', alpha = 0.6)
# #ax.set_aspect('equal')
# ax.grid(True, which='both')
# # set the x-spine (see below for more info on `set_position`)
# ax.spines['left'].set_position('zero')
# # turn off the right spine/ticks
# ax.spines['right'].set_color('none')
# ax.yaxis.tick_left()
# # set the y-spine
# ax.spines['bottom'].set_position('zero')
# # turn off the top spine/ticks
# ax.spines['top'].set_color('none')
# ax.xaxis.tick_bottom()
# width = 1/4
# bars = [0 + i*width for i in range(8)]
# for bar in bars:
# ax.bar(bar, f(bar), width = 1/4, align = 'edge', color = 'orange', edgecolor = 'black', alpha = 0.7)
# ax.text(bar, f(bar)/2, f'Rectangle {bars.index(bar) + 1}\nHeight = {f(bar):.2f}', fontsize = 14)
# ax.set_title(f'Approximating area under $f(x) = x^2 - 2$ with {len(bars)} rectangles.', fontsize = 20, loc = 'left')
# plt.savefig('images/p2d6.png')