description = "Calculus with Julia: Area between two curves",
tags = ["CalculusWithJulia", "integrals", "area between two curves"],
);
nothing
```
----
The definite integral gives the "signed" area between the function $f(x)$ and the $x$-axis over $[a,b]$. Conceptually, this is the area between two curves, $f(x)$ and $g(x)=0$. More generally, this integral:
```math
\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)$:
This should should be no surprise, given how the areas computed carve up the area under the line $y=x^1$ over $[0,1]$, so the answer should be ``1/2``.
```julia
p = plot(x, 0, 1, legend=false)
[plot!(p, x^n, 0, 1) for n in 2:20]
p
```
We can check using the `summation` function of `SymPy` which is similar in usage to `integrate`:
```julia;
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$.
```julia; hold=true; echo=false
f(x) = 2 - x^2
a,b = -1, 1/2
c = (a + b)/2
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)
xs = [a,c,b,a]
plot!(xs, f.(xs), linewidth=3)
```
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:
As we needed three secant lines, we used the `secant` function from `CalculusWithJulia` to create functions representing each. Once that was done, we used the `max` function to facilitate
integrating over the top bounding curve, alternatively, we could break
the integral over $[a,c]$ and $[c,b]$.
The area of the parabolic segment is more straightforward.
```julia;
A2 = quadgk(x -> 𝐟(x) - f2(x), 𝐚, 𝐛)[1]
```
Finally, if Archimedes was right, this relationship should bring about $0$ (or something within round-off error):
```julia;
A1 * 4/3 - A2
```
##### Example
Find the area bounded by $y=x^4$ and $y=e^x$ when $x^4 \geq e^x$ and
$x > 0$.
A graph over $[0,10]$ shows clearly the largest zero, for afterwards
the exponential dominates the power.
```julia;
h1(x) = x^4
h2(x) = exp(x)
plot(h1, 0, 10)
plot!(h2)
```
There must be another zero, though it is hard to see from the graph over $[0,10]$,
as $0^4=0$ and $e^0=1$, so the polynomial must cross below the
exponential to the left of $5$. (Otherwise, plotting over $[0,2]$ will
clearly reveal the other zero.) We now find these intersection points
numerically and then integrate:
```julia; hold=true
a,b = find_zeros(x -> h1(x) - h2(x), 0, 10)
quadgk(x -> h1(x) - h2(x), a, b)[1]
```
##### Examples
The area between $y=\sin(x)$ and $y=m\cdot x$ between $0$ and the
first positive intersection depends on $m$ (where $0 \leq m \leq
1$. The extremes are when $m=0$, the area is $2$ and when $m=1$ (the
line is tangent at $x=0$), the area is $0$. What is it for other
values of $m$? The picture for $m=1/2$ is:
```julia;
m = 1/2
plot(sin, 0, pi)
plot!(x -> m*x)
```
For a given $m$, the area is found after computing $b$, the
intersection point. We express this as a function of $m$ for later reuse:
When doing problems by hand this latter style can often reduce the complications, but when approaching the task numerically, the first two styles are generally easier, though computationally more expensive.
The last example suggested integrating in the $y$ variable. This could have more explanation.
It has been noted that different symmetries can aid in computing
integrals through their interpretation as areas. For example, if
$f(x)$ is odd, then $\int_{-b}^b f(x)dx=0$ and if $f(x)$ is even,
$\int_{-b}^b f(x) dx = 2\int_0^b f(x) dx$.
Another symmetry of the $x-y$ plane is the reflection through the line
$y=x$. This has the effect of taking the graph of $f(x)$ to the graph
of $f^{-1}(x)$ and vice versa. Here is an example with $f(x) = x^3$
over $[-1,1]$.
```julia; hold=true
f(x) = x^3
xs = range(-1, stop=1, length=50)
ys = f.(xs)
plot(ys, xs)
```
By switching the order of the `xs` and `ys` we "flip" the graph
through the line $x=y$.
We can use this symmetry to our advantage. Suppose instead of being given an equation $y=f(x)$, we are given it in "inverse" style: $x = f(y)$, for example suppose we have $x = y^3$. We can plot this as above via:
```julia; hold=true
ys = range(-1, stop=1, length=50)
xs = [y^3 for y in ys]
plot(xs, ys)
```
Suppose we wanted the area in the first quadrant between this graph,
the $y$ axis and the line $y=1$. What to do? With the problem
"flipped" through the $y=x$ line, this would just be $\int_0^1 x^3dx$.
Rather than mentally flipping the picture to integrate, instead we can just integrate in
the $y$ variable. That is, the area is $\int_0^1 y^3 dy$. The
mental picture for Riemann sums would be have the approximating rectangles laying flat and as
a function of $y$, are given a length of $y^3$ and height of "$dy$".
The figure above suggests that the area under ``f(x)`` over ``[a,b]`` could be represented as the area between the curves ``f^{-1}(y)`` and ``y=b`` from ``[f(a), f(b)]``.
----
For a less trivial problem, consider the area between $x = y^2$ and $x
= 2-y$ in the first quadrant.
```julia; hold=true
ys = range(0, stop=2, length=50)
xs = [y^2 for y in ys]
plot(xs, ys)
xs = [2-y for y in ys]
plot!(xs, ys)
plot!(zero)
```
We see the bounded area could be described in the "$x$" variable in
terms of two integrals, but in the $y$ variable in terms of the
difference of two functions with the limits of integration running
from $y=0$ to $y=1$. So, this area may be found as follows:
```julia; hold=true
f(y) = 2-y
g(y) = y^2
a, b = 0, 1
quadgk(y -> f(y) - g(y), a, b)[1]
```
## Questions
###### Question
Find the area enclosed by the curves $y=2-x^2$ and $y=x^2 - 3$.
Find the area between $f(x) = \cos(x)$, $g(x) = x$ and the $y$ axis.
```julia; hold=true; echo=false
f(x) = cos(x)
g(x) = x
a = 0
b = find_zero(x -> f(x) - g(x), 1)
val, _ = quadgk(x -> f(x) - g(x), a, b)
numericq(val)
```
###### Question
Find the area between the line $y=1/2(x+1)$ and half circle $y=\sqrt{1 - x^2}$.
```julia; hold=true; echo=false
f(x) = sqrt(1 - x^2)
g(x) = 1/2 * (x + 1)
a,b = find_zeros(x -> f(x) - g(x), -1, 1)
val, _ = quadgk(x -> f(x) - g(x), a, b)
numericq(val)
```
###### Question
Find the area in the first quadrant between the lines $y=x$, $y=1$, and the curve $y=x^2 + 4$.
```julia; hold=true; echo=false
f(x) = x
g(x) = 1.0
h(x) = min(f(x), g(x))
j(x) = x^2 / 4
a,b = find_zeros(x -> h(x) - j(x), 0, 3)
val, _ = quadgk(x -> h(x) - j(x), a, b)
numericq(val)
```
###### Question
Find the area between $y=x^2$ and $y=-x^4$ for $\lvert x \rvert \leq 1$.
```julia; hold=true; echo=false
f(x) = x^2
g(x) = -x^4
a,b = -1, 1
val, _ = quadgk(x -> f(x) - g(x), a, b)
numericq(val)
```
###### Question
Let `f(x) = 1/(sqrt(pi)*gamma(1/2)) * (1 + t^2)^(-1)` and `g(x) = 1/sqrt(2*pi) * exp(-x^2/2)`. These graphs intersect in two points. Find the area bounded by them.
```julia; hold=true; echo=false
import SpecialFunctions: gamma
f(x) = 1/(sqrt(pi)*gamma(1/2)) * (1 + x^2)^(-1)
g(x) = 1/sqrt(2*pi) * exp(-x^2/2)
a,b = find_zeros(x -> f(x) - g(x), -3, 3)
val, _ = quadgk(x -> f(x) - g(x), a, b)
numericq(val)
```
(Where `gamma(1/2)` is a call to the [gamma](http://en.wikipedia.org/wiki/Gamma_function) function.)
###### Question
Find the area in the first quadrant bounded by the graph of $x = (y-1)^2$, $x=3-y$ and $x=2\sqrt{y}$. (Hint: integrate in the $y$ variable.)
```julia; hold=true; echo=false
f(y) = (y-1)^2
g(y) = 3 - y
h(y) = 2sqrt(y)
a = 0
b = find_zero(y -> f(y) - g(y), 2)
f1(y) = max(f(y), zero(y))
g1(y) = min(g(y), h(y))
val, _ = quadgk(y -> g1(y) - f1(y), a, b)
numericq(val)
```
###### Question
Find the total area bounded by the lines $x=0$, $x=2$ and the curves $y=x^2$ and $y=x$. This would be $\int_a^b \lvert f(x) - g(x) \rvert dx$.
We used area to estimate weight in this example, but Galileo used weight to estimate area. It is [mentioned](https://www.maa.org/sites/default/files/pdf/cmj_ftp/CMJ/January%202010/3%20Articles/3%20Martin/08-170.pdf) by Martin that in order to estimate the area enclosed by one arch of a cycloid, Galileo cut the arch from from some material and compared the weight to the weight of the generating circle. He concluded the area is close to ``3`` times that of the circle, a conjecture proved by Roberval in 1634.
Formulas from the business world say that revenue is the integral of *marginal revenue* or the additional money from selling 1 more unit. (This is basically the derivative of profit). Cost is the integral of *marginal cost*, or the cost to produce 1 more. Suppose we have
Secant(f, a, b) = f(a) + (f(b)-f(a))/(b-a) * (x - a)
A1 = integrate(Secant(f, a, c) - Secant(f,a,b), (x,a,c)) + integrate(Secant(f,c,b)-Secant(f,a,b), (x, c, b))
A2 = integrate(f(x) - Secant(f,a,b), (x, a, b))
out = 4//3 * A1 - A2
```
Does `SymPy` get the correct output, ``0``, after calling `simplify`?
```julia; hold=true; echo=false
yesnoq(true)
```
###### Question
In [Martin](https://www.maa.org/sites/default/files/pdf/cmj_ftp/CMJ/January%202010/3%20Articles/3%20Martin/08-170.pdf) a fascinating history of the cycloid can be read.
```julia; hold=true;echo=false
imgfile="figures/cycloid-companion-curve.png"
caption = """
Figure from Martin showing the companion curve to the cycloid. As the generating circle rolls, from ``A`` to ``C``, the original point of contact, ``D``, traces out an arch of the cycloid. The companion curve is that found by congruent line segments. In the figure, when ``D`` was at point ``P`` the line segment ``PQ`` is congruent to ``EF`` (on the original position of the generating circle).
"""
ImageFile(:integrals, imgfile, caption)
```
In particular, it can be read that Roberval proved that the area between the cycloid and its companion curve is half the are of the generating circle. Roberval didn't know integration, so finding the area between two curves required other tricks. One is called "Cavalieri's principle." From the figure above, which of the following would you guess this principle to be:
```julia; hold=true; echo=false
choices = ["""
If two regions bounded by parallel lines are such that any parallel between them cuts each region in segments of equal length, then the regions have equal area.
""",
"""
The area of the cycloid is nearly the area of a semi-ellipse with known values, so one can approximate the area of the cycloid with formula for the area of an ellipse
"""]
radioq(choices, 1)
```
Suppose the generating circle has radius ``1``, so the area shown is ``\pi/2``. The companion curve is then ``1-\cos(\theta)`` (a fact not used by Roberval). The area *under* this curve is then
```julia; hold=true
@syms theta
integrate(1 - cos(theta), (theta, 0, SymPy.PI))
```
That means the area under **one-half** arch of the cycloid is
```julia; hold=true; echo=false
choices = ["``\\pi``",
"``(3/2)\\cdot \\pi``",
"``2\\pi``"
]
radioq(choices, 2, keep_order=true)
```
Doubling the answer above gives a value that Galileo had struggled with for many years.
Roberval, avoiding a trignometric 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``.