\section{Volume}{}{}\label{sec:volume}
Now that we have seen how to compute certain areas by using integration; we will now look into how some
volumes may also be computed by evaluating an integral. Generally, the
volumes that we can compute this way have cross-sections that are easy
to describe.

The volume of a general right cylinder, as shown in Figure \ref{fig:cross1} has a simple calculation, it is  equal to base $\times$ height. \hfill\null

\begin{figure}[H]
\centering
\includegraphics[width=0.2\textwidth]{/figures/figcross1_3D}
\caption{The volume of a general right cylinder}
\label{fig:cross1}
\end{figure}

\noindent We can use this fact as the building block in finding volumes of a variety of shapes.

Given an arbitrary solid, we can \textit{approximate} its volume by cutting it into $n$  thin slices. When the slices are thin, each slice can be approximated well by a general right cylinder. Thus the volume of each slice is approximately its cross-sectional area $\times$ thickness. (These slices are the differential elements.)

By orienting a solid along the $x$-axis, we can let $A(x_i)$ represent the cross-sectional area
of the $i\,^\text{th}$ slice, and let $\dx_i$ represent the thickness of this slice (the thickness is a small change in $x$). The total volume of the solid is approximately:
	\begin{align*} \text{Volume} &\approx \sum_{i=1}^n \Big[\text{Area}\ \times\ \text{thickness}\Big] \\
			&= \sum_{i=1}^n A(x_i)\dx_i.
	\end{align*}

Recognize that this is a Riemann Sum. By taking a limit (as the thickness of the slices goes to 0) we can find the volume exactly.

\begin{theorem}{Volume By Cross-Sectional Area}{volume_by_cross_section}
{The volume $V$ of a solid, oriented along the $x$-axis with cross-sectional area $A(x)$ from $x=a$ to $x=b$, is \index{integration!volume!cross-sectional area}
$$V = \int_a^b A(x)\ dx.$$
}
\end{theorem}

\begin{example}{Finding the volume of a solid}{ex_disk0}
{
Find the volume of a pyramid with a square base of side length $ 10 $ cm and a height of $ 5 $ cm.}
\end{example}

\begin{solution}
{There are many ways to ``orient'' the pyramid along the $x$-axis; Figure \ref{fig:disk0} gives one such way, with the pointed top of the pyramid at the origin and the $x$-axis going through the center of the base.

\begin{figure}[H]
\centering
\includegraphics[width=0.35\textwidth]{figures/figcross_area1}
\caption{Orienting a pyramid along the $x$-axis in Example \ref{ex_disk0}.}
\label{fig:disk0}
\end{figure}

Each cross section of the pyramid is a square; this is a sample differential element. To determine its area $A(x)$, we need to determine the side lengths of the square.

When $x=5$, the square has side length $ 10 $; when $x=0$, the square has side length $ 0 $. Since the edges of the pyramid are lines, it is easy to figure that each cross-sectional square sitting above $ x $, has side length $2x$, giving $A(x) = (2x)^2=4x^2$. %

If one were to cut a slice out of the pyramid at $x=3$, as shown in Figure \ref{fig:disk0a}, one would have a shape with square bottom and top with sloped sides. If the slice were thin, both the bottom and top squares would have sides lengths of about 6, and thus the cross--sectional area of the bottom and top would be about 36in$^2$. Letting $\Delta x_i$ represent the thickness of the slice, the volume of this slice would then be about $36\Delta x_i$in$^3$.

\begin{figure}
\centering
\includegraphics[width=0.6\textwidth]{figures/figcross_area1a_3D}
\caption{Cutting a slice in the pyramid in Example \ref{ex_disk0} at $x=3$.}
\label{fig:disk0a}
\end{figure}

Cutting the pyramid into $n$ slices divides the total volume into $n$ equally--spaced smaller pieces, each with volume $(2x_i)^2\Delta x$, where $x_i$ is the approximate location of the slice along the $x$-axis and $\Delta x$ represents the thickness of each slice. One can approximate total volume of the pyramid by summing up the volumes of these slices:
$$\text{Approximate volume } = \sum_{i=1}^n (2x_i)^2\Delta x.$$
Taking the limit as $n\to\infty$ gives the actual volume of the pyramid; recognizing this sum as a Riemann Sum allows us to find the exact answer using a definite integral, matching the definite integral given by Theorem \ref{thm:volume_by_cross_section}.

We have
\begin{align*} V &= \lim_{n\to\infty} \sum_{i=1}^n (2x_i)^2\Delta x\\
							&= \int_0^5 4x^2\ dx\\
				&= \frac43x^3\Big|_0^5 \\
				&=\frac{500}{3}\ \text{cm}^3 \approx 166.67\ \text{cm}^3.
\end{align*}
We can check our work by consulting the general equation for the volume of a pyramid (see the back cover under ``Volume of A General Cone''):

\hfill $\frac13\times \text{area of base}\times \text{height}$.\hfill \null

\noindent Certainly, using this formula from geometry is faster than our new method, but the calculus--based method can be applied to much more than just cones.
}
\end{solution}

\begin{example}{Volume of an Object}{Volume of an Object}
The base of a solid is the region between $\ds f(x)=x^2-1$ and
$\ds g(x)=-x^2+1$, and its cross-sections perpendicular to the $x$-axis
are equilateral triangles, as indicated in
Figure~\xrefn{fig:triangular cross-sections}.
%\texonly
The solid has been truncated to show a triangular
cross-section above $x=1/2$.
%\endtexonly
Find the volume of the solid.
\end{example}

\figure[H]
%\texonly
\centerline{\vbox{\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <3truecm,3truecm>
\setplotarea x from -1.1 to 1.1, y from -1.1 to 1.1
\put {\hbox{\epsfxsize6truecm\epsfbox{images/triangular_solid.eps}}} at 3 0
\axis bottom shiftedto y=0 ticks withvalues {$-1\quad$} {$1$} / at -1 1 / /
\axis left shiftedto x=0 ticks withvalues {$-1\quad$} {$1\quad$} / at -1 1 / /
\plot
-1.000 0.000 -0.900 -0.190 -0.800 -0.360 -0.700 -0.510 -0.600 -0.640
-0.500 -0.750 -0.400 -0.840 -0.300 -0.910 -0.200 -0.960 -0.100 -0.990
0.000 -1.000 0.100 -0.990 0.200 -0.960 0.300 -0.910 0.400 -0.840
0.500 -0.750 0.600 -0.640 0.700 -0.510 0.800 -0.360 0.900 -0.190
1.000 0.000 /
\plot
-1.000 0.000 -0.900 0.190 -0.800 0.360 -0.700 0.510 -0.600 0.640
-0.500 0.750 -0.400 0.840 -0.300 0.910 -0.200 0.960 -0.100 0.990
0.000 1.000 0.100 0.990 0.200 0.960 0.300 0.910 0.400 0.840
0.500 0.750 0.600 0.640 0.700 0.510 0.800 0.360 0.900 0.190
1.000 0.000 /
\endpicture}}
%\begincaption
%{Solid with equilateral triangles as cross-sections.
%(\expandafter\url\expandafter{\liveurl jmol_triangular_x_sections}%
%AP\endurl)}
%%(\expandafter\url\expandafter{\sageurl solid_with_triangular_x-sections}%
%%AP\endurl)}
%\endcaption
%\endtexonly
%\figrdef{fig:triangular cross-sections}
%\htmlfigure{Integration_applications-volume_equilateral_x_sections.html}
%\htmlonly
\caption
{Solid with equilateral triangles as cross-sections.\label{fig:triangular cross-sections}}
%You can download the Sage worksheet
%for this plot and upload it to your own sage account.
%\endcaption
%\endhtmlonly
\endfigure

\begin{solution}
A cross-section taken at say $ x_i$ on the $x$-axis is a triangle with
base $\ds 2(1-x_i^2)$ and height $\ds \sqrt3(1-x_i^2)$, so the area of the
cross-section is
$$
 A(x_i) {1\over2}(\hbox{base})(\hbox{height})=
  (1-x_i^2)\sqrt3(1-x_i^2),
$$
and the volume of a thin ``slab'' is then
$$(1-x_i^2)\sqrt3(1-x_i^2)\Delta x.$$
Thus the total volume is
$$\int_{-1}^1 A(x) \; dx = \int_{-1}^1 \sqrt3(1-x^2)^2\,dx={16\over15}\sqrt3.$$
\vskip-10pt
\end{solution}
%
%
%
%
An important special case of Theorem \ref{thm:volume_by_cross_section} is when the solid is a \textbf{solid of revolution}, that is, when the solid is formed by rotating a shape around some axis of rotation.

Start with a function $y=f(x)$ from $x=a$ to $x=b$. Revolving this curve about a horizontal axis creates a three-dimensional solid whose cross sections are disks (thin circles). Let $R(x)$ represent the radius of the cross-sectional disk at $x$; the area of this disk is $\pi R(x)^2$. Applying Theorem \ref{thm:volume_by_cross_section} gives the Disk Method.

For example, in Figure~\ref{fig:solid of rotation}
we see a plane region under a curve and between two
vertical lines; then the result of rotating this around the $\ds x$-axis, and
a typical circular cross-section is a circle.

\figure[H]
%\texonly
\centerline{
\vbox{\hbox{\hfill\raise53pt\vbox{
\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <5.5truemm,5.5truemm>
\setplotarea x from 0 to 8, y from -4 to 4
\axis left /
\axis bottom shiftedto y=0 /
\putrule from 1 0 to 1 4
\putrule from 6 0 to 6 3
\plot 1 4 1.150 3.546
1.312 3.125 1.475 2.772 1.638 2.484 1.800 2.255 1.962 2.082
2.125 1.961 2.288 1.886 2.450 1.854 2.612 1.859 2.775 1.898
2.938 1.967 3.100 2.060 3.262 2.174 3.425 2.303 3.588 2.444
3.750 2.593 3.912 2.744 4.075 2.894 4.238 3.037 4.400 3.170
4.562 3.289 4.725 3.388 4.888 3.464 5.050 3.511 5.212 3.527
5.375 3.505 5.538 3.443 5.700 3.334 5.862 3.176 6.025 2.964 /
\endpicture}
\quad\epsfxsize3.8cm\epsfbox{images/rotated_surface.eps}
\quad\epsfxsize3.8cm\epsfbox{images/one_disk.eps}
\hfill}\vglue-0pt}}
\caption{\label{fig:solid of rotation} A solid of rotation.}
\endfigure

\begin{formulabox}[The Disk Method \label{idea:disk_method}]
{Let a solid be formed by revolving the curve $y=f(x)$ from $x=a$ to $x=b$ around a horizontal axis, and let $R(x)$ be the radius of the cross-sectional disk at $x$. The volume of the solid is
\index{integration!volume!Disk Method}\index{Disk Method}
$$V = \pi \int_a^b R(x)^2\ dx.$$
}
\end{formulabox}

Of course a real ``slice'' of this figure will not have straight
sides, but we can approximate the volume of the slice by a cylinder or
disk with circular top and bottom and straight sides; the volume of
this disk will have the form $\ds \pi r^2\Delta x$. As long as we can
write $r$ in terms of $x$ we can compute the volume by an integral.

\begin{example}{Finding volume using the Disk Method}{ex_disk1}{
Find the volume of the solid formed by revolving the curve $y=1/x$, from $x=1$ to $x=2$, around the $x$-axis.}
\end{example}

%
\begin{solution}
{A sketch can help us understand this problem. In Figure \ref{fig:disk1}(a) the curve $y=1/x$ is sketched along with the differential element -- a disk -- at $x$ with radius $R(x)=1/x$. In Figure \ref{fig:disk1} (b) the whole solid is pictured, along with the differential element.

The volume of the differential element shown in part (a) of the figure is approximately $\pi R(x_i)^2\Delta x$, where $R(x_i)$ is the radius of the disk shown and $\Delta x$ is the thickness of that slice. The radius $R(x_i)$ is the distance from the $x$-axis to the curve, hence $R(x_i) = 1/x_i$.

\begin{figure}
	\centering
	\begin{subfigure}[t]{0.5\textwidth}
		\includegraphics[width=\textwidth]{figures/figdisk1}
        \label{fig:disk1}
        \caption{}
    \end{subfigure}%
    \begin{subfigure}[t]{0.5\textwidth}
    \includegraphics[width=\textwidth]{figures/figdisk1b}
        \label{fig:disk1b}
        \caption{}
    \end{subfigure}
    \caption{Sketching a solid in Example \ref{exa:ex_disk1} \label{fig:disk}}
\end{figure}

%\mtable{.5}{Sketching a solid in Example \ref{ex_disk1}.}{fig:disk1}{%
%		\begin{tabular}{c} %
%		\myincludegraphicsthree{width=110pt,3Dmenu,activate=onclick,deactivate=onclick,
%3Droll=126.0060482236849,
%3Dortho=0.004875946324318647,
%3Dc2c=0.44462037086486816 0.39410829544067383 0.8043577671051025,
%3Dcoo=67.21983337402344 3.5258262157440186 -29.566415786743164,
%3Droo=150.00000160608386,
%3Dlights=Headlamp,add3Djscript=asylabels.js}{}{figures/figdisk1}\\
%%\myincludegraphics{figures/figdisk1} \\
%		(a) \\
%		\myincludegraphicsthree{width=110pt,3Dmenu,activate=onclick,deactivate=onclick,
%3Droll=126.0060482236849,
%3Dortho=0.004875946324318647,
%3Dc2c=0.44462037086486816 0.39410829544067383 0.8043577671051025,
%3Dcoo=67.21983337402344 3.5258262157440186 -29.566415786743164,
%3Droo=150.00000160608386,
%3Dlights=Headlamp,add3Djscript=asylabels.js}{}{figures/figdisk1b}\\
%%\myincludegraphics{figures/figdisk1b} \\
%		(b)
%		\end{tabular}
%}
Slicing the solid into $n$ equally--spaced slices, we can approximate the total volume by adding up the approximate volume of each slice:
$$\text{Approximate volume } = \sum_{i=1}^n \pi \left(\frac1{x_i}\right)^2\Delta x.$$

Taking the limit of the above sum as $n\to\infty$ gives the actual volume; recognizing this sum as a Riemann sum allows us to evaluate the limit with a definite integral, which matches the formula given in Key Idea \ref{idea:disk_method}:

%Using Key Idea \ref{idea:disk_method}, we have
\begin{align*}
	V = \lim_{n\to\infty}\sum_{i=1}^n \pi \left(\frac1{x_i}\right)^2\Delta x
		= \pi\int_1^2 \left(\frac1x\right)^2\ dx
		= \pi\int_1^2 \frac1{x^2}\ dx
		&= \pi\left[-\frac1x\right]\Big|_1^2 \\
		&= \pi \left[-\frac12 - \left(-1\right)\right] \\
		&= \frac{\pi}{2}\ \text{units}^3.
\end{align*}

\vskip-\baselineskip
}
\end{solution}

