2.3: Integration

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$

( \newcommand{\kernel}{\mathrm{null}\,}\) $$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\id}{\mathrm{id}}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\kernel}{\mathrm{null}\,}$$

$$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$

$$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$

$$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\avec}{\mathbf a}$$ $$\newcommand{\bvec}{\mathbf b}$$ $$\newcommand{\cvec}{\mathbf c}$$ $$\newcommand{\dvec}{\mathbf d}$$ $$\newcommand{\dtil}{\widetilde{\mathbf d}}$$ $$\newcommand{\evec}{\mathbf e}$$ $$\newcommand{\fvec}{\mathbf f}$$ $$\newcommand{\nvec}{\mathbf n}$$ $$\newcommand{\pvec}{\mathbf p}$$ $$\newcommand{\qvec}{\mathbf q}$$ $$\newcommand{\svec}{\mathbf s}$$ $$\newcommand{\tvec}{\mathbf t}$$ $$\newcommand{\uvec}{\mathbf u}$$ $$\newcommand{\vvec}{\mathbf v}$$ $$\newcommand{\wvec}{\mathbf w}$$ $$\newcommand{\xvec}{\mathbf x}$$ $$\newcommand{\yvec}{\mathbf y}$$ $$\newcommand{\zvec}{\mathbf z}$$ $$\newcommand{\rvec}{\mathbf r}$$ $$\newcommand{\mvec}{\mathbf m}$$ $$\newcommand{\zerovec}{\mathbf 0}$$ $$\newcommand{\onevec}{\mathbf 1}$$ $$\newcommand{\real}{\mathbb R}$$ $$\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}$$ $$\newcommand{\laspan}[1]{\text{Span}\{#1\}}$$ $$\newcommand{\bcal}{\cal B}$$ $$\newcommand{\ccal}{\cal C}$$ $$\newcommand{\scal}{\cal S}$$ $$\newcommand{\wcal}{\cal W}$$ $$\newcommand{\ecal}{\cal E}$$ $$\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}$$ $$\newcommand{\gray}[1]{\color{gray}{#1}}$$ $$\newcommand{\lgray}[1]{\color{lightgray}{#1}}$$ $$\newcommand{\rank}{\operatorname{rank}}$$ $$\newcommand{\row}{\text{Row}}$$ $$\newcommand{\col}{\text{Col}}$$ $$\renewcommand{\row}{\text{Row}}$$ $$\newcommand{\nul}{\text{Nul}}$$ $$\newcommand{\var}{\text{Var}}$$ $$\newcommand{\corr}{\text{corr}}$$ $$\newcommand{\len}[1]{\left|#1\right|}$$ $$\newcommand{\bbar}{\overline{\bvec}}$$ $$\newcommand{\bhat}{\widehat{\bvec}}$$ $$\newcommand{\bperp}{\bvec^\perp}$$ $$\newcommand{\xhat}{\widehat{\xvec}}$$ $$\newcommand{\vhat}{\widehat{\vvec}}$$ $$\newcommand{\uhat}{\widehat{\uvec}}$$ $$\newcommand{\what}{\widehat{\wvec}}$$ $$\newcommand{\Sighat}{\widehat{\Sigma}}$$ $$\newcommand{\lt}{<}$$ $$\newcommand{\gt}{>}$$ $$\newcommand{\amp}{&}$$ $$\definecolor{fillinmathshade}{gray}{0.9}$$

Finding Area

Let's use a Monte-Carlo type method to calculate area. Suppose we have the following circular dart board and want to know its area.

We enclose the dart board with a square of known area, in this case 100in2, and perform following the following procedure:

1. Throw darts at the square, being careful not to favor any particular spot.
2. Record the percentage of darts that land inside the circle.
3. Multiply this percentage by the known area of the square (100in2)
4. That's our approximation!

Of course, doing this by hand would take a lot of time. Instead, we can use Python. In our program, we will randomly choose an x-value and y-value using the random.uniform(start,stop) function. This function returns a random float in [start,stop). We will center our circle at the origin, so we will use random.uniform(-5,5) for both the x and y values.

Next, we need to have a way to determine whether the point (x,y) lies inside the circle or not. We know that the equation of the circle under discussion will be

$x^2+y^2=5^2$

Solving for $$y$$, we get

$y=\pm\sqrt{25-x^2}.$

Thus, in our python code, we will check that

$y\leq\sqrt{25-x^2}\hspace{10pt}\textrm{ and }\hspace{10pt}y\geq-\sqrt{25-x^2}.$

That is, that it is below the top half of the circle and above the bottom half of the circle. Here we go!

import random

tries=10000
success=0
for a in range(1,tries+1):
x=random.uniform(-5,5)
y=random.uniform(-5,5)
if (y<=(25-x**2)**(1/2)) and (y>=-(25-x**2)**(1/2)):
success+=1
print(100*success/tries)


output:     78.52

Above, the percentage of "darts" that hit the circle is given by success/tries. We then multiplied that by the area of the square (100) to get the approximate area of the circle, which is given as 78.52, very close to our expected value of $$25\pi$$.

For fun, we could plot each of the dots to see what our board looks like. Here is an example with 100 dots:

import random
import numpy as np
import matplotlib.pyplot as plt

xc = np.linspace(-5, 5, 1000)
yc = (25-xc**2)**(1/2)
zc = -(25-xc**2)**(1/2)
plt.plot(xc, yc, color='red', linewidth=2.0, linestyle='-')
plt.plot(xc, zc, color='red', linewidth=2.0, linestyle='-')

#we need to make the axes have the same scale so it looks like a circle.
#these two lines do that.
ax = plt.gca()

tries=100
success=0
for a in range(1,tries+1):
x=random.uniform(-5,5)
y=random.uniform(-5,5)
if (y<=(25-x**2)**(1/2)) and (y>=-(25-x**2)**(1/2)):
success+=1
plt.plot(x,y,'o',color='black')
print(100*success/tries)
plt.show()


output:     80

Integrals

Now that we've found area using a Monte-Carlo type simulation, you might have an idea of how to calculate integrals: just find the area under the curve!

Example: Use a Monte-Carlo type simulation to find the area under the curve over $$[0,4]$$

$y=0.5x^2$

Answer: To start, we need to find a box that will fit the entire curve we are calculating. The x-values are easy: just use the given interval $$[0,4]$$. For the y-values, we need to find the least and greatest y-values that this curve achieves. Since this is a parabola, we can see that the least y-value is 0 and the greatest is $$0.5*(4)^2=8$$. So our box will be $$[0,4]x[0,8]$$. That means our random points will have x-values in [0,4] and y-values in [0,8]. We will determine whether the point is above or below the curve similar to the circle example above. We will consider a "success" - that is, consider our point to be under the curve - if:

$y\leq 0.5x^2.$

Note that we don't need to determine whether the point is above the x-axis because our y-values are only chosen from [0,8]. The area of our box is 4*8=32. Let's modify the above program:

import random
import numpy as np
import matplotlib.pyplot as plt

xc = np.linspace(0, 4, 1000)
yc = 0.5*xc**2
plt.plot(xc, yc, color='red', linewidth=2.0, linestyle='-')

tries=1000
success=0
for a in range(1,tries+1):
x=random.uniform(0,4)
y=random.uniform(0,8)
if (y<=(0.5*x**2)):
success+=1
plt.plot(x,y,'o',color='black')
print(32*success/tries)
plt.show()


output:     10.208

This is relatively close to our expected value (using integration) of 10.6667. If we increase the "tries" variable, we can get even closer. However, this will make our picture look like a blob!

Exercise

Use the techniques above to approximate the integral $\int_{\pi/2}^\pi$

1. $$11$$
2. $$3$$
3. $$-4$$
1. $$\frac{11}{1}$$
2. $$\frac{3}{1}$$
3. $$-\frac{4}{1}$$