This commit is contained in:
jverzani
2025-01-24 11:04:54 -05:00
parent ff0f8a060d
commit 92f4cba496
28 changed files with 1070 additions and 124 deletions

View File

@@ -26,12 +26,14 @@ $$
\int_a^b (f(x) - g(x)) dx
$$
can be interpreted as the "signed" area between $f(x)$ and $g(x)$ over $[a,b]$. If on this interval $[a,b]$ it is true that $f(x) \geq g(x)$, then this would just be the area, as seen in this figure. The rectangle in the figure has area: $(f(a)-g(a)) \cdot (b-a)$ which could be a term in a left Riemann sum of the integral of $f(x) - g(x)$:
can be interpreted as the "signed" area between $f(x)$ and $g(x)$ over $[a,b]$. If on this interval $[a,b]$ it is true that $f(x) \geq g(x)$, then this would just be the area, as seen in this figure. The rectangle in the figure has area: $(f(x_i)-g(x_i)) \cdot (x_{i+1}-x_i)$ for some $x_i, x_{i+1}$ suggestive of a term in a left Riemann sum of the integral of $f(x) - g(x)$:
```{julia}
#| hold: true
#| echo: false
#| label: fig-area-between-f-g
#| fig-cap: "Area between two functions"
f1(x) = x^2
g1(x) = sqrt(x)
a,b = 1/4, 3/4
@@ -42,9 +44,9 @@ ts = vcat(f1.(xs), g1.(reverse(xs)))
plot(f1, 0, 1, legend=false)
plot!(g1)
plot!(ss, ts, fill=(0, :red))
plot!(xs, f1.(xs), linewidth=5, color=:green)
plot!(xs, g1.(xs), linewidth=5, color=:green)
plot!(ss, ts, fill=(0, :forestgreen, 0.25))
plot!(xs, f1.(xs), linewidth=5, color=:royalblue)
plot!(xs, g1.(xs), linewidth=5, color=:royalblue)
plot!(xs, f1.(xs), legend=false, linewidth=5, color=:blue)
@@ -53,7 +55,7 @@ u,v = .4, .5
plot!([u,v,v,u,u], [f1(u), f1(u), g1(u), g1(u), f1(u)], color=:black, linewidth=3)
```
For the figure, we have $f(x) = \sqrt{x}$, $g(x)= x^2$ and $[a,b] = [1/4, 3/4]$. The shaded area is then found by:
In @fig-area-between-f-g we have $f(x) = \sqrt{x}$, $g(x)= x^2$ and $[a,b] = [1/4, 3/4]$. The shaded area is then found by:
$$
@@ -156,12 +158,14 @@ summation(1/(n+1)/(n+2), (n, 1, oo))
##### Example
Verify [Archimedes'](http://en.wikipedia.org/wiki/The_Quadrature_of_the_Parabola) finding that the area of the parabolic segment is $4/3$rds that of the triangle joining $a$, $(a+b)/2$ and $b$.
Verify [Archimedes'](http://en.wikipedia.org/wiki/The_Quadrature_of_the_Parabola) finding that the area of the parabolic segment is $4/3$rds that of the triangle joining $a$, $(a+b)/2$ and $b$. @fig-area-between-f-g clearly shows the bigger parabolic segment area.
```{julia}
#| hold: true
#| echo: false
#| label: fig-archimedes-triangle
#| fig-cap: "Area of parabolic segment and triangle"
f(x) = 2 - x^2
a,b = -1, 1/2
c = (a + b)/2
@@ -169,10 +173,15 @@ xs = range(-sqrt(2), stop=sqrt(2), length=50)
rxs = range(a, stop=b, length=50)
rys = map(f, rxs)
plot(f, a, b, legend=false, linewidth=3)
plot(f, a, b, legend=false,
line=(3, :royalblue),
axis=([], false)
)
plot!([a,b], [f(a),f(b)], line=(3, :royalblue))
xs = [a,c,b,a]
plot!(xs, f.(xs), linewidth=3)
triangle = Shape(xs, f.(xs))
plot!(triangle, fill=(:forestgreen, 3, 0.25))
```
For concreteness, let $f(x) = 2-x^2$ and $[a,b] = [-1, 1/2]$, as in the figure. Then the area of the triangle can be computed through:
@@ -515,6 +524,81 @@ a, b = 0, 1
quadgk(y -> f(y) - g(y), a, b)[1]
```
## The area enclosed in a simple polygon
A simple polygon is comprised of several non-intersecting line segments, save for the last segment ends where the first begins. These have an orientation, which we take to be counterclockwise. Polygons, as was seen when computing areas related to Archimedes efforts, can be partioned into simple geometric shapes, for which known areas apply.
### The trapezoid formula
In this example, we see how trapezoids can be used to find the interior area encolosed by a simply polygon, avoiding integration.
The trapezoid formula to compute the area of a simple polygon is
$$
A = - \sum_{i=1}^n \frac{y_{i+1} + y_i}{2} \cdot (x_{i+1} - x_i).
$$
Where the polygon is described by points $(x_1,y_1), (x_2,y_2), \cdots, (x_n, y_n), (x_{n+1}, y_{n+1})$ *with* $(x_1,y_1) = (x_{n+1}, y_{n+1})$.
Each term describes the area of a trapezoid, possibly signed.
This figure illustrates for a simple case:
```{julia}
using Plots
xs = [1, 3, 4, 2, 1] # n = 4 to give 5=n+1 values
ys = [1, 1, 2, 3, 1]
p = plot(xs, ys; line=(3, :black), ylims=(0,4), legend=false)
scatter!(p, xs, ys; marker=(7, :circle))
```
Going further, we draw the four trapezoids using different colors depending on the sign of the `xs[i+1] - xs[[i]` terms:
```{julia}
for i in 1:4
col = xs[i+1] - xs[i] > 0 ? :yellow : :blue
S = Shape([(xs[i],0), (xs[i+1],0), (xs[i+1],ys[i+1]), (xs[i], ys[i])])
plot!(p, S, fill=(col, 0.25))
end
p
```
The yellow trapezoids appear to be colored green, as they completely overlap with parts of the blue trapezoids and blue and yellow make green. As the signs of the differences of the $x$ values is different, these areas add to $0$ in the sum, leaving just the area of the interior when the sum is computed.
For this particular figure, the enclosed area is
```{julia}
- sum((ys[i+1] + ys[i]) / 2 * (xs[i+1] - xs[i]) for i in 1:length(xs)-1)
```
### The triangle formula
Similarly, we can create triangles to partition the polygon. The *signed* area of a triangle with vertices $(0,0), (x_i, y_i), (x_{i+1}, y_{i+1})$ can be computed by $\frac{1}{2} \cdot (x_i \cdot y_{i+1} - x_{i+1}\cdot y_i)$. (A formula that can be derived from a related one for the area of a parallelogram.
Visualizing, as before, we have the shape and the triangles after centering around the origin:
```{julia}
S = Shape(xs, ys)
c = Plots.center(S) # find centroid of the polygon
xs, ys = xs .- c[1], ys .- c[2]
p = plot(xs, ys; line=(3, :black), legend=false)
scatter!(p, xs, ys; marker=(7, :circle))
for i in 1:4
col = xs[i]*ys[i+1] - xs[i+1]*ys[i] > 0 ? :yellow : :blue
S = Shape([(0,0), (xs[i],ys[i]), (xs[i+1],ys[i+1])])
plot!(p, S, fill=(col, 0.25))
end
p
```
Here the triangles are all yellow, as each has a positive area to contribute to the following sum:
```{julia}
(1/2) * sum(xs[i]*ys[i+1] - xs[i+1]*ys[i] for i in 1:4)
```
## Questions
@@ -833,3 +917,56 @@ nothing
![Roberval, avoiding a trignoometric integral, instead used symmetry to show that the area under the companion curve was half the area of the rectangle, which in this figure is $2\pi$.
](./figures/companion-curve-bisects-rectangle.png)
###### Question
```{julia}
#| echo: false
#| label: fig-cavalieri-example
#| fig-cap: "Cavalieri example"
let
squareplus(x, b=2) = x/2 + sqrt(x^2 + b)/2
TeLU(x) = x * tanh(exp(x))
Δ(x) = squareplus(x) - TeLU(x)
a,b = -3, 3
xs = range(a, b, 10)
c = 3
Shift(f,c) = x -> c + f(x)
p = plot(Shift(squareplus, c), a, b;
legend=false,
line=(3, :royalblue),
axis=([], false))
plot!(Shift(TeLU, c),
line=(3, :royalblue)
)
plot!(Δ, line=(3, :forestgreen))
plot!(zero, line=(3, :forestgreen))
n = 20
xs = range(a, b, n+1)
for i in 1:n
S = Shape([xs[i],xs[i]],
[0, Δ(xs[i])])
plot!(Plots.translate(S, 0, TeLU(xs[i]) + c), fill=(:royalblue, 0.25))
plot!(S, fill=(:forestgreen, 0.25))
end
p
end
```
@fig-cavalieri-example shows on same scale the graphs of $f(x)$ and $g(x)$ and the graphs of $f(x) - g(x)$ and $0$ (the lower figure). Twenty lines were drawn with height $f(x) - g(x)$ on the lower figure and these were translated to the upper figure by an amount $g(x)$. All to illustrate that any parallel line in the $y$ direction intersects the two figures with the same length.
What does this imply:
```{julia}
#| echo: false
choices = ["The two enclosed areas should be equal",
"The two enclosed areas are clearly different, as they do not overap"],
radioq(choices, 1)
```
"

View File

@@ -27,56 +27,90 @@ So far we have seen that the *derivative* rules lead to *integration rules*. In
* The sum rule $[au(x) + bv(x)]' = au'(x) + bv'(x)$ gives rise to an integration rule: $\int (au(x) + bv(x))dx = a\int u(x)dx + b\int v(x))dx$. (That is, the linearity of the derivative means the integral has linearity.)
* The chain rule $[f(g(x))]' = f'(g(x)) g'(x)$ gives $\int_a^b f(g(x))g'(x)dx=\int_{g(a)}^{g(b)}f(x)dx$. That is, substitution reverses the chain rule.
Now we turn our attention to the implications of the *product rule*: $[uv]' = u'v + uv'$. The resulting technique is called integration by parts.
::: {.callout-note}
## Integration by parts
The following illustrates integration by parts of the integral $(uv)'$ over $[a,b]$ [original](http://en.wikipedia.org/wiki/Integration_by_parts#Visualization).
By the fundamental theorem of calculus:
$$
[u(x)\cdot v(x)]\big|_a^b = \int_a^b [u(x) v(x)]' dx = \int_a^b u'(x) \cdot v(x) dx + \int_a^b u(x) \cdot v'(x) dx.
$$
Or,
$$
\int_a^b u(x) v'(x) dx = [u(x)v(x)]\big|_a^b - \int_a^b v(x) u'(x)dx.
$$
:::
The following visually illustrates integration by parts:
```{julia}
#| echo: false
#| label: fig-integration-by-parts
#| fig-cap: "Integration by parts figure ([original](http://en.wikipedia.org/wiki/Integration_by_parts#Visualization))"
let
## parts picture
u(x) = sin(x*pi/2)
v(x) = x
xs = range(0, stop=1, length=50)
a,b = 1/4, 3/4
p = plot(u, v, 0, 1, legend=false)
plot!(p, zero, 0, 1)
scatter!(p, [u(a), u(b)], [v(a), v(b)], color=:orange, markersize=5)
## parts picture
u(x) = sin(x*pi/2)
v(x) = x
xs = range(0, stop=1, length=50)
a,b = 1/4, 3/4
plot!(p, [u(a),u(a),0, 0, u(b),u(b),u(a)],
[0, v(a), v(a), v(b), v(b), 0, 0],
linetype=:polygon, fillcolor=:orange, alpha=0.25)
annotate!(p, [(0.65, .25, "A"), (0.4, .55, "B")])
annotate!(p, [(u(a),v(a) + .08, "(u(a),v(a))"), (u(b),v(b)+.08, "(u(b),v(b))")])
p = plot(u, v, 0, 1, legend=false, axis=([], false))
plot!([0, u(1)], [0,0], line=(:black, 3))
plot!([0, 0], [0, v(1) ], line=(:black, 3))
plot!(p, zero, 0, 1)
xs = range(a, b, length=50)
plot!(Shape(vcat(u.(xs), reverse(u.(xs))),
vcat(zero.(xs), v.(reverse(xs)))),
fill=(:red, 0.15),
xlims=(-0.07, 1)
)
plot!(Shape([0,u(a),u(a),0],[0,0,v(a),v(a)]), fill=(:royalblue, 0.5))
scatter!(p, [u(a), u(b)], [v(a), v(b)], color=:mediumorchid3, markersize=5)
plot!(p, [u(a),u(a),0, 0, u(b),u(b),u(a)],
[0, v(a), v(a), v(b), v(b), 0, 0],
linetype=:polygon, fill=(:brown3, 0.25))
annotate!(p, [(0.65, .25, "A"),
(0.4, .55, "B"),
(u(a),v(a) + .08, "(u(a),v(a))"),
(u(b),v(b)+.08, "(u(b),v(b))"),
(u(a),0, "u(a)",:top),
(u(b),0, "u(b)",:top),
(0, v(a), "v(a) ",:right),
(0, v(b), "v(b) ",:right)
])
end
```
The figure is a parametric plot of $(u,v)$ with the points $(u(a), v(a))$ and $(u(b), v(b))$ marked. The difference $u(b)v(b) - u(a)v(a) = u(x)v(x) \mid_a^b$ is shaded. This area breaks into two pieces, $A$ and $B$, partitioned by the curve. If $u$ is increasing and the curve is parameterized by $t \rightarrow u^{-1}(t)$, then $A=\int_{u^{-1}(a)}^{u^{-1}(b)} v(u^{-1}(t))dt$. A $u$-substitution with $t = u(x)$ changes this into the integral $\int_a^b v(x) u'(x) dx$. Similarly, for increasing $v$, it can be seen that $B=\int_a^b u(x) v'(x) dx$. This suggests a relationship between the integral of $u v'$, the integral of $u' v$ and the value $u(b)v(b) - u(a)v(a)$.
@fig-integration-by-parts shows a parametric plot of $(u(t),v(t))$ for $a \leq t \leq b$..
The total shaded area, a rectangle, is $u(b)v(b)$, the area of $A$ and $B$ combined is just $u(b)v(b) - u(a)v(a)$ or $[u(x)v(x)]\big|_a^b$. We will show that that $A$ is $\int_a^b v(x)u'(x)dx$ and $B$ is $\int_a^b u(x)v'(x)dx$ giving the formula
In terms of formulas, by the fundamental theorem of calculus:
We can compute $A$ by a change of variables with $x=u^{-1}(t)$ (so $u'(x)dx = dt$):
$$
u(x)\cdot v(x)\big|_a^b = \int_a^b [u(x) v(x)]' dx = \int_a^b u'(x) \cdot v(x) dx + \int_a^b u(x) \cdot v'(x) dx.
\begin{align*}
A &= \int_{u(a)}^{u(b)} v(u^{-1}(t)) dt & \text{let } x = u^{-1}(t) \text{ or }u(x) = t \\
&= \int_{u^{-1}(u(a))}^{u^{-1}(u(b))} v(x) u'(x) dx \\
&= \int_a^b v(x) u'(x) dx.
\end{align*}
$$
This is re-expressed as
$B$ is similar with the roles of $u$ and $v$ reversed.
$$
\int_a^b u(x) \cdot v'(x) dx = u(x) \cdot v(x)\big|_a^b - \int_a^b v(x) \cdot u'(x) dx,
$$
Or, more informally, as $\int udv = uv - \int v du$.
This can sometimes be confusingly written as:
Informally, the integration by parts formula is sometimes seen as $\int udv = uv - \int v du$, as well can be somewhat confusingly written as:
$$
@@ -86,8 +120,7 @@ $$
(The confusion coming from the fact that the indefinite integrals are only defined up to a constant.)
How does this help? It allows us to differentiate parts of an integral in hopes it makes the result easier to integrate.
How does this formula help? It allows us to differentiate parts of an integral in hopes it makes the result easier to integrate.
An illustration can clarify.

View File

@@ -66,14 +66,18 @@ $$
\text{average} = \frac{1}{\pi-0} \int_0^\pi \sin(x) dx = \frac{1}{\pi} (-\cos(x)) \big|_0^\pi = \frac{2}{\pi}
$$
Visually, we have:
Visually:
```{julia}
plot(sin, 0, pi)
plot!(x -> 2/pi)
#| label: fig-integral-mean-value
#| fig-cap: "Area under sine curve is equal to area of rectangle"
plot(sin, 0, pi, legend=false, fill=(:forestgreen, 0.25, 0))
plot!(x -> 2/pi, fill=(:royalblue, 0.25, 0))
```
In @fig-integral-mean-value the area under the sine curve ($2 = (-\cos(\pi)) - (-\cos(0))$) is equal to the area under the average (also $2 = 2/\pi \cdot \pi$).
##### Example
@@ -91,7 +95,7 @@ What is the average value of the function $e^{-x}$ between $0$ and $\log(2)$?
$$
\begin{align*}
\text{average} = \frac{1}{\log(2) - 0} \int_0^{\log(2)} e^{-x} dx\\
\text{average} &= \frac{1}{\log(2) - 0} \int_0^{\log(2)} e^{-x} dx\\
&= \frac{1}{\log(2)} (-e^{-x}) \big|_0^{\log(2)}\\
&= -\frac{1}{\log(2)} (\frac{1}{2} - 1)\\
&= \frac{1}{2\log(2)}.
@@ -103,8 +107,8 @@ Visualizing, we have
```{julia}
plot(x -> exp(-x), 0, log(2))
plot!(x -> 1/(2*log(2)))
plot(x -> exp(-x), 0, log(2), legend=false, fill=(:forestgreen, 0.25, 0))
plot!(x -> 1/(2*log(2)), fill=(:royalblue, 0.25, 0))
```
## The mean value theorem for integrals

View File

@@ -213,7 +213,7 @@ surface(ws..., zlims=(-6,6), legend=false)
plot!([0,0], [0,0], [-3,3], color=:red, linewidth=5) # y axis emphasis
```
The `unzip` function is not part of base `Julia`, rather part of `CalculusWithJulia`. This function rearranges data into a form consumable by the plotting methods like `surface`. In this case, the result of `S.(us,vs')` is a grid (matrix) of points, the result of `unzip` is three grids of values, one for the $x$ values, one for the $y$ values, and one for the $z$ values. A manual adjustment to the `zlims` is used, as `aspect_ratio` does not have an effect with the `plotly()` backend and errors on 3d graphics with `pyplot()`.
The `unzip` function is not part of base `Julia`, rather part of `CalculusWithJulia` (it is really `SplitApplyCombine`'s `invert` function). This function rearranges data into a form consumable by the plotting methods like `surface`. In this case, the result of `S.(us,vs')` is a grid (matrix) of points, the result of `unzip` is three grids of values, one for the $x$ values, one for the $y$ values, and one for the $z$ values. A manual adjustment to the `zlims` is used, as `aspect_ratio` does not have an effect with the `plotly()` backend and errors on 3d graphics with `pyplot()`.
To rotate this about the $x$ axis, we have this pattern:

View File

@@ -26,13 +26,16 @@ function make_plot(a₀=7/8, q₀=-a₀ - 1/2a₀)
aspect_ratio=:equal, border=:none, legend=false)
f(x) = x^2
fp(x) = 2x
plot!(f, -1.5, 1.5)
plot!(zero)
plot!(f, -1.5, 1.5, line=(1, :royalblue))
plot!(zero, line=(1, :black))
tl = x -> f(a₀) + fp(a₀) * (x-a₀)
nl = x -> f(a₀) - 1/(fp(a₀)) * (x-a₀)
plot!(tl, -0.02, 1.6; linecolor=:black)
plot!(nl, -1.6, 1; linecolor=:black)
plot!(tl, -0.02, 1.6; line=(1, :forestgreen))
plot!(nl, -1.6, 1; line=(1, :forestgreen))
# add in right triangle
scatter!([a₀, q₀], f.([a₀, q₀]), markersize=5)
Δ = 0.01