\textbf{Note:} While Key Idea \ref{idea:disk_method} is given in terms of functions of $x$, the principle involved can be applied to functions of $y$ when the axis of rotation is vertical, not horizontal. We demonstrate this in the next example.\\

\clearpage

\begin{example}{Finding volume using the Disk Method}{ex_disk2}{
	Find the volume of the solid formed by revolving the curve $\ds y=\frac1x$, from $x=1$ to $x=2$, about the $y$-axis.}
\end{example}

\begin{solution}
{Since the axis of rotation is vertical, we need to convert the function into a function of $y$ and convert the $x$-bounds to $y$-bounds. Since $y=1/x$ defines the curve, we rewrite it as $x=1/y$. The bound $x=1$ corresponds to the $y$-bound $y=1$, and the bound $x=2$ corresponds to the $y$-bound $y=1/2$.

Thus we are rotating the curve $x=1/y$, from $y=1/2$ to $y=1$ about the $y$-axis to form a solid. The curve and sample differential element are sketched in Figure \ref{fig:disk2} (a), with a full sketch of the solid in Figure \ref{fig:disk2} (b).

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.5\textwidth}
		\includegraphics[width=\textwidth]{figures/figdisk1a}
		\label{fig:disk1a}
		\caption{}
	\end{subfigure}%
	~
	\begin{subfigure}[t]{0.5\textwidth}
		\includegraphics[width=\textwidth]{figures/figdisk2a}
		\label{fig:disk2a}
		\caption{}
	\end{subfigure}
	\caption{Sketching a solid in Example \ref{exa:ex_disk2}. \label{fig:disk2}}
\end{figure}

	We integrate to find the volume:
\[
V = \pi\int_{1/2}^1 \frac{1}{y^2}\ dy  = -\frac{\pi}y\Big|_{1/2}^1  = \pi\ \text{units}^3.
\]
}

\end{solution}

\begin{example}{Volume of a Right Circular Cone}{Volume of a Right Circular Cone}\label{Volume of a Right Circular Cone}
Find the volume of a right circular cone with base radius 10 and
height 20. (A right circular cone is one with a circular base and with
the tip of the cone directly over the center of the base.)
\end{example}

\begin{solution}
We can view this cone as produced by the rotation of the line $y=x/2$ between $ x=0 $ and $ x= 20 $ rotated about the $x$-axis, as indicated in
figure~\ref{fig:line to cone}.

% rotated around the y-axis
%\begin{tikzpicture}
% \begin{axis}[view={60}{30}]
%  \addplot3[surf,shader=flat,
%  samples=20,
%  domain=1:2,y domain=0:2*pi,
%  z buffer=sort]
%  ({x * cos(deg(y))}, {x * sin(deg(y))}, {1/x});
% \end{axis}
%\end{tikzpicture}

% rotated around the x-axis

\begin{figure}[H]
\begin{tikzpicture}[scale=.7,/pgf/declare function={f=(x/2);}]
\begin{axis}[
        xmin=0,xmax=20,ymin=0,ymax=10,
    domain=0:20,
    samples=10,
    y=0.4cm,
    x=0.4cm,
    axis lines=middle
]
\addplot [thick, blue, ,name path=A] {f}  node[midway, above, sloped] {$f(x) =x/2$};
 \addplot [draw=none,name path=B] {0};     % “fictional” curve
  \addplot [blue!50] fill between[of = A and B,soft clip={domain=0:20}]; % filling
  %\draw (axis cs:6,6) node { $\ds y=\frac{x}{2}$};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
 \begin{axis}[view={30}{10}, colormap={custom}{color(0)=(blue!20) color(1)=(blue)}]
  \addplot3[surf,shader=flat,
  samples=20,
  domain=1:20,y domain=0:2*pi,
  z buffer=sort]
  (x,{(x/2) * cos(deg(y))}, {(x/2) * sin(deg(y))});
 \end{axis}
\end{tikzpicture}
\caption{\label{fig:line to cone}
A region that generates a cone; approximating the volume
by circular disks.}
\end{figure}

%\figure[H]
%%\texonly
%\centerline{\vbox{\beginpicture
%\normalgraphs
%%\ninepoint
%\setcoordinatesystem units <0.25truecm,0.25truecm>
%\setplotarea x from 0 to 20, y from 0 to 10
%\axis bottom shiftedto y=0 ticks withvalues {$0$} {$20$} / at 0 20 / /
%\put {\hbox{\epsfxsize6cm\epsfbox{images/cone.eps}}} at 40 5
%\plot 0 0 20 10 /
%\putrule from 20 0 to 20 10
%\endpicture}}
%\caption{\label{fig:line to cone}
%A region that generates a cone; approximating the volume
%by circular disks.}
%%(\expandafter\url\expandafter{\liveurl cone.html}%
%%AP\endurl)
%%\endcaption
%%\endtexonly
%%\figrdef{fig:line to cone}
%%\htmlfigure{Integration_applications-volume_of_cone.html}
%%\begincaption
%%Approximating the volume of a cone
%%by circular disks.
%%\endcaption
%\endfigure

At a particular point on the $x$-axis, say $\ds x_i$, the radius of the
resulting cone is the $y$-coordinate of the corresponding point on the
line, namely $\ds y_i=x_i/2$. Thus the total volume is approximately
$$\sum_{i=0}^{n-1} \pi (x_i/2)^2\,dx$$
and the exact volume is
$$
  \int_0^{20} \pi
  {x^2\over4}\,dx={\pi\over4}{20^3\over3}={2000\pi\over3}.
$$
Note that we can instead do the calculation with a generic height and
radius:
$$
  \int_0^{h} \pi{r^2\over h^2}x^2\,dx
  ={\pi r^2\over h^2}{h^3\over3}={\pi r^2h\over3},
$$
giving us the usual formula for the volume of a cone.
\end{solution}

%
We can also compute the volume of solids of revolution that have a hole in the centre. The general principle is simple: compute the volume of the solid irrespective of the hole, then subtract the volume of the hole. If the outside radius of the solid is $R(x)$ and the inside radius (defining the hole) is $r(x)$, then the volume is
$$V = \pi\int_a^b R(x)^2 \ dx - \pi\int_a^b r(x)^2\ dx = \pi\int_a^b \left(R(x)^2-r(x)^2\right)\ dx.$$

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.5\textwidth}
		\includegraphics[width=\textwidth]{figures/figwasher_idea_a}
		%\label{fig:disk1a}
		\caption{}
	\end{subfigure}%
	~
	\begin{subfigure}[t]{0.5\textwidth}
		\includegraphics[width=\textwidth]{figures/figwasher_idea_b}
		%\label{fig:disk2a}
		\caption{}
	\end{subfigure}
	\caption{Establishing the Washer Method; see also Figure \ref{fig:washeridea_b}. \label{fig:washeridea}}
\end{figure}

One can generate a solid of revolution with a hole in the middle by revolving a region about an axis. Consider Figure \ref{fig:washeridea}(a), where a region is sketched along with a dashed, horizontal axis of rotation. By rotating the region about the axis, a solid is formed as sketched in Figure \ref{fig:washeridea}(b). The outside of the solid has radius $R(x)$, whereas the inside has radius $r(x)$. Each cross section of this solid will be a washer (a disk with a hole in the center) as sketched in Figure \ref{fig:washeridea_b}.	This leads us to the Washer Method.

\begin{figure}[H]
\centering
\includegraphics[width=0.65\textwidth]{figures/figwasher_idea_c}
\caption{Establishing the Washer Method; see also Figure \ref{fig:washeridea}.}
\label{fig:washeridea_b}
\end{figure}

%\mfigurethree{width=125pt,3Dmenu,activate=onclick,deactivate=onclick,
%3Droll=96.94265756936434,
%3Dortho=0.005309578496962786,
%3Dc2c=0.547386884689331 0.07732175290584564 0.833299994468689,
%3Dcoo=64.78053283691406 10.129053115844727 -47.566162109375,
%3Droo=149.99999789136484,
%3Dlights=Headlamp,add3Djscript=asylabels.js}{}{.8}{Establishing the Washer Method; see also Figure \ref{fig:washeridea}.}{fig:washeridea_b}{figures/figwasher_idea_c}

\begin{formulabox}[The Washer Method \label{idea:washermethod} ]
{Let a region bounded by $y=f(x)$, $y=g(x)$, $x=a$ and $x=b$ be rotated about a horizontal axis that does not intersect the region, forming a solid. Each cross section at $x$ will be a washer with outside radius $R(x)$ and inside radius $r(x)$. The volume of the solid is
\index{integration!volume!Washer Method}\index{Washer Method}
$$V = \pi\int_a^b \Big(R(x)^2-r(x)^2\Big)\ dx.$$
}
\end{formulabox}

%
Even though we introduced it first, the Disk Method is just a special case of the Washer Method with an inside radius of $r(x)=0$.		\\

\begin{example}{Volumes with the Washer Method}{Volume of an Object with a Hole}\label{Volume of an Object with a Hole}
Find the volume of the object generated when the area between
$\ds y=x^2$ and $y=x$ is rotated around the $x$-axis.
\end{example}

\begin{solution}
 We begin with a sketch. In figure~\ref{fig:solid with hole} we show the region
that is rotated, the resulting solid with the front half cut away,
the cone that forms the outer surface, the
horn-shaped hole, and a cross-section perpendicular to the $x$-axis.

\figure[H]
%\texonly
\centerline{\vbox{\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <3truecm,3truecm>
\setplotarea x from 0 to 1.1, y from 0 to 1.1
\axis bottom shiftedto y=0 ticks withvalues {$0$} {$1$} / at 0 1 / /
\axis left shiftedto x=0 ticks withvalues {$0$} {$1$} / at 0 1 / /
\put {\hbox{\epsfxsize3cm\epsfbox{images/cutaway_horn.eps}}} at 3 0
\put {\hbox{\epsfxsize3cm\epsfbox{images/outer_cone.eps}}} at 0 -2
\put {\hbox{\epsfxsize3cm\epsfbox{images/horn.eps}}} at 2 -2
\put {\hbox{\epsfxsize3cm\epsfbox{images/washer_section.eps}}} at 4 -2
\plot 0 0 1 1 /
\setquadratic
\plot
0.000 0.000 0.100 0.010 0.200 0.040 0.300 0.090 0.400 0.160
0.500 0.250 0.600 0.360 0.700 0.490 0.800 0.640 0.900 0.810
1.000 1.000 /
\endpicture}}
\caption{\label{fig:solid with hole}
Solid with a hole, showing the outer cone and the shape to
be removed to form the hole.}
%(\expandafter\url\expandafter{\liveurl solid_with_hole.html}%
%AP\endurl)
%\endcaption
%\endtexonly
%\figrdef{fig:solid with hole}
%\htmlfigure{Integration_applications-volume_with_hole.html}
%\htmlonly
%\begincaption
%Solid with a hole. You can download the Sage
%worksheet
%for this plot and upload it to your own sage account.
%\endcaption
%\endhtmlonly
\endfigure

We can approximate the volume of a slice of the solid with a washer-shaped volume, as indicated in
Figure~\ref{fig:solid with hole}.

The thickness is $dx$, while the area of
the face is the area of the outer circle minus the area of the inner
circle, say $\ds \pi R(x)^2-\pi r(x)^2$, or $\pi(\text{TOP})^2-\pi(\text{BOTTOM})^2$. In the present example,  $\ds R(x)= x$ and $r(x) =  x^2$. Hence, the whole volume is
$$
  \int_0^1 \pi\left(R(x)^2-r(x)^2\right)\,dx=
  \int_0^1 \pi x^2-\pi x^4\,dx=
  \left.\pi\left({x^3\over3}-{x^5\over5}\right)\right|_0^1=
  \pi\left({1\over3}-{1\over5}\right)={2\pi\over15}.
$$
\end{solution}

\begin{example}{Finding volume with the Washer Method}{ex_wash1}{
Find the volume of the solid formed by rotating the region bounded by $y=x^2-2x+2$ and $y=2x-1$ about the $x$-axis.}
\end{example}

\begin{solution}
{A sketch of the region will help, as given in Figure \ref{fig:wash1}(a).
%\mfigurethree{width=125pt,3Dmenu,activate=onclick,deactivate=onclick,
%3Droll=97.32968340849395,
%3Dortho=0.004881054162979126,
%3Dc2c=0.43117064237594604 0.05905330181121826 0.9003357887268066,
%3Dcoo=64.78053283691406 10.12905216217041 -47.566158294677734,
%3Droo=149.99999769784193,
%3Dlights=Headlamp,add3Djscript=asylabels.js}{}{.22}{A sketch of the region used in Example \ref{ex_wash1}.}{fig:wash1a}{figures/figwash1}
%\mfigure{.22}{A sketch of the region used in Example \ref{ex_wash1}.}{fig:wash1a}{figures/figwash1}
Rotating about the $x$-axis will produce cross sections in the shape of washers, as shown in Figure \ref{fig:wash1}(b); the complete solid is shown in part (c). The outside radius of this washer is $R(x) = 2x+1$; the inside radius is $r(x) = x^2-2x+2$. As the region is bounded from $x=1$ to $x=3$, we integrate as follows to compute the volume.
\begin{align*}
V &= \pi\int_1^3 \Big((2x-1)^2-(x^2-2x+2)^2\Big)\ dx \\
		&= \pi\int_1^3 \big(-x^4+4x^3-4x^2+4x-3\big)\ dx \\
		&= \pi\Big[-\frac{1}{5}x^5+x^4-\frac43x^3+2x^2-3x\Big]\Big|_1^3 \\
		&=\frac{104}{15}\pi \approx 21.78\ \text{units}^3.
\end{align*}
\vskip-1.5\baselineskip

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.33\textwidth}
		\includegraphics[width=\textwidth]{figures/figwash1}
		\label{fig:figwash1a}
		\caption{}
	\end{subfigure}%
	~
	\begin{subfigure}[t]{0.33\textwidth}
		\includegraphics[width=\textwidth]{figures/figwash1c}
		\label{fig:figwash1b}
		\caption{}
	\end{subfigure}%
		~
		\begin{subfigure}[t]{0.33\textwidth}
			\includegraphics[width=\textwidth]{figures/figwash1b}
			\label{fig:figwash1c}
			\caption{}
		\end{subfigure}
	\caption{Sketching the differential element and solid in Example \ref{exa:ex_wash1}. \label{fig:wash1}}
