\section{Arc Length}{}{}\label{sec:Arc Length}

In previous sections we used integration to answer the following questions:
	\begin{enumerate}
	\item		Given a region, what is its area?
	\item		Given a solid, what is its volume?
	\end{enumerate}

In this section, we address a related question: Given a curve, what is its length? This is often referred to as \textbf{arc length}.

Consider the graph of $y=\sin x$ on $[0,\pi]$ given in Figure \ref{fig:arcintro} (a). How long is this curve? That is, if we were to use a piece of string to exactly match the shape of this curve, how long would the string be?

As we have done in the past, we start by approximating; later, we will refine our answer using limits to get an exact solution.

The length of straight--line segments is easy to compute using the Distance Formula. We can approximate the length of the given curve by approximating the curve with straight lines and measuring their lengths.

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.5\textwidth}
		\begin{tikzpicture}
		\begin{axis}[ %width=\linewidth,%
		tick label style={font=\scriptsize},axis y line=middle,axis x line=middle,name=myplot,axis on top,%
					%x=.37\marginparwidth,
					%y=.37\marginparwidth,
					xtick=\empty,%
					extra x ticks={.79,1.57,2.36,3.14},
					extra x tick labels={$\frac{\pi}4$,$\frac{\pi}2$,$\frac{3\pi}{4}$,$\pi$},
		%			ytick={1},
		%			yticklabels={$-0.002$,$0.002$,$0.004$},
					%minor y tick num=1,
		%			extra y ticks={1.8},%
		%			extra y tick labels={$y$},
		%			minor x tick num=4,
					ymin=-.2,ymax=1.25,%
					xmin=-.1,xmax=3.5,%
		]

		\addplot [{\colorone},smooth,thick,domain=0:3.14] {sin(deg(x))};

		\end{axis}

		\node [right] at (myplot.right of origin) {\scriptsize $x$};
		\node [above] at (myplot.above origin) {\scriptsize $y$};
		\end{tikzpicture}
        \label{ }
        \caption{}
    \end{subfigure}%
    \begin{subfigure}[t]{0.5\textwidth}
    \begin{tikzpicture}
    \begin{axis}[ %width=\linewidth,%
    tick label style={font=\scriptsize},axis y line=middle,axis x line=middle,name=myplot,axis on top,%
    			%x=.37\marginparwidth,
    			%y=.37\marginparwidth,
    			xtick=\empty,%
    			extra x ticks={.79,1.57,2.36,3.14},
    			extra x tick labels={$\frac{\pi}4$,$\frac{\pi}2$,$\frac{3\pi}{4}$,$\pi$},
    %			ytick={1},
    %			yticklabels={$-0.002$,$0.002$,$0.004$},
    			%minor y tick num=1,
    			extra y ticks={.71},%
    			extra y tick labels={$\frac{\sqrt{2}}2$},
    %			minor x tick num=4,
    			ymin=-.2,ymax=1.25,%
    			xmin=-.1,xmax=3.5,%
    ]

    \addplot [{\colorone},smooth,thick,domain=0:3.14] {sin(deg(x))};

    \draw [{\colortwo},thick] (axis cs:0,0) -- (axis cs:.79,.71) -- (axis cs: 1.57,1) -- (axis cs:2.36,.71) -- (axis cs: 3.14,0);

    \filldraw (axis cs:0,0) circle (1pt) (axis cs:.79,.71) circle (1pt) (axis cs: 1.57,1) circle (1pt) (axis cs:2.36,.71) circle (1pt) (axis cs: 3.14,0)circle (1pt);

    \end{axis}

    \node [right] at (myplot.right of origin) {\scriptsize $x$};
    \node [above] at (myplot.above origin) {\scriptsize $y$};
    \end{tikzpicture}
        \label{ }
        \caption{}
    \end{subfigure}
    \caption{Graphing $y=\sin x$ on $[0,\pi]$ and approximating the curve with line segments. \label{fig:arcintro}
\end{figure}

In Figure \ref{fig:arcintro} (b), the curve $y=\sin x$ has been approximated with 4 line segments (the interval $[0,\pi]$ has been divided into 4 equally--lengthed subintervals). It is clear that these four line segments approximate $y=\sin x$ very well on the first and last subinterval, though not so well in the middle. Regardless, the sum of the lengths of the line segments is $3.79$, so we approximate the arc length of $y=\sin x$ on $[0,\pi]$ to be $3.79$.

%Using 6 evenly spaced subintervals is not too much more work and gives a better approximation. The six lines are shown in Figure \ref{fig:arcintro} (c) and provide an arc length approximation of $3.81$.

In general,  we can approximate the arc length of $y=f(x)$ on $[a,b]$ in the following manner. Let $a=x_1 < x_2 < \ldots < x_n< x_{n+1}=b$ be a partition of $[a,b]$ into $n$ subintervals. Let $\dx_i$ represent the length of the $i\,^\text{th}$ subinterval $[x_i,x_{i+1}]$.

%\mfigure{.35}{Zooming in on the $i\,^\text{th}$ subinterval $[x_i,x_{i+1}$] of a partition of $[a,b]$.}{fig:arcintro2}{figures/figarcintrod}
%%
%Figure \ref{fig:arcintro2} zooms in on the $i\,^\text{th}$ subinterval where $y=f(x)$ is approximated by a straight line segment. The dashed lines show that we can view this line segment as they hypotenuse of a right triangle whose sides have length $\dx_i$ and $\dy_i$. Using the Pythagorean Theorem, the length of this line segment is
%$\ds \sqrt{\dx_i^2 + \Delta y_i^2}.$ Summing over all subintervals gives an arc length approximation
%$$L \approx \sum_{i=1}^n \sqrt{\dx_i^2 + \Delta y_i^2}.$$
%
%As shown here, this is \textit{not} a Riemann Sum. While we could conclude that taking a limit as the subinterval length goes to zero gives the exact arc length, we would not be able to compute the answer with a definite integral. We need first to do a little algebra.
%
%In the above expression factor out a $\dx_i^2$ term:
%\begin{align*}
%\sum_{i=1}^n \sqrt{\dx_i^2 + \Delta y_i^2} &= \sum_{i=1}^n \sqrt{\dx_i^2\left(1 + \frac{\Delta y_i^2}{\dx_i^2}\right)}.\\
%\intertext{Now pull the $\dx_i^2$ term out of the square root:}%\\
%			&= \sum_{i=1}^n\sqrt{1 + \frac{\Delta y_i^2}{\dx_i^2}}\ \dx_i.\\
%\intertext{This is nearly a Riemann Sum. Consider the $\Delta y_i^2/\dx_i^2$ term. The expression $\Delta y_i/\dx_i$ measures the ``change in $y$/change in $x$,'' that is, the ``rise over run'' of $f$ on the $i\,^\text{th}$ subinterval. The Mean Value Theorem of Differentiation (Theorem \ref{thm:mvt}) states that there is a $c_i$ in the $i\,^\text{th}$ subinterval where $\fp(c_i) = \Delta y_i/\dx_i$. Thus we can rewrite our above expression as:}
%			&= \sum_{i=1}^n\sqrt{1+\fp(c_i)^2}\ \dx_i.\\
%\intertext{This \textit{is} a Riemann Sum. As long as \fp\ is continuous, we can invoke Theorem \ref{thm:riemann_sum} and conclude }%\\
%			&= \int_a^b\sqrt{1+\fp(x)^2}\ dx.
%\end{align*}
%
%\keyidea{idea:arclength}{Arc Length}
%{Let $f$ be differentiable on an open interval containing $[a,b]$, where $\fp$ is also continuous on $[a,b]$. Then the arc length of $f$ from $x=a$ to $x=b$ is
%\index{integration!arc length}\index{arc length}
%$$L = \int_a^b \sqrt{1+\fp(x)^2}\ dx.$$
%}
%
%As the integrand contains a square root, it is often difficult to use the formula in Key Idea \ref{idea:arclength} to find the length exactly. When exact answers are difficult to come by, we resort to using numerical methods of approximating definite integrals. The following examples will demonstrate this.\\
%
%\example{ex_arc1}{Finding arc length}{
%Find the arc length of $f(x) = x^{3/2}$ from $x=0$ to $x=4$. }
%{We begin by finding $\fp(x)= \frac32x^{1/2}$. Using the formula, we find the arc length $L$ as
%\begin{align*}
%	L &=	\int_0^4 \sqrt{1+\left(\frac32x^{1/2}\right)^2}\ dx \\
%		&=	\int_0^4 \sqrt{1+\frac94x} \ dx \\
%		&= 	\int_0^4 \left(1+\frac94x\right)^{1/2}\ dx \\
%		&=  \frac23\frac49\left(1+\frac94x\right)^{3/2}\Big|_0^4 \\
%		&=\frac{8}{27}\left(10^{3/2}-1\right) \approx 9.07 \text{units}.
%\end{align*}
%\mfigure{.8}{A graph of $f(x) = x^{3/2}$ from Example \ref{ex_arc1}.}{fig:arc1}{figures/figarc1}
%	A graph of $f$ is given in Figure \ref{fig:arc1}.
%}\\
%
%\example{ex_arc2}{Finding arc length}{
%Find the arc length of $\ds f(x) =\frac18x^2-\ln x$ from $x=1$ to $x=2$.}
%{This function was chosen specifically because the resulting integral can be evaluated exactly. We begin by finding $\fp(x) = x/4-1/x$. The arc length is
%\begin{align*}
%L		&=  \int_1^2 \sqrt{1+ \left(\frac x4-\frac1x\right)^2}\ dx \\
%		&= 	\int_1^2 \sqrt{1 + \frac{x^2}{16} -\frac12 + \frac1{x^2} } \ dx \\
%		&=	\int_1^2 \sqrt{\frac{x^2}{16} +\frac12 + \frac1{x^2} } \ dx \\
%		&=	\int_1^2	\sqrt{ \left(\frac x4 + \frac1x\right)^2}\ dx
%\end{align*}
%\begin{align*}
%\phantom{L}
%				&= \int_1^2 \left(\frac x4 + \frac1x\right) \ dx \\
%		&=  \left(\frac{x^2}8 + \ln x\right)\Bigg|_1^2\\
%		&=	\frac38+\ln 2 \approx 1.07 \ \text{units}.
%\end{align*}
%\mfigure{.8}{A graph of $f(x) =\frac18x^2-\ln x$ from Example \ref{ex_arc2}.}{fig:arc2}{figures/figarc2}
%A graph of $f$ is given in Figure \ref{fig:arc2}; the portion of the curve measured in this problem is in bold.
%}\\
%
%The previous examples found the arc length exactly through careful choice of the functions. In general, exact answers are much more difficult to come by and numerical approximations are necessary. \\
%
%\example{ex_arc3}{Approximating arc length numerically}{
%Find the length of the sine curve from $x=0$ to $x=\pi$.}
%{This is somewhat of a mathematical curiosity; in Example \ref{ex_ftc4} we found the area under one ``hump'' of the sine curve is 2 square units; now we are measuring its arc length.
%
%The setup is straightforward: $f(x) = \sin x$ and $\fp(x) = \cos x$. Thus
%$$L = \int_0^\pi \sqrt{1+\cos^2x}\ dx.$$
%This integral \textit{cannot} be evaluated in terms of elementary functions so we will approximate it with Simpson's Method with $n=4$.
%\mtable{.5}{A table of values of $y=\sqrt{1+\cos^2x}$ to evaluate a definite integral in Example \ref{ex_arc3}.}{fig:arc3}{%
%$$\begin{array}{cc}
%x & \sqrt{1+\cos^2x} \\ \hline
% 0 & \sqrt{2} \rule{0pt}{10pt}\\
% \pi/4 & \sqrt{3/2} \\
% \pi/2 & 1 \\
% 3 \pi/4 & \sqrt{3/2} \\
% \pi  & \sqrt{2} \\
%\end{array}$$
%}
%Figure \ref{fig:arc3} gives $\sqrt{1+\cos^2x}$ evaluated at 5 evenly spaced points in $[0,\pi]$. Simpson's Rule then states that
%\begin{align*}
%\int_0^\pi \sqrt{1+\cos^2x}\ dx &\approx	\frac{\pi-0}{4\cdot 3}\left(\sqrt{2}+4\sqrt{3/2}+2(1)+4\sqrt{3/2}+\sqrt{2}\right) \\
%			&=3.82918.
%\end{align*}
%Using a computer with $n=100$ the approximation is $L\approx 3.8202$; our approximation with $n=4$ is quite good.
%}\\

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

Here is another geometric application of the integral: Find the length
of a portion of a curve. As usual, we need to think about how we might
approximate the length, and turn the approximation into an integral.

We already know how to compute one simple arc length, that of a line
segment. If the endpoints are $\ds P_0(x_0,y_0)$ and $\ds P_1(x_1,y_1)$
then the length of the segment is the distance between the points,
$\ds \sqrt{(x_1-x_0)^2+(y_1-y_0)^2}$, from the Pythagorean theorem, as
illustrated in Figure~\ref{fig:length of a line segment}.

\figure[H]
%\texonly
\centerline{\vbox{\beginpicture
\normalgraphs
%\sevenpoint
\setcoordinatesystem units <1.5truecm,1.5truecm>
\setplotarea x from 0 to 5, y from 0 to 3
\axis bottom /
\axis left /
\putrule from 2 1 to 4.5 1
\putrule from 4.5 1 to 4.5 2.5
\plot 2 1 4.5 2.5 /
\put {$(x_1,y_1)$} [bl] <3pt,3pt> at 4.5 2.5
\put {$(x_0,y_0)$} [tr] <-3pt,-3pt> at 2 1
\put {$x_1-x_0$} [t] <0pt,-3pt> at 3.25 1
\put {$y_1-y_0$} [l] <3pt,0pt> at 4.5 1.75
\put {$\sqrt{(x_1-x_0)^2+(y_1-y_0)^2}$} [br] <-3pt,3pt> at 3.25 1.75
\endpicture}}
%\endtexonly
%\figrdef{fig:length of a line segment}
%\htmlfigure{Integration_applications-arc_length.html}
\caption{\label{fig:length of a line segment}
The length of a line segment.}
%\endcaption
\endfigure

Now if the graph of $f$ is ``nice'' (say, differentiable) it appears
that we can approximate the length of a portion of the curve with line
segments, and that as the number of segments increases, and their
lengths decrease, the sum of the lengths of the line segments will
approach the true arc length; see
Figure~\ref{fig:approximating arc length}.

\figure[H]
%\texonly
\centerline{\vbox{\beginpicture
\normalgraphs
%\sevenpoint
\setcoordinatesystem units <1.5truecm,0.8truecm>
\setplotarea x from 0 to 8, y from 0 to 5
\axis bottom /
\axis left /
\setquadratic\plot
1.000 1.000 1.150 1.953 1.300 2.694 1.450 3.246 1.600 3.636
1.750 3.884 1.900 4.012 2.050 4.041 2.200 3.990 2.350 3.874
2.500 3.711 2.650 3.515 2.800 3.299 2.950 3.075 3.100 2.853
3.250 2.644 3.400 2.454 3.550 2.290 3.700 2.157 3.850 2.060
4.000 2.000 4.150 1.979 4.300 1.996 4.450 2.050 4.600 2.138
4.750 2.255 4.900 2.396 5.050 2.554 5.200 2.721 5.350 2.886
5.500 3.039 5.650 3.168 5.800 3.258 5.950 3.294 6.100 3.261
6.250 3.140 6.400 2.912 6.550 2.556 6.700 2.051 6.850 1.374
7.000 0.500 /
\multiput {$\bullet$} at 1 1 2.050 4.041 3.250 2.644
4.300 1.996 5.500 3.039 6.250 3.140 7 0.5 /
\setlinear\plot
1 1 2.050 4.041 3.250 2.644
4.300 1.996 5.500 3.039 6.250 3.140 7 0.5 /
\endpicture}}
%\endtexonly
%\figrdef{fig:approximating arc length}
%\htmlfigure{Integration_applications-arc_length_line_segments.html}
\caption{\label{fig:approximating arc length}
Approximating arc length with line segments.}
%\endcaption
\endfigure

Now we need to write a formula for the sum of the lengths of the line
segments, in a form that we know becomes an integral in the limit.  So
we suppose we have divided the interval $[a,b]$ into $n$ subintervals
as usual, each with length $\Delta x =(b-a)/n$, and endpoints $\ds
a=x_0$, $\ds x_1$, $\ds x_2$, \dots, $\ds x_n=b$.  The length of a
typical line segment, joining $\ds (x_i,f(x_i))$ to $\ds
(x_{i+1},f(x_{i+1}))$, is $\ds\sqrt{(\Delta x )^2
  +(f(x_{i+1})-f(x_i))^2}$.  By the Mean Value Theorem, %(\xrefn{thm:mvt}),
there is a number $\ds t_i$ in $\ds (x_i,x_{i+1})$
such that $\ds f'(t_i)\Delta x=f(x_{i+1})-f(x_i)$, so the length of
the line segment can be written as
$$
  \sqrt{(\Delta x)^2 + (f'(t_i))^2\Delta x^2}=
  \sqrt{1+(f'(t_i))^2}\,\Delta x.
$$
Then arc length is:
$$
  \lim_{n\to\infty}\sum_{i=0}^{n-1} \sqrt{1+(f'(t_i))^2}\,\Delta x=
  \int_a^b \sqrt{1+(f'(x))^2}\,dx.
$$
Note that the sum looks a bit different than others we have
encountered, because the approximation contains a $\ds t_i$ instead of an
$\ds x_i$. In the past we have always used left endpoints (namely, $\ds x_i$)
to get a representative value of $f$ on $\ds [x_i,x_{i+1}]$; now we are
using a different point, but the principle is the same.

To summarize, to compute the length of a curve on the interval
$[a,b]$, we compute the integral
$$\int_a^b \sqrt{1+(f'(x))^2}\,dx.$$
Unfortunately, integrals of this form are typically difficult or
impossible to compute exactly, because usually none of our methods for
finding antiderivatives will work. In practice this means that the
integral will usually have to be approximated.

\begin{example}{Circumference of a Circle}{Circumference of a Circle}\label{Circumference of a Circle}
Let $\ds f(x) = \sqrt{r^2-x^2}$, the upper half circle of radius
$r$. The length of this curve is half the circumference, namely $\pi
r$. Compute this with the arc length formula.
\end{example}

\begin{solution}
The derivative $f'$ is $\ds \ds -x/\sqrt{r^2-x^2}$ so the integral is
$$
  \int_{-r}^r \sqrt{1+{x^2\over r^2-x^2}}\,dx
  =\int_{-r}^r \sqrt{r^2\over r^2-x^2}\,dx
  =r\int_{-r}^r \sqrt{1\over r^2-x^2}\,dx.
$$
Using a trigonometric substitution, we find the antiderivative, namely
$\ds \arcsin(x/r)$. Notice that the integral is improper at both
endpoints, as the function $\ds \sqrt{1/(r^2-x^2)}$ is undefined when
$x=\pm r$. So we need to compute
$$
  \lim_{D\to-r^+}\int_D^0  \sqrt{1\over r^2-x^2}\,dx +
  \lim_{D\to r^-}\int_0^D  \sqrt{1\over r^2-x^2}\,dx.
$$
This is not difficult, and has value $\pi$, so the original integral,
with the extra $r$ in front, has value $\pi r$ as expected.
\end{solution}

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

\begin{enumialphparenastyle}

%%%%%%%%%%
\begin{ex}
 Find the arc length of $\ds f(x)=x^{3/2}$ on $[0,2]$.
\begin{sol}
 $\ds (22\sqrt{22}-8)/27$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the arc length of $\ds f(x) = x^2/8-\ln x$
on $[1,2]$.
\begin{sol}
 $\ln(2)+3/8$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}

Find the arc length of $\ds f(x) = (1/3)(x^2 +2)^{3/2}$
on the interval $[0,a]$.
\begin{sol}
 $\ds a+a^3/3$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the arc length of $f(x)=\ln(\sin x)$ on the
interval $[\pi/4,\pi/3]$.
\begin{sol}
 $\ds \ln((\sqrt2+1)/\sqrt3)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Let $a>0$. Show that the length of $y=\cosh x$ on
$[0,a]$ is equal to $\ds \int _0 ^a \cosh x\,dx$.
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the arc length of $f(x)=\cosh x$ on $[0, \ln 2]$.
\begin{sol}
 $3/4$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Set up the integral to find the arc length of $\sin x$
on the interval $[0,\pi]$; do not evaluate the integral. If you have
access to appropriate software, approximate the value of the integral.
\begin{sol}
 $\approx 3.82$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Set up the integral to find the arc length of $\ds y=xe^{-x}$
on the interval $[2,3]$; do not evaluate the integral. If you have
access to appropriate software, approximate the value of the integral.
\begin{sol}
 $\approx 1.01$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the arc length of $\ds y=e^x$ on the interval $[0,1]$.
(This can be done exactly; it is a bit tricky and a bit long.)
\begin{sol}
 $\ds \sqrt{1+e^2}-\sqrt2+
{1\over2}\ln\left({\sqrt{1+e^2}-1\over\sqrt{1+e^2}+1}\right)+
{1\over2}\ln(3+2\sqrt2)$
\end{sol}
\end{ex}

\end{enumialphparenastyle}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Higher-Ranked Polymorphism Subtyping Algorithm}
\label{chap:ITP}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In this chapter, we present a new algorithm for polymorphic subtyping
with mechanical formalizations in the Abella theorem prover.
There is little work on formalizing type inference algorithms before,
especially for higher-ranked systems,
due to the fact that environments and variable bindings are
tricky to mechanize in theorem provers.
In order to overcome the difficulty in formalization,
we propose the novel algorithm by means of \emph{worklist judgments}.
Worklist judgments turn complicated
global propagation of unification constraints into simple local substitutions.
Moreover, we exploit several ideas in the recent inductive
formulation of a type-inference algorithm by
\citet{dunfield2013complete}, which turn out to be useful
for mechanization in a theorem prover.

Building on these ideas we develop a complete formalization of
polymorphic subtyping in the Abella theorem prover. Moreover, we
 show that the algorithm is \emph{sound}, \emph{complete}, and \emph{decidable} with
respect to the well-known declarative formulation of polymorphic subtyping by
\citet{odersky1996putting}.
While these meta-theoretical results are not new, as far
as we know our work is the first to mechanically formalize them.

\section{Overview: Polymorphic Subtyping}

% We hope that
% this work encourages other researchers to use theorem provers for formalizing
% type-inference algorithms.

\input{Sources/ITP/sec2.tex}
\input{Sources/ITP/sec3.tex}
\input{Sources/ITP/sec4.tex}

\input{Sources/ITP/abella.tex}

%Dunfield and Krishnaswami calculus tracks the (partial) solutions of existential variables
%in the algorithmic context; they denote a delayed substitution that is
%incrementally applied to outstanding work as it is encoutered.
%Instead of reifying the substitution, our algorithm keeps track of an explicit list of
%outstanding work.


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{tikz}
\setlength{\parindent}{0pt}

\newtheorem*{theorem}{Theorem}
\newtheorem*{definition}{Definition}
\newtheorem*{lemma}{Lemma}
\newtheorem*{corollary}{Corollary}
\newtheorem{example}{Example}
\newtheorem*{trick}{Trick}
\newtheorem*{question}{Question}

\title{Lecture 13: Lagrange Multipliers}
\author{}
\date{}

\begin{document}

\maketitle

\section{Motivation of Lagrange Multipliers}

The usage of Lagrange multipliers is to maximize/minimize the value of a
function $f(x, y, z)$ where $x$, $y$, and $z$ are not independent, in other
words there is a constraint $g(x, y, z) = c$.

Real world example: In thermodynamics, we often deal with a system with
parameters such as temperature T, pressure P, and volume V. These parameters are
not independent, and they satisfy a relation $PV = nRT$.

Those kinds of problems cannot be solved by only checking the critical points of
the function $f(x, y, z)$, because they probably don't satisfy the existing
constraint $g(x, y, z) = c$.

\begin{example}
  Find the point closest to the origin on the hyperbola $xy = 3$.

  We need to minimize the function $f(x, y) = \sqrt{x^2 + y^2}$, or more
  conveniently, the function $f(x, y) = x^2 + y^2$, with the constraint
  $g(x, y) = xy = 3$.

  From geometric perspective, we can plot the function graph of
  $g(x, y) = xy = 3$, and the contour plot of the function
  $f(x, y) = x^2 + y^2$. We can see that with a large constant $c_1$, the graphs
  of $g(x, y) = 3$ and $f(x, y) = c_1$ have four intersection points; with a
  small constant $c_2$, the two graphs have no intersection point. With the
  correct solution $c_0$, the two graphs have exactly two intersection points.
\end{example}

\section{Solution with Lanrange Multipliers}

One key observation to the above example: At the minimum, the level curve of the
function $f(x, y)$ is tangent to the hyperbola $xy = 3$, which is another level
curve of the function $g(x, y)$.

Then how to find the point $(x, y)$ where the level curves of $f(x, y)$ and
$g(x, y)$ are tangent to each other?

Notice: The level curves of $f(x, y)$ and $g(x, y)$ are tangent to each other \\
$\iff$ The level curves of $f(x, y)$ and $g(x, y)$ have the same tangent line \\
$\iff$ The gradients of $f(x, y)$ and $g(x, y)$ are parallel to each other \\
$\iff$ $\nabla f = \lambda \nabla g$, where $\lambda \neq 0$.

Therefore, we can derive a system of equations from the above statement:

From $\nabla f = \lambda \nabla g$, we can derive that
\begin{equation*}
  \begin{cases}
    f_x = \lambda g_x \\
    f_y = \lambda g_y \\
  \end{cases}
\end{equation*}
Also we have the constraint $g(x, y) = c$.

Therefore, for a optimization problem of a function with two variables, the
derived system of equations is
\begin{equation*}
  \begin{cases}
    f_x = \lambda g_x \\
    f_y = \lambda g_y \\
    g(x, y) = c \\
  \end{cases}
\end{equation*}
It is sufficient to solve the point $(x, y)$ as well as the factor $\lambda$.
This factor $\lambda$ is called the Lagrange multiplier.

\begin{example}
  Find the point closest to the origin on the hyperbola $xy = 3$ (Continue'd).

  \begin{gather*}
    f(x, y) = x^2 + y^2 \\
    f_x = 2x \\
    f_y = 2y \\
    g(x, y) = xy \\
    g_x = y \\
    g_y = x \\
  \end{gather*}
  Therefore, we can derive the following system of equations to solve the
  minimum point of $f(x, y)$ under the constraint $g(x, y) = xy = 3$.
  \begin{equation*}
    \begin{cases}
      2x = \lambda y \\
      2y = \lambda x \\
      xy = 3 \\
    \end{cases}
  \end{equation*}
  If we focus on the first two equations, we can rewrite them as
  \begin{gather*}
    \begin{cases}
      2x - \lambda y = 0 \\
      \lambda x - 2y = 0 \\
    \end{cases} \\
    \begin{bmatrix}
      2 & -\lambda \\
      \lambda & -2 \\
    \end{bmatrix} \cdot
    \begin{bmatrix}
      x \\
      y \\
    \end{bmatrix} =
    \begin{bmatrix}
      0 \\
      0 \\
    \end{bmatrix} \\
  \end{gather*}
  For this linear system, we have a trivial solution $x = y = 0$, but it doesn't
  satisfy the constraint. Based on our exploration from geometric perspective,
  we know that there are two solutions other than $x = y = 0$ for this system of
  equations. Hence the linear system has more than one solution, which is
  equivalent to
  \begin{gather*}
    det(A) = 0 \\
    \begin{vmatrix}
      2 & -\lambda \\
      \lambda & -2 \\
    \end{vmatrix} = 0 \\
    \lambda^2 - 4 = 0 \\
    \lambda = \pm 2 \\
  \end{gather*}
  When $\lambda = 2$,
  \begin{gather*}
    \begin{cases}
      2x - 2y = 0 \\
      2x - 2y = 0 \\
      xy = 3 \\
    \end{cases}\\
    x = \sqrt{3}, y = \sqrt{3} \\
    or\, x = -\sqrt{3}, y = -\sqrt{3} \\
  \end{gather*}
  When $\lambda = -2$,
  \begin{gather*}
    \begin{cases}
      2x + 2y = 0 \\
      2x + 2y = 0 \\
      xy = 3 \\
    \end{cases} \\
    \textnormal{There is no solution.}
  \end{gather*}
  Therefore, there are two points closest to the origin on the hyperbola
  $xy = 3$, which are $(\sqrt{3}, \sqrt{3})$ and $(-\sqrt{3}, -\sqrt{3})$.
\end{example}

\section{Justification of Lagrange Multipliers}

At the constrained maximum/minimum points of a function $f$, in any direction
along the level sets of $g = c$, the rate of change of $f$ must be 0. If not,
then an adjacent point along the direction must be greater than or less than the
maximum/minimum point, which is a contradiction to the fact of maximum/minimum
point.

For any direction $\hat{u}$ tangent to the level set $g = c$, it holds that
\begin{gather*}
  \frac{df}{ds}|_{\hat{u}} = 0 \\
  \nabla f \cdot \hat{u} = 0 \\
\end{gather*}
Therefore, $\nabla f$ is perpendicular to any direction $\hat{u}$ tangent to the
level set of $g$. Therefore, $\nabla f$ is perpendicular to the level set of
$g$. Also according to the property of gradient, $\nabla g$ is also
perpendicular to the level set of $g$. Therefore
\[ \nabla f \parallel \nabla g \]

\section{Restriction of Lagrange Multipliers}

WARNING: the method of Lagrange multipliers doesn't tell whether a solution is a
maximum or minimum points. We need to check other solutions as well as boundary
values to determine whether it is a maximum point or minimum point.

Also, we cannot use the second derivative test to determine the type of a
solution by the method of Lagrange multipliers.

\begin{example}
  Suppose that we want to build a pyramid with the given triangular base and the
  given volume, find the shape with the minimum total surface area.

  Recall that the formula of the volume of a tetraheron is
  \[ V = \frac{1}{3}(\textnormal{base area})(\textnormal{height}) \]

  Since we are given the triangular base and the given volume, the height of the
  tetraheron is actually fixed. Let $h$ denote the fixed height.

  One method is to suppose that the coordinate of the top vertex is $(x, y, h)$.
  The coordinates of all other vertices are known: $(x_1, y_1, 0)$,
  $(x_2, y_2, 0)$, and $(x_3, y_3, 0)$. So we can get the function of the total
  surface area, which is a function of $x$ and $y$, and then try to minimize the
  function. The problem with this method is that the function can be too
  complex to minimize.

  \begin{tikzpicture}
    [help line/.style={dashed}]
    \draw (-3, -1.732) node[below left] {$P_1$};
    \draw (3, -1.732) node[below right] {$P_2$};
    \draw (0, 3.464) node[above] {$P_3$};
    \draw (-3, -1.732) -- (0, -1.732) node[below] {$a_1$} -- (3, -1.732);
    \draw (-3, -1.732) -- (-1.5, 0.866) node[above left] {$a_2$} -- (0, 3.464);
    \draw (3, -1.732) -- (1.5, 0.866) node[above right] {$a_3$} -- (0, 3.464);
    \draw (0, 0) node[right] {Q};
    \draw[help line] (0, 0) -- (0, -0.866) node[right] {$u_1$} -- (0, -1.732);
    \draw[help line] (0, 0) -- (-0.75, 0.433) node[right] {$u_2$} -- (-1.5, 0.866);
    \draw[help line] (0, 0) -- (0.75, 0.433) node[right] {$u_3$} -- (1.5, 0.866);
  \end{tikzpicture}

  Another method is to use different variables to calculate the total surface
  area. As shown in the above diagram, suppose that the cast point of the top
  vertex in the base is the point $Q$, and the lengths of the three edges are
  $a_1$, $a_2$, and $a_3$. From $Q$ we construct perpendicular lines to the
  three edges of the base, and let $u_1$, $u_2$, and $u_3$ denote their lengths.

  The heights of the three faces can be calculated as
  \begin{gather*}
    h_1 = \sqrt{u_1^2 + h^2} \\
    h_2 = \sqrt{u_2^2 + h^2} \\
    h_3 = \sqrt{u_3^2 + h^2} \\
  \end{gather*}
  The area of the three faces can be calculated as
  \begin{gather*}
    s_1 = \frac{1}{2}a_1\sqrt{u_1^2 + h^2} \\
    s_2 = \frac{1}{2}a_2\sqrt{u_2^2 + h^2} \\
    s_3 = \frac{1}{2}a_3\sqrt{u_3^2 + h^2} \\
  \end{gather*}
  Therefore, the total area of the three faces can be calculated as
  \begin{equation*}
    s = f(u_1, u_2, u_3) = \frac{1}{2}a_1\sqrt{u_1^2 + h^2} + \frac{1}{2}a_2\sqrt{u_2^2 + h^2} + \frac{1}{2}a_3\sqrt{u_3^2 + h^2}
  \end{equation*}
  Also we have the constraint about the base area:
  \begin{equation*}
    s_b = g(u_1, u_2, u_3) = \frac{1}{2}a_1u_1 + \frac{1}{2}a_2u_2 + \frac{1}{2}a_3u_3
  \end{equation*}
  We need to find the minimum point of the total area of the three faces
  $s = f(u_1, u_2, u_3)$. According to the method of Lagrange multipliers, we
  can derive the following system of equations:
  \begin{gather*}
    \begin{cases}
      f_{u_1} = \lambda g_{u_1} \\
      f_{u_2} = \lambda g_{u_2} \\
      f_{u_3} = \lambda g_{u_3} \\
      g(u_1, u_2, u_3) = c \\
    \end{cases} \\
    \begin{cases}
      \frac{a_1}{2}\frac{u_1}{\sqrt{u_1^2 + h^2}} = \lambda \frac{a_1}{2} \\
      \frac{a_2}{2}\frac{u_2}{\sqrt{u_2^2 + h^2}} = \lambda \frac{a_2}{2} \\
      \frac{a_3}{2}\frac{u_3}{\sqrt{u_3^2 + h^2}} = \lambda \frac{a_3}{2} \\
      g(u_1, u_2, u_3) = c \\
    \end{cases} \\
    \begin{cases}
      \frac{u_1}{\sqrt{u_1^2 + h^2}} = \lambda \\
      \frac{u_2}{\sqrt{u_2^2 + h^2}} = \lambda \\
      \frac{u_3}{\sqrt{u_3^2 + h^2}} = \lambda \\
      g(u_1, u_2, u_3) = c \\
    \end{cases} \\
  \end{gather*}
  Therefore, when the total surface area is minimum, $u_1 = u_2 = u_3$.
\end{example}

\end{document}


\chapter{Rod Cutting Algorithm}
The objective of this challenge is to determine how to cut an imaginary rod to
receive the maximum revenue. Revenue per rod length and initial rod lengths are
given. Note that, if the price for the initial rod length is high enough, the
optimal solution may require no cutting at all.

\section{Brute Force}
\imb[4][10]{\alg/rodcutting.cpp}
\imb[35][39]{\alg/rodcutting.cpp}
\imb[47][47]{\alg/rodcutting.cpp}
\imb[48][49]{\alg/rodcutting.cpp}

Since there are $n-1$ places to either cut or leave, there are $2^{n-1}$ ways in
total to cut a rod of $n$ inches. Therefore a brute force algorithm such as the
one presented solves all possible outcomes and finds the maximum value. Time
Complexity Analysis shows an exponential time function.
\newpage

\section{Dynamic Programming: Top-Down Approach}
\imb[12][22]{\alg/rodcutting.cpp}
\imb[35][39]{\alg/rodcutting.cpp}
\imb[41][45]{\alg/rodcutting.cpp}
\imb[48][49]{\alg/rodcutting.cpp}

The most critical reason for the brute force algorithm's exponentailly long time
conplexity is that brute force repeats sonving what is essentially the same set
of divisions. For example, when processing the cutting of a $9$ inch rod, brute
force algorithm processes the same 2, 3, and 4-inch dsivision as 6 different
methods $(2,~3,~4),~(3,~2,~4),\cdots$. This can be solved by recording results
of previous iterations of the recursion, or the \textbf{memoisation} of previous
data. With the top-down method presented above, the new array \imc{int* arr} is
a storage array for the previously processed results.

\section{Dynamic Programming: Bottom-Up Approach}
\imb[24][33]{\alg/rodcutting.cpp}
\imb[35][39]{\alg/rodcutting.cpp}
\imb[40][40]{\alg/rodcutting.cpp}
\imb[48][49]{\alg/rodcutting.cpp}

Unusual circumstances can lead to failures in top-down algorithms where not all
possible outcomes are tested. The bottom-up approach above often shows strength
in constant factors and overall stability, and can be mathematically proven in
its ability to always process all subproblems, although such proof will not be
provided in this manual.

Both Top-Down and Bottom-Up approaches show a time complexity of $T(n)=\Theta
\left(n^2\right)$.


\chapter{Introduction to Digital Systems}\label{cha:intr-digit-syst}

\setcounter{section}{8}

\clearpage

\section{From Binary Logic (Mathematics) to Logic Gates (Circuits)}\label{sec:from-binary-logic}

From what we have studied in Discrete Mathematics, we have the
following basic three logic functions:

\hfil\includegraphics[width=.7\textwidth]{../Graphics/Ch01Art29.png}\hfil

\clearpage

\hfil\includegraphics[width=\textwidth]{../Graphics/Ch01Art30b.png}\hfil

\clearpage

\hfil\includegraphics[width=\textwidth]{../Graphics/Ch01Art31a.png}\hfil

\clearpage

\hfil\includegraphics[width=\textwidth]{../Graphics/Ch01Art31b.png}\hfil

\clearpage

\hfil\includegraphics[width=.7\textwidth]{../Graphics/Ch01Art34a.png}\hfil

\clearpage

\section*{Revisiting Course Objectives}\label{sec:revis-course-object}

\subsection*{Theory (hands on paper)}\label{sec:theory-hands-paper}

\hfil\includegraphics[width=.5\textwidth]{../Graphics/Ch03Art42.png}\hfil

\subsection*{Simulation using Verilog}\label{sec:simul-using-veril}

\begin{lstlisting}
// Verilog model: Simple_Circuit
module Simple_Circuit (A, B, C, D, E);
  output D, E;
  input  A, B, C;
  wire	 w1;

  and 	 G1 (w1, A, B); //Optional gate instance
  not	 G2 (E, C);
  or	 G3 (D, w1, E);
endmodule
\end{lstlisting}

\subsection*{Hardware Implementation}\label{sec:hardw-impl}

\clearpage
\refstepcounter{PageDummyCounter}\label{sec:hardw-impl-5}
\hfil\includegraphics[height=\textheight]{../Graphics/Ch11Art513.png}\hfil

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../LectureNotesCS221"
%%% End:


\documentclass[letterpaper]{article}

\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Notes}
\date{December 1, 2014}
\maketitle
\section*{5.2 \#13}
if $\varphi:\mathbb{Z}+\mathbb{Z}\to\mathbb{Z}$ is a ring homomorphism then for every $m,n\in\mathbb{Z}$ we have $\varphi(m,n)=\varphi((m,0)+(0,n))=\varphi(m,0)+\varphi(0,n)=\varphi(\underbrace{1+1+\dots+1}_m,0)+\varphi(0,\underbrace{1+1+\dots+1}_n)=\underbrace{\varphi(1,0)+\dots\varphi(1,0)}_m+\underbrace{\varphi(0,1)+\dots\varphi(0,1)}_n=m\varphi(1,0)+n\varphi(0,1)$

now $\alpha=\varphi(1,0)$ and $\beta=\varphi(0,1)$ and $\alpha+\beta=\varphi(1,1)=1$ and so $\alpha+\beta=1$

also $\alpha\beta=\varphi(0,0)=0$.

\begin{enumerate}
\item
$\alpha=0$ then $\beta=1$ and so $\varphi(m,n)=m\cdot0+n\cdot 1=n$ which is a ring homomorphism
\item
$\beta=0$ then $\alpha=1$ and so $\varphi(m,n)=m$ which is a ring homomorphism.
\end{enumerate}

we start with a homomorphism, check all possible outputs and check that all outputs are homomorphisms.
\section*{last time}
$\varphi:R\to S$ is a ring hom

$\ker\varphi=x\in R:\varphi(x)=0$

$R/\ker\varphi=\{[x]:x\in R\}$

$x\sim y\Leftrightarrow x-y\in \ker\varphi$

$\Leftrightarrow\varphi(x)=\varphi(y)$

\section*{5.3}
\subsection*{definition}
given a comm ring $R$ and a non-empty subset $I\subseteq R$ we say that $I$ is an {\bfseries ideal} of(in) $R$ if
\begin{enumerate}
\item
for every two elements $x,y\in I$ we have $x+y,x-y\in I$ in particular $0\in I$.
\item
for every $r\in R$ and every $x\in I$ we have $rx\in I$

\end{enumerate}

\subsection*{examples}
every commutative ring has at least two ideals (unless $0=1$)

$\{0\}$ is an ideal of $R$

$R$ is an ideal of $R$.

if these are the only possible ideals then $R$ is a field

\subsection*{thrm}
let $R$ be a commutative ring. then $R$ is a field if and only if $\{0\}$ and $R$ are the only ideals of $R$.

\subsubsection*{proof}
$\Rightarrow$ assume that $R$ is a field, and let $I$ be an ideal of $R$. if $I=\{0\}$ we are done so assome $I\ne \{0\}$. we want to prove that $I=R$. let $x\ne0\in I$. because $R$ is a field then $x^{-1}\in R$ and $x^{-1}x\in I$ because $x\in I$ and so $1\in I$ and $r=r\cdot 1\in I$ and so $I=R$

$\Leftarrow$
Take $a\ne 0\in R$. we want to prove that $a$ is invertible.
$I=\{ra:r\in R\}\subseteq R$

claim: $I$ is an ideal of $R$. Now we have an ideal different from zero because $a\in I$ therefor $I=R$ and so $1\in I$. $\exists r\in R$ such that $ra=1$ and so $r=a^{-1}$.

\subsubsection*{observation}
\begin{enumerate}
\item
given $a\in R$ then $\{ra:r\in R\}$ is an ideal of $R$ denoted $Ra$ or $(a)$ or $aR$. In fact this is the smallest ideal of $R$ that contains $a$.

we call this the ideal generated by $a$.
\item
$1\in I$ iff $I=R$.
\end{enumerate}
\subsection*{definition}
for $R$ commutative ring, we say the $R$ is a {\bfseries principle ideal} if every ideal of $R$ is principal. that is every  ideal of $R$ is of the form $(a)$

\subsection*{definition}
we say that $R$ is a {\bfseries principal ideal domain} (PID) if $R$ is an integral domain and a principal ideal ring.

\subsection*{example}
fields are always principal ideal rings generated by $1$ (and PID).

$\mathbb{Z}$ is a PID.
\subsection*{example 5.3.1}
let $I\in \mathbb{Z}$ be an ideal different from 0. Let $a\in I$ be the smallest positive integer in $I$. (note that there is an element not zero in $I$ and so if there is a negative in $I$ then it's additive inverse is in $I$.

$\supseteq$

$a\in I\to ra\in I$

$\subseteq$

pick some $x\in I$ $x=qa+r$ where $0\le  r<0$ then $r=x+-q(a)$ and because $a\in I$ then $-qa\in I$ and $r\in I$ and so $r=0$ and so $x\in (a)$

\section*{examples}
let $K$ be a field then $K[x]$ is a PID.

let $I\subseteq K[x]$ be a nonzero ideal. let $q(x)\in I$ be a non-zero polynomial of minimal degree

claim:$I=(q(x))$
\subsubsection*{proof}
$q(x)K[x]\subseteq I$ is clear

let $f(x)\in I$. $f(x)=b(x)q(x)+r(x)$ where $r(x)=0$ or $\deg r(x)<\deg q(x)$

but $r(x)=f(x)+(-b(x)q(x))\in I$ of course $q(x)$ has minimal degree and so by this choice we must have $r(x)=0$ and so $f(x)$ is a multiple of $q(x)$ and we are done.
\end{document}


\documentclass[]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{natbib}

\usepackage[a4paper, margin=1in]{geometry}

%opening
\title{Magnetic Field Model Equations and a Load of Legendre Polynomials}
\author{Matt James}

\begin{document}

\maketitle

\section{Legendre Polynomials}

	This is the form of a Legendre polynomial (Rodrigues' formula):

	\begin{equation}
		P_n(x) = \frac{1}{2^n n!}\frac{\text{d}^n}{\text{d}x^n}(x^2 - 1)^n \label{EqLegendre}
	\end{equation}
	where $n$ is the degree of the polynomial. The first 5 degrees (0-4) are shown below:

	\begin{align}
		P_0(x) &= \frac{\text{d}^0}{\text{d}x^0} (x^2 - 1)^0 &= 1, \label{EqP0} \\
		P_1(x) &= \frac{1}{2} \frac{\text{d}}{\text{d}x} (x^2 - 1) &= x, \label{EqP1} \\
		P_2(x) &= \frac{1}{8} \frac{\text{d}^2}{\text{d}x^2} (x^2 - 1)^2 &= \frac{1}{2}(3x^2 - 1), \label{EqP2} \\
		P_3(x) &= \frac{1}{48} \frac{\text{d}^3}{\text{d}x^3} (x^2 - 1)^3 &= \frac{1}{2}(5x^3 - 3x), \label{EqP3} \\
		P_4(x) &= \frac{1}{384} \frac{\text{d}^4}{\text{d}x^4} (x^2 - 1)^4 &= \frac{1}{8}(35x^4 - 30x^2 + 3), \label{EqP4}
	\end{align}
 	and $x$ can be substituted for $\cos{\theta}$:

	\begin{align}
		P_0(\cos{\theta}) &= 1 \label{EqP0t}, \\
		P_1(\cos{\theta}) &= \cos{\theta}, \label{EqP1t} \\
		P_2(\cos{\theta}) &= \frac{1}{2}(3\cos^2{\theta} - 1 \label{EqP2t}), \\
		P_3(\cos{\theta}) &= \frac{1}{2}(5\cos^3{\theta} - 3\cos{\theta}), \label{EqP3t} \\
		P_4(\cos{\theta}) &= \frac{1}{8}(35\cos^4{\theta} - 30x^2 + 3). \label{EqP4t}
	\end{align}

	\subsection{Derivatives}

		These derivatives of the above equations \ref{EqP0}--\ref{EqP4} with respect to $x$ will come in handy later on...

		For equation \ref{EqP0}:

		\begin{align}
			\frac{\text{d}}{\text{d} x}P_0 (x) &= 0. \label{Eqd1P0}
		\end{align}

		For equation \ref{EqP1}:

		\begin{align}
			\frac{\text{d}}{\text{d} x} P_1 (x) &= 1. \label{Eqd1P1}
		\end{align}

		For equation \ref{EqP2}:

		\begin{align}
			\frac{\text{d}}{\text{d} x} P_2 (x) &= 3x, \label{Eqd1P2} \\
			\frac{\text{d}^2}{\text{d} x^2} P_2 (x) &= 3. \label{Eqd2P2}
		\end{align}

		For equation \ref{EqP3}:

		\begin{align}
			\frac{\text{d}}{\text{d} x} P_3 (x) &= \frac{1}{2}(15x^2 - 3), \label{Eqd1P3} \\
			\frac{\text{d}^2}{\text{d} x^2} P_3 (x) &= 15x, \label{Eqd2P3} \\
			\frac{\text{d}^3}{\text{d} x^3} P_3 (x) &= 15. \label{Eqd3P3}
		\end{align}

		For equation \ref{EqP4}:

		\begin{align}
			\frac{\text{d}}{\text{d} x} P_4 (x) &= \frac{5}{2}(7x^3 - 3x), \label{Eqd1P4} \\
			\frac{\text{d}^2}{\text{d} x^2} P_4 (x) &= \frac{15}{2}(7x^2 - 1), \label{Eqd2P4} \\
			\frac{\text{d}^3}{\text{d} x^3} P_4 (x) &= 105x, \label{Eqd3P4} \\
			\frac{\text{d}^4}{\text{d} x^4} P_4 (x) &= 105. \label{Eqd4P4}
		\end{align}

\section{Ferrers Normalized Legendre Polynomials}

	The associated Legendre polynomials are defined by \cite{Ferrers1877} as:

	\begin{equation}
		P_{n,m}(x) = (1 - x^2)^\frac{m}{2} \frac{\text{d}^m}{\text{d}x^m} P_n(x) \label{EqFerrers},
	\end{equation}
	where $m$ is the order.

	Some equations and their derivatives...

	\begin{align}
		P_{0,0} (x) &= 1, \\
		P_{0,0} (\cos{\theta}) &= 1, \\
		\frac{\text{d}}{\text{d} \theta} P_{0,0} &= 0.
	\end{align}

	\begin{align}
		P_{1,0} (x) &= x, \\
		P_{1,0} (\cos{\theta}) &= \cos{\theta}, \\
		\frac{\text{d}}{\text{d} \theta} P_{1,0} &= -\sin{\theta}.
	\end{align}

	\begin{align}
		P_{1,1} (x) &= (1 - x^2)^\frac{1}{2}, \\
		P_{1,1} (\cos{\theta}) &= \sin{\theta}, \\
		\frac{\text{d}}{\text{d} \theta} P_{1,1} &= \cos{\theta}.
	\end{align}

	\begin{align}
		P_{2,0} (x) &= \frac{1}{2} (3 x^2 - 1), \\
		P_{2,0} (\cos{\theta}) &= \frac{1}{2}(3\cos^2{\theta} - 1), \\
		\frac{\text{d}}{\text{d} \theta} P_{2,0} &= -3\cos{\theta}\sin{\theta}.
	\end{align}

	\begin{align}
		P_{2,1} (x) &= 3x(1-x^2)^\frac{1}{2}, \\
		P_{2,1} (\cos{\theta}) &= 3\cos{\theta}\sin{\theta}, \\
		\frac{\text{d}}{\text{d} \theta} P_{2,1} &= 3(2\cos^2{\theta}-1).
	\end{align}

	\begin{align}
		P_{2,2} (x) &= 3(1-x^2), \\
		P_{2,2} (\cos{\theta}) &= 3\sin^2{\theta}, \\
		\frac{\text{d}}{\text{d} \theta} P_{2,2} &= 6\sin{\theta}\cos{\theta}.
	\end{align}

	\begin{align}
		P_{3,0} (x) &= \frac{1}{2} (5x^3 - 3x), \\
		P_{3,0} (\cos{\theta}) &= \frac{1}{2} (5 \cos^3{\theta} - 3\cos{\theta}), \\
		\frac{\text{d}}{\text{d} \theta} P_{3,0} &= \frac{3}{2} \sin{\theta} (1 - 5 \cos^2{\theta}).
	\end{align}

	\begin{align}
		P_{3,1} (x) &= \frac{1}{2} \sqrt{(1-x^2)}(15x^2 - 3), \\
		P_{3,1} (\cos{\theta}) &= \frac{3}{2} \sin{\theta}(5\cos^2{\theta} -1), \\
		\frac{\text{d}}{\text{d} \theta} P_{3,1} &= \frac{3}{2} \left[\sin{\theta}\frac{\text{d}}{\text{d}\theta}(5\cos^2{\theta}-1) + (5\cos^2{\theta} - 1)\frac{\text{d}}{\text{d}\theta}\sin{\theta}\right], \\
		&= \frac{3}{2} (15\cos^3{\theta} - 11\cos{\theta}).
	\end{align}

	\begin{align}
		P_{3,2} (x) &= 15x (1-x^2), \\
		P_{3,2} (\cos{\theta}) &= 15 \cos{\theta} \sin^2{\theta}, \\
		\frac{\text{d}}{\text{d} \theta} P_{3,2} &= 15 \sin{\theta} (3\cos^2{\theta} - 1).
	\end{align}

	\begin{align}
		P_{3,3} (x) &= 15(1-x^2)^\frac{3}{2}, \\
		P_{3,3} (\cos{\theta}) &= 15 \sin^3{\theta}, \\
		\frac{\text{d}}{\text{d} \theta} P_{3,3} &= 45 \sin^2{\theta}\cos{\theta}.
	\end{align}

	\begin{align}
		P_{4,0} (x) &= \frac{1}{8}(35x^4 - 30x^2 + 3), \\
		P_{4,0} (\cos{\theta}) &= \frac{1}{8}(35 \cos^4{\theta} - 30\cos^2{\theta} + 3), \\
		\frac{\text{d}}{\text{d} \theta} P_{4,0} &= \frac{5}{2}(7\cos^3{\theta} - 3\cos{\theta}).
	\end{align}

	\begin{align}
		P_{4,1} (x) &= \frac{5}{2}(7x^3 - 3x) (1-x^2)^\frac{1}{2}, \\
		P_{4,1} (\cos{\theta}) &= \frac{5}{2} \sin{\theta}(7\cos^3{\theta} - 3\cos{\theta}), \\
		\frac{\text{d}}{\text{d} \theta} P_{4,1} &= \frac{5}{2}\left[(7\cos^3{\theta} - 3\cos{\theta})\frac{\text{d}}{\text{d}\theta}\sin{\theta} + \sin{\theta} \frac{\text{d}}{\text{d}\theta}(7\cos^3{\theta} - 3\cos{\theta})\right], \\
		&= \frac{5}{2}(28 \cos^4{\theta} - 27 \cos^3{\theta} + 3).
	\end{align}

	\begin{align}
		P_{4,2} (x) &= \frac{15}{2}(7x^2 - 1) (1-x^2), \\
		P_{4,2} (\cos{\theta}) &= \frac{15}{2} \sin^2{\theta}(7\cos^2{\theta} - 1), \\
		\frac{\text{d}}{\text{d} \theta} P_{4,2} &= \frac{15}{2}\left[(7\cos^2{\theta} - 1)\frac{\text{d}}{\text{d}\theta}\sin^2{\theta} + \sin^2{\theta} \frac{\text{d}}{\text{d}\theta}(7\cos^2{\theta} - 1)\right], \\
		&= 30\cos{\theta}\sin{\theta}(7\cos^2{\theta} - 4).
	\end{align}

	\begin{align}
		P_{4,3} (x) &= 105x(1-x^2)^\frac{3}{2}, \\
		P_{4,3} (\cos{\theta}) &= 105 \cos{\theta}\sin^3{\theta}, \\
		\frac{\text{d}}{\text{d} \theta} P_{4,3} &= 105\left[\cos{\theta}\frac{\text{d}}{\text{d}\theta}\sin^3{\theta} + \sin^3{\theta}\frac{\text{d}}{\text{d}\theta}\cos{\theta}\right], \\
		&= 105 \sin^2{\theta}(4\cos^2{\theta} - 1).
	\end{align}

	\begin{align}
		P_{4,4} (x) &= 105(1-x^2)^2, \\
		P_{4,4} (\cos{\theta}) &= 105 \sin^4{\theta}, \\
		\frac{\text{d}}{\text{d} \theta} P_{4,4} &= 420 \sin^3{\theta} \cos{\theta}.
	\end{align}

	\subsection{Recurrence Relations}
		\label{SectRecurrence}
		There are three recurrence relations which are used to calculate the associate polynomials from lower order/degree polynomials.

		\begin{align}
			&\text{A: }  m < n-1  \nonumber \\
			&P_{n,m} (\cos{\theta}) = \frac{1}{n-m} \left[(2n-1)\cos{\theta} P_{n-1,m} - (n + m -1)P_{n-2,m}\right] \\
			&\text{B: }  m = n-1  \nonumber \\
			&P_{n,m} (\cos{\theta}) = (2n-1)\sin{\theta} P_{n-1,m-1}, \\
			&\text{C: }  m = n  \nonumber \\
			&P_{n,m} (\cos{\theta}) = (2n-1)\sin{\theta} P_{n-1,m-1}.
		\end{align}

		They are based on code used elsewhere, but the difference being that a factor of $(-1)^m$ was removed from rule C (and also from their calculation of $S_{n,m}$ as the seem to cancel each other). Also rule B uses $\sin$ here as opposed to $\cos$ (I think the original code had a mistake in it). These changes effectively make rules B and C identical. The examples below should all match with those derived directly from equations \ref{EqLegendre} and \ref{EqFerrers} in the previous subsection.

		Examples for case A:

		\begin{align}
			P_{2,0} &= \frac{1}{2}\left[ 3\cos{\theta} P_{1,0} - P_{0,0} \right] \\
					&= \frac{1}{2}(3\cos^2{\theta} - 1) \\
			P_{3,0} &= \frac{1}{3}\left[5\cos{\theta}P_{2,0} - 2P_{1,0}\right] \\
					&= \frac{1}{2}(5 \cos^3{\theta} - 3\cos{\theta}) \\
			P_{3,1} &= \frac{1}{2}\left[5 \cos{\theta} P_{2,1} - 3P_{1,1}\right] \\
					&= \frac{3}{2}\sin{\theta}\left[5 \cos^2{\theta} - 1\right] \\
			P_{4,0} &= \frac{1}{4}\left[7\cos{\theta}P_{3,0} - 3P_{2,0}\right] \\
					&= \frac{1}{8}(35 \cos^4{\theta} - 30\cos^2{\theta} + 3) \\
			P_{4,1} &= \frac{1}{3}\left[7\cos{\theta} P_{3,1} - 4P_{2,1}\right] \\
					&= \frac{5}{2}\sin{\theta}\cos{\theta}(7\cos^2{\theta} - 3) \\
			P_{4,2} &= \frac{1}{2}\left[7\cos{\theta}P_{3,2} - 5P_{2,2}\right] \\
					&= \frac{15}{2}\sin^2{\theta}(7\cos^2{\theta} -1)
		\end{align}

		Examples for case B:

		\begin{align}
			P_{2,1} &= 3\sin{\theta}P_{1,0} \\
					&= 3\sin{\theta}\cos{\theta} \\
			P_{3,2} &= 5\sin{\theta}P_{2,1} \\
					&= 15\sin^2{\theta}\cos{\theta} \\
			P_{4,3} &= 7\sin{\theta}P_{3,2} \\
					&= 105\sin^3{\theta}\cos{\theta}
		\end{align}

		Examples for case C (I think this rule is basically the same as B):

		\begin{align}
			P_{1,1} &= \sin{\theta}P_{0,0} \\
					&= \sin{\theta} \\
			P_{2,2} &= 3\sin{\theta}P_{1,1} \\
					&= 3\sin^2{\theta} \\
			P_{3,3} &= 5\sin{\theta}P_{2,2} \\
					&= 15\sin^3{\theta} \\
			P_{4,4} &= 7\sin{\theta}P_{3,3} \\
					&= 105\sin^4{\theta}
		\end{align}

		The derivatives can be calculated using similar rules:

		\begin{align}
			&\text{A: }  m < n-1  \nonumber \\
			&\frac{\text{d}}{\text{d}\theta} P_{n,m} = \frac{1}{n-m} \left[(2n-1)\left(\cos{\theta}\frac{\text{d}}{\text{d}\theta}P_{n-1,m} - \sin{\theta}P_{n-1,m}\right) - (n+m-1)\frac{\text{d}}{\text{d}\theta}P_{n-2,m}\right] \\
			&\text{B: }  m = n-1  \nonumber \\
			&\frac{\text{d}}{\text{d}\theta} P_{n,m} = (2n-1) \left[\sin{\theta}\frac{\text{d}}{\text{d}\theta}P_{n-1,m-1} + \cos{\theta}P_{n-1,m-1}\right] \\
			&\text{C: }  m = n  \nonumber \\
			&\frac{\text{d}}{\text{d}\theta} P_{n,m} = (2n-1) \left[\sin{\theta}\frac{\text{d}}{\text{d}\theta}P_{n-1,m-1} + \cos{\theta}P_{n-1,m-1}\right]
		\end{align}

		Examples for case A:

		\begin{align}
			\frac{\text{d}}{\text{d}\theta}P_{2,0} &= \frac{1}{2}\left[3\left(\cos{\theta}\frac{\text{d}}{\text{d}\theta}P_{1,0} - \sin{\theta}P_{1,0}\right) - \frac{\text{d}}{\text{d} \theta} P_{0,0}\right] \\
				&= \frac{1}{2} (3 \cos^2{\theta} -1)\\
			\frac{\text{d}}{\text{d}\theta}P_{3,0} &= \frac{1}{3}\left[5\left(\cos{\theta}\frac{\text{d}}{\text{d}\theta}P_{2,0} - \sin{\theta}P_{2,0}\right) - 2\frac{\text{d}}{\text{d} \theta} P_{1,0}\right] \\
				&= \frac{1}{2}(5\cos^3{\theta} - 3\cos{\theta})\\
			\frac{\text{d}}{\text{d}\theta}P_{3,1} &= \frac{1}{2}\left[5\left(\cos{\theta}\frac{\text{d}}{\text{d}\theta}P_{2,1} - \sin{\theta}P_{2,1}\right) - 3\frac{\text{d}}{\text{d} \theta} P_{1,1}\right] \\
				&= \frac{3}{2}\sin{\theta}(5\cos^2{\theta} - 1)\\
			\frac{\text{d}}{\text{d}\theta}P_{4,0} &= \frac{1}{4}\left[7\left(\cos{\theta}\frac{\text{d}}{\text{d}\theta}P_{3,0} - \sin{\theta}P_{3,0}\right) - 3\frac{\text{d}}{\text{d} \theta} P_{2,0}\right] \\
				&= \frac{1}{8}(35\cos^4{\theta} - 30\cos^2{\theta} + 3)\\
			\frac{\text{d}}{\text{d}\theta}P_{4,1} &= \frac{1}{3}\left[7\left(\cos{\theta}\frac{\text{d}}{\text{d}\theta}P_{3,1} - \sin{\theta}P_{3,1}\right) - 4\frac{\text{d}}{\text{d} \theta} P_{2,1}\right] \\
				&= \frac{5}{2} \sin{\theta}\cos{\theta}(7\cos^2{\theta} - 3) \\
			\frac{\text{d}}{\text{d}\theta}P_{4,2} &= \frac{1}{2}\left[7\left(\cos{\theta}\frac{\text{d}}{\text{d}\theta}P_{3,2} - \sin{\theta}P_{3,2}\right) - 5\frac{\text{d}}{\text{d} \theta} P_{2,2}\right] \\
				&= \frac{15}{2}\sin^2{\theta}(7\cos^2{\theta} - 1)
		\end{align}

		Examples for case B:

		\begin{align}
			\frac{\text{d}}{\text{d}\theta}P_{2,1} &= 3 \left[\sin{\theta}\frac{\text{d}}{\text{d}\theta}P_{1,0} + \cos{\theta}P_{1,0}\right] \\
			&= 6 \cos^2{\theta} - 3\\
			\frac{\text{d}}{\text{d}\theta}P_{3,2} &= 5 \left[\sin{\theta}\frac{\text{d}}{\text{d}\theta}P_{2,1} + \cos{\theta}P_{2,1}\right] \\
			&= 15\sin{\theta}(3\cos^2{\theta} -1)\\
			\frac{\text{d}}{\text{d}\theta}P_{4,3} &= 7 \left[\sin{\theta}\frac{\text{d}}{\text{d}\theta}P_{3,2} + \cos{\theta}P_{3,2}\right] \\
			&= 105\sin^2{\theta}(4\cos^2{\theta} - 1)
		\end{align}

		Examples for case C:

		\begin{align}
			\frac{\text{d}}{\text{d}\theta}P_{2,2} &= 3 \left[\sin{\theta}\frac{\text{d}}{\text{d}\theta}P_{1,1} + \cos{\theta}P_{1,1}\right] \\
			&= 6 \cos{\theta}\sin{\theta} \\
			\frac{\text{d}}{\text{d}\theta}P_{3,3} &= 5 \left[\sin{\theta}\frac{\text{d}}{\text{d}\theta}P_{2,2} + \cos{\theta}P_{2,2}\right] \\
			&= 45\cos{\theta}\sin^2{\theta}\\
			\frac{\text{d}}{\text{d}\theta}P_{4,4} &= 7 \left[\sin{\theta}\frac{\text{d}}{\text{d}\theta}P_{3,3} + \cos{\theta}P_{3,3}\right] \\
			&= 420\cos{\theta}\sin^3{\theta}
		\end{align}

		The recurrence relations listed above can be used to calculate any of the associated Legendre polynomials provided that $P_{0,0}$, $P_{1,0}$ and $P_{1,1}$ are defined initially. Here are some examples below, where the left polynomial is formed using the polynomial(s) to the right of the arrow and the letter in brackets corresponds to the three rules listed above:

		\begin{align}
			P_{2,0} &\leftarrow P_{1,0},P_{0,0} &(C) \\
			P_{2,1} &\leftarrow P_{1,0}			&(B) \\
			P_{2,2} &\leftarrow P_{1,1}			&(A) \\
			\nonumber \\
			P_{3,0} &\leftarrow P_{2,0},P_{1,0} &(C) \\
			P_{3,1} &\leftarrow P_{2,1},P_{1,1} &(C) \\
			P_{3,2} &\leftarrow P_{2,1}			&(B) \\
			P_{3,3} &\leftarrow P_{2,2}			&(A) \\
			\nonumber \\
			P_{4,0} &\leftarrow P_{3,0},P_{2,0} &(C) \\
			P_{4,1} &\leftarrow P_{3,1},P_{2,1} &(C) \\
			P_{4,2} &\leftarrow P_{3,2},P_{2,2} &(C) \\
			P_{4,3} &\leftarrow P_{3,2}			&(B) \\
			P_{4,4} &\leftarrow P_{3,3}			&(A)
		\end{align}

		Note that the same rules apply for the derivatives too.

\section{Schmidt Normalized Legendre Polynomials}

	The Schmidt normalized Legendre polynomials, $P_n^m$, are defined by

	\begin{equation}
		P_n^m = S_n^m P_{n,m},
	\end{equation}

	where

	\begin{equation}
		S_{n,m} = \sqrt{(2-\delta_m^0)\frac{(n-m)!}{(n+m)!}},
	\end{equation}

	and $\delta_m^0 = 1$ when $m = 0$, and is $\delta_m^0 = 0$ otherwise.

\section{Calculating the Magnetic Field Model}

	The magnetic field, $\mathbf{B}$, is calculated from a scalar potential,

	\begin{equation}
		\mathbf{B} = -\mathbf{\nabla} V, \label{EqScPot}
	\end{equation}

	where $\mathbf{\nabla} = \left(\frac{\partial}{\partial r},\frac{1}{r}\frac{\partial}{\partial \theta},\frac{1}{r \sin{\theta}} \frac{\partial}{\partial \phi} \right) $ in spherical polar coordinates, $V$ is defined as \cite[e.g.][]{Connerney1998,Winch2005},

	\begin{equation}
		V = a\sum_{n=1}^{n_{max}} \left(\frac{a}{r}\right)^{n+1} \sum_{m=0}^{n} \left\{P_n^m(\cos{\theta}) \left[g_n^m \cos{(m\phi)} + h_n^m\sin{(m\phi)}\right]\right\}, \label{EqSphHarm}
	\end{equation}

	and $a$ is the radius of Jupiter (71,398~km).

	Using equation \ref{EqScPot} on \ref{EqSphHarm}, each component of the magnetic field is defined by,

	\begin{align}
		B_r &= -\frac{\partial V}{\partial r} &= \sum_{n=1}^{n_{max}} \left(\frac{a}{r}\right)^{n+2} (n+1) \sum_{m=0}^{n} \left\{P_n^m(\cos{\theta}) \left[g_n^m \cos{(m\phi)} + h_n^m\sin{(m\phi)}\right]\right\}, \\
		B_\theta &= -\frac{1}{r}\frac{\partial V}{\partial \theta} &= -\sum_{n=1}^{n_{max}} \left(\frac{a}{r}\right)^{n+2} \sum_{m=0}^{n} \left\{\frac{\text{d}}{\text{d}\theta} P_n^m(\cos{\theta}) \left[g_n^m \cos{(m\phi)} + h_n^m\sin{(m\phi)}\right]\right\}, \\
		B_\phi &= -\frac{1}{r\sin{\theta}}\frac{\partial V}{\partial \phi} &= -\frac{1}{\sin{\theta}}\sum_{n=1}^{n_{max}} \left(\frac{a}{r}\right)^{n+2} \sum_{m=0}^{n} \left\{mP_n^m(\cos{\theta}) \left[h_n^m \cos{(m\phi)} - g_n^m\sin{(m\phi)}\right]\right\}.
	\end{align}

	In the code, the above equations use $a =1$ and $r$ in units of $R_J$.

	\subsection{Cartesian Solution}

		Possibly dodgy derivation of the Cartesian solution to equation \ref{EqScPot}, i.e.

		\begin{equation}
			\mathbf{B} = -\left(\frac{\partial}{\partial x},\frac{\partial}{\partial y},\frac{\partial}{\partial z}\right) V. \label{EqSphHarmCart}
		\end{equation}

		This will be done by splitting equation \ref{EqSphHarm} into several parts and combining them at the end...

		\subsubsection{Part 1}

			The derivatives of
			\begin{equation}
				a \left(\frac{a}{r}\right)^{n+1},
			\end{equation}
			where $r = \sqrt{x^2 + y^2 + z^2}$ and we will define $u = a/r$.
			\begin{align}
				\frac{\partial}{\partial x} \left[ a \left( \frac{a}{r}\right)^{n+1} \right] &= a \frac{\text{d} u}{\text{d} u} u^{n+1} \cdot \frac{\text{d} u}{\text{d} r} \cdot \frac{\text{d} r}{\text{d} x},\\
				&= a\left((n+1)u^n\right)\cdot \left(-\frac{u^2}{a}\right) \cdot \left( \frac{a}{r}\right), \\
				&= -(n+1)\left(\frac{x}{r}\right)\left(\frac{a}{r}\right)^{n+2}.
			\end{align}

			All three components have similar solutions:

			\begin{align}
				\frac{\partial}{\partial x} \left[ a \left( \frac{a}{r}\right)^{n+1} \right] &= -(n+1)\left(\frac{x}{r}\right)\left(\frac{a}{r}\right)^{n+2}, \\
				\frac{\partial}{\partial y} \left[ a \left( \frac{a}{r}\right)^{n+1} \right] &= -(n+1)\left(\frac{y}{r}\right)\left(\frac{a}{r}\right)^{n+2}, \\
				\frac{\partial}{\partial z} \left[ a \left( \frac{a}{r}\right)^{n+1} \right] &= -(n+1)\left(\frac{z}{r}\right)\left(\frac{a}{r}\right)^{n+2}.
			\end{align}

		\subsubsection{Part 2}

			The derivatives of $P_n^m(\cos\theta)$ for each component can be done using the chain rule, e.g.:

			\begin{equation}
				\frac{\partial}{\partial x} P_n^m(\cos\theta) = \frac{\text{d}}{\text{d} \theta} P_n^m(\cos\theta) \frac{\partial \theta}{\partial x}.
			\end{equation}

			Using
			\begin{equation}
			\theta = \arccos\frac{z}{r} = \arccos{\frac{z}{\sqrt{x^2 + y^2 + z^2}}},
			\end{equation}
			and
			\begin{equation}
				\frac{\text{d}}{\text{d} u} \arccos{u} = \frac{-1}{\sqrt{1 - u^2}},
			\end{equation}
			the derivatives of $\theta$ are:
			\begin{align}
				\frac{\partial \theta}{\partial x} &= \frac{xz}{\rho r^2}, \\
				\frac{\partial \theta}{\partial y} &= \frac{yz}{\rho r^2}, \\
				\frac{\partial \theta}{\partial z} &= -\frac{\rho}{r^2},
			\end{align}
			where $\rho = \sqrt{x^2 + y^2}$.

			Legendre polynomials are therefore,
			\begin{align}
				\frac{\partial}{\partial x} P_n^m(\cos\theta) &= \left(\frac{xz}{\rho r^2}\right)\frac{\text{d}}{\text{d} \theta} P_n^m(\cos\theta), \\
				\frac{\partial}{\partial y} P_n^m(\cos\theta) &= \left(\frac{yz}{\rho r^2}\right)\frac{\text{d}}{\text{d} \theta} P_n^m(\cos\theta), \\
				\frac{\partial}{\partial z} P_n^m(\cos\theta) &= \left(-\frac{\rho}{r^2}\right)\frac{\text{d}}{\text{d} \theta} P_n^m(\cos\theta),
			\end{align}
			where $\frac{\text{d}}{\text{d} \theta} P_n^m(\cos\theta)$ is already calculated using the spherical polar version of the code (see section \ref{SectRecurrence}).

		\subsubsection{Part 3}

			This section deals with the derivative of
			\begin{equation}
				g_n^m \cos{(m\phi)} + h_n^m\sin{(m\phi)},
			\end{equation}
			which will be treated as a chain rule, e.g.
			\begin{equation}
				\frac{\partial}{\partial x}\left(g_n^m \cos{(m\phi)} + h_n^m\sin{(m\phi)}\right) = \frac{\text{d}}{\text{d}\phi}\left(g_n^m \cos{(m\phi)} + h_n^m\sin{(m\phi)}\right)\cdot\frac{\partial\phi}{\partial x}.
			\end{equation}

			The first step in that chain rule is as follows (and used in the spherical polar version),
			\begin{equation}
				\frac{\text{d}}{\text{d}\phi}\left(g_n^m \cos{(m\phi)} + h_n^m\sin{(m\phi)}\right) = m\left( h_n^m \cos{(m\phi)} - g_n^m \sin{(m\phi)}\right). \label{Eqdghdphi}
			\end{equation}

			Using $\phi = \arctan{(y/x)}$ and the fact that
			\begin{equation}
				\frac{\text{d}}{\text{d} u} \arctan{u} = \frac{1}{1 + u^2},
			\end{equation}
			the three derivatives of $\phi$ are,
			\begin{align}
				\frac{\partial \phi}{\partial x} = \frac{1}{1 + \left(\frac{y}{x}\right)^2}\cdot\frac{-y}{x^2} &= \frac{-y}{\rho^2}, \label{Eqdphidx} \\
				\frac{\partial \phi}{\partial y} = \frac{1}{1 + \left(\frac{y}{x}\right)^2}\cdot\frac{1}{x} &= \frac{x}{\rho^2},\label{Eqdphidy} \\
				\frac{\partial \phi}{\partial z} = \frac{1}{1 + \left(\frac{y}{x}\right)^2}\cdot 0 &= 0. \label{Eqdphidz}
			\end{align}

			Combining equation \ref{Eqdghdphi} with equations \ref{Eqdphidx}-\ref{Eqdphidz} gives,
			\begin{align}
				\frac{\partial}{\partial x}\left(g_n^m \cos{(m\phi)} + h_n^m\sin{(m\phi)}\right) &= -m\frac{y}{\rho^2}\left( h_n^m \cos{(m\phi)} - g_n^m \sin{(m\phi)}\right), \\
				\frac{\partial}{\partial y}\left(g_n^m \cos{(m\phi)} + h_n^m\sin{(m\phi)}\right) &= m\frac{x}{\rho^2}\left( h_n^m \cos{(m\phi)} - g_n^m \sin{(m\phi)}\right), \\
				\frac{\partial}{\partial z}\left(g_n^m \cos{(m\phi)} + h_n^m\sin{(m\phi)}\right) &= 0
			\end{align}

		\subsubsection{Combined solution}

			Using the three parts above to solve equation \ref{EqSphHarmCart}:

			\begin{align}
				B_x = -\frac{\partial V}{\partial x} &= \sum_{n=1}^{n_{max}} (n + 1)\left(\frac{x}{r}\right)\left(\frac{a}{r}\right)^{(n+2)} \sum_{m=0}^{n} \left\{ P_n^m(\cos{\theta}) \left[ g_n^m \cos{(m\phi)} + h_n^m \sin{(m\phi)}\right] \right\}    \nonumber \\
				&- a \sum_{n=1}^{n_{max}} \left(\frac{a}{r}\right)^{(n+1)} \sum_{m=0}^{n} \biggl\{ \left(\frac{xz}{\rho r^2}\right)\frac{\text{d}}{\text{d} \theta} P_n^m(\cos{\theta}) \left[g_n^m \cos{(m\phi)} + h_n^m \sin{(m\phi)} \right] \nonumber \\
				&+ m\frac{y}{\rho^2} P_n^m(\cos{\theta}) \left[ g_n^m\sin{(m\phi)} - h_n^m \cos{(m\phi)}\right]  \biggr\} \\
				B_y = -\frac{\partial V}{\partial y} &= \sum_{n=1}^{n_{max}} (n + 1)\left(\frac{y}{r}\right)\left(\frac{a}{r}\right)^{(n+2)} \sum_{m=0}^{n} \left\{ P_n^m(\cos{\theta}) \left[ g_n^m \cos{(m\phi)} + h_n^m \sin{(m\phi)}\right] \right\}    \nonumber \\
				&- a \sum_{n=1}^{n_{max}} \left(\frac{a}{r}\right)^{(n+1)} \sum_{m=0}^{n} \biggl\{ \left(\frac{yz}{\rho r^2}\right)\frac{\text{d}}{\text{d} \theta} P_n^m(\cos{\theta}) \left[g_n^m \cos{(m\phi)} + h_n^m \sin{(m\phi)} \right] \nonumber \\
				&+ m\frac{x}{\rho^2} P_n^m(\cos{\theta}) \left[ h_n^m \cos{(m\phi)} - g_n^m\sin{(m\phi)}\right]  \biggr\} \\
				B_z = -\frac{\partial V}{\partial z} &= \sum_{n=1}^{n_{max}} (n + 1)\left(\frac{z}{r}\right)\left(\frac{a}{r}\right)^{(n+2)} \sum_{m=0}^{n} \left\{ P_n^m(\cos{\theta}) \left[ g_n^m \cos{(m\phi)} + h_n^m \sin{(m\phi)}\right] \right\}    \nonumber \\
				&- a \sum_{n=1}^{n_{max}} \left(\frac{a}{r}\right)^{(n+1)} \sum_{m=0}^{n} \biggl\{ \left(\frac{\rho}{ r^2}\right)\frac{\text{d}}{\text{d} \theta} P_n^m(\cos{\theta}) \left[g_n^m \cos{(m\phi)} + h_n^m \sin{(m\phi)} \right]  \biggr\}.
			\end{align}

\bibliographystyle{agufull08}
\bibliography{ref}
\end{document}


\documentclass{article}
\begin{document}

\section{From V\&V by Oberkampf and Roy}

validation metrics - mathematical operators that compute the difference between the experimentally measured results and the simulation results.  V\&V in SC, pg 469 (See also page 473)

type 1 error: model builder's risk.  The error of rejecting the model when the model is actually correct (such as because of comparison with incorrect data) V\&V in SC, pg 481

type 2 error: model user's risk.  The error of accepting validity of the model when the model is actually invalid.  V\&V in SC, pg 482

Comparison of mean values.  V\&V in SC, pg 485.  $L_1$ or $L_2$ vector norms usually used:

\begin{equation}
  \| S - E \|_p = \left(\frac{1}{N}\sum^N_{i=1} | S(x_i) - E(x_i)|^p \right)^\frac{1}{p}
\end{equation}

Comparison of mean with experimental mean and variance. V\&V in SC, pg
493-497.  The goal is to determine the true error based on $\tilde{E}
= y_m - \bar{y}_e$ where $y_m$ is the computed value, $\bar{y}_e$ is
the experimental mean and $\tilde{E}$ is the estimated error.  The
experimental mean is:

\begin{equation}
  \bar{y}_e = \frac{1}{n} \sum^n_{i=1} y^i_e
\end{equation}

The sample standard deviations is:

\begin{equation}
  s = \left[ \frac{1}{n-1} \sum^n_{i=1} (y^i_e - \bar{y}_e)^2 \right ]^\frac{1}{2}
\end{equation}

The interval containing the true error $E = y_m - \mu$ where the level of confidence is $100(1-\alpha)$\% is:

\begin{equation}
  \left ( \tilde{E}-t_{\alpha/2,v}\cdot\frac{s}{\sqrt{n}},\tilde{E}+t_{\alpha/2,v}\cdot\frac{s}{\sqrt{n}}  \right )
\end{equation}

Comparison of means using interpolation of experimental data. V\&V in
SC, pg 500-501.  If there are multiple sets of experimental data that
relate input variables to results, then the interpolation of the sets
can be used for statistics.  The standard deviation can be calculated
as (where the sets of interpolated experimental data are $y^i_e(x)$):

\begin{equation}
  s(x) \sim \left [ \frac{1}{n-1} \sum^n_{i=1}\left ( y^i_e(x) - \bar{y}_e(x) \right ) ^2 \right ] ^\frac{1}{2}
\end{equation}

The true error is in the interval:

\begin{equation}
  \left ( \tilde{E}(x)-t_{\alpha/2,v}\cdot\frac{s(x)}{\sqrt{n}},\tilde{E}(x)+t_{\alpha/2,v}\cdot\frac{s(x)}{\sqrt{n}}  \right )
\end{equation}

I think this may not be very useful because of the number of
assumptions required and the shear amount of data that is required to
get enough $y^i_e$ values.

Comparison of means requiring linear regression of the experimental
data. V\&V in SC, pg 508-510. This is where there are a set of $n$
experimental measurements $(y^i_e,x_i)$.

The linear regression function is:

\begin{equation}
  \bar{y}_e(x) = \theta_1 + x\theta_2+\epsilon
\end{equation}

The estimate interval for the true mean is:

\begin{equation}
  \mu(x) \sim (\bar{y}(x) - SCI(x),\bar{y}(x)+SCI(x))
\end{equation}

Where $SCI(x)$ is the width of the Scheff\'e confidence interval as a function of $x$ and is:

\begin{equation}
  SCI(x) = s\sqrt{[2F(2,n-2,1-\alpha)]\left [ \frac{1}{n}+\frac{(x-\bar{x})^2}{(n-1)s^2_x} \right ] }
\end{equation}

\begin{equation}
  s = \sqrt{\frac{1}{n-1}\sum^n_{i=1}\left [ y^i_e-\bar{y}_e(x_i) \right ]^2}
\end{equation}
\begin{equation}
  \bar{x} = \frac{1}{n}\sum^n_{i=1}x_i
\end{equation}
\begin{equation}
  s^2_x=\frac{1}{n-1}\sum^n_{i=1}(x_i-\bar{x})^2
\end{equation}

\section{From Talking with Cristian}

%2015 March 31

The goal is to be able to tell if the sign of the correlation is
correct.  So say that we have the output variables, and they match in
probability distribution.  It is still a problem if for example the
main cause of the probability change is input i, but in the
experiment, low i causes high output, but in the simulation low i
causes low output, that is, the sign of the correlation is wrong.

This should be similar to the formula used before, but now we have
$P(ex | S)$ instead of just $P(ex)$.  This is the probability of ex
conditional on S.  The probability of the simulation (s) is the
results of the input chosen, the input chosen is related to the
experimental reading via the correlation of the sensors in the
experiments.

\begin{equation}
  \int_{-\infty}^{\infty}dSP(S) \int_{-\infty}^{\infty} dex(S-ex) P(ex | S)
\end{equation}

\section{Goals for 2015}

\begin{itemize}
\item Determine if the correlation between an input and output are the same in the experiment and in the calculated version. If the probability distributions are the same, is the same thing being calculated?
\item Look at the correlation between input and output.
\item Look at examining multiple figures of merit (distance measure?).  Especially if some of the output is correlated.
\item Look at the correlation between inputs.
\item (Optional)Look at time dependent analysis (fourier transforms and such.)
\end{itemize}

\end{document}


\documentclass[11pt,letterpaper]{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}

\usepackage[charter]{mathdesign}
\usepackage{fullpage}
\pagestyle{empty}

\input{../fncextra}
\newcommand{\pp}[2]{\frac{\partial #1}{\partial #2}}

\addtolength{\textheight}{2em}

\begin{document}

\begin{center}
  \bf
  Project: Wave goodbye
\end{center}

Water waves have occupied mathematicians for centuries. Almost all of the interesting models are nonlinear. One of these is the \emph{KdV equation},
\begin{equation}
\label{eq:KdV}
u_t + 6uu_x + u_{xxx} = 0.
\end{equation}
Unlike the linear advection equation, most solutions of~\eqref{eq:KdV} do not propagate unchanged. However, there are so-called \emph{soliton} solutions that do hold their shape as they move. The soliton solutions are
\begin{equation}
\label{eq:soliton}
   s(x,t;c) = \frac{c}{2} \operatorname{sech}^2 \left(  \frac{\sqrt{c}}{2}(x-ct) \right),
\end{equation}
where $c>0$ is the constant velocity. Solitons can be experimentally observed under the right conditions. They have an odd property: if a faster soliton overtakes a smaller one, they pass through each other with unchanged shapes, but the path of each soliton undergoes a shift; i.e., the solution $s(x,t;c)$ becomes $s(x-\xi,t;c)$ after the interaction.

If equation~\eqref{eq:KdV} is linearized about the soliton solution $s$, the perturbation $w$ is governed by
\begin{equation}
\label{eq:KdVlin}
  w_t = -6 ( s w_x + s_x w) - w_{xxx},
\end{equation}
which discretizes in $m$ points to
\newcommand{\bfs}{\mathbf{s}}
\begin{equation}
\label{eq:KdVlin-disc}
  \bfw'(t) = \bigl[ -6 \diag(\bfs)\mD_x -6 \diag(\mD_x\bfs) -\mD_{xxx} \bigr]\, \bfw = \mA\bfw,
\end{equation}
where $\bfs$ is the spatial discretization of $s(x,t;c)$ at a fixed time $t_*$. As usual, the eigenvalues of the Jacobian matrix in this equation govern time step restrictions due to absolute stability. The presence of the third derivative makes this problem quite stiff in general.

Another, more easily generalized model of waves is provided by the \emph{shallow water equations},
\begin{equation}
\label{eq:shallow}
  \begin{split}
    h_t + \pp{}{x} \bigl( (h-\delta) u  \bigr) + \pp{}{y} \bigl( (h-\delta) v  \bigr) &= 0 \\
    u_t + u u_x + vu_y &= -h_x - \epsilon u \\
    v_t + uv_x + vv_y &= -h_y - \epsilon v,
  \end{split}
\end{equation}
where $h(x,y,t)$ is the height of water above a neutral $z=0$ plane, $u(x,y,t)$ and $v(x,y,t)$ are the $x$-velocity and $y$-velocity of a particle on the surface of the water, and $\delta(x,y)<0$ describes the depth of the solid bed underneath the surface. The terms with $\epsilon$ represent frictional loss; and are often set to zero in theoretical work. As far as I know, the shallow water equations don't have pretty solutions like those of KdV.

\subsection*{Project assignment}
\label{sec:project-assignment}

You should submit only M-files. Five of them are required to produce output as described in detail below. \textbf{Each of these scripts should start with the line}
\begin{verbatim}
    close all, clear all
\end{verbatim}
While you may use book functions and other codes written for the class as guides, none of them will completely address any of the problems. You may also include as many other M-files as are needed to make the scripts run. In your submission, \textbf{include all other files needed, including those from the book.} You can locate all dependencies by entering, for example,
\begin{verbatim}
    matlab.codetools.requiredFilesAndProducts('p1')
\end{verbatim}

\textbf{If a script does not run  successfully, you may receive no credit on that part of the assignment. If any code is found to be plagiarized from the internet or another student, you may receive a zero on the entire assignment.}

\begin{enumerate}
\item Let equation~\eqref{eq:KdV} by posed over $-20<x<20$ with periodic end conditions. Using $m=300$ points and the initial condition $s(x,0;1)$ from~\eqref{eq:soliton}, compute the solution for $0\le t \le 10$. (Remember that the MATLAB IVP solvers allow you to request output at particular times; see the documentation.)
\begin{description}
    \item[p1.m] This script produces two separate figures. One shows the numerical solution as a function of $x$ at $t=0,2,4,6,8,10$. The other shows the difference between the numerical solution and the exact solution, as a function of $x$, for $t=0,2,4,6,8,10$.
\end{description}
\item Continuing with the setup in step 1, find the matrix $\mA$ of the linearization equation~\eqref{eq:KdVlin-disc} about the solution $s(x,0;1)$.
\begin{description}
    \item[p2.m] This script makes a plot of the eigenvalues of $\mA$ in the complex plane. Don't forget to use \texttt{axis}~\texttt{equal}!
\end{description}
\item Look up the stability region of the 4th order R-K method in Figure~12.5. The intersections of the region with the coordinate axes are at the origin, $-2\sqrt{2}+0i$, and $\pm2\sqrt{2}i$. Use this information, compute an upper bound for the time step in RK4. Then apply \texttt{ode45} to solve the problem (same conditions as in step~1), and find the maximum step size taken by the solver. (If it is larger than your upper bound, you did something wrong.)
\begin{description}
    \item[p3.m] This script prints out the upper bound and the observed maximum step size.
\end{description}
\item Still with $-20<x<20$, periodic end conditions, $m=300$, and $0\le t \le 10$, compute the numerical solution with initial condition $s(x,0;1) + s(x+8,0;2)$ at 81 equally spaced times in $[0,10]$ (including $t=0$ and $t=10$). You should be able to see clearly that the faster soliton overtakes the slower one, leaving them both shifted sideways from their original paths.
\begin{description}
    \item[p4.m] This script makes a \texttt{pcolor} plot showing the numerical solution as a function of $x$ and $t$.
\end{description}
\item Let the shallow water equations~\eqref{eq:shallow} be posed over $(x,y) \in [-8,8]\times[-4,4]$ and subject to periodicity in both coordinates. Let the depth function be
  \begin{equation*}
    \delta(x,y) = -2 + 1.75 \exp[-2(x-4)^2-2y^2],
  \end{equation*}
corresponding to a flat surface with a tall bump at $(4,0)$, and let $\epsilon=0.05$. Use $m=n=100$ points in space and let the initial conditions be $h(x,y,0)=\exp(-x^2/2)$, $u(x,y,0)=0$, and $v(x,y,0)=0$. Simulate for 100 equally spaced times between $t=0$ and $t=10$ (I recommend a nonstiff IVP solver). You should see the original bump split into two waves. The waves aren't perfect, though---they shed little waves behind them as they go. Each of the two main waves will interact with the bump as they flow past the line $x=4$.
\begin{description}
\item[p5.m] This script should generate an animated GIF of a \texttt{surf} or \texttt{pcolor} plot of $h(x,y,t)$ for the 100 times in your numerical solution. You will want a separate function file to define the ODE to be solved, so include that file too.
\end{description}

\end{enumerate}

\end{document}