\end{figure}

%\mtable{.37}{Sketching the differential element and solid in Example \ref{ex_wash1}.}{fig:wash1}{%
%\begin{tabular}{c}
%\myincludegraphicsthree{width=125pt,3Dmenu,activate=onclick,deactivate=onclick,
%3Droll=97.32968340849395,
%3Dortho=0.004881054162979126,
%3Dc2c=0.43117064237594604 0.05905330181121826 0.9003357887268066,
%3Dcoo=64.78053283691406 10.12905216217041 -47.566158294677734,
%3Droo=149.99999769784193,
%3Dlights=Headlamp,add3Djscript=asylabels.js}{}{figures/figwash1}\\
%%\myincludegraphics{figures/figwash1c}\\
%(a)\\
%\myincludegraphicsthree{width=125pt,3Dmenu,activate=onclick,deactivate=onclick,
%3Droll=97.32968340849395,
%3Dortho=0.004881054162979126,
%3Dc2c=0.43117064237594604 0.05905330181121826 0.9003357887268066,
%3Dcoo=64.78053283691406 10.12905216217041 -47.566158294677734,
%3Droo=149.99999769784193,
%3Dlights=Headlamp,add3Djscript=asylabels.js}{}{figures/figwash1c}\\
%%\myincludegraphics{figures/figwash1c}\\
%(b)\\
%\myincludegraphicsthree{width=125pt,3Dmenu,activate=onclick,deactivate=onclick,
%3Droll=97.32968340849395,
%3Dortho=0.004881054162979126,
%3Dc2c=0.43117064237594604 0.05905330181121826 0.9003357887268066,
%3Dcoo=64.78053283691406 10.12905216217041 -47.566158294677734,
%3Droo=149.99999769784193,
%3Dlights=Headlamp,add3Djscript=asylabels.js}{}{figures/figwash1b}\\
%%\myincludegraphics{figures/figwash1b}\\
%(c)
%\end{tabular}
%}
}
\end{solution}

When rotating about a vertical axis, the outside and inside radius functions must be functions of $y$.\\

\begin{example}{Finding volume with the Washer Method}{ex_wash2}{
Find the volume of the solid formed by rotating the triangular region with vertices at $(1,1)$, $(2,1)$ and $(2,3)$ about the $y$-axis.}
\end{example}

\begin{solution}
{The triangular region is sketched in Figure \ref{fig:wash2}(a); the differential element is sketched in (b) and the full solid is drawn in (c). They help us establish the outside and inside radii. Since the axis of rotation is vertical, each radius is a function of $y$.

The outside radius $R(y)$ is formed by the line connecting $(2,1)$ and $(2,3)$; it is a constant function, as regardless of the $y$-value the distance from the line to the axis of rotation is 2. Thus $R(y)=2$.

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.33\textwidth}
		\includegraphics[width=\textwidth]{figures/figwash2a}
		\label{fig:figwash2a}
		\caption{}
	\end{subfigure}%
	~
	\begin{subfigure}[t]{0.33\textwidth}
		\includegraphics[width=\textwidth]{figures/figwash2b}
		\label{fig:figwash2b}
		\caption{}
	\end{subfigure}%
		~
		\begin{subfigure}[t]{0.33\textwidth}
			\includegraphics[width=\textwidth]{figures/figwash2c}
			\label{fig:figwash2c}
			\caption{}
		\end{subfigure}
	\caption{Sketching the solid in Example \ref{ex_wash2}. \label{fig:wash2}}
\end{figure}

%\mtable{.63}{Sketching the solid in Example \ref{ex_wash2}.}{fig:wash2}{%
%\begin{tabular}{c}
%\myincludegraphicsthree{width=125pt,3Dmenu,activate=onclick,deactivate=onclick,
%3Droll=123.42078064170005,
%3Dortho=0.00488104997202754,
%3Dc2c=0.41855040192604065 0.31316813826560974 0.8524911999702454,
%3Dcoo=-7.115118026733398 66.85215759277344 -16.372848510742188,
%3Droo=149.99999819286032,
%3Dlights=Headlamp,add3Djscript=asylabels.js}{}{figures/figwash2a}\\
%%\myincludegraphics{figures/figwash2a} \\
%(a) \\
%\myincludegraphicsthree{width=125pt,3Dmenu,activate=onclick,deactivate=onclick,
%3Droll=123.42078064170005,
%3Dortho=0.00488104997202754,
%3Dc2c=0.41855040192604065 0.31316813826560974 0.8524911999702454,
%3Dcoo=-7.115118026733398 66.85215759277344 -16.372848510742188,
%3Droo=149.99999819286032,
%3Dlights=Headlamp,add3Djscript=asylabels.js}{}{figures/figwash2b}\\
%%\myincludegraphics{figures/figwash2b} \\
%(b) %\\
%\myincludegraphicsthree{width=125pt,3Dmenu,activate=onclick,deactivate=onclick,
%3Droll=123.42078064170005,
%3Dortho=0.00488104997202754,
%3Dc2c=0.41855040192604065 0.31316813826560974 0.8524911999702454,
%3Dcoo=-7.115118026733398 66.85215759277344 -16.372848510742188,
%3Droo=149.99999819286032,
%3Dlights=Headlamp,add3Djscript=asylabels.js}{}{figures/figwash2c}\\
%%\myincludegraphics{figures/figwash2c} \\
%(c)
%\end{tabular}
%}

The inside radius is formed by the line connecting $(1,1)$ and $(2,3)$. The equation of this line is $y=2x-1$, but we need to refer to it as a function of $y$. Solving for $x$ gives $r(y) = \frac12(y+1)$.

We integrate over the $y$-bounds of $y=1$ to $y=3$. Thus the volume is
\begin{align*}
V 	&=	\pi\int_1^3\Big(2^2 - \big(\frac12(y+1)\big)^2\Big)\ dy \\
		&=	\pi\int_1^3\Big(-\frac14y^2-\frac12y+\frac{15}4\Big)\ dy \\
		&= 	\pi\Big[-\frac1{12}y^3-\frac14y^2+\frac{15}4y\Big]\Big|_1^3\\
		&= \frac{10}3\pi \approx 10.47\ \text{units}^3.
\end{align*}
%\mfigurethree{width=125pt,3Dmenu,activate=onclick,deactivate=onclick,
%3Droll=123.42078064170005,
%3Dortho=0.00488104997202754,
%3Dc2c=0.41855040192604065 0.31316813826560974 0.8524911999702454,
%3Dcoo=-7.115118026733398 66.85215759277344 -16.372848510742188,
%3Droo=149.99999819286032,
%3Dlights=Headlamp,add3Djscript=asylabels.js}{}{.8}{Sketching the solid in Example \ref{ex_wash2}.}{fig:wash2b}{figures/figwash2c}\\
%%\myincludegraphics{figures/figwash2c}
}
\end{solution}

%
This section introduced a new application of the definite integral. Our default view of the definite integral is that it gives ``the area under the curve.'' However, we can establish definite integrals that represent other quantities; in this section, we computed volume.

The ultimate goal of this section is not to compute volumes of solids. That can be useful, but what is more useful is the understanding of this basic principle of integral calculus: to find the exact value of some quantity,
\begin{itemize}
	\item we start with an approximation (in this section, slice the solid and approximate the volume of each slice),
	\item then make the approximation better by refining our original approximation (i.e., use more slices),
	\item	then use limits to establish a definite integral which gives the exact value.
\end{itemize}
%
%We practice this principle in the next section where we find volumes by slicing solids in a different way.

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %

%\figure[H]
%%\texonly
%\vbox{\beginpicture
%\normalgraphs
%%\ninepoint
%\setcoordinatesystem units <0.3truecm,0.3truecm>
%\setplotarea x from -10 to 10, y from -3 to 20
%\axis bottom shiftedto y=0 ticks withvalues {$x_i$} / at 7 / /
%\put {\hbox{\epsfxsize9truecm\epsfbox{images/pyramid_steps.eps}}} at 30 10
%\put {$y_i\rightarrow$} [l] <-5pt,-1truept> at -10 6
%\plot -10 0 0 20 10 0 /
%\putrule from -10 0 to -10 1
%\putrule from -10 1 to 10 1
%\putrule from 10 1 to 10 0
%\putrule from -9.5 1 to -9.5 2
%\putrule from -9.5 2 to 9.5 2
%\putrule from 9.5 2 to 9.5 1
%\putrule from -9 2 to -9 3
%\putrule from -9 3 to 9 3
%\putrule from 9 3 to 9 2
%\putrule from -8.5 3 to -8.5 4
%\putrule from -8.5 4 to 8.5 4
%\putrule from 8.5 4 to 8.5 3
%\putrule from -8 4 to -8 5
%\putrule from -8 5 to 8 5
%\putrule from 8 5 to 8 4
%\putrule from -7.5 5 to -7.5 6
%\putrule from -7.5 6 to 7.5 6
%\putrule from 7.5 6 to 7.5 5
%\putrule from -7 6 to -7 7
%\putrule from -7 7 to 7 7
%\putrule from 7 7 to 7 6
%\putrule from -6.5 7 to -6.5 8
%\putrule from -6.5 8 to 6.5 8
%\putrule from 6.5 8 to 6.5 7
%\putrule from -6 8 to -6 9
%\putrule from -6 9 to 6 9
%\putrule from 6 9 to 6 8
%\put {$\vdots$} at 0 13
%\endpicture}
%%\caption{
%%Volume of a pyramid approximated by rectangular prisms.
%%(\expandafter\url\expandafter{\liveurl pyramid.html}%
%%AP\endurl)
%%\endcaption
%%\endtexonly
%%\figrdef{fig:pyramid}
%%\htmlfigure{Integration_applications-volume_pyramid.html}
%%\htmlonly
%\caption{\label{fig:pyramid}
%Volume of a pyramid approximated by rectangular prisms.}
%%\endhtmlonly
%\endfigure
%
%\begin{example}{Volume of a Pyramid}{Volume of a Pyramid}\label{Volume of a Pyramid}
%Find the volume of a pyramid with a square base that is 20 meters tall
%and 20 meters on a side at the base.
%\end{example}
%
%\begin{solution}
%As with most of our applications
%of integration, we begin by asking how we might approximate the
%volume. Since we can easily compute the volume of a rectangular prism
%(that is, a ``box''), we will use some boxes to approximate the volume of
%the pyramid, as shown in figure~\ref{fig:pyramid}: On the left is a
%cross-sectional view, on the right is a 3D view of part of the pyramid
%with some of the boxes used to approximate the volume.
%
%Each box has volume of the form $\ds (2x_i)(2x_i)\Delta y$. Unfortunately,
%there are two variables here; fortunately, we can write $x$ in terms
%of $y$: From the cross-sectional view we see that a height of 20 is achieved at the midpoint of the base. We will also position the cross-sectional view symmetrically about the $y$-axis. Thus at $x=0$, $y=20$, and we have a slope of $m=-2$. So
%\begin{align*}
%y&=-2x+b	\\
%20&=-2(0)+b	\\
%20&=b.
%\end{align*}
%
%Therefore, $y=20-2x$, and in the terms of $x$: $x=10-y/2$ or $\ds x_i=10-y_i/2$. Then the total volume is
%approximately
%$$\sum_{i=0}^{n-1} 4(10-y_i/2)^2\Delta y$$
%and in the limit we get the volume as the value of an integral:
%$$
%  \int_0^{20} 4(10-y/2)^2\,dy=\int_0^{20} (20-y)^2\,dy=
%  \left.-{(20-y)^3\over3}\right|_0^{20}=
%  -{0^3\over3}--{20^3\over3}={8000\over3}.
%$$
%As you may know, the volume of a pyramid is
%$(1/3)(\hbox{height})(\hbox{area of base})=(1/3)(20)(400)$, which
%agrees with our answer.
%\end{solution}
%
%\begin{example}{Volume of an Object}{Volume of an Object}
%The base of a solid is the region between $\ds f(x)=x^2-1$ and
%$\ds g(x)=-x^2+1$, and its cross-sections perpendicular to the $x$-axis
%are equilateral triangles, as indicated in
%Figure~\xrefn{fig:triangular cross-sections}.
%%\texonly
%The solid has been truncated to show a triangular
%cross-section above $x=1/2$.
%%\endtexonly
%Find the volume of the solid.
%\end{example}
%
%\figure[H]
%%\texonly
%\centerline{\vbox{\beginpicture
%\normalgraphs
%%\ninepoint
%\setcoordinatesystem units <3truecm,3truecm>
%\setplotarea x from -1.1 to 1.1, y from -1.1 to 1.1
%\put {\hbox{\epsfxsize6truecm\epsfbox{images/triangular_solid.eps}}} at 3 0
%\axis bottom shiftedto y=0 ticks withvalues {$-1\quad$} {$1$} / at -1 1 / /
%\axis left shiftedto x=0 ticks withvalues {$-1\quad$} {$1\quad$} / at -1 1 / /
%\plot
%-1.000 0.000 -0.900 -0.190 -0.800 -0.360 -0.700 -0.510 -0.600 -0.640
%-0.500 -0.750 -0.400 -0.840 -0.300 -0.910 -0.200 -0.960 -0.100 -0.990
%0.000 -1.000 0.100 -0.990 0.200 -0.960 0.300 -0.910 0.400 -0.840
%0.500 -0.750 0.600 -0.640 0.700 -0.510 0.800 -0.360 0.900 -0.190
%1.000 0.000 /
%\plot
%-1.000 0.000 -0.900 0.190 -0.800 0.360 -0.700 0.510 -0.600 0.640
%-0.500 0.750 -0.400 0.840 -0.300 0.910 -0.200 0.960 -0.100 0.990
%0.000 1.000 0.100 0.990 0.200 0.960 0.300 0.910 0.400 0.840
%0.500 0.750 0.600 0.640 0.700 0.510 0.800 0.360 0.900 0.190
%1.000 0.000 /
%\endpicture}}
%%\begincaption
%%{Solid with equilateral triangles as cross-sections.
%%(\expandafter\url\expandafter{\liveurl jmol_triangular_x_sections}%
%%AP\endurl)}
%%%(\expandafter\url\expandafter{\sageurl solid_with_triangular_x-sections}%
%%%AP\endurl)}
%%\endcaption
%%\endtexonly
%%\figrdef{fig:triangular cross-sections}
%%\htmlfigure{Integration_applications-volume_equilateral_x_sections.html}
%%\htmlonly
%\caption
%{Solid with equilateral triangles as cross-sections.\label{fig:triangular cross-sections}}
%%You can download the Sage worksheet
%%for this plot and upload it to your own sage account.
%%\endcaption
%%\endhtmlonly
%\endfigure
%
%\begin{solution}
%A cross-section at a value $\ds x_i$ on the $x$-axis is a triangle with
%base $\ds 2(1-x_i^2)$ and height $\ds \sqrt3(1-x_i^2)$, so the area of the
%cross-section is
%$$
%  {1\over2}(\hbox{base})(\hbox{height})=
%  (1-x_i^2)\sqrt3(1-x_i^2),
%$$
%and the volume of a thin ``slab'' is then
%$$(1-x_i^2)\sqrt3(1-x_i^2)\Delta x.$$
%Thus the total volume is
%$$\int_{-1}^1 \sqrt3(1-x^2)^2\,dx={16\over15}\sqrt3.$$
%\vskip-10pt
%\end{solution}
%
%One easy way to get ``nice'' cross-sections is by rotating a plane
%figure around a line. For example, in Figure~\ref{fig:solid of rotation}
%we see a plane region under a curve and between two
%vertical lines; then the result of rotating this around the $\ds x$-axis, and
%a typical circular cross-section is a circle.
%
%\figure[H]
%%\texonly
%\centerline{
%\vbox{\hbox{\hfill\raise53pt\vbox{
%\beginpicture
%\normalgraphs
%%\ninepoint
%\setcoordinatesystem units <5.5truemm,5.5truemm>
%\setplotarea x from 0 to 8, y from -4 to 4
%\axis left /
%\axis bottom shiftedto y=0 /
%\putrule from 1 0 to 1 4
%\putrule from 6 0 to 6 3
%\plot 1 4 1.150 3.546
%1.312 3.125 1.475 2.772 1.638 2.484 1.800 2.255 1.962 2.082
%2.125 1.961 2.288 1.886 2.450 1.854 2.612 1.859 2.775 1.898
%2.938 1.967 3.100 2.060 3.262 2.174 3.425 2.303 3.588 2.444
%3.750 2.593 3.912 2.744 4.075 2.894 4.238 3.037 4.400 3.170
%4.562 3.289 4.725 3.388 4.888 3.464 5.050 3.511 5.212 3.527
%5.375 3.505 5.538 3.443 5.700 3.334 5.862 3.176 6.025 2.964 /
%\endpicture}
%\quad\epsfxsize3.8cm\epsfbox{images/rotated_surface.eps}
%\quad\epsfxsize3.8cm\epsfbox{images/one_disk.eps}
%\hfill}\vglue-0pt}}
%\caption{\label{fig:solid of rotation} A solid of rotation.}
%\endfigure
%
%Of course a real ``slice'' of this figure will not have straight
%sides, but we can approximate the volume of the slice by a cylinder or
%disk with circular top and bottom and straight sides; the volume of
%this disk will have the form $\ds \pi r^2\Delta x$. As long as we can
%write $r$ in terms of $x$ we can compute the volume by an integral.
%
%%\begin{example}{Volume of a Right Circular Cone}{Volume of a Right Circular Cone}\label{Volume of a Right Circular Cone}
%%Find the volume of a right circular cone with base radius 10 and
%%height 20. (A right circular cone is one with a circular base and with
%%the tip of the cone directly over the center of the base.)
%%\end{example}
%%
%%\begin{solution}
%%We can view this cone as produced by the rotation of the line
%%$y=x/2$ rotated about the $x$-axis, as indicated in
%%figure~\ref{fig:line to cone}.
%%
%%\figure[H]
%%%\texonly
%%\centerline{\vbox{\beginpicture
%%\normalgraphs
%%%\ninepoint
%%\setcoordinatesystem units <0.25truecm,0.25truecm>
%%\setplotarea x from 0 to 20, y from 0 to 10
%%\axis bottom shiftedto y=0 ticks withvalues {$0$} {$20$} / at 0 20 / /
%%\put {\hbox{\epsfxsize6cm\epsfbox{images/cone.eps}}} at 40 5
%%\plot 0 0 20 10 /
%%\putrule from 20 0 to 20 10
%%\endpicture}}
%%\caption{\label{fig:line to cone}
%%A region that generates a cone; approximating the volume
%%by circular disks.}
%%%(\expandafter\url\expandafter{\liveurl cone.html}%
%%%AP\endurl)
%%%\endcaption
%%%\endtexonly
%%%\figrdef{fig:line to cone}
%%%\htmlfigure{Integration_applications-volume_of_cone.html}
%%%\begincaption
%%%Approximating the volume of a cone
%%%by circular disks.
%%%\endcaption
%%\endfigure
%%
%%At a particular point on the $x$-axis, say $\ds x_i$, the radius of the
%%resulting cone is the $y$-coordinate of the corresponding point on the
%%line, namely $\ds y_i=x_i/2$. Thus the total volume is approximately
%%$$\sum_{i=0}^{n-1} \pi (x_i/2)^2\,dx$$
%%and the exact volume is
%%$$
%%  \int_0^{20} \pi
%%  {x^2\over4}\,dx={\pi\over4}{20^3\over3}={2000\pi\over3}.
%%$$
%%Note that we can instead do the calculation with a generic height and
%%radius:
%%$$
%%  \int_0^{h} \pi{r^2\over h^2}x^2\,dx
%%  ={\pi r^2\over h^2}{h^3\over3}={\pi r^2h\over3},
%%$$
%%giving us the usual formula for the volume of a cone.
%%\end{solution}
%
%\begin{example}{Volume of an Object with a Hole}{Volume of an Object with a Hole}\label{Volume of an Object with a Hole}
%Find the volume of the object generated when the area between
%$\ds y=x^2$ and $y=x$ is rotated around the $x$-axis.
%\end{example}
%
%\begin{solution}
%This solid has a
%``hole'' in the middle; we can compute the volume by subtracting the
%volume of the hole from the volume enclosed by the outer surface of
%the solid. In figure~\ref{fig:solid with hole} we show the region
%that is rotated, the resulting solid with the front half cut away,
%the cone that forms the outer surface, the
%horn-shaped hole, and a cross-section perpendicular to the $x$-axis.
%
%\figure[H]
%%\texonly
%\centerline{\vbox{\beginpicture
%\normalgraphs
%%\ninepoint
%\setcoordinatesystem units <3truecm,3truecm>
%\setplotarea x from 0 to 1.1, y from 0 to 1.1
%\axis bottom shiftedto y=0 ticks withvalues {$0$} {$1$} / at 0 1 / /
%\axis left shiftedto x=0 ticks withvalues {$0$} {$1$} / at 0 1 / /
%\put {\hbox{\epsfxsize3cm\epsfbox{images/cutaway_horn.eps}}} at 3 0
%\put {\hbox{\epsfxsize3cm\epsfbox{images/outer_cone.eps}}} at 0 -2
%\put {\hbox{\epsfxsize3cm\epsfbox{images/horn.eps}}} at 2 -2
%\put {\hbox{\epsfxsize3cm\epsfbox{images/washer_section.eps}}} at 4 -2
%\plot 0 0 1 1 /
%\setquadratic
%\plot
%0.000 0.000 0.100 0.010 0.200 0.040 0.300 0.090 0.400 0.160
%0.500 0.250 0.600 0.360 0.700 0.490 0.800 0.640 0.900 0.810
%1.000 1.000 /
%\endpicture}}
%\caption{\label{fig:solid with hole}
%Solid with a hole, showing the outer cone and the shape to
%be removed to form the hole.}
%%(\expandafter\url\expandafter{\liveurl solid_with_hole.html}%
%%AP\endurl)
%%\endcaption
%%\endtexonly
%%\figrdef{fig:solid with hole}
%%\htmlfigure{Integration_applications-volume_with_hole.html}
%%\htmlonly
%%\begincaption
%%Solid with a hole. You can download the Sage
%%worksheet
%%for this plot and upload it to your own sage account.
%%\endcaption
%%\endhtmlonly
%\endfigure
%
%We have already computed the volume of a cone; in this case it is
%$\pi/3$. At a particular value of $x$, say $\ds x_i$, the cross-section of
%the horn is a circle with radius $\ds x_i^2$, so the volume of the horn is
%$$\int_0^1 \pi(x^2)^2\,dx=\int_0^1 \pi x^4\,dx=\pi{1\over 5},$$
%so the desired volume is $\pi/3-\pi/5=2\pi/15$.
%
%As with the area between curves, there is an alternate approach that
%computes the desired volume ``all at once'' by approximating the
%volume of the actual solid. We can approximate the volume of a slice
%of the solid with a washer-shaped volume, as indicated in
%Figure~\ref{fig:solid with hole}.
%
%The volume of such a washer is the area of the face times the
%thickness. The thickness, as usual, is $\Delta x$, while the area of
%the face is the area of the outer circle minus the area of the inner
%circle, say $\ds \pi R^2-\pi r^2$, or $\pi(\text{TOP})^2-\pi(\text{BOTTOM})^2$. In the present example, at a particular $\ds x_i$,
%the radius $R$ (The ``TOP'' function) is $\ds x_i$ and $r$ (The ``BOTTOM'' function) $\ds x_i^2$. Hence, the whole volume is
%$$
%  \int_0^1 \pi\left(\text{TOP}^2-\text{BOTTOM}^2\right)\,dx=
%  \int_0^1 \pi x^2-\pi x^4\,dx=
%  \left.\pi\left({x^3\over3}-{x^5\over5}\right)\right|_0^1=
%  \pi\left({1\over3}-{1\over5}\right)={2\pi\over15}.
%$$
%Of course, what we have done here is exactly the same calculation as
%before, except we have in effect recomputed the volume of the outer cone.
%\end{solution}
%
%Suppose the region between $f(x)=x+1$ and $\ds g(x)=(x-1)^2$ is rotated around
%the $y$-axis; see Figure~\ref{fig:shell method}. It is possible, but
%inconvenient, to compute the  volume of the resulting solid by the
%method we have used so far. The problem is that there are two
%``kinds'' of typical rectangles: Those that go from the line to the
%parabola and those that touch the parabola on both ends. To compute
%the volume using this approach, we need to break the problem into two
%parts and compute two integrals:
%$$
%  \pi\int_0^1 (1+\sqrt{y})^2-(1-\sqrt{y})^2\,dy+
%  \pi\int_1^4  (1+\sqrt{y})^2-(y-1)^2\,dy={8\over3}\pi + {65\over6}\pi
%  ={27\over2}\pi.
%$$
%If instead we consider a typical vertical rectangle, {but still rotate
%around the $y$-axis,} we get a thin ``shell'' instead of a thin
%``washer''. Note that ``washers'' are related to the area of a circle, $\pi r^2$, whereas ``shells'' are related to the surface area of a cylinder, $2\pi rh$. If we add up the volume of such thin shells we will get an
%approximation to the true volume. What is the volume of such a shell?
%Consider the shell at $\ds x_i$.
%Imagine that we cut the shell vertically in one place and ``unroll''
%it into a thin, flat sheet, namely the surface of a cylinder. This sheet will be almost a rectangular
%prism that is $\Delta x$ thick, $\ds f(x_i)-g(x_i)$ (TOP$-$BOTTOM) tall, and $\ds 2\pi x_i$
%wide. The volume will then be approximately the volume of a rectangular
%prism with these dimensions: $\ds 2\pi x_i(f(x_i)-g(x_i))\Delta x$. If we
%add these up and take the limit as usual, we get the integral
%$$
%  \int_0^3 2\pi x(f(x)-g(x))\,dx=
%  \int_0^3 2\pi x\left(\text{TOP}-\text{BOTTOM}\right)\,dx=
%  \int_0^3 2\pi x(x+1-(x-1)^2)\,dx={27\over2}\pi.
%$$
%Not only does this accomplish the task with only one integral, the
%integral is somewhat easier than those in the previous
%calculation. Things are not always so neat, but it is often the case
%that one of the two methods will be simpler than the other, so it is
%worth considering both before starting to do calculations.
%
%\figure[H]
%%\texonly
%\centerline{\vbox{\beginpicture
%\normalgraphs
%%\ninepoint
%\setcoordinatesystem units <1truecm,1truecm>
%\setplotarea x from 0 to 3.1, y from 0 to 4.1
%\axis bottom shiftedto y=0 ticks numbered from 0 to 3 by 1 /
%\axis left shiftedto x=0 ticks numbered from 0 to 4 by 1 /
%\putrule from 1 2 to 2.4142 2
%\putrule from 1 1.8 to 2.4142 1.8
%\putrule from 1 1.8 to 1 2
%\putrule from 2.4142 1.8 to 2.4142 2
%\putrule from 0.25 .5625 to 1.75 .5625
%\putrule from 0.25 .3625 to 1.75 .3625
%\putrule from 0.25 .3625 to 0.25 .5625
%\putrule from 1.75 .3625 to 1.75 .5625
%\plot 0 1 3 4 /
%\setquadratic
%\plot
%0.000 1.000 0.150 0.722 0.300 0.490 0.450 0.302 0.600 0.160
%0.750 0.062 0.900 0.010 1.050 0.002 1.200 0.040 1.350 0.122
%1.500 0.250 1.650 0.422 1.800 0.640 1.950 0.902 2.100 1.210
%2.250 1.562 2.400 1.960 2.550 2.402 2.700 2.890 2.850 3.422
%3.000 4.000 /
%\setcoordinatesystem units <1truecm,1truecm> point at -5 0
%\setplotarea x from 0 to 3.1, y from 0 to 4.1
%\axis bottom shiftedto y=0 ticks numbered from 0 to 3 by 1 /
%\axis left shiftedto x=0 ticks numbered from 0 to 4 by 1 /
%\put {\hbox{\epsfxsize4cm\epsfbox{images/shell.eps}}} at 7 2
%\putrule from 1.5 0.25 to 1.5 2.5
%\putrule from 1.7 0.25 to 1.7 2.5
%\putrule from 1.5 0.25 to 1.7 0.25
%\putrule from 1.5 2.5 to 1.7 2.5
%\setlinear
%\plot 0 1 3 4 /
%\setquadratic
%\plot
%0.000 1.000 0.150 0.722 0.300 0.490 0.450 0.302 0.600 0.160
%0.750 0.062 0.900 0.010 1.050 0.002 1.200 0.040 1.350 0.122
%1.500 0.250 1.650 0.422 1.800 0.640 1.950 0.902 2.100 1.210
%2.250 1.562 2.400 1.960 2.550 2.402 2.700 2.890 2.850 3.422
%3.000 4.000 /
%\endpicture}}
%\caption{\label{fig:shell method}
%Computing volumes with ``shells''.}
%%(\url{http://www.whitman.edu/mathematics/calculus/live/shell.html}%
%%AP\endurl)}
%%(\expandafter\url\expandafter{\liveurl shell.html}%
%%AP\endurl)
%%\endcaption
%%\endtexonly
%%\figrdef{fig:shell method}
%%\htmlfigure{Integration_applications-volume_shell_method.html}
%%\htmlonly
%%\begincaption
%%Computing volumes with ``shells''.
%%\endcaption
%%\endhtmlonly
%\endfigure
%
%\begin{example}{}{}\label{}
%Suppose the area under $\ds y=-x^2+1$ between $x=0$ and $x=1$ is
%rotated around the $x$-axis. Find the volume by both methods.
%\end{example}
%
%\begin{solution}
%Using the disk method we obtain:
%$$\ds \int_0^1 \pi(1-x^2)^2\,dx={8\over15}\pi.$$
%Using the shell method we obtain:
%$$\ds \int_0^1 2\pi y \sqrt{1-y}\,dy={8\over15}\pi.$$
%\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:volume}}

\begin{enumialphparenastyle}

%%%%%%%%%%
\begin{ex}
Verify that $\ds\pi\int_0^1 (1+\sqrt{y})^2-(1-\sqrt{y})^2\,dy+
\pi\int_1^4  (1+\sqrt{y})^2-(y-1)^2={8\over3}\pi + {65\over6}\pi
={27\over2}\pi$.
\end{ex}

%%%%%%%%%%
\begin{ex}
 Verify that $\ds\int_0^3 2\pi x(x+1-(x-1)^2)\,dx={27\over2}\pi$.
\end{ex}

%%%%%%%%%%
\begin{ex}
 Verify that $\ds \int_0^1 \pi(1-x^2)^2\,dx={8\over15}\pi$.
\end{ex}

%%%%%%%%%%
\begin{ex}
 Verify that $\ds \int_0^1 2\pi y \sqrt{1-y}\,dy={8\over15}\pi$.
\end{ex}

%%%%%%%%%%
\begin{ex}
Use integration to find the volume of the solid obtained by revolving
the region bounded by $x+y=2$ and the $x$ and $y$ axes around the
$x$-axis.
\begin{sol}
 $8\pi/3$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Find the volume of the solid obtained by revolving
the region bounded by $\ds y=x-x^2$
and the $x$-axis around the
$x$-axis.
\begin{sol}
 $\pi/30$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Find the volume of the solid obtained by revolving
the region bounded by $\ds y=\sqrt{\sin x}$ between $x=0$ and
$x=\pi/2$, the $y$-axis, and the line
$y=1$ around the
$x$-axis.
\begin{sol}
 $\pi(\pi/2-1)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Let $S$ be the region of the $xy$-plane bounded above by the curve
$\ds x^3y=64$, below by the line $y=1$, on the left by  the line $x=2$, and
on the right by the line $x=4$.  Find
the volume of the solid obtained by rotating $S$ around:
\begin{multicols}{2}
\begin{enumerate}
	\item	the $x$-axis;
	\item	the line $y=1$;
	\item	the $y$-axis; and
	\item	the line $x=2$.
\end{enumerate}
\end{multicols}
\begin{sol}
\begin{multicols}{2}
\begin{enumerate}
	\item	$114\pi/5$
	\item	$74\pi/5$
	\item	$20\pi$
	\item	$4\pi$
\end{enumerate}
\end{multicols}
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 The equation $\ds x^2/9+y^2/4=1$ describes an ellipse.  Find the
volume of the solid obtained by rotating the ellipse around the
$x$-axis and also around the $y$-axis. These solids are
called \dfont{ellipsoids}; one is vaguely rugby-ball shaped, one is
sort of flying-saucer shaped, or perhaps squished-beach-ball-shaped.
\begin{sol}
 $16\pi$, $24\pi$
\end{sol}
\end{ex}

\figure[H]
%\texonly
\centerline{\vbox{\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <3truecm,3truecm>
\setplotarea x from 0 to 1.1, y from 0 to 0.5
\put {\hbox{\epsfxsize3cm\epsfbox{images/rugby.eps}}} at 0 0
\put {\hbox{\epsfxsize3cm\epsfbox{images/ufo.eps}}} at 2 0
\endpicture}}
\caption{Ellipsoids.\label{fig:ellipsoids}}
%(\url{http://www.whitman.edu/mathematics/calculus/live/ellipsoid.html}%
%AP\endurl)}
%(\expandafter\url\expandafter{\liveurl ellipsoid.html}%
%AP\endurl)}
%\endcaption
%\endtexonly
%\figrdef{fig:ellipsoids}
%\htmlfigure{Integration_applications-ellipsoids.html}
%\htmlonly
%\begincaption
%Ellipsoids.
%\endcaption
%\endhtmlonly
\endfigure

%%%%%%%%%%
\begin{ex}
 Use integration to compute the volume of a sphere of radius
$r$. You should of course get the well-known formula $\ds 4\pi r^3/3$.
\end{ex}

%%%%%%%%%%
\begin{ex}
A hemispheric bowl of radius $r$ contains water to a depth $h$.  Find
the volume of water in the bowl.
\begin{sol}
 $\ds \pi h^2(3r-h)/3$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 The base of a tetrahedron (a triangular pyramid) of height $h$
is an equilateral triangle of side $s$.  Its cross-sections
perpendicular to an altitude are equilateral triangles.  Express its
volume $V$ as an integral, and find a formula for $V$ in terms of $h$
and $s$. Verify that your answer is $(1/3)(\hbox{area of base})(\hbox{height})$.
%% fixme: include picture? see exercise_9.3.13.mw
\end{ex}

%%%%%%%%%%
\begin{ex}
The base of a solid is the region between $f(x)=\cos x$ and
$g(x)=-\cos x$, $-\pi/2\le x\le\pi/2$,
and its cross-sections perpendicular to the $x$-axis
are squares.
Find the volume of the solid.
\begin{sol}
 $2\pi$
\end{sol}
\end{ex}

\end{enumialphparenastyle}


%!TEX root = /home/renaud/Documents/EPL/tfe/latex/tfe.tex
\chapter{The "overturner" model}
\section{Mathematical model}
\subsection{Estimation of the parameter values}
\textcolor{red}{Vérifier que c'est bien les valeurs finales}
The numerical values used here are based on personal communications with \textit{E. Deleersnijder}.
The model for the idealized meridian velocity field in the Atlantic ocean will be complete once we have assigned plausible values to the parameters. For this purpose, some physical insight is needed. First, the Atlantic ocean extends approximately from $50\degree$ South to $60\degree$ North, hence over $\frac{11}{18}\pi$ radians. With the radius of the Earth estimated to $6\,371$ km, we get that $L$ must be close to $(\frac{11}{18}\pi) (6\,371) = 12\, 231$ km. Moreover, the mean depth of the Atlantic ocean is about $4$ km. Hence, we choose $L = 12\,000$ km and $H = 4$ km. In virtue of the properties of the streamfunction, the maximum $\Psi = \psi(y_0,z_0)$ of the meridian streamfunction is equal to the volume flow rate accross any curve connecting $(y_0,z_0)$ to a point on the boundary of the domain: it is thus a measure of the intensity of the meridian circulation. With an estimated rate of deep convection in the Atlantic ocean of about $20$ Sv and a mean width of about $5\,000$ km, this yields $\Psi = 4$ $\rm{m^2/s}$. Finally, we use $y_0 = 11\,000$ km and $z_0 = 3.5$ km based on qualitative inspection of the meridian streamfunction graph. Characteristic values $V$ and $W$ of the meridional and vertical speed are, in virtue of relations~\eqref{eq:u-psi} :
\begin{equation}\label{eq:VW}
	V = \frac{\Psi}{H} \quad \mbox{and} \quad W = \frac{\Psi}{L}.
\end{equation}
According to \textit{E. Deleersnijder} [personal communication], the characteristic time scale $T$ should be of the order of a few hundred years in order to be physically significant. It is expressed as
\begin{equation} \label{eq:T}
	T = \frac{L}{V} = \frac{H}{W} = \frac{LH}{\Psi} = 1.5\e{6} \mbox{ s} \approx 475.6 \mbox{ years},
\end{equation}
an acceptable value.

With those values of the parameters, the isolines of the adimensional streamfunction $\psi/\Psi$ are shown in figure~\ref{fig:psi_overturner}, and the meridional and vertical components of the velocity field are illustrated in figure~\ref{fig:vw_overturner}.
\begin{figure}[ht]
	\centering
	\includegraphics[width=.6\textwidth]{fig/overturner/psi.eps}
	\caption{Some isolines of the adimensional meridian streamfunction $\psi(y,z)/\Psi$, which are also streamlines of the idealised meridian circulation in the Atlantic ocean.}
	\label{fig:psi_overturner}
\end{figure}

\begin{figure}[ht]
	\centering
	\begin{subfigure}[t]{0.4\textwidth}
		\includegraphics[width=\textwidth]{fig/overturner/V_samecaxis.eps}
		\caption{$v(y,z)$.}
		\label{fig:v_overturner}
	\end{subfigure}
	\begin{subfigure}[t]{0.4\textwidth}
		\includegraphics[width=\textwidth]{fig/overturner/W_samecaxis.eps}
		\caption{$w(y,z)$.}
		\label{fig:w_overturner}
	\end{subfigure}
	\begin{subfigure}[t]{0.1\textwidth}
		\includegraphics[height = 4\textwidth]{fig/overturner/colorbarVW.eps}
	\end{subfigure}
	\caption{Meridional and vertical components of the idealised velocity fields in the adimensional domain. Here, $y_0 = \frac{11}{12}L$ and $z_0 = \frac{7}{8}H$.}
	\label{fig:vw_overturner}
\end{figure}

\subsection{Injection of a passive tracer into the ocean} \label{sec:transport_overturner}
The fate of a passive tracer injected at location $(y_*,z_*)$ into the idealised Atlantic ocean depicted previously can be described by a differential problem on that tracer's concentration. The tracer could be any passive tracer whose concentration in the athmosphere is negligible, for example a dye or a set of seawater particles initially located at $(y_*,z_*)$. The concentration of the tracer $C(t,y,z)$ in the ocean obeys the following partial differential equation :
\begin{equation}\label{eq:C_PDE_vec}
	\frac{\partial C}{\partial t} = -\nabla \cdot \left(\b uC - \b K \nabla C\right),
\end{equation}
where $\b K$ is the \textit{diffusivity tensor}. Without loss of generality, we can assume $\b K$ to be symmetric. This is essentially because the impact of the anti-symmetric part of $\b K$, if any, may be viewed as additional advection. More details may be found in appendix A of \cite{deleersnijder2001concept}. Of course, the symmetric tensor $\b K$ must then be positive-definite in order to represent truly diffusive processes, namely phenoma which tend, at any time and location, to homogenise the concentration of any constituent. For our problem, we consider that $\b K$ has the form
\begin{equation} \label{eq:K}
	\b K(y,z) = \begin{pmatrix} K_h & 0 \\ 0 & K_v(y,z) \end{pmatrix},
\end{equation}
where $K_h$ is a positive constant and
\begin{equation} \label{eq:Kv}
	K_v(y,z) = \left\{
		\begin{array}{lrrr}
			K_{v_1} & \mbox{if} & y_0 \le y \le L, & 0 \le z \le H,\\
			K_{v_2} & \mbox{if} & 0 \le y < y_0, & 0 \le z < z_0,\\
			K_{v_3} & \mbox{if} & 0 \le y < y_0, & z_0 \le z \le H,
		\end{array}
	\right.
\end{equation}
with $K_{v_1}$, $K_{v_2}$ and $K_{v_3}$ positive constants. In the framework of the idealised model of the meridian circulation in the Atlantic ocean, \textit{E. Deleersnijder} [personal communication] proposes the values $K_h = 10^3$ $\rm{m^2/s}$, $K_{v_1} = 10^{-1}$ $\rm{m^2/s}$, $K_{v_2} = 10^{-4}$ $\rm{m^2/s}$ and $K_{v_3} = 10^{-3}$ $\rm{m^2/s}$. The relatively large value of $K_{v_1}$ allows to represent deep convection in the corresponding zone without having to implement a convective adjustment algorithm. The developped form of~\eqref{eq:C_PDE_vec} is then
\begin{equation}\label{eq:C_PDE_dev}
	\frac{\partial C}{\partial t} = -\frac{\partial}{\partial y}\left(vC - K_h\frac{\partial C}{\partial y}\right) -\frac{\partial}{\partial z}\left(wC - K_v(y,z)\frac{\partial C}{\partial z}\right).
\end{equation}
No-flux conditions are imposed at the boundaries \textcolor{red}{vérifier/discuter le flux nul à la surface}
\begin{equation}\label{eq:C_PDE_BC}
	\left. K_h\frac{\partial C}{\partial y} \right|_{y=0} = 0, \quad  \left. K_h\frac{\partial C}{\partial y} \right|_{y=L} = 0, \quad \left. K_v\frac{\partial C}{\partial z} \right|_{z=0} = 0, \quad \mbox{and} \quad \left. K_v\frac{\partial C}{\partial z} \right|_{z=H} = 0.
\end{equation}
The initial condition is
\begin{equation} \label{eq:C_PDE_CI}
	C(0,y,z) = \delta(y-y_*)\delta(z-z_*),
\end{equation}
where $\delta$ is the Dirac delta function, such that
\begin{equation}
	\int_0^L \int_0^H C(0,y,z) \rm dy \rm dz = 1.
\end{equation}

In order to get a formulation of the problem using as few independent parameters as possible, it is interesting to consider the adimensional formulation. Such a scaling is particularly interesting for sensitivity analysis. The adimensional independent variables are
\begin{equation}
	t' = \frac{t}{T} = \frac{t\Psi}{LH}, \quad y' = \frac{y}{L} \quad \mbox{and} \quad z' = \frac{z}{H},
\end{equation}
where $T$ is the time scale introduced in~\eqref{eq:T}. The adimensional hydrodynamic variables are:
\begin{equation}
	\psi' = \frac{\psi}{\Psi}, \quad v' = \frac{v}{V} = \frac{vH}{\Psi}, \quad \mbox{and} \quad w' = \frac{w}{W} = \frac{wL}{\Psi},
\end{equation}
where $V$ and $W$ are the velocity scales introduced in~\eqref{eq:VW}. The adimensional concentration is
\begin{equation}
	C' = \frac{C}{C_r},
\end{equation}
where $C_r$ is a characteristic value of the concentration. We will see shortly that there is no needed to assign a particular value to $C_r$. The adimensional form of equation~\eqref{eq:C_PDE_dev} is then
\begin{equation}\label{eq:PDE_adim}
	\frac{\partial C'}{\partial t'} = -\frac{\partial}{\partial y'}\left(v'C' - \frac{1}{Pe_h}\frac{\partial C'}{\partial y'}\right) -\frac{\partial}{\partial z'}\left(w'C' - \frac{1}{Pe_v(y,z)}\frac{\partial C'}{\partial z'}\right),
\end{equation}
where
\begin{equation}
	Pe_h = \frac{\Psi L}{K_h H} \quad \mbox{and} \quad Pe_v(y,z) = \frac{\Psi H}{K_v(y,z)L}
\end{equation}
are the horizontal and vertical Péclet numbers. They correspond to the ratio between the characteristic advective and diffusive velocity scales. Indeed, the horizontal and vertical diffusive velocity scales $V_{d}$ and $W_{d}$ are
\begin{equation}
	V_{d} = \frac{K_h}{L} \quad \mbox{and} \quad W_{d}(y,z) = \frac{K_v(y,z)}{H}.
\end{equation}
There are three different vertical diffusive velocity scale depending on which zone of the ocean we consider \textcolor{red}{Donner des noms aux zones dans le chap 1 : 1 = ?, 2 = "Deep convection" et 3 = "surface flow"}. The advective velocity scales $V$ and $W$ have already been introduced in~\eqref{eq:VW}. The Péclet numbers may then be rewritten as
\begin{equation}
	Pe_h = \frac{V}{V_{d}} = 12,
\end{equation}
and
\begin{equation}
	Pe_{v}(y,z) =  \frac{W}{W_{d}(y,z)}=  \left\{
	\begin{array}{lrrr}
			Pe_{v_1} = 1.33\e{-2} & \mbox{if} & y_0 \le y \le L, & 0 \le z \le H,\\
			Pe_{v_2} = 13.3 & \mbox{if} & 0 \le y < y_0, & 0 \le z < z_0,\\
			Pe_{v_3} = 1.33 & \mbox{if} & 0 \le y < y_0, & z_0 \le z \le H.
	\end{array}
	\right.
\end{equation}
This shows that the advective and diffusive processes are of equal importance in the dynamics of our model, excepted in the zone of deep convection where the vertical diffusion dominates the vertical convection. This is because we have chosen to represent deep convection via a heavy vertical mixing in that zone.


\chapter{Primitive recursive functions}
\label{chapter:primrec}

\section{Introduction}
Primitive recursive functions are a small class of total functions, corresponding to the expressive power of a simple imperative programming language without \textbf{while} loops, in which every program execution terminates.

Primitive recursive functions are total functions from $\mathbb{N}^n$ to
$\mathbb{N}$, for some $n\in\mathbb{N}$. Note that not all
total $n$-ary recursive functions are primitive recursive
(see for instance Sect.~\vref{sect:ack-not-PR}).

The traditional definition of the set of primitive recursive functions is structured as an inductive definition
in five rules: three base cases, and two recursive construction rules.

\begin{description}
  \item[zero] the constant function of value $0$ is primitive recursive.
\item[S] The successor function $S:\mathbb{N}\rightarrow\mathbb{N}$ is primitive recursive.
 \item[projections] For any pair $0< i\leq n$, the projection $\pi_{i,n}: \mathbb{N}^n\rightarrow\mathbb{N}$, defined by $\pi_{i,n}(x_1,x_2,\dots,x_{n})=x_i$, is primitive recursive.
\item[composition] For any $n$ and $m$, if $h: \mathbb{N}^m\rightarrow\mathbb{N}$, and
$g_0,\dots, g_{m-1}: \mathbb{N}^n\rightarrow\mathbb{N}$ are primitive recursive of $n$ arguments, then the function which maps any
tuple $(x_0,\dots,x_{n-1})$ to $h(g_0(x0,\dots,x_{n-1}),\dots, g_{m-1}(x0,\dots,x_{n-1})): \mathbb{N}^n\rightarrow\mathbb{N}$ is primitive recursive.
\item[primitive recursion]
If $g: \mathbb{N}^n\rightarrow\mathbb{N}$ and $h: \mathbb{N}^{n+2}\rightarrow\mathbb{N}$ are primitive recursive, then the function from $\mathbb{N}^{n+1}$ into $\mathbb{N}$ defined by
\begin{align}
f(0,x_1,\dots,x_n)&=g(x_1,\dots,x_n)\\
f(S(p),x_1,\dots,x_n)&=h(p,f(p, x_1,\dots,x_n),  x_1,\dots,x_n)
\end{align}
is primitive recursive.
\end{description}

Please note the use of dots: $\ldots$ in the definition above.
Dots are not part of \gallina's syntax. Thus, the formal definition of the set of primitive recursive function will have to overcome this representation problem.

  We present in this chapter a formalization of  primitive recursive functions, taken from  Russel O'Connor's formalization in \coq{} of
G\"odel's incompleteness theorems~\cite{OConnor05}.

\begin{remark}
 The theory of primitive recursive function is now hosted in
the \texttt{theories/ordinals/Ackermann} directory.
The specific part on G\"odel's theorem,  is also on
coq-community (\url{https://github.com/coq-community/goedel}) and requires the
\href{https://github.com/coq-community/pocklington}{Pocklington library} for lemmas on primality.
\end{remark}

This chapter contains some comments on Russel's library, as well as a few extensions.
Contributions (under the form of comments, new examples or exercises) are welcome!.

\section{First look at the Ackermann library}

O'Connor's library on Gödel's incompleteness theorems contains a little more
than 45K lines of scripts. The part dedicated to primitive recursive functions and Peano arithmetic is 32K lines long and is originally structured in 38 modules.
Thus, we propose a partial exploration of this library, through examples and exercises. Our additions to the original library --- mainly examples and counter-examples ---,
are stored in the directory \texttt{theories/ordinals/MoreAck}.

In particular, the library \href{../theories/html/hydras.MoreAck.html}{MoreAck.AckNotPR} contains the well-known  proof that the Ackermann function is not primitive recursive (see Section~\vref{sect:ack-not-PR}).
Moreover, the library \href{../theories/html/hydras.Hydra_Theorems.html}{Hydra.Hydra\_Theorems} contains
a proof that the length of an hydra battle (according to the initial replication factor) is not primitive recursive in general.

\section{Basic definitions}
\index{maths}{Primitive recursive functions}

The formal definition of primitive recursive functions lies in the library
\href{../theories/html/hydras.Ackermann.primRec.html}{Ackermann.primRec},
with preliminary definitions in
\href{../theories/html/hydras.Ackermann.extEqualNat.html}{Ackermann.extEqualNat}
and
\href{../theories/html/hydras.Ackermann.misc.html}{Ackermann.misc}.

\subsection{Functions of arbitrary arity}

The  \texttt{primRec} library allows us to consider primitive functions on \texttt{nat}, with any number of arguments, in
curried form. This is made possible in
\href{../theories/html/hydras.Ackermann.extEqualNat.html}{Ackermann.extEqualNat} by the following definition:

\index{primrec}{Types!naryFunc}
\input{movies/snippets/extEqualNat/naryFunc.tex}

For instance (\texttt{naryFunc 1}) is convertible to \texttt{nat -> nat} and (\texttt{naryFunc 3})
to \texttt{nat -> nat -> nat -> nat}.

\vspace{4pt}
\noindent
\emph{From \href{../theories/html/hydras.MoreAck.PrimRecExamples.html}{MoreAck.PrimRecExamples}}.

\input{movies/snippets/PrimRecExamples/naryFunc3}
\input{movies/snippets/PrimRecExamples/checknaryFunc}

Likewise, arbitrary boolean predicates may have an arbitrary number of arguments. The dependent type
(\texttt{naryRel $n$}), defined in the same way as \texttt{naryFunc}, is the type of $n$-ary functions from
\texttt{nat} into \texttt{bool}.

\input{movies/snippets/PrimRecExamples/naryRel2}

The magic of dependent types makes it possible to define recursively extensional equality between functions of the same arity.

\index{coq}{Dependent types}
\index{coq}{Dependently typed functions}
\vspace{4pt}
\noindent
\emph{From \href{../theories/html/hydras.Ackermann.extEqualNat.html}{Ackermann.extEqualNat}}

\index{primrec}{Predicates!extEqual}

\input{movies/snippets/extEqualNat/extEqualDef}

\input{movies/snippets/PrimRecExamples/extEqual2a}

  Getting rid of the term \texttt{x-x}, we generate two easy-to-solve sub-goals.

\vspace{6pt}
\noindent
\input{movies/snippets/PrimRecExamples/extEqual2b}

\subsection{A Data-type for Primitive Recursive Functions}

O'Connor's formalization of primitive recursive functions takes the form of two mutually inductive dependent data types, each constructor of which is associated with one of these  rules.
These two types are (\texttt{PrimRec $n$}) (primitive recursive functions of $n$ arguments), and
(\texttt{PrimRecs $n$ $m$}) ($m$-tuples of primitive recursive functions of $n$ arguments).

\index{coq}{Dependent types}
\index{coq}{Mutually inductive types}

\index{primrec}{Types!PrimRec}
\index{primrec}{Types!PrimRecs}
\label{def:Primrec}
\vspace{4pt}
\noindent
\emph{From \href{../theories/html/hydras.Ackermann.primRec.html}{Ackermann.primRec}.}

\input{movies/snippets/primRec/PrimRecDef}

\begin{remark}
\label{projFunc-order-of-args}
Beware of the conventions used in the \texttt{primRec} library!
The constructor (\texttt{projFunc $n$ $m$})  is associated with the projection $\pi_{n-m,n}$ and \emph{not}
$\pi_{n, m}$.
For instance, the projection $\pi_{2,5}$ defined by $\pi_{2,5}(a,b,c,d,e)=b$ corresponds to the term
(\texttt{projFunc 5 3 H}), where \texttt{H} is a proof of $3<5$.
 This fact is reported in the comments of \texttt{primRec.v}. We presume that this convention makes it easier to define the evaluation function (\texttt{evalProjFunc $n$}) (see the next sub-section). Trying the other convention is left as an exercise.
\end{remark}

\subsection{A little bit of semantics}
Please note that inhabitants of type (\texttt{PrimRec $n$}) are not \coq{} functions like \texttt{Nat.mul}, or factorial, etc. The data-type (\texttt{PrimRec $n$}) is indeed an abstract syntax for the language of primitive recursive functions. The bridge between this language and the word of usual functions
is an interpretation function (\texttt{evalprimRec $n$})  of type
$\texttt{PrimRec}\,n \rightarrow  \texttt{naryFunc}\,n$.
This function is defined by mutual recursion,  together with the  function
(\texttt{evalprimRecS $n$ $m$}) of type
$\texttt{PrimRecs}\,n\,m \rightarrow  \texttt{Vector.t}\,(\texttt{naryFunc}\,n)\,m$.

\index{primrec}{Functions!evalPrimRec}
\index{primrec}{Functions!evalPrimRecs}

\index{coq}{Dependent pattern matching}
Both functions are mutually defined through dependent pattern matching. We advise the readers who
would feel uneasy with dependent types to consult Adam Chlipala's \emph{cpdt}  book~\cite{chlipalacpdt2011}. We leave it to the reader  to look also at the helper functions in
\href{../theories/html/hydras.Ackermann.primRec.html}{Ackermann.primRec}.

\vspace{4pt}

\input{movies/snippets/primRec/evalPrimRecDef}

\vspace{4pt}

Looks complicated? The following examples show that, when
the arity is fixed, these definitions behave well w.r.t.
\coq's reduction rules. Moreover, they make the interpretation functions more ``concrete''.

\vspace{4pt}
\noindent
\emph{From \href{../theories/html/hydras.MoreAck.PrimRecExamples.html}{MoreAck.PrimRecExamples}.}

\input{movies/snippets/PrimRecExamples/evalPrimRecEx}

\vspace{4pt}
\noindent

Another example?
Let us consider the following term\footnote{Of course, we never typed this term \emph{verbatim}; we obtained it by an interactive proof the reader will be able to make after
reading Sect.\vref{sect:proofs-of-isPR}.}:

\label{sect:bigfac}

\input{movies/snippets/PrimRecExamples/bigPRa}

Let us now interpret this term as an arithmetic function.

\vspace{4pt}
\noindent

\input{movies/snippets/PrimRecExamples/bigPRb}

After this test, the term \texttt{bigPR} looks to be a primitive recursive definition of the factorial function, although we haven't proved this fact yet. Fortunately, we will see in the following sections simple ways to prove that a given function is primitive recursive, without building such an unreadable term.

\section{Proving that a given arithmetic function is primitive recursive}
\label{sect:proofs-of-isPR}

The example in the preceding section clearly shows that, in order to prove that a given arithmetic function
(defined in \gallina{} as usual) is primitive recursive, trying to give  by hand a term  of type (\texttt{PrimRec $n$}) is not a good method, since such terms may be huge and complex, even for simple arithmetic functions. The method proposed in Library \texttt{primRec} is the following one:

\begin{enumerate}
\item Define a type corresponding to the statement "the function \texttt{$f$:naryFunc $n$} is primitive recursive ''.
\item Prove handy lemmas which may help to prove that a given function is primitive recursive.
\end{enumerate}

Thus, the proof that a function, like \texttt{factorial}, is primitive recursive may be interactive, without having to type complex terms at any step of the development.

\subsection{The predicate \texttt{isPR}}

\index{primrec}{Predicates!isPR}
\index{coq}{Extensionally equal functions}

Let $f$ be an arithmetic function of arity $n$. We say that $f$ is primitive recursive if $f$ is \textbf{extensionally}
equal to the interpretation of some term of type \texttt{PrimRec $n$}.

\vspace{4pt}
\noindent
\emph{From \href{../theories/html/hydras.Ackermann.primRec.html}{Ackermann.primRec}.}

\input{movies/snippets/primRec/isPRDef}

The library \texttt{primRec} contains a large catalogue of lemmas allowing to prove statements
of the form (\texttt{isPR $n$ $f$}). We won't list all these lemmas here, but give a few examples of
how they may be applied.

\begin{remark}
In the library \texttt{primRec}, all these lemmas are opaque (registered with \texttt{Qed}. Thus they do not allow the user to look at the witness of a proof of a \texttt{isPR} statement. Our example of page~\pageref{sect:bigfac} was built using a  copy of \texttt{primRec.v} where many \texttt{Qed}s have been replaced with
\texttt{Defined}s.

If it does not cause compatibility problems (with
\href{https://github.com/coq-community/goedel}{goedel library} for instance), we plan to make all theses lemmas transparent.
\end{remark}

\subsubsection{Elementary proofs of \texttt{isPR} statements}

The constructors \texttt{zeroFunc}, \texttt{succFunc},  and \texttt{projFunc} of type
\texttt{PrimRec} allows us to write trivial proofs of primitive recursivity.
Although  the following lemmas are already proven in
\href{../theories/html/hydras.Ackermann.primRec.html}{Ackermann.primRec},
we wrote alternate proofs in
\href{../theories/html/hydras.MoreAck.PrimRecExamples.html}%
{Ackermann.MoreAck.PrimRecExamples.v}, in order to illustrate the main proof patterns.

\input{movies/snippets/PrimRecExamples/zeroIsPR}

Likewise, we prove that the successor function on \texttt{nat} is primitive recursive too.

\input{movies/snippets/PrimRecExamples/SuccIsPR}

Projections are proved primitive recursive, case by case (many examples in
\href{../theories/html/hydras.Ackermann.primRec.html}{Ackermann.primRec}).
\emph{Please notice again that the name of the projection follows the mathematical tradition,
whilst the arguments of  \texttt{projFunc} use another convention (\emph{cf} remark~\vref{projFunc-order-of-args}).}

\input{movies/snippets/PrimRecExamples/pi25IsPR}

Please note that the projection $\pi_{1,1}$ is just the identity on \texttt{nat}, and is realized by
(\texttt{projFunc 1 0}).

\vspace{4pt}
\noindent
\emph{From \href{../theories/html/hydras.Ackermann.primRec.html}{Ackermann.primRec}.}

\input{movies/snippets/primRec/idIsPR}

\subsubsection{Using function composition}

Let us look at the proof that any constant $n$ of type \texttt{nat} has type (\texttt{PR 0})
(lemma  \texttt{const1\_NIsPR} of \texttt{primRec}). We carry out a proof by induction on $n$, the base case of which is already proven.
Now, let us assume $n$ is \texttt{PR $n$}, with $x:\texttt{PrimRec}\,0$ as a ``realizer''.
Thus we would like to compose this constant function with the unary successor function.

This is exactly the role of the instance \texttt{composeFunc 0 1} of the dependently typed
function \texttt{composeFunc}, as shown by the following lemma.

\vspace{4pt}
\input{movies/snippets/PrimRecExamples/compose01}

\vspace{4pt}
Thus, we get a quite simple proof of \texttt{const1\_NIsPR}.

\vspace{4pt}
\noindent
\emph{From \href{../theories/html/hydras.MoreAck.PrimRecExamples.html}{MoreAck.PrimRecExamples}}.

\input{movies/snippets/PrimRecExamples/const0NIsPR}

\subsubsection{Proving that \texttt{plus} is primitive recursive}

The lemma \texttt{plusIsPR} is already proven in \href{../theories/html/hydras.Ackermann.primRec.html}{Ackermann.primRec}. We present in
\href{../theories/html/hydras.MoreAck.PrimRecExamples.html}{MoreAck.PrimRecExamples}
a commented version of this proof,

First, we look for lemmas which may help to prove that a given function obtained with the recursor \texttt{nat\_rec} is primitive recursive.

\input{movies/snippets/PrimRecExamples/PrimRecExamplesSearch}

The following lemma shows it suffices to prove that
Standard library's function \texttt{plus} is extensionally equal to a function defined with
\texttt{nat\_rec}.

\input{movies/snippets/PrimRecExamples/isPRExtEqualTrans}

Thus, let us define an helper and prove its equivalence with \texttt{plus}.

\input{movies/snippets/PrimRecExamples/plusAlt}

\vspace{4pt}

We are now able to complete the proof.

\input{movies/snippets/PrimRecExamples/plusIsPRa}

We already proved that \texttt{S} is \texttt{PR 1}, but we need to consider a function of three arguments, which ignores its first and third arguments.
Fortunately, the library \texttt{primRec} already contains lemmas adapted to this kind of situation.

\vspace{4pt}
\input{movies/snippets/PrimRecExamples/checkFilter0101IsPR}
\vspace{4pt}

Thus, our first subgoal is solved easily. The rest of the proof
is just an application of already proven lemmas.

\vspace{4pt}

\input{movies/snippets/PrimRecExamples/plusIsPRb}

\begin{todo}
Comment more examples from   \href{../theories/html/hydras.MoreAck.PrimRecExamples.html}{MoreAck.PrimRecExamples}.
\end{todo}

\index{primrec}{Exercises}
\begin{exercise}
There is a lot of lemmas similar to \texttt{filter010IsPR} in the \texttt{primRec} library, useful to control the arity of functions.
Thus, the reader may look at them, and invent simple examples of application for each lemma.
\end{exercise}

\index{primrec}{Exercises}
\begin{exercise}
Multiplication of natural number is already proven in the \texttt{primRec} library. Write a proof of your own, then compare to the library's version.
\end{exercise}

%\input{movies/snippets/PrimRecExamples/justATest}

\subsubsection{More examples}

The following proof decomposes the \texttt{double} function as the composition of
multiplication with the identity and the constant function which returns $2$.
\emph{Note that the lemma \texttt{const1\_NIsPR} considers this function as an unary function (unlike \texttt{const0\_NIsPR})}.
\input{movies/snippets/PrimRecExamples/doubleIsPRa}

\input{movies/snippets/PrimRecExamples/doubleIsPRb}

\index{primrec}{Exercises}
\begin{exercise}
Prove that the following functions are primitive recursive.

\input{movies/snippets/MorePRExamples/factDef}

\input{movies/snippets/MorePRExamples/expDef}

\input{movies/snippets/MorePRExamples/tower2Def}

\textbf{Hint:} You may have to look again at the lemmas of the library
\href{../theories/html/hydras.Ackermann.primRec.html}{Ackermann.primRec} if you meet some difficulty.
You may start this exercise with the file
    \href{https://github.com/coq-community/hydra-battles/blob/master/exercises/primrec/MorePRExamples.v}{exercises/primrec/MorePRExamples.v}.
\end{exercise}

\index{primrec}{Exercises}
\begin{exercise}
Show that the function \texttt{min: naryFunc\,2} is primitive
recursive.

\emph{You may start this exercise with
    \href{https://github.com/coq-community/hydra-battles/blob/master/exercises/primrec/MinPR.v}{exercises/primrec/MinPR.v}.}

\end{exercise}

\index{primrec}{Exercises}

\begin{exercise}
Write a simple and readable proof that the Fibonacci function is primitive recursive.

\input{movies/snippets/FibonacciPR/fibDef}

\textbf{Hint:}  You may use as a helper the function which computes the pair \linebreak
$(\texttt{fib}(n+1),\texttt{fib}(n))$.
Library \href{../theories/html/hydras.Ackermann.cPair.html}{Ackermann.cPair} contains
the definition of the encoding of $\mathbb{N}^2$ into $\mathbb{N}$, and the proofs that
the associated constructor and projections are primitive recursive.
\emph{You may start this exercise with the file
    \href{https://github.com/coq-community/hydra-battles/blob/master/exercises/primrec/FibonacciPR.v}{exercises/primrec/FibonacciPR.v}.}

\end{exercise}

\index{primrec}{Exercises}
\begin{exercise}

Prove the following lemmas (which may help to solve the next  exercise).

\inputsnippets{isqrt/boundedSearch3, isqrt/boundedSearch4}

\textbf{Note:}  The function \texttt{boundedSearch} is defined
in Library \href{../theories/html/hydras.Ackermann.primRec.html}{Ackermann.primRec}.
\end{exercise}

\index{primrec}{Exercises}

\begin{exercise}
Prove that the function which returns the  integer square root of any natural number  is primitive recursive.

\emph{You may start this exercise with the file
    \href{https://github.com/coq-community/hydra-battles/blob/master/exercises/primrec/isqrt.v}{exercises/primrec/isqrt.v}.}

\end{exercise}

\section{Proving that a given function is \emph{not} primitive recursive}
\label{sect:ack-not-PR}

The best known example of a total recursive function which is not primitive recursive is the Ackermann function. We show how to adapt the classic proof (see for instance~\cite{planetmath}) to the constraints of \gallina. We hope this formal proof
 is a nice opportunity to explore
the treatment of primitive recursive functions by R. O'Connor,
and to play with dependent types.

\subsection{Ackermann function}

Ackermann function is traditionally defined as a function from
$\mathbb{N}\times \mathbb{N}$ into $\mathbb{N}$, through
three equations:

\begin{align}
A(0,n)&=n+1\\
A(m+1,0)&=A(m,1)\\
A(m+1,n+1)&=A(m,A(m+1,n))
\end{align}

Let us try to define this function in \coq{} (in curried form).

\input{movies/snippets/Ack/AckFixpointFail.tex}

A possible workaround is to make \texttt{m} be the
decreasing argument, and define --- within \texttt{m}'s scope --- a local helper function which computes (\texttt{Ack m n}) for any \texttt{n}.
This way, both functions \texttt{Ack} and \texttt{Ackm} have a (structurally) strictly decreasing argument.

\input{movies/snippets/Ack/AckFixpointAlt.tex}

We preferred to define a variant which uses explicitly
 the functional \texttt{iterate},
where (\texttt{iterate\,$f$\,$n$})
is the $n$-th iteration of $f$\,\footnote{Please not confuse with \texttt{primRec.iterate}, which is monomorphic and does not share the same order of arguments.}. It makes it possible to apply a few lemmas proved in
\href{../theories/html/hydras.Prelude.Iterates.html}{Prelude.Iterates}, for instance about the monotony of the $n$-th iterate of a given function.

\vspace{4pt}
\noindent
\emph{From \href{../theories/html/hydras.Prelude.Iterates.html}{Prelude.Iterates}}.
\index{hydras}{Library Prelude!iterate}

\input{movies/snippets/Iterates/iterateDef}

\input{movies/snippets/Iterates/iterateLeNSN}

Thus, our definition of the Ackermann function is as follows:

\vspace{4pt}
\noindent
\emph{From \href{../theories/html/hydras.MoreAck.Ack.html}{MoreAck.Ack}}.
\index{maths}{Ackermann function}
\index{primrec}{Ackermann function}

\input{movies/snippets/Ack/AckFixpointIterate.tex}

\index{hydras}{Exercises}

\begin{exercise}
The file \href{../theories/html/hydras.MoreAck.Ack.html}{MoreAck.Ack} presents two other definitions of the Ackermann functions based on the lexicographic ordering on $\mathbb{N}\times\mathbb{N}$.
Prove that the four functions are extensionally equal.
\end{exercise}

\subsubsection{First properties of the Ackermann function}

The three first lemmas make us sure that our function
\texttt{Ack} satisfies the ``usual'' equations.

\input{movies/snippets/Ack/AckRewrite}

\vspace{4pt}

The order of growth of the Ackermann function w.r.t. its first argument is illustrated by the following equalities.

\input{movies/snippets/Ack/Ack1N}
\input{movies/snippets/Ack/Ack2N}
\input{movies/snippets/Ack/Ack3N}
\input{movies/snippets/Ack/Ack4N}

\begin{remark}
 The statements above can be rewritten in a more uniform way:

 \begin{quote}
   For $m\in 1..4$, $\texttt{Ack}\,m\,n = f_m\,(n+3)-3$, where
   \begin{align*}
   f_1(n)=&\,n+2 \\
   f_2(n)=&\,n\times 2\\
   f_3(n)=&\,2^n\\
   f_4(n)=&\,2^{2^{\dots^2}}\quad(n\;\textit{levels})
   \end{align*}
 \end{quote}
\end{remark}

An important property of the Ackermann function helps us
to overcome the difficulty raised by nested recursion, by climbing up the hierarchy $\texttt{Ack}\,n\,\_\;(n\in\mathbb{N})$.

\noindent
\emph{From \href{../theories/html/hydras.MoreAck.Ack.html}{MoreAck.Ack}}.

\input{movies/snippets/Ack/nestedAckBound}

Please note also that for any given $n$, the unary function
(\texttt{Ack\,$n$}) is primitive recursive.

\vspace{4pt}

\noindent

\emph{From \href{../theories/html/hydras.MoreAck.AckNotPR.html}{MoreAck.AckNotPR}}.

\input{movies/snippets/AckNotPR/AckNIsPR}

\subsection{A proof by induction on all primitive recursive functions}

In order to prove that \texttt{Ack} (considered as a function of two arguments) is not primitive recursive, the usual method consists in two steps:

\begin{enumerate}
\item Prove that for any primitive recursive function $f:\mathbb{N}\rightarrow\mathbb{N}\rightarrow\mathbb{N}$, there exists some natural number $n$ depending on $f$, such that, for any $x$ and $y$,
$f\,x\,y \leq \texttt{Ack}\,n\,(\textrm{max}\,x\,y)$ (we say that $f$ is \emph{``majorized''}  by \texttt{Ack}).
\item Show that \texttt{Ack} fails to satisfy this property.
\end{enumerate}

First, we prove that any primitive function of two arguments is majorized by \texttt{Ack}.
If we look at the inductive definition of primitive recursive functions, page~\pageref{def:Primrec}, it is obvious that a proof by induction on the construction of primitive recursive functions must consider functions of any arity.

The following scheme allows us to write proofs by induction on the class of primitive recursive functions.

\vspace{4pt}
\noindent
\emph{From \href{../theories/html/hydras.Ackermann.primRec.html}{Ackermann.primRec}.}

\index{coq}{Commands!Scheme}

\input{movies/snippets/primRec/SchemePrimRecInd}

Please note that, in order to prove a property shared by any primitive recursive function of, say, arity 2, this induction scheme  leads you to consider an extension of the considered property to primitive recursive function of any arity.

Thus the lemma we will have to prove is the following one:

  \begin{quote}
    For any $n$, and any primitive recursive function $f$ of  arity $n$, there exists some natural number $q$ such that the following inequality holds:
 \[
  \forall x_1,\dots,x_n,
      f(x_1,\dots,\,x_n)\leq\textrm{Ack}(q,\textrm{max}(x_1,\dots,x_n))
\]
 \end{quote}

But dots don't belong to \gallina's syntax! So, we may use \coq's vectors for denoting arbitrary tuples.

First, we extend \texttt{max} to vectors of natural numbers (using the notations of module \texttt{VectorNotations} and some more definitions from
\href{../theories/html/hydras.Prelude.MoreVectors.html}{Prelude.MoreVectors}). So, (\texttt{t\,$A$\,$n$}) is the type of vectors of $n$ elements of type $A$, and the constants \texttt{cons}, \texttt{nil}, \texttt{map}, etc., refer to vectors and not to lists. Likewise, the notation \texttt{x::v} is an abbreviation for
\texttt{VectorDef.cons x \_ v}.

\index{coq}{Dependently typed functions}

\input{movies/snippets/MoreVectors/maxvDef}

\input{movies/snippets/MoreVectors/maxvLemmasa}

\input{movies/snippets/MoreVectors/maxvLemmasb}

\input{movies/snippets/MoreVectors/maxvLemmasc}

We have also to convert any application
$(f\,x_1\,x_2\,\dots\,x_n)$ into an application of a function
to a single argument: the vector of all the $x_i$\,s.
This is already defined in
Library~\href{../theories/html/hydras.Ackermann.primRec.html}{Ackermann.primRec}.

\input{movies/snippets/primRec/evalListDef}

Indeed, (\texttt{evalList $m$ $v$ $f$}) is the application to the vector $v$ of
an uncurried version of $f$.
In Library\href{../theories/html/hydras.MoreAck.AckNotPR.html}{MoreAck.AckNotPR}, we introduce a lighter notation.

\index{coq}{Dependently typed functions}

\input{movies/snippets/AckNotPR/vApply}

We are now able to translate in \gallina{} the notion of ``majorization'':

\index{coq}{Dependently typed functions}

\input{movies/snippets/AckNotPR/majorizedDefs}

Now, it remains to prove that any primitive function is majorized by \texttt{Ack}.
The three base cases  are as follows:

\input{movies/snippets/AckNotPR/majorSucc}

\input{movies/snippets/AckNotPR/majorZero}

\input{movies/snippets/AckNotPR/majorProjection}

The remaining cases are proved within a mutual  induction.

\index{coq}{Mutual induction}

\input{movies/snippets/AckNotPR/majorAnyPRa}

\input{movies/snippets/AckNotPR/majorAnyPRb}

The last two goals deal with vectors of functions.

\input{movies/snippets/AckNotPR/majorAnyPRVec}

\subsection{Looking for a contradiction}

The following lemma is just a specialization of \texttt{majorAnyPR} to
binary functions (forgetting vectors, coming back to usual notations).

\input{movies/snippets/AckNotPR/majorPR2}

We prove also a strict version of this lemma, thanks to the following property (proved in Library
\href{../theories/html/hydras.MoreAck.Ack.html}{MoreAck.Ack}~).

\input{movies/snippets/Ack/AckStrictMonoL}

\vspace{4pt}
\noindent
\emph{From \href{../theories/html/hydras.MoreAck.AckNotPR.html}{MoreAck.AckNotPR}.}

\input{movies/snippets/AckNotPR/majorPR2Strict}

If the Ackermann function were primitive recursive, then there would exist some natural number $n$, such that, for all $x$ and $y$, the inequality
$\texttt{Ack}\,x\,y\leq \texttt{Ack}\,n\,(\texttt{max}\,x\,y)$ holds.
Thus, our impossibility proof is just a sequence of easy small steps.

\input{movies/snippets/AckNotPR/AckNotPR}

\begin{remark}
It is easy to prove that any unary function which dominates \texttt{fun n => Ack n n} fails to be primitive recursive. To this end, we use an instance of \texttt{majorAnyPR} dealing with unary functions.

\vspace{4pt}
\noindent

\emph{From \href{../theories/html/hydras.MoreAck.AckNotPR.html}{MoreAck.AckNotPR}}.

\input{movies/snippets/AckNotPR/majorPR1}

Then, we write  a short proof by contradiction.

\input{movies/snippets/AckNotPR/domAckNotPR}

\end{remark}

\begin{remark}
Note that the Ackermann function is a counter-example to the (false) statement:
\begin{quote}
{\color{red}
  ``Let $f$ be a function of type \texttt{naryFunc\,2}. If, for any $n$, the function $f(n)$ is primitive recursive, then f is primitive recursive.''}
\end{quote}
\end{remark}

\section{The length of standard hydra battles}
\label{sect:battle-length-notPR}

The module \href{../theories/html/hydras.Hydra.Hydra_Theorems.html}{Hydra\_Theorems} contains a proof that the function which computes the length of standard hydra battles is not primitive recursive. More precisely, we consider, for a given hydra $h=\iota(\alpha)$, the length of a standard battle which starts with the replication factor $k$ (see Sect~\vref{def:L-alpha}).

This proof is  a little more complex than the preceding one.

\subsection{Definitions}

The function we consider is defined and proven correct in
Module~\href{../theories/html/hydras.Hydra.Battle_length.html}{Hydra.Battle\_length}.

\input{movies/snippets/Battle_length/BattleLength}

\subsection{Proof steps}

Now, let us assume that the function \texttt{l\_std} is primitive recursive.

\emph{From \href{../theories/html/hydras.Hydra.Hydra_Theorems.html}{Hydra.Hydra\_Theorems}}.

\input{movies/snippets/Hydra_Theorems/battleLengthNotPRa}

Let us consider the hydra represented by the ordinal $\omega^\omega$.

\input{movies/snippets/Hydra_Theorems/battleLengthNotPRb}

In order to get rid of the subtraction in the definition of \texttt{l\_std}, we work with a helper function.

\input{movies/snippets/Hydra_Theorems/battleLengthNotPRc}

Under the hypothesis \texttt{H}, $m$ is also primitive recursive.

\input{movies/snippets/Hydra_Theorems/battleLengthNotPRd}

\subsubsection{Comparison between $F$ and $H'$}

In \href{../theories/html/hydras.Epsilon0.F_alpha.html}{Epsilon0.F\_alpha}, we prove a relation between the $F$ and $H'$ functionals. For any $\alpha$ and $k>0$,
$H'_{\omega^\alpha}(k)\geq F_\alpha(k)$.

\input{movies/snippets/F_alpha/HprimeF}

Our proof of this lemma is not trivial at all, it uses some properties of the Ketonen-Solovay's toolkit. We advise the reader to explore this proof, with the help of an IDE or software like Alectryon.
%%
%%% To move the path chapter
%%%%

% \begin{Coqsrc}
%   alpha : E0
%   IHalpha : forall beta : E0, beta o< alpha -> P beta
%   Halpha : Limitb alpha
%   n : nat
%   ============================
%   H'_ (Phi0 (CanonS alpha n)) (S n) <=
%   H'_ (Phi0 (CanonS alpha (S n))) (S n)
% \end{Coqsrc}

% In mathematical notation: $H'_{\omega^{\canonseq{\alpha}{n}}}(n+1) \leq
% H'_{\omega^{\canonseq{\alpha}{n+1}}}(n+1)$.

% \vspace{4pt}

% But there exists no lemma saying that, if
% $\beta\leq \alpha$, then $H'_\beta(k)\leq H'_\alpha(k)$, for any $\alpha$ and $\beta$. For instance,
% $H'_{42}(3)=45> H'_\omega(3)=7$.

% Looking for lemmas of the form $H'_\beta(k)\leq H'_\alpha(k)$, we find this one (from our library
% \href{../theories/html/hydras.Epsilon0.Hprime.html}{Epsilon0.Hprime}):

% \begin{Coqanswer}
% H'_restricted_mono_l :
%     forall (alpha beta : E0) (n : nat),
%       Canon_plus n alpha beta ->
%       H'_ beta n <= H'_ alpha n.
% \end{Coqanswer}

% Thus, it remains to prove that
% there exists a path from ${\omega^{\canonseq{\alpha}{n+1}}}$
% to ${\omega^{\canonseq{\alpha}{n}}}$ composed of
% $n+1$-steps.

% Fortunately, the Ketonen-Solovay machinery contains three lemmas which help us to build such a path.

% \begin{Coqanswer}
% KS_thm_2_4_lemma5 :
%   forall [i : nat] [alpha beta : T1],
%   const_pathS i alpha beta ->
%   nf alpha -> alpha <> zero ->
%   const_pathS i (phi0 alpha) (phi0 beta)

% KS_thm_2_4 :
%   forall [lambda : T1], nf lambda ->limitb lambda ->
%   forall i j : nat, i < j ->
%    const_pathS 0 (canonS lambda j) (canonS lambda i)

% Cor12_1 :
% forall [alpha : T1], nf alpha ->
%       forall (beta : T1) (i n : nat),
%       beta t1< alpha ->
%      i <= n -> const_pathS i alpha beta ->
%      const_pathS n alpha beta
% \end{Coqanswer}

\subsubsection{End of the proof}

We finish the proof by comparing several fast growing functions.

\vspace{4pt}

\emph{From \href{../theories/html/hydras.Epsilon0.L_alpha.html}{Epsilon0.L\_alpha}}

\input{movies/snippets/L_alpha/HprimeL}

\vspace{4pt}

\emph{From \href{../theories/html/hydras.Epsilon0.F_omega.html}{Epsilon0.F\_omega}}
\input{movies/snippets/F_omega/FVsAck}

\vspace{4pt}

By transitivity, we get the inequality
$F_\omega(k+1)\leq m(k+1)$, for any $k$.

\input{movies/snippets/Hydra_Theorems/mGeFOmega}

We finish the proof by noting that the function $m$ (composed with \texttt{S}) dominates the Ackermann function, which leads to a contradiction.

\input{movies/snippets/Hydra_Theorems/mDominatesAck}

\input{movies/snippets/Hydra_Theorems/SmNotPR}

\vspace{4pt}

\input{movies/snippets/Hydra_Theorems/LNotPR}


\section{Deep Unsupervised Learning}
\subsection*{Autoencoders}
learn low-dim representation $z \in \mathbb{R}^d$ for given data.
Linear autoencoder with weights $C \in \mathbb{R}^{dxm}$ (encoder) and $D \in \mathbb{R}^{mxd}$ (decoder). objective min $\frac{1}{2n} \sum_{i=1}^n \|x_i-DCx_i \|^2$. Frobenius norm optimal approx (in this case) via SVD $X^\top X=U\Sigma V^\top$ then $C^*=U_d^\top$ and $D^*=U_d$ (first $d$ columns of $U$).
\subsection*{Variational Autoencoders (VAEs)}
Put a gaussian prior on distribution of continous latent vector z: $p(z_l) \sim \mathcal{N}(\mu_l, \Sigma_l)$ for $l \in \{1,..L\}$. This allows to easily generate new data points. \\
\textbf{log-likelihood}: $\log p_\theta(x) = \log \int p_\theta (x|z) p(z) dz \geq E_q[\log p_\theta(x|z)] + E_q[\log \frac{p(z)}{q(z)}]$ where $-D_{KL}(q||p) = E_q[\log \frac{p(z)}{q(z)}]$. KL-divergence tells how much two distributions diverge. Optimal $q(z)=p(z|x)$. \\
\textbf{NN approach}:
\begin{inparaenum}[\color{red} 1.]
\item recognition/inference model: learns variational distribution $q(z)$ i.e given $x$ it returns params of normal distribution $(\mu_l,\Sigma_l)$ for $l=1,..,L$ from which we than can sample the $z_l$'s.
\item generative model: implements $p_\theta(x|z)$ and deterministically maps $z$ to $x$ (reconstruction).
\end{inparaenum}
\subsection*{Autoregressive Models}
generate output one variable at a time based on chain rule $p(x_{1:m}) = \Pi_{i=1}^m p(x_t|x_{1:t-1})$. \\
\textbf{PixelCNN}: $nxn$ image with pixels $x_1,..,x_{n^2}$. Generate pixel $x_i$ by conditioning on previously generated pixels $x_1,..,x_{i-1}$. Use a masking filter for implem. \\
\textbf{RNN}:observed sequence $x_1,..,x_T$ and corresponding labels $y_1,..,y_T$. Use feedbackloop $h_t=f(h_{t-1},x_t)$ with hidden state $h_t$. LSTM units to avoid vanishing gradient problem. \\
\textbf{PixelRNN}: use RNN for mapping $x_1,..x_{i-1}$ to $x_i$. Row LSTM: convolute along each row from top to bottom (triangular receptive field i.e loss of context). Diagonal BiLSTM: convolute along the diagonal (receptive field includes all previously generated pixels).


\section{Mean Value Analysis}
\label{sec:Mean-Value-Analysis}

The Mean Value Analysis (MVA) is an alternative method to analyze Closed Jackson Networks.
It is efficient and intuitive, but it only provides mean metrics.

Given a Closed Jackson Network with $M$ jobs, we denote with $\expected{N_{j}^{(M)}}$ the mean number of jobs at the $j$-th server when there are $M$ jobs in the system.

The MVA relies on the following theorem.

\begin{theorem}[Arrival Theorem]
\label{thm:Arrival-Theorem}
	In a Closed Jackson Network with $M>1$ jobs, an arrival to server $j$ sees a distribution of the number of jobs at each server equal to the steady-state distribution of the number of jobs at each server in the same network with $M-1$ jobs.
\end{theorem}

The Arrival Theorem is counterpart to PASTA for closed networks of queues.

Thanks to the \Cref{thm:Arrival-Theorem}, the MVA recursively relates $\expected{N_{j}^{(M)}}$ to $\expected{N_{j}^{(M-1)}}$, until we arrive to $\expected{N_{j}^{(1)}}$, which is very easy to reason about.

\begin{theorem}[MVA-Response Time]
\label{thm:MVA-Response-Time}

	\begin{equation}
	\label{eqn:MVA-Response-Time}
	\expected{T_{j}^{M}} =
	\frac{1}{\mu_{j}} +
	\frac{p_{j} \lambda^{(M-1)} \expected{T_{j}^{(M-1)}}}{\mu_{j}}
	\end{equation}

	where $p_{j}$ is the fraction of arrivals that arrive to server $j$, namely

	\begin{equation}
	\label{eqn:MVA-Response-Time-Fraction-Arrivals}
	p_{j} = \frac{\lambda_{j}^{M}}{\lambda^{M}} = \frac{V_{j}}{\sum_{j=1}^{m} V_{j}}
	\end{equation}

	and $\lambda^{(M)}$ is the total arrival rate into all the $m$ servers, namely

	\begin{equation}
	\label{eqn:MVA-Response-Time-Total-Arrival-Rate}
	\lambda^{(M-1)} = \frac{M-1}{\sum_{j=1}^{m} p_{j} \expected{T_{j}^{(M-1)}}}
	\end{equation}

	\begin{proof}
		For a formal demonstration, see \cite{harchol2013performance} at page 337-341.
	\end{proof}
\end{theorem}

Once determined $\expected{T_{j}^{M}}$, we can obtain other mean metrics by using the Little's Law.

For instance, $\expected{N_{i}^{M}} = \lambda_{i}^{M} \cdot \expected{T_{i}^{M}}$