\chapter{Matrix Decomposition}
\label{chapter:Matrix Decomposition}
\section{LU Decomposition}
\section{QR Decomposition}
\section{Cholesky Decomposition}
\section{Eigen value Decomposition}
\begin{definition}
	An $\mathbf{eigenvector}$ of an $n\times n$ matrix $A$ is a nonzero vector $\vec{x}$ such that $A\vec{x}=\lambda\vec{x}$ for some scalar $\lambda$.A scalar $\lambda$ is called $\mathbf{eigenvalue}$ of $A$ if there is a nontrivial solution \vec{x} of $A\vec{x}=\lambda\vec{x}$;such an \vec{x} is called an eigenvector corresponding to $\lambda$\footnote{An eigenvalue may be zero}.
\end{definition}

\section{Singular Value Decomposition}

\subsection{Definition}
\begin{definition}
	Any matrix can be decomposed as follows
	\begin{equation}\label{eqn:SVD}
	\underbrace{\vec{X}}_{N \times D}=\underbrace{\vec{U}}_{N \times N}\underbrace{\vec{\Sigma}}_{N \times D}\underbrace{\vec{V}^T}_{D \times D}
	\end{equation}
	where $\vec{U}$ is an $N \times N$ matrix whose columns are orthornormal(so $\vec{U}^T\vec{U}=\vec{I}$), $\vec{V}$ is $D \times D$ matrix whose rows and columns are orthonormal (so $\vec{V}^T\vec{V}=\vec{V}\vec{V}^T=\vec{I}_D$), and $\vec{\Sigma}$ is a $N \times D$ matrix containing the $r=\min(N,D)$ singular values $\sigma_i \geq 0$ on the main diagonal, with 0s filling the rest of the matrix.
\end{definition}
\subsection{Proof}
Let $A$ be an $m\times n$ matrix.Then $A^TA$ is symmetric and can be orthogonally diagonalized with eigenvectors.The \textbf{singular values} of $A$ are the square root of the eigenvalues of $A^TA$,denoted by $\sigma_1,\sigma_2,...,\sigma_n$.That is $\sigma_i = \sqrt{\lambda_i}$ for $1\leq i \leq n$.
The eigenvalues are usually arranged so that
\begin{equation}
\lambda_1 \geq \lambda_2 \geq...\geq \lambda_n \geq 0
\end{equation}

\begin{theorem}
	Suppose {$\vec{v}_1,\vec{v_2}...,\vec{v_n}$} is an orthogonal basis of $\mathbb{R}^n$ consisting of eigenvector of $A^TA$,arranged so that the corresponding eigenvalues of  $A^TA$ satisfy $\lambda_1 \geq \lambda_2 \geq...\geq \lambda_n \geq 0$  and suppose $A$ has r nonzero singular values.Then {$A\vec{v_1},...,A\vec{v_r}$} is an orthogonal basis for $ColA$,and $rankA=r$.
\end{theorem}
\begin{proof}
	Because $\mathbf{v_i}$ and $\vec{v_j}$ are orthogonal for $i\neq j$,
	\begin{equation}
	(A\vec{v_i})^T(A\vec{v_j}) = \vec{v_i}^TA^TA\vec{v_j} = \vec{v_i}^T(\lambda_j\vec{v_j})=0
	\end{equation}
\end{proof}
We therefore have
\begin{equation}
A\vec{v_i} = \sigma_i\vec{u_i}
\end{equation}
For a general vector $\vec{x}$,since eigenvectors are orthogonal unit vectors,we have
\begin{equation}
\vec{x} = (\vec{v_1}\cdot\vec{x})\vec{v_1} + (\vec{v_2}\cdot\vec{x})\vec{v_2} +...+(\vec{v_n}\cdot\vec{x})\vec{v_n}
\end{equation}
This means that
\begin{align}
& M\vec{x} = (\vec{v_1}\cdot\vec{x})M\vec{v_1} + (\vec{v_2}\cdot\vec{x})M\vec{v_2} +...+(\vec{v_n}\cdot\vec{x})M\vec{v_n} \\
& M\vec{x} = (\vec{v_1}\cdot\vec{x})\sigma_1\vec{u_1} + (\vec{v_2}\cdot\vec{x})\sigma_2\vec{u_2} +...+(\vec{v_n}\cdot\vec{x})\sigma_n\vec{u_n}
\end{align}
Remember that dot product can be computed using the vector transpose
\begin{equation}
\vec{v}\cdot\vec{u} = \vec{v^T}\vec{u}
\end{equation}
which leads to
\begin{align}
& M\vec{x} = \vec{u_1}\sigma_1\vec{v_1^T}\vec{x}+\vec{u_2}\sigma_2\vec{v_2^T}\vec{x}+...+\vec{u_n}\sigma_n\vec{v_n^T}\vec{x} \\
&  M = \vec{u_1}\sigma_1\vec{v_1^T}+\vec{u_2}\sigma_2\vec{v_2^T}+...+\vec{u_n}\sigma_n\vec{v_n^T}
\end{align}
And this is usually expressed by writing
\begin{equation}
M = U\Sigma V^T
\end{equation}
As for $\vec{u_i}$,we have
\begin{align*}
&\begin{cases}
(A^TA)\vec{v_i} = \lambda_i\vec{v_i}	\\
A\vec{v_i}    = \sigma_i\vec{u_i}	\\
\end{cases} \\
&\Rightarrow A^T\sigma_i\vec{u_i} = \lambda_i\vec{v_i} \\
&\Rightarrow \sigma_iA^T\vec{u_i} = \lambda_i\vec{v_i}\\
&\Rightarrow (AA^T)\vec{u_i}=\sigma_iA\vec{v_i} = \lambda_i\vec{u_i}\\
\end{align*}
So we can see that $\vec{u_i}$ is the eigenvector of symmetric matrix $AA^T$,and $\vec{v_i}$ is the eigenvector of symmetric matrix $A^TA$.In summary,$\vec{u_i}$ and \vec{v_i} are the \textbf{left-eigenvector} and \textbf{right-eigenvectors} of matrix $A$.
\subsection{Application}
\subsubsection{Principal Component Analysis}
The projection vectors for principal component projection are the left-eigenvectors


\section{Application: Simplification of quadratic forms}

\begin{outcome}
  \begin{enumerate}
  \item Determine whether or not a function of several variables is a
    quadratic form.
  \item Convert a quadratic form to and from matrix form.
  \item Apply a change of variables to a quadratic form.
  \item Diagonalize a quadratic form.
  \item Find the principal axes of a quadratic form.
  \item Sketch quadratic curves, such as circles and ellipses.
  \end{enumerate}
\end{outcome}

In this section, we will explore an application of the diagonalization
of symmetric matrices, namely, the simplification of quadratic forms.
Quadratic forms are special kinds of functions that arise, for
example, in calculus when we approximate some quantity up to terms of
second order.

\begin{definition}{Quadratic form}{quadratic-form}
  A \textbf{quadratic form}%
  \index{quadratic form} is a polynomial in $n$ variables in which
  each term is of degree 2. For example, the following is a quadratic
  form in $3$ variables:
  \begin{equation*}
    f(x,y,z) = 3x^2 + 3y^2 + 2xy - 4xz + 4yz.
  \end{equation*}
  More generally, a quadratic form is a function of the form
  \begin{equation*}
    f(x_1,\ldots,x_n) = q_1\,x_1^2 + \ldots + q_n\,x_n^2 + q_{12}\,x_1x_2 +
    \ldots + q_{ij}\,x_ix_j + \ldots + q_{n-1,n}\,x_{n-1}x_n.
  \end{equation*}
  The numbers $q_1,\ldots,q_n,q_{12},\ldots,q_{n-1,n}$, which may be
  positive, negative, or zero, are called the \textbf{coefficients}%
  \index{coefficient!of a quadratic form}%
  \index{quadratic form!coefficient} of the quadratic form.
\end{definition}

\begin{example}{Quadratic forms}{quadratic-form}
  Which of the following are quadratic forms?
  \begin{enumialphparenastyle}
    \begin{enumerate}
    \item $f(x,y,z) = 2xy + 3xz - 5yz$.
    \item $g(x,y,z) = x^2 + 2xy + y^2 + 3$.
    \item $h(x,y,z) = (x+y)^2 - z^2$.
    \end{enumerate}
  \end{enumialphparenastyle}
\end{example}

\begin{solution}
  The function $f$ is a quadratic form. The function $g$ is not a
  quadratic form, because the constant term, $+3$, is not of degree
  2. It should be either a coefficient times the square of a variable,
  or a coefficient times the product of two variables. The function
  $h$ is a quadratic form. We can simplify it to $h(x,y,z) = x^2 + 2xy
  + y^2 - z^2$.
\end{solution}

\begin{definition}{Matrix form of a quadratic form}{quadratic-form-matrix}
  Let $A$ be a symmetric $n\times n$-matrix, and let
  $\vect{v} = \mat{x_1,\ldots,x_n}^T$.  Then
  \begin{equation*}
    f(x_1,\ldots,x_n) = \vect{v}^T A\vect{v}
  \end{equation*}
  is a quadratic form in $n$ variables. Conversely, every quadratic
  form in $n$ variables can be uniquely written in this way. We call
  this the \textbf{matrix form} of the quadratic form%
  \index{quadratic form!matrix form}%
  \index{matrix form of quadratic form}.
\end{definition}

\begin{example}{Matrix form of a quadratic form}{quadratic-form-matrix}
  Find the coefficients of the quadratic form $f(x,y,z) = \vect{v}^T
  A\vect{v}$, where
  \begin{equation*}
    A = \begin{mymatrix}{rrr}
      1 &  2 &  4 \\
      2 &  0 & -1 \\
      4 & -1 & -2 \\
    \end{mymatrix}.
  \end{equation*}
\end{example}

\begin{solution}
  We have
  \begin{equation*}
    f(x,y,z)
    ~=~
    \begin{mymatrix}{ccc} x & y & z \end{mymatrix}
    \begin{mymatrix}{rrr}
      1 &  2 &  4 \\
      2 &  0 & -1 \\
      4 & -1 & -2 \\
    \end{mymatrix}
    \begin{mymatrix}{c} x \\ y \\ z \end{mymatrix}
    ~=~ x^2 - 2z^2 + 4xy + 8xz - 2yz.
  \end{equation*}
  Note that there is a term $2xy$ and a term $2yx$, which together
  yield $4xy$. Similarly the term $4xz$ and $4zx$ are combined into
  $8xz$, and the terms $-yz$ and $-zy$ are combined into $-2yz$.
\end{solution}

\begin{example}{Matrix form of a quadratic form}{quadratic-form-matrix2}
  Write the quadratic form
  \begin{equation*}
    f(x,y,z) = 5x^2 - y^2 + z^2 + 2xy - 4xz + 3yz
  \end{equation*}
  in matrix form.
\end{example}

\begin{solution}
  We can write this as
  \begin{equation*}
    f(x,y,z)
    ~=~
    \begin{mymatrix}{ccc} x & y & z \end{mymatrix}
    \begin{mymatrix}{rrr}
      5  &   1 &  -2 \\
      1  &  -1 & 1.5 \\
      -2 & 1.5 &   1 \\
    \end{mymatrix}
    \begin{mymatrix}{c} x \\ y \\ z \end{mymatrix}.
  \end{equation*}
  Note: since the matrix $A$ must be symmetric, we have no choice but
  to split the term $2xy$ evenly into $1xy$ and $1yx$. This explains
  why the $(1,2)$- and $(2,1)$-entries of the matrix are $1$. Also,
  $-4xz$ has been split into $-2xz$ and $-2zx$, and $3yz$ has been
  split into $1.5yz$ and $1.5zy$. In general, the matrix of the
  quadratic form
  \begin{equation*}
    f(x,y,z) = ax^2 + by^2 + cz^2 + dxy + exz + fyz
  \end{equation*}
  is
  \begin{equation*}
    \def\arraystretch{1.3}
    A = \begin{mymatrix}{rrr}
      a & \frac{d}{2} & \frac{e}{2} \\
      \frac{d}{2} & b & \frac{f}{2} \\
      \frac{e}{2} & \frac{f}{2} & c \\
    \end{mymatrix}.
  \end{equation*}
\end{solution}

We will now turn to the question of how to simplify quadratic
forms. The primary tool we have for doing so is a \textbf{change of
  variables}%
\index{change of variables!for a quadratic form}%
\index{quadratic form!change of variables}. This means replacing the
variables $x_1,\ldots,x_n$ by new variables $y_1,\ldots,y_n$ that are
linear combinations of $x_1,\ldots,x_n$.

\begin{example}{Change of variables}{quadratic-form-change-of-variables}
  Apply the change of variables
  \begin{eqnarray*}
    x &=& u \\
    y &=& v-w \\
    z &=& w
  \end{eqnarray*}
  to the quadratic form
  \begin{equation*}
    3x^2 + y^2 + 2xy + 2xz + 2yz
  \end{equation*}
\end{example}

\begin{solution}
  We have
  \begin{equation*}
    \begin{array}{l}
      3x^2 + y^2 + 2xy + 2xz + 2yz \\
      =~~ 3u^2 + (v-w)^2 + 2u(v-w) + 2uw + 2(v-w)w \\
      =~~ 3u^2 + v^2 - w^2 + 2uv.
    \end{array}
  \end{equation*}
\end{solution}

The simplest kind of quadratic form is one that involves only squared
variables, and no products of two different variables. We call such
quadratic forms \textbf{diagonal}.

\begin{definition}{Diagonal quadratic form}{diagonal-quadratic-form}
  A quadratic form is \textbf{diagonal}%
  \index{diagonal quadratic form}%
  \index{quadratic form!diagonal} if it is of the form
  \begin{equation*}
    f(x_1,\ldots,x_n) = q_1\,x_1^2 + \ldots + q_n\,x_n^2.
  \end{equation*}
\end{definition}

\begin{proposition}{Diagonalization of quadratic forms}{quadratic-form-diagonalize}
  Every quadratic form can be made diagonal by a change of variables.
\end{proposition}

\begin{proof}
  We first write the quadratic form in matrix form, i.e.,
  \begin{equation*}
    f(x_1,\ldots,x_n) = \vect{v}^T A \vect{v},
  \end{equation*}
  where $A$ is a symmetric matrix and
  $\vect{v}=\mat{x_1,\ldots,x_n}^T$. By
  Theorem~\ref{thm:diagonalization-symmetric}, $A$ is orthogonally
  diagonalizable. So let $P$ be an orthogonal matrix such that
  \begin{equation*}
    P^{-1}AP
    ~=~ P^TAP
    ~=~ D
    ~=~ \begin{mymatrix}{ccc}
      \eigenvar_1 & \cdots & 0      \\
      \vdots & \ddots & \vdots \\
      0      & \cdots & \eigenvar_{nn} \\
    \end{mymatrix}
  \end{equation*}
  is diagonal. Let $\vect{w}=\mat{y_1,\ldots,y_n}^T$ be a new set of
  variables such that $\vect{v} = P \vect{w}$.  Then
  \begin{equation*}
    \vect{v}^T A \vect{v}
    ~=~ \vect{w}^T P^TAP \vect{w}
    ~=~ \vect{w}^T D \vect{w}
    ~=~ \eigenvar_1y_1^2 + \ldots + \eigenvar_ny_n^2.
  \end{equation*}
  So the quadratic form is diagonal in the variables $y_1,\ldots,y_n$.
\end{proof}

\begin{example}{Diagonalization of quadratic forms}{quadratic-form-diagonalize}
  Consider the quadratic form
  \begin{equation*}
    f(x,y) = 3x^2 + 4xy + 6y^2.
  \end{equation*}
  Perform a change of variables so that the quadratic form becomes diagonal.
\end{example}

\begin{solution}
  We first write $f(x,y)$ in matrix form:
  \begin{equation*}
    f(x,y)
    = \begin{mymatrix}{cc} x & y \end{mymatrix}
    \begin{mymatrix}{rr} 3 & 2 \\ 2 & 6 \end{mymatrix}
    \begin{mymatrix}{c} x \\ y \end{mymatrix}.
  \end{equation*}
  Next, we orthogonally diagonalize the matrix
  $A = \begin{mymatrix}{rr} 3 & 2 \\ 2 & 6 \end{mymatrix}$. In
  Example~\ref{exa:diagonalization-symmetric}, we found that $P^{-1}AP = D$, where
  \begin{equation*}
    P = \frac{1}{\sqrt{5}} \begin{mymatrix}{rr} 1 & -2 \\ 2 & 1 \end{mymatrix}
    \quad\mbox{and}\quad
    D = \begin{mymatrix}{rr} 7 & 0 \\ 0 & 2 \end{mymatrix}.
  \end{equation*}
  Since $P$ is orthogonal, we also have $P^TAP=D$. Next, let $u,v$ be
  new variables such that
  \begin{equation*}
    \begin{mymatrix}{c} x \\ y \end{mymatrix}
    = P \begin{mymatrix}{c} u \\ v \end{mymatrix}
    = \begin{mymatrix}{c} (u-2v)/\sqrt{5} \\ (2u+v)/\sqrt{5} \end{mymatrix}
  \end{equation*}
  Then the change of variables gives
  \begin{equation*}
    f(x,y)
    ~=~ \begin{mymatrix}{cc} x & y \end{mymatrix}
    A
    \begin{mymatrix}{c} x \\ y \end{mymatrix}
    ~=~
    \begin{mymatrix}{cc} u & v \end{mymatrix}
    P^TAP
    \begin{mymatrix}{c} u \\ v \end{mymatrix}
    ~=~
    \begin{mymatrix}{cc} u & v \end{mymatrix}
    D
    \begin{mymatrix}{c} u \\ v \end{mymatrix}
    ~=~
    7u^2 + 2v^2.
  \end{equation*}
\end{solution}

\begin{example}{Diagonalization of quadratic forms}{quadratic-form-diagonalize2}
  Diagonalize the quadratic form
  \begin{equation*}
    f(x,y,z) = 3x^2 + 3y^2 + 2xy - 4xz + 4yz.
  \end{equation*}
\end{example}

\begin{solution}
  The matrix form of $f(x,y,z)$ is
  \begin{equation*}
    f(x,y,z)
    = \begin{mymatrix}{ccc} x & y & z \end{mymatrix}
    \begin{mymatrix}{rrr}
      3  & 1 & -2 \\
      1  & 3 &  2 \\
      -2 & 2 &  0 \\
    \end{mymatrix}
    \begin{mymatrix}{c} x \\ y \\ z \end{mymatrix}
    = \vect{v}^T A\vect{v}.
  \end{equation*}
  We orthogonally diagonalize the matrix $A$. In
  Example~\ref{exa:diagonalization-symmetric}, we found that
  $P^{-1}AP = D$, where
  \begin{equation*}
    P = \begin{mymatrix}{ccc}
      \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{3}} & \frac{1}{\sqrt{6}}  \\
      \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{3}}  & -\frac{1}{\sqrt{6}} \\
      0                  & \frac{1}{\sqrt{3}}  & \frac{2}{\sqrt{6}}  \\
    \end{mymatrix}
    \quad\mbox{and}\quad
    D = \begin{mymatrix}{rrr} 4 & 0 & 0 \\ 0 & 4 & 0 \\ 0 & 0 & -2 \end{mymatrix}.
  \end{equation*}
  Let $u,v,w$ be new variables such that
  \begin{equation*}
    \begin{mymatrix}{c} x \\ y \\ z \end{mymatrix}
    = P \begin{mymatrix}{c} u \\ v \\ w \end{mymatrix}.
  \end{equation*}
  Then the change of variables gives
  \begin{eqnarray*}
    f(x,y,z)
    &=& \begin{mymatrix}{ccc} x & y & z \end{mymatrix}
    A
    \begin{mymatrix}{c} x \\ y \\ z \end{mymatrix} \\
    &=&
    \begin{mymatrix}{ccc} u & v & w \end{mymatrix}
    P^TAP
    \begin{mymatrix}{c} u \\ v \\ w \end{mymatrix} \\
    &=&
    \begin{mymatrix}{ccc} u & v & w \end{mymatrix}
    D
    \begin{mymatrix}{c} u \\ v \\ w \end{mymatrix} \\
    &=&
    4u^2 + 4v^2 - 2w^2.
  \end{eqnarray*}
\end{solution}

Next, we turn our attention to the task of sketching the solutions of
quadratic equations in 2 or more variables.

\begin{example}{Sketching circles and ellipses}{sketching-circles-ellipses}
  Sketch the following curves:
  \begin{enumialphparenastyle}
    \begin{enumerate}
    \item $x^2+y^2=1$,
    \item $x^2+2y^2=1$.
    \end{enumerate}
  \end{enumialphparenastyle}
\end{example}

\begin{solution}
  (a) The curve $x^2+y^2=1$ is the familiar unit circle.
  \begin{equation*}
    \begin{tikzpicture}[scale=2]
      \draw[thick,red] (0,0) circle [radius=1];
      \draw[->] (-1.5,0) -- (1.5,0) node[right] {$x$};
      \draw[->] (0,-1.5) -- (0,1.5) node[below right] {$y$};
      \draw (-1,0) -- +(0,-.1) node[below left] {$-1$};
      \draw (1,0) -- +(0,-.1) node[below right] {$1$};
      \draw (0,-1) -- +(-.1,0) node[below left] {$-1$};
      \draw (0,1) -- +(-.1,0) node[above left] {$1$};
    \end{tikzpicture}
  \end{equation*}
  (b) The curve $x^2+2y^2=1$ is the same, except that it has been
  stretched by a factor of $1/\sqrt{2}$ in the $y$-direction.
  In other words, it is an ellipse with $x$-intercepts $\pm 1$ and
  $y$-intercepts $\pm 1/\sqrt{2}$.
  \begin{equation*}
    \begin{tikzpicture}[scale=2]
      \draw[thick,red,yscale={1/sqrt(2)}] (0,0) circle [radius=1];
      \draw[->] (-1.5,0) -- (1.5,0) node[right] {$x$};
      \draw[->] (0,-1.1) -- (0,1.1) node[below right] {$y$};
      \draw (-1,0) -- +(0,-.1) node[below left] {$-1$};
      \draw (1,0) -- +(0,-.1) node[below right] {$1$};
      \draw (0,{-1/sqrt(2)}) -- +(-.1,0) node[below left] {$-\frac{1}{\sqrt{2}}$};
      \draw (0,{1/sqrt(2)}) -- +(-.1,0) node[above left] {$\frac{1}{\sqrt{2}}$};
    \end{tikzpicture}
  \end{equation*}
\end{solution}

In general, for $a,b>0$, the curve $ax^2 + by^2 = 1$ is an ellipse
with $x$-intercepts $\pm 1/\sqrt{a}$ and $y$-intercepts
$\pm 1/\sqrt{b}$. Similarly, for $a,b,c>0$, the equation
$ax^2 + by^2 + cz^2 = 1$ describes a $3$-dimensional ellipsoid with
$x$-intercepts $\pm 1/\sqrt{a}$, $y$-intercepts $\pm 1/\sqrt{b}$, and
$z$-intercepts $\pm 1/\sqrt{c}$:
\begin{equation*}
  \begin{tikzpicture}[scale=2]
    \begin{scope}[y={(1cm,-0.3cm)},x={(-0.8cm,-0.4cm)},z={(0cm,1cm)}]
      \draw[->] (-1.5,0,0) -- (1.5,0,0) node[left] {$x$};
      \draw[->] (0,-1.5,0) -- (0,1.5,0) node[right] {$y$};
      \draw[->] (0,0,-1.5) -- (0,0,1.5) node[below right] {$z$};
    \end{scope}
    \fill[ball color=red!70] (0,0,0) circle [x radius=1.17, y radius=0.7, rotate=-10];
    \begin{scope}[y={(1cm,-0.3cm)},x={(-0.8cm,-0.4cm)},z={(0cm,1cm)}]
      \draw[->] (0.7,0,0) -- (1.5,0,0);
      \draw[->] (0,1,0) -- (0,1.5,0);
      \draw[->] (0,0,0.58) -- (0,0,1.5);
      \draw (0.7,0,0) -- +(0,0,0.1) -- +(0,0,-0.1) +(0,0,-0.45) node {$\frac{1}{\sqrt{a}}$};
      \draw (0.7,0,0) -- +(0,0.1,0) -- +(0,-0.1,0);
      \draw (0,1,0) -- +(0,0,0.1) -- +(0,0,-0.1) +(0,0,-0.4) node {$\frac{1}{\sqrt{b}}$};
      \draw (0,1,0) -- +(0.1,0,0) -- +(-0.1,0,0);
      \draw (0,0,0.58) -- +(-0.1,0,0) -- +(0.1,0,0);
      \draw (0,0,0.58) -- +(0,0.1,0) -- +(0,-0.1,0) +(0,-0.3,0) node {$\frac{1}{\sqrt{c}}$};
    \end{scope}
  \end{tikzpicture}
\end{equation*}

\noindent
Each ellipse or ellipsoid has a set of \textbf{principal axes}%
\index{principal axis}%
\index{quadratic form!principal axes}, which are its axes of
symmetry. When the quadratic forms are diagonal, as in the above
examples, the principal axes are the standard coordinate axes. When
the quadratic forms are not diagonal, the principal axes are the
eigenvectors of the matrix $A$. This is the content of the following
proposition.

\begin{proposition}{Principal axes of a quadratic form}{principal-axes}
  Consider a quadratic form in matrix form,
  $f(\vect{v}) = \vect{v}^TA\vect{v}$, where $A$ is a positive definite
  $n\times n$-matrix. Let $\set{\vect{u}_1,\ldots,\vect{u}_n}$ be an
  orthonormal set of eigenvectors of $A$, and let
  $\eigenvar_1,\ldots,\eigenvar_n$ be the corresponding eigenvalues.
  Then the solutions of the equation
  \begin{equation*}
    \vect{v}^TA\vect{v} = 1
  \end{equation*}
  form an $n$-dimensional ellipsoid whose principal axes are parallel
  to $\vect{u}_1,\ldots,\vect{u}_n$ and whose $\vect{u}_i$-intercepts
  are $\pm\frac{1}{\sqrt{\eigenvar_i}}$.
\end{proposition}

\begin{proof}
  Let $P$ be the orthogonal matrix whose columns are
  $\vect{u}_1,\ldots,\vect{u}_n$. From the proof of
  Proposition~\ref{prop:quadratic-form-diagonalize}, we know that the
  equation $\vect{v}^TA\vect{v} = 1$ is equivalent to
  $\eigenvar_1y_1^2 + \ldots + \eigenvar_ny_n^2=1$, where
  $y_1,\ldots,y_n$ are variables such that
  \begin{equation}\label{eqn:principal-axes}
    \vect{v} = P\begin{mymatrix}{c} y_1 \\ \vdots \\ y_n \end{mymatrix}.
  \end{equation}
  Since $A$ is positive definite, we have
  $\eigenvar_1,\ldots,\eigenvar_n>0$ by
  Proposition~\ref{prop:characterize-positive}.  We therefore know
  that in the $(y_1,\ldots,y_n)$-coordinate system, the equation
  $\eigenvar_1y_1^2 + \ldots + \eigenvar_ny_n^2 = 1$ describes an
  ellipsoid whose $y_1$-intercepts are $\pm1/\sqrt{\eigenvar_1}$, whose
  $y_2$-intercepts are $\pm1/\sqrt{\eigenvar_2}$, and so on. The only
  thing that remains to do is to figure out the direction of the
  coordinate axes. The $y_1$-axis points in the direction of the point
  with coordinates $(y_1,\ldots,y_n) = (1,0,\ldots,0)$.  Using the
  change of variables formula {\eqref{eqn:principal-axes}}, we find
  that this corresponds to the first column of $P$, i.e.,
  $\vect{v}=\vect{u}_1$. Similarly, the $y_2$-axis points in the
  direction of $\vect{u}_2$, and so on.
\end{proof}

\begin{example}{Sketching a quadratic curve}{sketching-quadratic}
  Sketch the curve $3x^2 + 4xy + 6y^2 = 1$.
\end{example}

\begin{solution}
  The matrix for the quadratic form $3x^2 + 4xy + 6y^2$ is
  \begin{equation*}
    A = \begin{mymatrix}{rr} 3 & 2 \\ 2 & 6 \end{mymatrix}.
  \end{equation*}
  From Example~\ref{exa:quadratic-form-diagonalize}, we know that the
  normalized eigenvectors of $A$ are
  \begin{equation*}
    \vect{u}_1
    ~=~ \frac{1}{\sqrt{5}} \begin{mymatrix}{r} 1 \\ 2 \end{mymatrix}
    \quad\mbox{and}\quad
    \vect{u}_2
    ~=~ \frac{1}{\sqrt{5}} \begin{mymatrix}{r} -2 \\ 1 \end{mymatrix},
  \end{equation*}
  with respective eigenvalues $\eigenvar_1 = 7$ and $\eigenvar_2 =
  2$. Therefore, by Proposition~\ref{prop:principal-axes}, the curve
  $3x^2 + 4xy + 6y^2 = 1$ is an ellipse with principal axes
  $\vect{u}_1$ and $\vect{u}_2$, and with respective intercepts
  $\pm1/\sqrt{7}$ and $\pm1/\sqrt{2}$.
  \begin{equation*}
    \begin{tikzpicture}[scale=3]
      \begin{scope}[color=black]
        \draw[->] (-1.5,0) -- (1.5,0) node[right] {$x$};
        \draw[->] (0,-1.5) -- (0,1.5) node[below right] {$y$};
        \draw (-1,0) -- +(0,-.1) node[below] {$-1$};
        \draw (1,0) -- +(0,-.1) node[below] {$1$};
        \draw (0,-1) -- +(-.1,0) node[left] {$-1$};
        \draw (0,1) -- +(-.1,0) node[left] {$1$};
      \end{scope}
      \begin{scope}[color=blue,cm={(1/sqrt(5),2/sqrt(5),-2/sqrt(5),1/sqrt(5),(0,0))}]
        \draw[thick,red] (0,0) circle [x radius={1/sqrt(7)}, y radius={1/sqrt(2)}];
        \draw[->,blue!50] (-1.5,0) -- (1.5,0);
        \draw[->,blue!50] (0,-1.5) -- (0,1.5);
        \draw[blue!50] (-1,0) -- +(0,.1) +(0,0.2) node {$-1$};
        \draw[blue!50] (1,0) -- +(0,.1) +(0,0.2) node {$1$};
        \draw[blue!50] (0,-1) -- +(-.1,0) +(-0.2,0) node {$-1$};
        \draw[blue!50] (0,1) -- +(-.1,0) +(-0.2,0) node {$1$};
        \draw ({-1/sqrt(7)},0) -- +(0,.1) +(-0.15,0.2) node {$-\frac{1}{\sqrt{7}}$};
        \draw ({1/sqrt(7)},0) -- +(0,.1) +(0.1,0.15) node {$\frac{1}{\sqrt{7}}$};
        \draw (0,{-1/sqrt(2)}) -- +(-.1,0) +(-0.25,0) node {$-\frac{1}{\sqrt{2}}$};
        \draw (0,{1/sqrt(2)}) -- +(-.1,0) +(-0.225,0) node {$\frac{1}{\sqrt{2}}$};
        %\fill[color=black] (1,0) circle [radius=0.9pt] node[right=5pt] {$(x,y)=(\frac{1}{\sqrt{5}},\frac{2}{\sqrt{5}})$};
        %\fill[color=black] (0,1) circle [radius=0.9pt] node[left=11pt] {$(x,y)=(-\frac{2}{\sqrt{5}},\frac{1}{\sqrt{5}})$};
        \draw[thick,blue,->] (0,0) -- (1,0) node[right] {$\vect{u}_1$};
        \draw[thick,blue,->] (0,0) -- (0,1) node[above] {$\vect{u}_2$};
      \end{scope}
    \end{tikzpicture}
  \end{equation*}
\end{solution}


\subsection{Orthogonal vectors}

Given a metric \(M\), two vectors \(v\) and \(u\) are orthogonal if:

\(v^TMu=0\)

For example if we have the metric \(M=I\), then two vectors are orthogonal if:

\(v^Tu=0\)


\section{Portfolio Theory}

\subsection*{Preliminaries}

Portfolio return and variance, two assets
$ \bar{r}_p = w_1 \bar{r}_1 +  w_2 \bar{r}_2 $ \\
$ \sigma_p^2 = w_1^2 \sigma_1^2 + w_2^2 \sigma_2^2 + 2 w_1 w_2 \sigma_{12}  $ \\
$ \sigma_p^2 = w_1^2 \sigma_1^2 + w_2^2 \sigma_2^2 + 2 w_1 w_2 \rho_{12} \sigma_1 \sigma_2  $ \\
$  \rho_{12} = \frac{\sigma_{12}}{\sigma_1 \sigma_2}$ \\

Quadratic formula is handy when solving for weights in the two asset case:
$x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}$

In general, for a vector of weights $\bm{w}$ , returns $\bm{r}$ and covariance matrix $\bm{\Sigma}$:
$ r_p = \bm{w} \cdotp \bm{r} $ \\ 	and $\bm{ \sigma_p}^2 = \bm{w}^\prime \cdot \bm{\Sigma} \cdot \bm{w} $
in Excel  \texttt{$\sigma_p^2$=MMULT(MMULT(TRANSPOSE($\bm{w}$),$\bm{\Sigma}$),$\bm{w}$)  }

\subsection*{Sharpe Ratio}
$SR = \frac{r-r_f}{\sigma}$ ,
Return-to-Risk Ratio $RRR_{i,p} = \frac{r_i-r_f}{Cov(r_i,r_p)/\sigma_p}$

\subsection*{Portfolio and individual assets}
In the presence of a risk-free asset, portfolio’s return is: $\bar{r}_p =r_F + \sum_{i=1}^{N} w_i (\bar{r}_i -r_F)$

{Individual contribution to expected return:}
$ \frac{\partial \bar{r}_p}{\partial w_i}  = \bar{r}_i - r_F$

{Individual contribution to volatility:}
$ \frac{\partial \sigma_p}{\partial w_i}  = \frac{Cov(\bar{r}_i,\bar{r}_p )}{\sigma_p}$

\subsection*{Tangency Portfolio}
Note $\bm{\bar{x}}$ is vector of excess returns and $\bm{\vec{1}}$ is a vector of ones of size $N$, number of stocks.

min $\bm{w}^\prime \cdot \bm{\Sigma} \cdot \bm{w} $  s.t. $\bm{w}^\prime \cdot \bm{\bar{x}} = m  $ solution: $w_T=\lambda \bm{\Sigma}^{-1} \bm{\bar{x}}$
where $\lambda = \frac{1}{\bm{\bar{x}^\prime}  \bm{\Sigma}^{-1}  \bm{\vec{1}}} $.
In summary, the tangency weights are: $w_T=\frac{1}{\bm{\bar{x}^\prime}  \bm{\Sigma}^{-1}  \bm{\vec{1}}} \bm{\Sigma}^{-1} \bm{\bar{x}}$.
In Excel use \texttt{$\bm{\Sigma}^{-1}$=MINVERSE($\bm{\Sigma}$)}, then \texttt{$\lambda=$MMULT(MMULT(TRANSPOSE($\bm{\bar{x}}$),$\bm{\Sigma}^{-1}$)),$\bm{\vec{1}}$)  } , lambda is a scalar,
then \\
\texttt{$w_T=\lambda$ MMULT($\bm{\Sigma}^{-1}$),$\bm{\bar{x}}$ )}. \\
Once the tangency portfolio is found, $RRR$ is the same for all stocks, meaning that we cannot perturb the weights of individual assets in this portfolio to further increase its risk return trade off.

\subsection*{R14Q4}
$r_i = b_{1} F_1 + b_{2,i} F_2 + \epsilon_i $\\
inputs:
$b_1=10$, $b_{2,i}=i$,
$F_1$ has $E[F_1]=0\%$ and $\sigma_{F_1}=1\%$ , $F_2$ has  $E[F_2]=1\%$ and $\sigma_{F_2}=1\%$, and $\epsilon_i$ has $E[\epsilon_i]=0\%$ and $\sigma_{\epsilon_i}=30\%$. $F_1$, $F_2$, $\epsilon_i$ are indep. of each other, and $r_f=0.75\%$.\\
find sharpe ratio and return-to-risk ratio RRR \\
$E[r_i] = b_1E[F_1] + b_2,iE[F_2] + E[\epsilon_i]=i \cdot 1\%$ \\

$V [r_i] = V [b_1F_1 + b_{2,i}F2 + \epsilon_i]= b_1^2V[F_1] + b_{2,i}^2V [F_2] + V [\epsilon_i] =  10^2  \times 0.01^2 + i^2  \times 0.01^2 + 0.3^2$ \\
$Cov(r_i,r_j) = Cov(b_1 F_1 + b_{2,i}F+2 + \epsilon_i, b_1 F_1 + b_{2,j} F_2 + \epsilon_j) = V[b_1 F_1] + Cov(b_{2,i}F_2 , b_{2,j} F_2 ) = b_1^2 V[F_1] + b_{2,i}b_{2,j}V[F_2] = 10^2 \times 0.01^2 + i \times j \times 0.01^2 $. With the variances (note variances have an extra-term) and covariances now we can form $\Sigma$ and compute sharpe ratio.

$RRR_{i,p} = \frac{r_i-r_f}{Cov(r_i,r_p)/\sigma_p}$  note $Cov(r_i, r_p) = Cov(r_i,\sum_{j=1}^{I} w_j r_j) = \sum_{j=I}^{I}w_j Cov(r_i, r_j) $ \\


\subsection{Periodic activation function}
By dilating $\sigma$ if necessary, we may assume without loss of generality that $\sigma$ is periodic on $[0,1]$. Consider the Fourier series of $\sigma$
\begin{equation}
\sigma(x) = \displaystyle\sum_{i=-\infty}^\infty a_i e^{2\pi ix},
\end{equation}
with coefficients
\begin{equation}\label{eq_1027}
a_i =  \int_0^{1} \sigma(b)e^{-2\pi ib}db.
\end{equation}
The assumption that $\sigma$ is non-constant means that there exists some $i$ such that $a_i \neq 0$. Note that we do not need the Fourier series to converge pointwise to $\sigma$, all we need is for some $a_i$ to be non-zero and the integrals in \eqref{eq_1027} to converge (which is does since $\sigma\in W^{m,\infty}$). Notice that shifting $\sigma$ by $t$, i.e. replacing $\sigma$ by $\sigma(\cdot+t)$, scales the coefficient $a_i$ by $e^{it}$. Setting $t = (\omega \cdot x)$, we get
\begin{equation}
e^{2\pi i\omega\cdot x} = \frac{1}{a_i}\int_0^{1} \sigma\left(\omega\cdot x + b\right)e^{-2\pi ib}db.
\end{equation}
Plugging this into the Fourier representation of $u$, we see that
\begin{equation}
u(x) = \int_{\mathbb{R}^d} e^{2\pi i\omega\cdot x}\hat{u}(\omega)d\omega = \frac{1}{ a_i}
\int_{\mathbb{R}^d}\int_0^{1}\sigma\left(\omega\cdot x + b\right)e^{-2\pi ib}\hat{u}(\omega)dbd\omega.
\end{equation}
Since $u(x)$ is real, we can add this to its conjugate to obtain the representation
\begin{equation}\label{eq_1029}
u(x) = \int_{\mathbb{R}^d} e^{2\pi i\omega\cdot x}\hat{u}(\omega)d\omega = \frac{1}{|a_i|}
\int_{\mathbb{R}^d}\int_0^{1}\sigma\left(\omega\cdot x + b\right)e^{-ib}\hat{u}(\omega)dbd\omega.
\end{equation}


\section{Continuity}

\subsection{Exercise 1}
Continuity implies $\lim_{h \to 0} [f(x + h) - f(x - h)] = 0$, since we can choose $h$ to be within
$\delta$ of $x$ such that $\abs{f(x + h) - f(x) + f(x) - f(x - h)} \leq \abs{f(x + h) - f(x)}
+ \abs{f(x - h) - f(x)} < \epsilon$. However, the converse (as asked in the question) need not be
true, since we don't have to have $\lim_{h \to 0} f(x + h) = f(x) = \lim_{h \to 0} f(x - h)$.
For example, consider $x \neq 0 \implies f(x) = \frac{1}{\abs{x}}, \: f(0) = 0$.

\subsection{Exercise 2}
Suppose $p$ is a limit point of $E$. Then there is a sequence $(x_n) \in E \: | \: \lim_{n \to \infty} x_n = p$.
Since $f$ is continuous, we have that $lim_{n \to \infty} f(x_n) = f(p)$, which implies that $f(p)$ is
a limit point of $f(E)$ giving us that $f(\overline{E}) \subset  \overline{f(E)}$.

To see that $f(\overline{E})$ can be a proper subset, consider $f: \mathbb{Z}^{+} \to \mathbb{Q}$ with
$f(x) = \frac{1}{x}$. Then $f$ is continuous and $0 \notin f(\overline{\mathbb{Z}^{+}}) = f(\mathbb{Z}^{+})$.

\subsection{Exercise 3}
Similar to Exercise 2: if $p$ is a limit point of $Z(f)$, then there exists some sequence $(x_n) \in E \: | \:
\lim_{n \to \infty} x_n = p$. Since $f$ is continuous, we have that $\lim_{n \to  \infty} f(x_n) = f(p)$.
Then it follows that $x_n \in Z(f) \implies f(x_n) = 0 \implies f(p) = 0$.

\subsection{Exercise 4}
The fact that $f(E)$ is dense in $f(X)$ follows from Exercise 2, since $X = \overline{E}$.
Similarly, $\lim_{n \to \infty} g(p_n) = g(p) \implies \lim_{n \to \infty} f(p_n) = g(p)$
since $p_n \in E$. Thus, $g(p) = f(p)$ for all $p \in X$.

\subsection{Exercise 5}
If $f$ is defined on an open set in $\mathbb{R}^1$, then it need not be defined at its endpoints.
For example, consider $f(x) = \frac{1}{x}$ defined on $(0, 1)$. However, if $f$ is defined on a
closed subset $E \subset \mathbb{R}^1$, then $E^c$ is an open set in $\mathbb{R}$ and can thus be
decomposed into the union of a countable number of open intervals $(a_n, b_n)$. We can thus take
$g$ to be $g(x) = \frac{b_n - x}{b_n - a_n} f(a_n) + (1 - \frac{b_n - x}{b_n - a_n}) f(b_n)$
(the straight line interpolation between $f(a_n)$ and $f(b_n)$).

\subsection{Exercise 6}
$f$ is a bijection from $E$ to its graph $G(E)$. If $f$ is continuous, then we can take the inverse image of
an open cover of $G(E)$ to get an open cover of $E$. Since $E$ is compact, this open cover must have a
finite subcover whose image under $f$ will be a finite subcover for $G(E)$, thereby giving the compactness of
$G(E)$.

I looked up a hint on the reverse direction. Consider an infinite (finite case presents no issues) closed set
$V \subset G(E)$. Take some arbitrary subsequence $(x_k, f(x_k)) \in V$.
By the compactness of $G(E)$, this subsequence has a limit point $(x, f(x)) \in G(E)$, and this limit point is
contained in $V$ since $V$ is closed. Thus, $f^{-1} (V)$ also contains $x_k \to x$, implying that $f^{-1} (V)$
contains all of its limit points and is therefore closed. This shows that $f$ is continuous.

For what it's worth, I think
\href{http://at.yorku.ca/cgi-bin/bbqa?forum=ask_an_analyst_2005&task=show_msg&msg=3485.0001.0001.0001}{this}
argument using projections is much nicer.

\subsection{Exercise 7}
Suppose for any $M$ that $\exists x, y \: | \: f(x, y) > M$ (we consider only the case where $x > 0$, as the
other case is identical). Then we can solve the resulting quadratic to see that, if such $x$ and $y$ exist,
then $x > \frac{y^2 (1 + \sqrt{1 - 4M^2})}{2M}$. However, $\sqrt{1 - 4M^2}$ is not defined in $\mathbb{R}$
for $M > \frac{1}{2}$, so $f$ must be bounded. Performing the same analysis for $g$ yields
$x > \frac{y^2 (1 + \sqrt{1 - 4y^2M^2})}{2M}$. Since $y$ can be chosen to make the inequality for $x$ have
a solution in $\mathbb{R}$, $g$ is unbounded.

To show that $f$ is discontinuous at $(0, 0)$, we need only consider the sequence consisting of
$(0, \frac{n}{n + 1})$ to see that $\lim_{n \to \infty} f(0, \frac{n}{n+1}) = 1 \neq 0$. Plugging in
$y = ax + b$ leads to $f$ and $g$ being quotients of two polynomials with non-zero denominator,
indicating that they're both continuous.

\subsection{Exercise 8}
Suppose $f$ is not bounded. Then there is a sequence $f(x_n) \: | \: \forall N, \exists m, n \geq N \: \abs{f(x_n) - f(x_m)} > \epsilon$ for some
$\epsilon$, since otherwise $f(x_n)$ would converge to some point of $\mathbb{R}$. As $f$ is
uniformly continuous, this means that $\abs{x_n - x_m} > \delta$ for infinitely many $n, m$.
However, that would then imply that $E$ is not bounded, which is a contradiction. Thus, $f$ is bounded on
$E$.

If $E$ is not bounded, we can just take $f(x) = x$.

\subsection{Exercise 9}
Let $E$ consist of all $x, y \: | \: d_X (x, y) < \delta$. Then $\text{diam} E < \delta$. Similarly, if
$\forall x, y \: d_Y(f(x), f(y)) < \epsilon$, then $\text{diam} f(E) < \epsilon$.

\subsection{Exercise 10}
Suppose $f$ is not uniformly continuous. Then there is a sequence $x_n \in X \: | \: x_n \to x$, but
$\forall N, \exists m, n \geq N \: | \: d_Y(f(x_n), f(x_m)) > \epsilon$ for some $\epsilon > 0$. This,
however, makes $f(x_n)$ an infinite subset of $f(X)$ which does not have a limit point, thereby
contradicting the fact that $f(X)$ is compact.

\subsection{Exercise 11}
The first part of this exercise is basically what I was doing for Exercises 8 and 10. Since $f$
is uniformly continuous, $\exists \delta \: | \: d_X(x_n, x_m) < \delta \implies d_Y(f(x_n), f(x_m)) < \epsilon$
. Since $(x_n)$ Cauchy converges, we can make $d_X(x_n, x_m)$ arbitrarily small, which then implies that
we can make $d_Y(f(x_n), f(x_m))$ arbitrarily small, indicating that $f(x_n)$ Cauchy converges as well.

\subsection{Exercise 12}
To state it more precisely: if $f: X \to Y$ and $g: Y \to Z$ are both uniformly continuous, then $g \circ f$
is also uniformly continuous.

From uniform continuity of $g$, $\exists \delta \: | \: d_Y(y_1, y_2) < \delta \implies d_Z(g(y_1), g(y_2)) < \epsilon$.
Since $f$ is uniformly continuous, $\exists \delta' \: | \: d_X(x_1, x_2) < \delta' \implies
d_Y(f(x_1), f(x_2)) < \delta$. The existence of this $\delta'$ gives us that $g \circ f$ is uniformly
continuous.

\subsection{Exercise 13}
Suppose $p$ is a limit point of $E$ and $x_n \in E \: | \: x_n \to p$.
Then $f(x_n)$ Cauchy converges to a point $q$ in the codomain of $f$. We can simply take $g(p) = q$
whenever $p \notin E$ to get a continuous extension of $f$. Since this proof depends only on the convergence
of the Cauchy sequence $f(x_n)$ to a point in the codomain, it will hold for the codomain being any
complete metric space.

\subsection{Exercise 16}
The function $[x]$ has a simple discontinuity at every integer $x$, since the left-hand limit is  $x-1$
and the right-hand limit is $x$. Similarly, the function $(x)$ also has a simple discontinuity at every integer,
 since the left-hand limit is 1 and the right-hand limit is 0.

\subsection{Exercise 17}
We proceed as hinted in the text. The two types of simple discontinuity we need to consider are
$f(x-) \neq f(x+)$ and $f(x-) = f(x+) \neq f(x)$. For the first case, suppose (WLOG) that
$f(x-) < f(x+)$. Then we can construct a rational triple $(p, q, r)$ such that
\begin{align*}
        &f(x-) < p < f(x+) \\
        &a < q < t < x \implies f(t) < p \\
        &x < t < r < b \implies f(t) > p
\end{align*}
To see that such a triple can only be associated with one such $x$, consider $x' = x + \epsilon$ with
$\epsilon > 0$ (the other case is identical). Then we can choose $t \in (x, x')$ with $q < x < t < r < x'$,
which means $t > q$ does not imply $f(t) < p$. This handles simple discontinuities of the form
$f(x-) \neq f(x+)$.

We can similarly handle the case where $f(x-) = f(x+) \neq f(x)$. Suppose (WLOG) that $f(x) > f(x+)$ ;
we can then construct a rational triple $(p, q, r)$ such that
\begin{align*}
        &f(x+) < p < f(x) \\
        &a < q < t < x \implies f(t) < p \\
        &x < t < r < b \implies f(t) < p
\end{align*}
Again, such a triple can only be associated with a single $x$, since $x \in (x, x + \epsilon)$ and
$f(x) > p$. Therefore $f$ has only countably many simple discontinuities.

\subsection{Exercise 23}
From the definition of convexity, we have that
\begin{align*}
        f(\lambda x + (1 - \lambda) p) &\leq \lambda f(x) + (1 - \lambda) f(p) \\
        f(\lambda x + (1 - \lambda) p) - f(p) &\leq \lambda (f(x) - f(p)) \\
        f(p) - f(\lambda x + (1 - \lambda) p) &\geq \lambda (f(p) - f(x)) \\
        \implies \lim_{\lambda \to 0} f(\lambda x + (1 - \lambda) p) &= f(p)
\end{align*}
Since $\lim_{\lambda \to 0} \lambda x + (1 - \lambda) p = 0$ for all choices of $x$, we have that $f$
is continuous.


\documentclass{tufte-handout}

\usepackage{xcolor}
\usepackage{graphicx}

% set hyperlink attributes
\hypersetup{colorlinks}

\usepackage{amsmath}

% set image attributes:
\usepackage{graphicx}
\graphicspath{ {images/} }

% create environment for bottom paragraph:
\newenvironment{bottompar}{\par\vspace*{\fill}}{\clearpage}

% ============================================================

% define the title
\title{SOC 4930/5050: Week 03 Equations Quick \\Reference}
\author{Christopher Prener, Ph.D.}
\date{September 11\textsuperscript{th}, 2017}
% ============================================================
\begin{document}
% ============================================================
\maketitle % generates the title
% ============================================================

\vspace{5mm}
\section{Median (odd \textit{n} of items)}
Let $m =$ the median item's term:
\begin{equation}
\scalebox{2} {$ m = \left( \frac { n+1 }{ 2 }  \right) ^{ th } $}
\end{equation}

\vspace{10mm}
\section{Median (even \textit{n} of items)}
\begin{subequations}
\noindent Let ${ m }_{ a } =$ the median item's term:
\begin{align}
\scalebox{2} {$ m_{ a } = \left( \frac { n+1 }{ 2 }  \right) ^{ th } $}
\end{align}

\vspace{2mm}
\noindent Let ${ x }_{ a } =$ the next lower value before ${ m }_{ a }$. Let ${ x }_{ b } =$ the next higher value after ${ m }_{ a }$.

\vspace{2mm}
\noindent Let ${ m }_{ b } =$ the median:
\begin{align}
\scalebox{2} {$ { m }_{ b } = \left( \frac { { x }_{ a } + { x }_{ b } }{ 2 } \right) $}
\end{align}
\end{subequations}

\vspace{10mm}
\section{Mean}
\begin{equation}
\scalebox{2} {$ \bar{ x } =\frac { \sum _{ i=1 }^{ n }{ x }  }{ n } $}
\end{equation}

\newpage
\section{Standard Deviation}
\begin{subequations}

\paragraph{Deviance}
\begin{equation}
\scalebox{2} {$ { D }= { (x-\bar{ x } ) } $}
\end{equation}

\vspace{3mm}
\paragraph{Total Error}
\begin{equation}
\scalebox{2} {$ { TE }= \sum _{ i=1 }^{ n }{ (x-\bar{ x } ) } $}
\end{equation}

\vspace{3mm}
\paragraph{Sum of Squared Error}
\begin{equation}
\scalebox{2} {$ { SS }= \sum _{ i=1 }^{ n }{ (x-\bar{ x } )^{ 2 } } $}
\end{equation}

\vspace{3mm}
\paragraph{Variance}
\begin{equation}
\scalebox{2} {$ { s }^{ 2 }=\frac { \sum _{ i=1 }^{ n }{ (x-\bar{ x } )^{ 2 } }  }{ n-1 } $}
\end{equation}

\vspace{3mm}
\paragraph{Standard Deviation}
\begin{equation}
\scalebox{2} {$ { s }=\sqrt { \frac { \sum _{ i=1 }^{ n }{ (x-\bar{ x } )^{ 2 } }  }{ n-1 } } $}
\end{equation}
\end{subequations}

% ============================================================
\end{document}


\label{functions}

Functions are a form of expression that may take input arguments surrounded by parentheses. Most of the traditional BASIC \emph{statements} that does not return a value are \emph{functions} in \tbas , and like those, while \tbas\ functions can be called without parentheses, it is highly \emph{discouraged} because of the ambiguities in syntax. \textbf{Always use parentheses on function call!}

\section{Mathematical}

    \subsection{ABS}
        \index{ABS (function)}\codeline{Y \textbf{= ABS(}X\textbf{)}}\par
        Returns absolute value of \code{X}.
    \subsection{ACO}
        \index{ACO (function)}\codeline{Y \textbf{= ACO(}X\textbf{)}}\par
        Returns inverse cosine of \code{X}.
    \subsection{ASN}
        \index{ASN (function)}\codeline{Y \textbf{= ASN(}X\textbf{)}}\par
        Returns inverse sine of \code{X}.
    \subsection{ATN}
        \index{ATN (function)}\codeline{Y \textbf{= ATN(}X\textbf{)}}\par
        Returns inverse tangent of \code{X}.
    \subsection{CBR}
        \index{CBR (function)}\codeline{Y \textbf{= CBR(}X\textbf{)}}\par
        Returns cubic root of \code{X}.
    \subsection{CEIL}
        \index{CEIL (function)}\codeline{Y \textbf{= CEIL(}X\textbf{)}}\par
        Returns integer value of \code{X}, truncated towards positive infinity.
    \subsection{COS}
        \index{COS (function)}\codeline{Y \textbf{= COS(}X\textbf{)}}\par
        Returns cosine of \code{X}.
    \subsection{COSH}
        \index{COSH (function)}\codeline{Y \textbf{= COSH(}X\textbf{)}}\par
        Returns hyperbolic cosine of \code{X}.
    \subsection{EXP}
        \index{EXP (function)}\codeline{Y \textbf{= EXP(}X\textbf{)}}\par
        Returns exponential of \code{X}, i.e. $e^X$.
    \subsection{FIX}
        \index{FIX (function)}\codeline{Y \textbf{= FIX(}X\textbf{)}}\par
        Returns integer value of \code{X}, truncated towards zero.
    \subsection{FLOOR, INT}
        \index{FLOOR (function)}\codeline{Y \textbf{= FLOOR(}X\textbf{)}}
        \index{INT (function)}\codeline{Y \textbf{= INT(}X\textbf{)}}\par
        Returns integer value of \code{X}, truncated towards negative infinity.
    \subsection{LEN}
        \index{LEN (function)}\codeline{Y \textbf{= LEN(}X\textbf{)}}\par
        Returns length of \code{X}. \code{X} can be either a string or an array.
    \subsection{LOG}
        \index{LOG (function)}\codeline{Y \textbf{= LOG(}X\textbf{)}}\par
        Returns natural logarithm of \code{X}.
    \subsection{ROUND}
        \index{ROUND (function)}\codeline{Y \textbf{= ROUND(}X\textbf{)}}\par
        Returns closest integer value of \code{X}, rounding towards positive infinity.
    \subsection{RND}
        \index{RND (function)}\codeline{Y \textbf{= RND(}X\textbf{)}}\par
        Returns a random number within the range of $[0..1)$. If \code{X} is zero, previous random number will be returned; otherwise new random number will be returned.
    \subsection{SIN}
        \index{SIN (function)}\codeline{Y \textbf{= SIN(}X\textbf{)}}\par
        Returns sine of \code{X}.
    \subsection{SINH}
        \index{SINH (function)}\codeline{Y \textbf{= SINH(}X\textbf{)}}\par
        Returns hyperbolic sine of \code{X}.
    \subsection{SGN}
        \index{SGN (function)}\codeline{Y \textbf{= SGN(}X\textbf{)}}\par
        Returns sign of \code{X}: 1 for positive, -1 for negative, 0 otherwise.
    \subsection{SQR}
        \index{SQR (function)}\codeline{Y \textbf{= SQR(}X\textbf{)}}\par
        Returns square root of \code{X}.
    \subsection{TAN}
        \index{TAN (function)}\codeline{Y \textbf{= TAN(}X\textbf{)}}\par
        Returns tangent of \code{X}.
    \subsection{TANH}
        \index{TANH (function)}\codeline{Y \textbf{= TANH(}X\textbf{)}}\par
        Returns hyperbolic tangent of \code{X}.
    \subsection{TEST}
        \index{TEST (function)}\codeline{Y \textbf{= TEST(}X\textbf{)}}\par
        Tests if the value is truthy or not. If the value must be interpreted as \code{TRUE} for the if-statement, said value is \emph{truthy}.

\section{Input}

    \subsection{CIN}
        \index{CIN (function)}\codeline{S \textbf{= CIN()}}\par
        Waits for the user input and returns it.
    \subsection{DATA}
        \index{DATA (function)}\codeline{\textbf{DATA} CONST0 [\textbf{,} CONST1]\ldots}\par
        Adds data that can be read by \code{READ} function. \code{DATA} declarations need not be reacheable in the program flow.
    \subsection{DGET}
        \index{DGET (function)}\codeline{S \textbf{= DGET()}}\par
        Fetches a data declared from \code{DATA} statements and returns it, incrementing the \code{DATA} position.
    \subsection{DIM}
        \index{DIM (function)}\codeline{Y \textbf{= DIM(}X\textbf{)}}\par
        Returns array with size of \code{X}, all filled with zero.
    \subsection{GETKEYSDOWN}
        \index{GETKEYSDOWN (function)}\codeline{K \textbf{= GETKEYSDOWN()}}\par
        Stores array that contains keycode of keys held down into the given variable.\par
        Actual keycode and the array length depends on the machine: in \thismachine , array length will be fixed to 8. For the list of available keycodes, see \ref{implementation}.
    \subsection{INPUT}
        \index{INPUT (function)}\codeline{\textbf{INPUT} VARIABLE}\par
        Prints out \code{? } to the console and waits for user input. Input can be any length and terminated with the return key. The input will be stored in the given variable.\par
        This behaviour is to keep compatibility with the traditional BASIC. For function-like usage, use \code{CIN} instead.
    \subsection{READ}
        \index{READ (function)}\codeline{\textbf{READ} VARIABLE}\par
        Assigns data declared from \code{DATA} statements to given variable. Reading starts at the current \code{DATA} position, and the data position will be incremented by one. The position is reset to the zero by the \code{RUN} command.\par
        This behaviour is to keep the compatibility with the traditional BASIC. For function-like usage, use \code{DGET} instead.

\section{Output}

    \subsection{EMIT}
        \index{EMIT (function)}\codeline{\textbf{EMIT(} EXPR [\{\textbf{,}|\textbf{;}\} EXPR]\ldots\ \textbf{)}}\par
        Prints out characters corresponding to given number on the code page being used. For the code page itself, see \ref{codepage}.\par
        \code{EXPR} is numeric expression.
    \subsection{PRINT}
        \index{PRINT (function)}\codeline{\textbf{PRINT(} EXPR [\{\textbf{,}|\textbf{;}\} EXPR]\ldots\ \textbf{)}}\par
        Prints out given string expressions.\par
        \code{EXPR} is a string, numeric expression, or array.\par
        \code{PRINT} is one of the few function that differentiates two style of argument separator: \codebf{;} will simply concatenate two expressions (unlike traditional BASIC, numbers will not have surrounding spaces), \codebf{,} tabulates the expressions.

\section{Program Manipulation}

    \subsection{CLEAR}
        \index{CLEAR (function)}\codeline{\textbf{CLEAR}}\par
        Clears all declared variables.
    \subsection{END}
        \index{END (function)}\codeline{\textbf{END}}\par
        Stops program execution and returns control to the user.
    \subsection{FOR}
        \index{FOR (function)}\codeline{\textbf{FOR} LOOPVAR \textbf{=} START \textbf{TO} STOP [\textbf{STEP} STEP]}
        \codeline{\textbf{FOR} LOOPVAR \textbf{=} GENERATOR}\par
        Starts a FOR--NEXT loop.\par
        Initially, \code{LOOPVAR} is set to \code{START} then statements between the \code{FOR} statement and corresponding \code{NEXT} statements are executed and \code{LOOPVAR} is incremented by \code{STEP}, or by 1 if \code{STEP} is not specified. The program flow will continue to loop around until \code{LOOPVAR} is outside the range of \code{START}--\code{STOP}. The value of the \code{LOOPVAR} is equal to \code{STOP}$+$\code{STEP} when the looping finishes.
    \subsection{FOREACH}
        \index{FOREACH (function)}\codeline{\textbf{FOREACH} LOOPVAR \textbf{IN} ARRAY}\par
        Same as \code{FOR} but fetches \code{LOOPVAR} from given \code{ARRAY}.
    \subsection{GOSUB}
        \index{GOSUB (function)}\codeline{\textbf{GOSUB} LINENUM}\par
        Jumps to a subroutine at \code{LINENUM}. The next \code{RETURN} statements makes program flow to jump back to the statement after the \code{GOSUB}.\par
        \code{LINENUM} can be either a numeric expression or a Label.
    \subsection{GOTO}
        \index{GOTO (function)}\codeline{\textbf{GOTO} LINENUM}\par
        Jumps to \code{LINENUM}.\par
        \code{LINENUM} can be either a numeric expression or a Label.
    \subsection{LABEL}
        \index{LABEL (function)}\codeline{\textbf{LABEL} NAME}\par
        Puts a name onto the line number where the statement is located.
        \subsubsection*{Notes}
        \begin{itemlist}
        \item \code{NAME} must be a valid variable name.
        \end{itemlist}
    \subsection{NEXT}
        \index{NEXT (function)}\codeline{\textbf{NEXT}}\par
        Iterates FOR--NEXT loop and increments the loop variable from the most recent \code{FOR} statement and jumps to that statement.
    \subsection{RESTORE}
        \index{RESTORE (function)}\codeline{\textbf{RESTORE}}\par
        Resets the \code{DATA} pointer.
    \subsection{RETURN}
        \index{RETURN (function)}\codeline{\textbf{RETURN}}\par
        Returns from the \code{GOSUB} statement.

\section{String Manipulation}

    \subsection{CHR}
        \index{CHR (function)}\codeline{CHAR \textbf{= CHR(}X\textbf{)}}\par
        Returns the character with code point of \code{X}. Code point is a numeric expression in the range of $[0-255]$.
    \subsection{LEFT}
        \index{LEFT (function)}\codeline{SUBSTR \textbf{= LEFT(} STR \textbf{,} NUM\_CHARS \textbf{)}}\par
        Returns the leftmost \code{NUM\_CHARS} characters of \code{STR}.
    \subsection{MID}
        \index{MID (function)}\codeline{SUBSTR \textbf{= MID(} STR \textbf{,} POSITION \textbf{,} LENGTH \textbf{)}}\par
        Returns a substring of \code{STR} starting at \code{POSITION} with specified \code{LENGTH}.\par
        When \code{OPTIONBASE 1} is specified, the position starts from 1; otherwise it will start from 0.
    \subsection{RIGHT}
        \index{RIGHT (function)}\codeline{SUBSTR \textbf{= RIGHT(} STR \textbf{,} NUM\_CHARS \textbf{)}}\par
        Returns the rightmost \code{NUM\_CHARS} characters of \code{STR}.
    \subsection{SPC}
        \index{SPC (function)}\codeline{STR \textbf{= SPC(} STR \textbf{,} NUM\_CHARS \textbf{)}}\par
        Returns a string of \code{NUM\_CHARS} spaces.

\section{Array Manipulation}

    \subsection{HEAD}
        \index{HEAD (function)}\codeline{K \textbf{= HEAD(}X\textbf{)}}\par
        Returns the head element of the array \code{X}.
    \subsection{INIT}
        \index{INIT (function)}\codeline{K \textbf{= INIT(}X\textbf{)}}\par
        Constructs the new array from array \code{X} that has its last element removed.
    \subsection{LAST}
        \index{LAST (function)}\codeline{K \textbf{= LAST(}X\textbf{)}}\par
        Returns the last element of the array \code{X}.
    \subsection{TAIL}
        \index{TAIL (function)}\codeline{K \textbf{= TAIL(}X\textbf{)}}\par
        Constructs the new array from array \code{X} that has its head element removed.

\section{Monad Manipulation}

    \subsection{MJOIN}
        \index{MJOIN (function)}\codeline{K \textbf{= MJOIN(}M\textbf{)}}\par
        Returns the inner value of the given monad.
    \subsection{MRET}
        \index{MRET (function)}\codeline{M \textbf{= MRET(}X\textbf{)}}\par
        Returns a value-monad that contains a given value.

\section{Graphics}

    \subsection{CLPX}
        \index{CLPX (function)}\codeline{\textbf{CLPX}}\par
        Clears plotted pixels.
    \subsection{CLS}
        \index{CLS (function)}\codeline{\textbf{CLS}}\par
        Clears text view and moves text cursor to top-left.
    \subsection{GOTOYX}
        \index{GOTOYX (function)}\codeline{\textbf{GOTOYX(} ROW \textbf{,} COLUMN \textbf{)}}\par
        Moves text cursor to given row and column.\par
        When \code{OPTIONBASE 1} is specified, first row and column will be 1, otherwise it will be 0.
    \subsection{PLOT}
        \index{PLOT (function)}\codeline{\textbf{PLOT(} X\_POS \textbf{,} Y\_POS \textbf{,} COLOUR \textbf{)}}\par
        Plots a pixel to the framebuffer of the display, at XY-position of \code{X\_POS} and \code{Y\_POS}, with colour of \code{COLOUR}. For the available colours, see \ref{colourpalette}\par
        Top-left corner of the pixel will be 1 if \code{OPTIONBASE 1} is specified, otherwise it will be 0.

\section{Meta}

    \subsection{OPTIONBASE}
        \index{OPTIONBASE (function)}\codeline{\textbf{OPTIONBASE} \{\textbf{0}|\textbf{1}\}}\par
        Specifies at which number the array/string/pixel indices begin.
    \subsection{OPTIONDEBUG}
        \index{OPTIONDEBUG (function)}\codeline{\textbf{OPTIONDEBUG} \{\textbf{0}|\textbf{1}\}}\par
        Specifies whether or not the debugging messages should be printed out. The messages will be printed out to the \emph{serial debugging console}, or to the stdout.\par
        {\LARGE Big Warning Sign:} Do not turn debug mode on unless you know what you're doing; debug mode will severely slow down the interpreter and literally gigabytes of log messages will pile up in a minute or two of the execution.
    \subsection{OPTIONTRACE}
        \index{OPTIONTRACE (function)}\codeline{\textbf{OPTIONTRACE} \{\textbf{0}|\textbf{1}\}}\par
        Specifies whether or not the line numbers should be printed out. The messages will be printed out to the \emph{serial debugging console}, or to the stdout.
    \subsection{TYPEOF}
        \index{TYPEOF (function)}\codeline{X \textbf{= TYPEOF(} VALUE \textbf{)}}\par
        Returns a type of given value.\par
        \begin{longtable}{*{2}{m{\textwidth}}}\hline
        \endfirsthead
        \endhead

        \endfoot
        \hline
        \endlastfoot
        \centering
        \begin{tabulary}{\textwidth}{rl}
        BASIC Type & Returned Value \\
        \hline
        Number & \ttfamily{num} \\
        Boolean & \ttfamily{bool} \\
        String & \ttfamily{str} \\
        Array & \ttfamily{array} \\
        \end{tabulary}
        \begin{tabulary}{\textwidth}{rl}
        BASIC Type & Returned Value \\
        \hline
        Generator & \ttfamily{generator} \\
        User Function & \ttfamily{usrdefun} \\
        Monad & \ttfamily{<subtype>-monad} \\
        \ & \ \\
        \end{tabulary}
        \end{longtable}

\section{System}

    \subsection{PEEK}
        \index{PEEK (function)}\codeline{BYTE \textbf{= PEEK(} MEM\_ADDR \textbf{)}}\par
        Returns whatever the value stored in the \code{MEM\_ADDR} of the Scratchpad- or Machine Memory.\par
        Address mirroring, illegal access, etc. are entirely up to the virtual machine which the BASIC interpreter is running on.\par
        For Machine Memory addresses, see \ref{mmio}.
    \subsection{POKE}
        \index{POKE (function)}\codeline{\textbf{POKE(} MEM\_ADDR \textbf{,} BYTE \textbf{)}}\par
        Puts a \code{BYTE} into the \code{MEM\_ADDR} of the Scratchpad- or Machine Memory.

\section{Higher-order Function}

    \subsection{DO}
        \index{DO (function)}\codeline{\textbf{DO(} EXPR0 [\textbf{;} EXPR1]\ldots\ \textbf{)}}\par
        Executes \code{EXPRn}s sequentially.
    \subsection{FILTER}
        \index{FILTER (function)}\codeline{NEWLIST \textbf{= FILTER(} FUNCTION \textbf{,} FUNCTOR \textbf{)}}\par
        Returns an array of values from the \code{FUNCTOR} that passes the given function. i.e. values that makes \code{FUNCTION(VALUE\_FROM\_FUNCTOR)} true.
        \subsubsection*{Parameters}
        \begin{itemlist}
        \item \code{FUNCTION} is a user-defined or builtin function with single parameter.
        \item \code{FUNCTOR} is either an array or a generator.
        \end{itemlist}
    \subsection{FOLD}
        \index{FOLD (function)}\codeline{NEWVALUE \textbf{= FOLD(} FUNCTION \textbf{,} INIT\_VALUE \textbf{,} FUNCTOR \textbf{)}}\par
        Iteratively applies given function with accumulator and the value from the \code{FUNCTOR}, returning the final accumulator. Accumulator will be set to \code{INIT\_VALUE} before iterating over the functor. In the first execution, the accumulator will be set to \code{ACC=FUNCTION(ACC,FUNCTOR(0))}, and the execution will continue to remaining values within the functor until all values are consumed. The \code{FUNCTOR} will not be modified after the execution.
        \subsubsection*{Parameters}
        \begin{itemlist}
        \item \code{FUNCTION} is a user-defined function with two parameters: first parameter being accumulator and second being a value.
        \end{itemlist}
    \subsection{MAP}
        \index{MAP (function)}\codeline{NEWLIST \textbf{= MAP(} FUNCTION \textbf{,} FUNCTOR \textbf{)}}\par
        Applies given function onto the every element in the functor, and returns an array that contains such items. i.e. returns tranformation of \code{FUNCTOR} of which the transformation is \code{FUNCTION}. The \code{FUNCTOR} will not be modified after the execution.
        \subsubsection*{Parameters}
        \begin{itemlist}
        \item \code{FUNCTION} is a user-defined or builtin function with single parameter.
        \end{itemlist}


% declare document class and geometry
\documentclass[12pt]{article} % use larger type; default would be 10pt
\usepackage[margin=1in]{geometry} % handle page geometry

% import packages and commands
\input{../header.tex}

\title{Phys 220A -- Classical Mechanics -- HW01}
\author{UCLA, Fall 2014}
\date{\formatdate{09}{10}{2014}} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed

\begin{document}
\maketitle

\section*{Problem 1: Math recap (15 pts)}
\begin{description}

\item[(a)]
\textit{
Show that
\begin{equation}
(\v{a} \times \v{b} ) \times \v{c} = (\v{a} \cdot \v{c}) \v{b} - (\v{b} \cdot \v{c}) \v{a}.
\end{equation}
}

\item[(b)]
\textit{
Spherical coordinates $r, \theta, \phi$ are defined as
\begin{equation}
x = r \sin\theta \cos\phi, \qquad y = r \sin\theta \sin\phi, \qquad z = r \cos\theta.
\end{equation}
Express the cartesian differential operators
\begin{equation}
\v{\nabla} = (\pd_x, \pd_y, \pd_z), \qquad \v{\nabla}^2 = \pd_x^2 + \pd_y^2 + \pd_z^2
\end{equation}
in spherical coordinates.
}

\item[(c)]
\textit{
Show the following identities:
\begin{align}
\v{\nabla} \times (\v{\nabla} f(\v{x})) &= 0, \text{ and} \\
\v{\nabla} \cdot (\v{\nabla} \times \v{a}) &= 0.
\end{align}
}

\end{description}

\section*{Problem 2: Conservation is important in a drought! (15 pts)}

\begin{description}

\item[(a)]
\textit{
Show that in an isolated system of $n$ point masses $m_i$ (no external forces), the momentum $\v{p}_\text{tot} = \sum_{i=1}^n \v{p}_i$ is conserved.
}

\item[(b)]
\textit{
Show that in an isolated system of $n$ point masses $m_i$ (no external forces) the total angular momentum $\v{L}_\text{tot} = \sum_{i=1}^n \v{r}_i \times \v{p}_i$ is conserved.
}

\item[(c)]
\textit{
What about the total energy of such an isolated system. Is it necessarily conserved?
}

\end{description}

\section*{Problem 3: Work, work work (15 pts)}

\textit{
Check whether the following forces are conservative by calculating
\begin{equation}
W_{if} = \int_{i \rightarrow f} d\v{x} \cdot \v{F}
\end{equation}
for two different paths. (If you believe the force is non-conservative try to find a path which makes this clear.)
}

\begin{description}

\item[(a)]
\textit{
For a central force $\v{F} = r^2 \uv{r}$.
}

\item[(b)]
\textit{
For the Lorentz force $\v{F} = -e\v{v} \times \v{B}$ with a constant magnetic field $\v{B}$.
}

\item[(c)]
\textit{
For a friction force $\v{F} = -b\v{v}$.
}

\end{description}

\section*{Problem 4: Central motion (30 pts)}
\textit{
A planet of mass $m$ is moving around a Sun of mass $M$ subject to Newtonian gravitational force.
}

\begin{description}

\item[(a)]
\textit{
Write down the Newtonian equation of motion for the planet and the sun, and show that they can be separated into a free motion of the center of mass and a motion in the central potential of a mass point with reduced mass $\mu$.
}

\item[(b)]
\textit{
Show that conservation of angular momentum implies that the motion of the mass point lies in a plane with coordinates $\rho, \phi$.
}

\item[(c)]
\textit{
Use the substitution $u = 1 / \rho$ to write down a differential equation for the trajectory $u = u(\phi)$.
}

\item[(d)]
\textit{
What is the equilibrium solution of this equation? What does it represent?
}

\item[(e)]
\textit{
If the planet is not initially on the equilibrium orbit, there will be small oscillations around the equilibrium point. What is the period of these oscillations?
}

\item[(f)]
\textit{
Assume there is a perturbing potential $V = -B / \rho^2$, calculate the effect of this perturbation on the orbit $u(\phi)$.
}

\end{description}

\end{document}


\subsection{Network Operations}

\subsubsection*{Convolutional Layer}

The output of an convolutional layer is defined by
\begin{equation}
   z(i,j) = (f*g)(i,j) = \sum_{m=-\infty}^{\infty} \sum_{n=-\infty}^{\infty} f(m,n) g(m-i,n-j)
\end{equation}

It is explained in more detail here: \cite{dumoulin2016guide}

\subsubsection*{Pooling Layer}

\subsubsection*{Fully Connected Layer}

The output of an fully connected layer is defined by
\begin{equation}
	z = xW + b
\end{equation}
where $x \in \mathbb{R}^{b,m}$,  $W \in \mathbb{R}^{m,n}$ and $b \in \mathbb{R}^{n}$.

\subsubsection*{Rectified Linear Unit (ReLU)}

\begin{equation}
	f(x) = \begin{cases}
		x \quad \text{if} \quad x > 0 \\
		0 \quad \text{else}
	\end{cases}
\end{equation}

\subsubsection*{Softmax}

For a vector $x \in \mathbb{R}^{n}$ the softmax function is defined as
\begin{equation}
	f_{\text{Softmax}}: f(x) = \frac{\exp{x_i}}{\sum_i^n \exp{x_i}}
\end{equation}


\section{Plant Model}
\label{chap:Vehicle_model}
The starting point for Model-Based design is to develop and implement a plant model, a model representing the physics of the system considered.\\
Several models are available in literature to model a vehicle. Since the target of the project is to develop a lateral controller, one of the most suitable model is the so called \textit{Bicycle Model}.\\
The kinematic bicycle model is described by the following non linear system:
\begin{align}
    \dot{X} = Vcos(\psi + \beta)\\
    \dot{Y} = Vsin(\psi + \beta)\\
    \dot{\psi} = \frac{Vcos(\beta)}{l_f + l_r}\left(tan(\delta_f) - tan(\delta_r)\right)\\
    \beta = tan^{-1}\left(\frac{l_ftan(\delta_r) + l_rtan(\delta_f)}{l_f + l_r}\right)
\end{align}
Where:
\begin{itemize}
    \item X and Y are the coordinates of the body of the vehicle in the global reference frame;
    \item $\psi$ is the yaw (orientation angle);
    \item $\beta$ is the slip angle of the body;
    \item $\beta + \psi$ is the body speed direction known as \textit{Course Angle};
    \item $\delta_f$ is the front steering angle;
    \item $\delta_r$ is the rear steering angle;
    \item V is the magnitude of the body speed;
    \item $l_f$ is a geometric parameter which indicates the distance of the CoG from the front wheel;
    \item $l_r$ is a geometric parameter which indicates the distance of the CoG from the rear wheel.
\end{itemize}
\begin{figure}[H]
    \centering
    \includegraphics[width=0.5\textwidth]{Figures/Bicycle_model.jpg}
    \caption{Bicycle kinematic vehicle model}
    \label{fig:Bicycle_kin}
\end{figure}
This model can be further simplified assuming that, as in most commercial vehicles, only the front wheels are able to steer, so the rear steering angle $\delta_r$ is constant and equal to 0.\\
Standard MPC controllers work on linear systems, so we have decided to implement a linearized version of the Bicycle Model to feed the controller. This linearized model is described by the following State Space equations:
\begin{equation}
    \label{equation:dynamic1}
    \Dot{x} = Ax + Bu
\end{equation}
\begin{equation}
    \label{equation:dynamic2}
    y = Cx + Du
\end{equation}
where:

\begin{equation}
\label{equation:sys_bicycle_kin}
   \begin{aligned}
    x =
        \begin{bmatrix} % STATES
        X \\
        Y \\
        \psi \\
        v \\
        \end{bmatrix}\quad
    A =
        \begin{bmatrix} % MATRIX A
       0 & 0 & -Vsin(\psi) & cos(\psi)\\
       0 & 0 & Vcos(\psi) & sin(\psi) \\
       0 & 0 & 0 & \frac{tan(\delta)}{l_r + l_f}\\
       0 & 0 & 0 & 0 \\
        \end{bmatrix}\quad
    B =
        \begin{bmatrix} % MATRIX B
        0 & 0\\
        0 & 0 \\
        0 & \frac{Vtan(\delta)^2 + 1}{l_r + l_f}\\
        1 & 0 \\
        \end{bmatrix}\\[10pt]
    u =
        \begin{bmatrix} % INPUTS
        Throttle \\
        \delta \\
        \end{bmatrix}\quad\quad\quad
    C = I^{4\times 4}\quad\quad\quad\quad\quad
    D =
        \begin{bmatrix} % MATRIX A
      0 & 0 & 0 & 0\\
      0 & 0 & 0 & 0\\
        \end{bmatrix}
    \end{aligned}
\end{equation}

These matrices are obtained evaluating the Taylor expansion of the non linear bicycle model, as explained in the MathWorks example \cite{StaticObs}.\\
A more complex vehicle model can be useful to test the performance of the controller to be developed. The \textit{Dynamic Bicycle Model} \cite{7225830} can be built starting from the previous non linear model, deriving the following second order derivative equations:
\begin{align}
    \ddot{x} = \dot{\psi}\dot{y} + Throttle\\
    \ddot{y} = -\dot{\psi}\dot{x} + \frac{2}{m}\left(F_fcos(\delta) + F_r\right)     \label{dynamic_equation}\\
    \ddot{\psi} = \frac{2}{I_z}\left(l_fF_f - l_rF_r\right)\\
    \dot{X} = \dot{x}cos(\psi) - \dot{y}sin(\psi)\\
    \dot{Y} = \dot{x}sin(\psi) + \dot{y}cos(\psi)
\end{align}
where $m$ is the mass of the vehicle, $I_z$ is the inertia of the vehicle with respect to the vertical axle passing for the CoG of it, while $F_f$ and $F_r$ are respectively the side slip forces acting on the front and rear wheels, and they can be evaluated as:
\begin{align}
    F_f = 2C_f\left(\delta - \theta_f\right)\\
    F_r = 2C_r\left(-\theta_r\right)
\end{align}
with $C_i$ side slip friction coefficient of the i-th wheel couple and $\theta_i$ side slip angle of the wheels.\\
Tyre side slip angles can be approximated by the following equations:
\begin{equation}
    \theta_f=tan^{-1}\left(\frac{\dot{y} + l_f\dot{\psi}}{\dot{x}}\right)
\end{equation}
\begin{equation}
    \theta_r=tan^{-1}\left(\frac{\dot{y} - l_r\dot{\psi}}{\dot{x}}\right)
\end{equation}
The models introduced are, more or less, independent of the longitudinal dynamics of the vehicle, since they link it to a simple coefficient, that is the $Throttle$. In our model, this coefficient is considered as an acceleration, hence it can be both positive (driving) or negative (braking). The range of values for this parameter is dependent on lots of variables, such as the engine power, the vehicle mass and inertia, the ground type, the rubber of the wheels and so on. Our assumption is to give a fixed interval of feasible values for this parameter, to simulate a small commercial vehicle on dry asphalt, with a maximum acceleration of 4 $m/s^2$ and a maximum braking deceleration of -0.8 $g$. To summarize:
\begin{itemize}
    \item $Throttle \in [-7.85, 4.00]$ $m/s^2$
    \label{item:Throttle}
\end{itemize}
The parameters considered for the bicycle model are taken from real vehicle data \cite{10.2307/44733900} and are reported in the following table:

\begin{table}[H]
\resizebox{\textwidth}{!}{%
\begin{tabular}{|l|l|r|r|r|r|r|}
\hline
\textbf{ID} &
  \textbf{Vehicle name} &
  \multicolumn{1}{l|}{\textbf{Wheel base {[}$m${]}}} &
  \multicolumn{1}{l|}{\textbf{l\_r {[}$m${]}}} &
  \multicolumn{1}{l|}{\textbf{l\_f {[}$m${]}}} &
  \multicolumn{1}{l|}{\textbf{Mass {[}$kg${]}}} &
  \multicolumn{1}{l|}{\textbf{Inertia {[}$kg \cdot m^2${]}}} \\ \hline
1 & \textit{Hyundai Azera}    & 2.843 & 1.738 & 1.105 & 1200 & 1000 \\ \hline
2 & \textit{BMW 325i}          & 2.570 & 1.369 & 1.201 & 1251 & 2027 \\ \hline
3 & \textit{Ford E150}        & 3.505 & 1.634 & 1.871 & 2995 & 6536 \\ \hline
4 & \textit{Suzuki Samurai}    & 2.032 & 0.870 & 1.162 & 1229 & 1341 \\ \hline
5 & \textit{Volkswagen Beetle} & 2.408 & 0.996 & 1.412 & 857  & 1289 \\ \hline
\end{tabular}%
}
\caption{Vehicle data considered for development and validation}
\label{tab:vehicle_data}
\end{table}
Values reported in Table \ref{tab:vehicle_data} are stored in a MATLAB file and can be accessed through the \textit{loadParameters} function, which takes as input the vehicle ID and returns the parameters associated with that vehicle. To avoid calling  this function improperly, a default set of parameters has been provided with the following values:
\begin{table}[H]
\resizebox{\textwidth}{!}{
\begin{tabular}{|l|r|r|r|r|r|}
\hline
\textbf{Vehicle name} &
  \multicolumn{1}{l|}{\textbf{Wheel base {[}$m${]}}} &
  \multicolumn{1}{l|}{\textbf{l\_r {[}$m${]}}} &
  \multicolumn{1}{l|}{\textbf{l\_f {[}$m${]}}} &
  \multicolumn{1}{l|}{\textbf{Mass {[}$kg${]}}} &
  \multicolumn{1}{l|}{\textbf{Inertia {[}$kg \cdot m^2${]}}} \\ \hline
\textit{Default}    & 2 & 1 & 1 & 1000 & 1000 \\ \hline
\end{tabular}
}
\end{table}
For the development phase, we have used the set of data from Vehicle 1 ( \textit{Hyundai Azera}), where the mass and inertia given, represent realistic values for the vehicle and not the real values. The other data contained in Table \ref{tab:vehicle_data}, instead, are meant to be used during the validation phase in order to test the controller with different vehicles.
For what concerns the side slip friction coefficient values, we have considered two fixed values common to all the vehicles:
\begin{itemize}
    \item $C_f$ $=$ $1.0745\times10^5$ $N/rad$
    \item $C_r$ $=$ $1.9032\times10^5$ $N/rad$
\end{itemize}
while in the \textit{``Default"} condition they are both equal to $10^5$ $N/rad$.

\subsection{Model comparison}
As specified in the subsection \ref{partitioning_subsection} we have decided to use two models, a simplified and linearized model for the MPC and a more complex one for the simulation of the model. The former is defined as \textit{Kinematic Bicycle Model} and the latter as \textit{Dynamic Bicycle Model}.
Hence we have decided to test both systems to show how they behave with different throttle and steering inputs using the \textit{Simulink Test} tool. Thus, the tests performed are:
\begin{itemize}
    \item \textbf{Free evolution test}: this test has been performed considering a constant steering of $0^{\circ}$ and a constant throttle of 0 $m/s^2$;
    \item \textbf{Only throttle test}: this test has been performed keeping the steering angle constant and equal to $0^{\circ}$ and varying the throttle as shown in Figure \ref{fig:InputThrottle};
    \item \textbf{Constant steering test}: this test has been performed keeping the throttle constant and equal to 0 $m/s^2$ and the steering angle constant and equal to $2^{\circ}$;
    \item \textbf{Ramp steering test}: this test has been performed keeping the throttle equal to 0 $m/s^2$ and giving a ramp steering angle signal (varying linearly from $0^{\circ}$ to $36^{\circ}$);
    \item \textbf{Small sinusoidal steering test}: this test has been performed keeping the throttle constant and equal to 0 $m/s^2$ and giving a sinusoidal steering angle signal with frequency 0.2 $Hz$ and amplitude $5^{\circ}$;
    \item \textbf{Big sinusoidal steering test}: here we have performed the same test as before (sinusoidal steering input) but with a larger amplitude of the sine wave ($15^{\circ}$);
    \item \textbf{Combined test 1}: this test has been performed keeping the steering angle constant and equal to $2^{\circ}$ and varying the throttle as shown in Figure \ref{fig:InputThrottle};
    \item \textbf{Combined test 2}: this test has been performed keeping the throttle equal to 0.2 $m/s^2$ and giving a ramp steering angle signal (varying linearly from $0^{\circ}$ to $36^{\circ}$).
\end{itemize}

\begin{figure}[H]
    \centering
    \includegraphics[width=0.65\textwidth]{Figures/InputThrottle.png}
    \caption{Throttle Input used in \textit{Only throttle test} and \textit{Combined test 1} (Maximum and minimum values are coherent with the range described in Section \ref{item:Throttle} in order to explore the whole set of throttle values)}
      \label{fig:InputThrottle}
\end{figure}

\subsubsection{Simulink Test}
The above-mentioned tests have been performed using the \textit{Simulink Test} tool implemented by MathWorks. Simulink Test provides tools for authoring, managing, and executing systematic, simulation-based tests of models, generated code, and simulated or physical hardware. It includes simulation, baseline, and equivalence test templates that let you perform functional, unit, regression, and back-to-back testing using software-in-the-loop (SIL), processor-in-the-loop (PIL), and real-time hardware-in-the-loop (HIL) modes. With Simulink Test you can create non-intrusive test harnesses to isolate the component under test. You can define requirements-based assessments using a text-based language, and specify test input, expected outputs, and tolerances in a variety of formats. \cite{SimulinkTest}

\subsubsection{Results analysis}

Exploiting Simulink Test, we have created two test harnesses: \textit{Dynamic} and \textit{Kinematic}. Those are referred to the models described at the beginning of this section. Then, we have executed an Equivalence Test which allowed us to make a comparison between two simulations. In particular, the Dynamic Model has been considered as the \textit{baseline} to which our Kinematic Model has been compared in the test. Being more accurate than the Kinematic model, the baseline represents the expected output, which in turn is called \textit{Compare to} model inside the Simulink Test automatic report generator\footnote{The ``Model Comparison - Test Report" file generated by Simulink Test is included in the /Documentation/Test Reports/ file path.}.
Since these tests aim to give a qualitative analysis of the two models and compare them, we have not included equivalence criteria.
The only parameter we have set up is the relative tolerance assigning to it a value of 1\% in order to ignore the negligible offsets between the dynamic model and the kinematic model.\\
Figure \ref{fig:trajectories} shows the results of the simulations we have carried out.
As shown in Figures \ref{subfig:free_evo} and \ref{subfig:only_throttle} respectively, as expected the two models behave in the same way when no inputs are present or when only the longitudinal input (throttle) is changed. \\ Another relevant result is shown in Figures \ref{subfig:small_sinusoidal} and \ref{subfig:big_sinusoidal}: as long as the amplitude of the sinusoidal input is fairly small, the two models behave in a similar fashion but the greater the amplitude of the sinusoidal input, the greater the deviation of the kinematic model from the dynamic one.\\
The other images shown below, underline as well the offset between the two vehicle models, which is due to the fact that when using the dynamic model we take into account lateral slips which are not considered in the kinematic model.

\begin{figure}[H]
\centering

    \begin{subfigure}{.5\textwidth}
    \centering
   \includegraphics[width=0.8\textwidth,keepaspectratio]{Figures/Free_evo_traj.png}
    \caption{Free evolution test - trajectories}
    \label{subfig:free_evo}
    \end{subfigure}%
    \begin{subfigure}{.5\textwidth}
    \centering
    \includegraphics[width=0.8\textwidth,keepaspectratio]{Figures/Throttle_traj.png}
    \caption{Only throttle test - trajectories}
    \label{subfig:only_throttle}
    \end{subfigure}

    \vspace{10mm}

    \begin{subfigure}{.5\textwidth}
    \centering
   \includegraphics[width=0.8\textwidth,keepaspectratio]{Figures/Const_steer_traj.png}
   \caption{Constant steering test - trajectories}
   \label{subfig:const_steer}
    \end{subfigure}%
    \begin{subfigure}{.5\textwidth}
    \centering
    \includegraphics[width=0.8\textwidth,keepaspectratio]{Figures/Ramp_traj.png}
    \caption{Ramp steering test - trajectories}
    \label{subfig:ramp_steer}
    \end{subfigure}

     \vspace{10mm}

    \begin{subfigure}{.5\textwidth}
    \centering
   \includegraphics[width=0.8\textwidth,keepaspectratio]{Figures/Small_sin_traj.png}
   \caption{Small sinusoidal test - trajectories}
   \label{subfig:small_sinusoidal}
    \end{subfigure}%
    \begin{subfigure}{.5\textwidth}
    \centering
    \includegraphics[width=0.8\textwidth,keepaspectratio]{Figures/Big_sin_traj.png}
    \caption{Big sinusoidal test - trajectories}
    \label{subfig:big_sinusoidal}
    \end{subfigure}

     \vspace{10mm}

    \begin{subfigure}{.5\textwidth}
    \centering
  \includegraphics[width=0.8\textwidth,keepaspectratio]{Figures/Comb1_traj.png}
    \caption{Combined 1 test - trajectories}
    \label{subfig:Comb_1}
    \end{subfigure}%
    \begin{subfigure}{.5\textwidth}
    \centering
  \includegraphics[width=0.8\textwidth,keepaspectratio]{Figures/Comb2_traj.png}
    \caption{Combined 2 test - trajectories}
    \label{subfig:Comb_2}
    \end{subfigure}

    \caption{Output trajectories of the test set}
    \label{fig:trajectories}
\end{figure}
\pagebreak

After the analysis of the results, we have decided to provide the MPC with the linearized bicycle model as the vehicle plant, because the controller is usually updated with the current state in some fractions of a second, and for this reason the differences between the model and the \textit{``Real World"} system, that in our case is represented by the dynamic model, shouldn't affect too much the behaviour of the controller itself. As a matter of fact, by looking at the images above it is clear that the difference between the two models output is negligible at the first time steps of the simulation.\\
Moreover, as already discussed in this section, with small inputs the performances of the two models are very similar, and this condition is fulfilled if we consider to use our controller in normal driving scenarios, such as, for instance, a highway.\\
Keeping these assumptions in mind, we have carried on the project, with the possibility of applying a change in the vehicle model for further improvements on the MPC performances.


\subsection{The Quadratic Formula and Completing the Square}
The technique of \dfont{completing the square} allows us to solve quadratic equations and also to determine the center of a circle/ellipse or the vertex of a parabola.

The main idea behind completing the square is to turn:
$$ ax^2 + bx + c$$
into
$$a(x - h)^2 + k.$$
One way to complete the square is to use the following formula:
$$ax^2+bx+c=a\left(x+\frac{b}{2a}\right)^2-\frac{b^2}{4a^2}+c.$$
But this formula is a bit complicated, so some students prefer following the steps outlined in the next example. \\

\begin{example}{Completing the Square}{CompletingSquare}
Solve $2x^2+12x-32=0$ by completing the square.
\end{example}

\begin{solution}
In this instance, we will \ifont{not} divide by $2$ first (usually you would) in order to demonstrate what you should do when the `$a$' value is not $1$.

\bigskip

\begin{tabular}{rl}
$2x^2+12x-32=0$ & Start with original equation.\\
\\
$2x^2+12x=32$ & Move the number over to the other side.\\
\\
$2(x^2+6x)=32$ & Factor out the $a$ from the $ax^2+bx$ expression.\\
\\
$6~~\to~~\frac{6}{2}=3~~\to~~3^2=\dfont{9}$ & Take the number in front of $x$, \\
	&  \dfont{divide by $2$}, \\
	&  then \dfont{square} it.\\
\\
$\ifont{2}(x^2+6x+\dfont{9})=32+\ifont{2}\cdot\dfont{9}$ & Add the result to both sides, \\
	&  taking $a=2$ into account.\\
\\
$2(x+3)^2=50$ & Factor the resulting perfect square trinomial.\\
\\
~ & \ifont{You have now completed the square!}\\
\\
$(x+3)^2=25~~\to~~x=2 \mbox{ or } x=-8$ & To solve for $x$, simply divide by $a=2$ \\
	& and take square roots.\\
\end{tabular}
\end{solution}

Suppose we want to solve for $x$ in the quadratic
equation $ax^2+bx+c=0$, where $a\neq 0$.
The solution(s) to this equation are given by the \dfont{quadratic formula}.\\

\begin{theorem}{The Quadratic Formula}{Quadratic Formula}
\label{QuadForm}
The solutions to $ax^2+bx+c=0$ (with $a\neq 0$) are $\ds{x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}}$.
\end{theorem}

\begin{proof}
To prove the Quadratic Formula (Theorem \ref{QuadForm} ) we use the technique of \ifont{completing
the square}. The general technique involves taking an expression of the
form $x^2+rx$ and trying to find a number we can add so that we end up
with a perfect square (that is, $(x+n)^2$). It turns out if you add $(r/2)^2$
then you can factor it as a perfect square.

For example, suppose we want to solve for $x$ in the equation $ax^2+bx+c=0$, where $a\neq 0$.
Then we can move $c$ to the other side and divide by $a$ (remember, $a\neq 0$ so we can divide by it) to get
$$x^2+\frac{b}{a}x=-\frac{c}{a}.$$
To write the left side as a perfect square we use what was mentioned previously.
We have $r=(b/a)$ in this case, so we must add $(r/2)^2=(b/2a)^2$ to both sides
$$x^2+\frac{b}{a}x+\left(\frac{b}{2a}\right)^2=-\frac{c}{a}+\left(\frac{b}{2a}\right)^2.$$
We know that the left side can be factored as a perfect square
$$\left(x+\frac{b}{2a}\right)^2=-\frac{c}{a}+\left(\frac{b}{2a}\right)^2.$$
The right side simplifies by using the exponent rules and finding a common denominator
$$\left(x+\frac{b}{2a}\right)^2=\frac{-4ac+b^2}{4a^2}.$$
Taking the square root we get
$$x+\frac{b}{2a}=\pm\sqrt{\frac{-4ac+b^2}{4a^2}},$$
which can be rearranged as
$$x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}.$$
In essence, the quadratic formula is just completing the square.
\end{proof}


\documentclass{article}
\usepackage{amsmath}
\usepackage[margin=1.0in]{geometry}
\usepackage{xcolor}

\begin{document}

\noindent
Does $\displaystyle \sum_{n=1}^\infty \frac{1}{5^n-1}$
diverge, converge absolutely, or converge conditionally?

\subsection*{Solution 1}

We need to find a fixed number $K > 0$ such that
\[ \frac{1}{5^n-1} \leq \frac{K}{5^n}.\]
In our scratch work, by multiplying both sides by $5^n(5^n-1)$, we want the following inequality to be true:
\[ 5^n \leq K(5^n - 1) \]
Now, this inequality is false when $K=1$. However, when $K=2$, this inequality is already true for $n \geq 1$. (Note when $K=2$ and $n=1$, we are looking at $5 \leq 8$.

So, with that scratch work done, let us begin:
\[ 5^n \leq 2(5^n - 1) \]
is true for all $n \geq 1$. Then, dividing both sides of this inequality by $5^n(5^n-1)$, we get
\[ \frac{1}{5^n-1} \leq \frac{2}{5^n}.\]

Since
\[ \frac{1}{5^n+1} \leq \frac{2}{5^n}\]
and since $\displaystyle \sum_{n=1}^\infty  \frac{2}{5^n}$ converges by the Geometric Series Test ($r=\frac15$), the series $\displaystyle \sum_{n=1}^\infty \frac{1}{5^n+1}$ converges by the Direct Comparison Test.

Since $\sum |a_n| = \sum a_n$, the series $\displaystyle \sum_{n=1}^\infty \frac{1}{5^n-1}$ converges absolutely.

\subsection*{Solution 2}

Let $a_n =  \frac{1}{5^n-1}$ and $b_n = \frac{1}{5^n}$. Then
\begin{align*}
\lim_{n \to \infty} \frac{a_n}{b_n}
&= \lim_{n \to \infty} \frac{5^n-1}{5^n} \\
&= \lim_{n \to \infty} \frac{5^n \ln 5}{5^n \ln 5}\quad\text{by L'hopital's rule} \\
& = \lim_{n \to \infty} 1\\
& = 1
\end{align*}
which is a finite positive number. Since the series  $\displaystyle \sum_{n=1}^\infty  \frac{1}{5^n}$ converges by the Geometric Series Test ($r=\frac15$), the series $\displaystyle \sum_{n=1}^\infty \frac{1}{5^n-1}$ converges by the Limit Comparison Test.

Since $\sum |a_n| = \sum a_n$, the series $\displaystyle \sum_{n=1}^\infty \frac{1}{5^n-1}$ converges absolutely.

\end{document}%%%%%%%%%%%%%%%%%

\begin{align*}
L&=\lim_{n \to \infty} \sqrt[n]{|a_n|}\\
&= \lim_{n \to \infty} \sqrt[n]{\left| \right|}\\
\end{align*}

Since $\sum |a_n| = \sum a_n$, the series $\displaystyle \sum_{n=1}^\infty AAAAAAAAAAAAAA$ converges absolutely.

Since $|r| < 1$, the series ...  converges by the Geometric Series Test.

Since $|r| \geq 1$, the series ...  diverges by the Geometric Series Test.

The function $f(x)=\frac{}{}$ is continuous, positive, and decreasing on $[1,\infty)$.

\subsection*{Solution}


\subsection{Real-valued functions}\label{subsec:real_valued_functions}

\begin{definition}\label{def:functions_vanish_nowhere}
  Let \( \mathcal{F} \) be a family of functions from a set \( S \) to a ring \( R \). We say that \( \mathcal{F} \) \term{vanishes nowhere} if for every \( x \in S \) there exists a function \( f \in \mathcal{F} \) such that \( f(x) \neq 0_R \).
\end{definition}

\begin{definition}\label{def:epigraph}
  Let \( X \) be an arbitrary set. The \term{epigraph} of the function \( f: X \to \BbbR \) is defined as
  \begin{equation*}
    \epi f \coloneqq \{ (x, r) \in X \times \BbbR \colon r \geq f(x) \},
  \end{equation*}
\end{definition}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}

\title{Calculus With Calvin QD. 27}
\author{Shreenabh Agrawal}
\date{\today}

\begin{document}

\maketitle

\section{Question}
Compute the Integral:
$$I=\int\limits_{0}^{1} \frac{\ln (1+x) \ln (x)}{x} \: d x$$

\section{Solution}
We know the Series Expansion:
$$\ln (1+x)=\sum_{n=1}^{\infty} \frac{(-1)^{n-1} x^{n}}{n}$$
$$\Rightarrow I=\int\limits_{0}^{1} \sum_{n=1}^{\infty} \frac{(-1)^{n-1} x^{n-1}}{n} \: \ln (x) \: d x$$

$$\therefore I=\sum_{n=1}^{\infty} \frac{(-1)^{n-1}}{n} \int\limits_{0}^{1} x^{n-1} \ln (x) \: d x$$

We know the Formula,
$$I_{(m, n)}=\int\limits_{0}^{1} x^{m} \ln ^{n}(x) \: d x=(-1)^{n} \frac{n !}{(m+1)^{n+1}}$$

[This can be derived using IBP and Recursion] \\

Comparing $I_{(m, n)}$ with $I$, we have,
$$m = n-1$$
$$n = 1$$

$$\therefore I_{(n-1,1)}=\frac{(-1)}{n^{2}}$$

$$\begin{array}{l}
\begin{aligned}
\Rightarrow \quad I &=\sum_{n=1}^{\infty} \frac{(-1)^{n-1}}{n} \times \frac{(-1)}{n^{2}} \\
&=\sum_{n=1}^{\infty} \frac{(-1)^{n}}{n^{3}}
\end{aligned}
\end{array}$$

Expanding this, we have:

$$\begin{aligned}
I&=-\frac{1}{1^{3}}+\frac{1}{2^{3}}-\frac{1}{3^{3}}+\frac{1}{4^{3}}-\ldots \\
&=-\frac{1}{1^{3}}-\frac{1}{2^{3}}-\frac{1}{3^{3}}-\frac{1}{4^{3}}-\ldots
{+2\left[\frac{1}{2^{3}}+\frac{1}{4^{3}}+\ldots\right]}\\
&=-\zeta(3)+\left(\frac{1}{2^{3} \cdot 1^{3}}+\frac{1}{2^{3} \cdot 2^{3}}+\frac{1}{2^{3} \cdot 3^{3}}+\ldots\right) \\
&=-\zeta(3)+\frac{1}{4} \zeta(3) \\
\end{aligned}$$

Thus, the final answer is:
$$\boxed{I=-\frac{3 \: \zeta(3)}{4}}$$

\end{document}

Calculus/Calculus_Calvin's Qustions/QD 27 (Aligned Integral).tex


\chapter{Set Theory I}

\epigraph{A set is a Many that allows itself to be thought of as a One.}{\emph{Georg Cantor}}

Watch the \href{https://www.khanacademy.org/math/statistics-probability/probability-library/basic-set-ops/v/intersection-and-union-of-sets}{Khan Academy} videos on basic set operations!

\begin{boxdefine}{Set}{}
	A {\bf set} is a collection of distinct objects. Each object that appears in this collection is called an {\bf element} of the set.
\end{boxdefine}

Elements in sets must be well-defined, unordered, and distinct. You may \emph{describe} sets with duplicate elements, but duplicates do not enlarge the set at all. For example $\{1,2,3,2\}=\{1,2,3\}$, and the number of elements in $\{1,2,3,2\}$ is 3.

\begin{boxexample}{}{}
	Here are a few examples of sets.

	\begin{itemize}
		\item Known speices of birds
		\item Coke brands
		\item The numbers 6, 28, 496, and 8128
	\end{itemize}
\end{boxexample}

\section{Describing a Set}

There are three ways that a set can be described.

\begin{enumerate}
	\item A written description. For example, ``The set of even negative numbers.''
	\item A list (also called roster method/tabular form). For example, $S = \{2,3,5,7\}$.
	\item Set-builder notation (or rule notation). For example, $P = \{2n+1\;:\;n \in \mathbb{Z}\}$.
\end{enumerate}

\section{Set-Builder Notation}

I think the first two are fairly self explanitory, so lets break down builder notation. Consider $P = \{2n+1\;:\;n \in \mathbb{Z}\}$.

\begin{itemize}
	\item The first term, ``$2n+1$'', defines the elements in the set. Note that $n$ is variable.
	\item The symbol ``$:$'' mean ``such that'', and signals we are doing something with $n$.
	\item The symbol $\in$ means ``is an element of''. ``$n \in \mathbb{Z}$'' means that $n$ is an integer.
	\item $\mathbb{Z}$ is the symbol for the set of integers ($\{\dots,-2,-1,0,1,2,3,\dots\}$).
\end{itemize}

So, this reads as ``the set of numbers $2n+1$, where $n$ is an integer.'' This also known as the set of odd numbers. Take your time to understand reading this, you will see this notation a lot.

\begin{boxnotation*}{}{}
	The such-that symbol ``$:$'' can also be written as ``$|$'' or ``$\ni$''.
\end{boxnotation*}

\begin{boxexample}{}{}
	Here are a few examples of set-builder notation.

	\begin{itemize}
		\item $S = \{x \;:\; x > 5, x \in \mathbb{N}\}$. This is the set of natural numbers above 5
		\item $P = \{n \;:\; \text{factors}(n) = \{1,n\}, 1 < n\}$. This is the set of prime numbers
		\item $F = \{g \;:\; 0\% \leq g < 55\%\}$. This is the set of where you don't want your final grade to end up
	\end{itemize}
\end{boxexample}

\section{Some Common Definitions}

Here are some terms and definitions you might run into. The rational number definition looks scary, but try to understand it piece-by-piece.

\medskip
\begin{tabular}{r|c|l}
	\hline
	Symbol & Name & Definition\\
	\hline
	$\varnothing$ & Null Set & The set with no elements\\
	 & Singleton Set & A set with one element\\
	 & Finite Set & A set with finite elements\\
	 & Infinite Set & A set with infinite elements\\
	$\mathbb{N}$ & Natural Numbers & $\{1,2,3,\dots\}$\\
	$\mathbb{W}$ & Whole Numbers & $\{0,1,2,3,\dots\}$\\
	$\mathbb{Z}$ & Integers & $\{\dots, -2, -1, 0, 1, 2, \dots\}$\\
	$\mathbb{Q}$ & Rational Numbers &
	$\displaystyle \bigg\{ \frac{p}{q} : p \in \mathbb{Z}, q \in \mathbb{Z}, q \neq 0 \bigg\}$\\
	$\mathbb{R}$ & Real Numbers & The set of all points on the number line\\
	$\mathbb{R}\setminus\mathbb{Q}$ & Irrational Numbers & Real numbers that are not rational, like $\pi$\\
	$\mathbb{C}$ & Complex Numbers & $\{a+b\mathrm{i} : a \in \mathbb{Z}, b \in \mathbb{Z}\}$\\
	\hline
\end{tabular}
\medskip

\begin{boxnotation*}{}{}
	The null set can also be written as ``$\{\}$''.
\end{boxnotation*}

\begin{boxremark*}{Imaginary Numbers}{}
	{\bf This is completely optional!} I don't think we'll encounter complex numbers in this course. But they're included in the official notes, for some reason. If you're wondering what they are, than good for you! Here's an example. Solve the following system for $y \in \mathbb{R}$:

	\begin{align*}
		(x-4)^2+36=0\\
		\frac{x^2}{2}-4x=y\\
	\end{align*}
	We solve the first equation for $x$:
	\begin{align*}
		(x-4)^2+36=0\\
		\implies (x-4)^2=-36\\
		\implies x-4=\pm\sqrt{-36}\\
	\end{align*}

	But it is impossible to take the square root of a negative number! Does that mean this system is \emph{unsolveable}? Trust me for a moment here, we will get a \emph{real} answer. Suppose we define the imaginary unit $\mathrm{i} = \sqrt{-1}$ (and $\mathrm{i}^2=-1$). Then we have

	\begin{align*}
		x-4=\pm\sqrt{-36}\\
		\implies x-4=\pm6\sqrt{-1}\\
		\implies x-4=\pm6\mathrm{i}\\
		\implies x=4\pm6\mathrm{i}\\
	\end{align*}

	Now we have two complex values for $x$! Substitute $x$ into the second equation to get $y$.

	\begin{align*}
		y=\frac{x^2}{2}-4x\\
		=\frac{(4\pm6\mathrm{i})^2}{2}-4(4\pm6\mathrm{i})\\
		=\frac{16\pm48\mathrm{i}+36\mathrm{i}^2}{2}-16\mp24\mathrm{i}\\
		=8\pm24\mathrm{i}+18\mathrm{i}^2-16\mp24\mathrm{i}\\
		=18\mathrm{i}^2-8\\
		=-18-8=-26\\
	\end{align*}

	Even though $x$ has two posible complex values, we managed to get one real solution for $y$! There is no physical interpretation for imaginary numbers, but we sometimes see them when we break physical phenomenon into systems of equations. If you've reached this far, than congrats for learning complex numbers!
\end{boxremark*}

\section{Compairing Sets}

There are multiple ways to compair sets. We can look at their {\bf cardinality} (number of elements), or we can check if they're {\bf equivalent} (same size) or {\bf equal} (same elements).

\begin{boxdefine}{Cardinality}{}
	The number of elements in a finite set $S$ is called the {\bf cardinality} of $S$, denoted by $|S|$.
\end{boxdefine}

\begin{boxnotation*}{}{}
	The cardinality of set $S$ is also denoted by $n(S)$.
\end{boxnotation*}

\begin{boxexample}{}{}
	$|\{ n \;:\; n \leq 5, n \in \mathbb{W}\}| = |\{0,1,2,3,4,5\}| = 6$
\end{boxexample}

\begin{boxdefine}{Equivalent}{}
	The sets $A$ and $B$ are said to be {\bf equivalent} if $|A| = |B|$, denoted by $A \sim B$.
\end{boxdefine}

\begin{boxexample}{}{}
	The set $A = \{1,2,3,4,5\}$ is equivalent to the set $B = \{5,-2,17,32,12\}$, because $|A| = |B| = 5$.
\end{boxexample}

\begin{boxdefine}{Equal}{}
	The sets $A$ and $B$ are said to be {\bf equal} if they have the same elements, and the same number of elements.
\end{boxdefine}

\begin{boxexample}{}{}
	$\{1,2,3\} = \{3,2,1\}$, but $\{1,2,3\} \ne \{1,2,5\} \ne \{1,2\}$.
\end{boxexample}

We can also check to see if a set is a {\bf subset} of another. For example, the even numbers are a subset of the integers. {\bf Proper subsets} are simular, except that they strictly have less elements than the other set.

\begin{boxdefine}{Subset}{}
	A set $S$ is called a {\bf subset} of a set $T$, written as $S \subseteq T$, when every element of $S$ belongs to $T$.
\end{boxdefine}

\begin{boxexample}{}{}
	$\{1,2,3\} \subseteq \{1,2,3,4\}$, and $\{1,2,3,4\} \subseteq \{1,2,3,4\}$
\end{boxexample}

\begin{boxdefine}{Proper Subset}{}
	A set $S$ is called a {\bf proper subset} of a set $T$, written as $S \subset T$, when $S$ is a subset of $T$ and there exists an element in $T$ which does not belong to $S$.
\end{boxdefine}

\begin{boxexample}{}{}
	$\{1,2,3\} \subset \{1,2,3,4\}$, but $\{1,2,3,4\}$ is not a proper subset of $\{1,2,3,4\}$.
\end{boxexample}

The {\bf superset} and {\bf proper superset} are the opposite definition. For example, if $A \subset B$, then $B \supset A$. When sets share no elements, they are called {\bf disjoint}.

\begin{boxdefine}{Disjoint}{}
	Two sets are {\bf disjoint} when they do not share any elements, ie $S \cap T=\varnothing$.
\end{boxdefine}

\begin{boxexample}{}{}
	The sets $\{1,2,3\}$ and $\{4,5,6\}$ are disjoint, since they share no members.
\end{boxexample}

\section{Set Operations}

Operations are used to transform existing sets into news sets.  The first operation is the {\bf power set}. Basically, it's the set of all the possible combinations.

\begin{boxdefine}{Powerset}{}
	The {\bf power set} is the set of all subsets, denoted by $\mathcal{P}(A)$.
\end{boxdefine}

\begin{boxproposition}{Cardinality of the Power Set}{}
	The cardinality of the power set of $A$ is given by $2^{|A|}$.
\end{boxproposition}

\begin{boxexample}{}{}
	If $A=\{a,b\}$, than $\mathcal{P}(A) = \{\varnothing,\{a\},\{b\},\{a,b\}\}$. The cardinality of $\mathcal{P}(A)$ is $2^{2}=4$.
\end{boxexample}

The {\bf compliment} of a set is ``everything that's not in the set''. Since we cannot literally put ``everything'' into a set, we first define what {\bf universe} we're in.

\begin{boxdefine}{Universe of Discourse}{}
	The {\bf universe of discourse} (or {\bf universal set}) contains all of the objects we might encounter in a given situation, denoted by $\mathcal{U}$.
\end{boxdefine}

\begin{boxexample}{}{}
	There are 400 Pokemon in the Galar Pokedex. You might say those Pokemon are our universal set $\mathcal{U}$, if we are trying to complete the Pokedex.
\end{boxexample}

\begin{boxdefine}{Complement}{}
	The {\bf compliment} of a set $S$, written as $S^\complement$, is the set of all elements in $\mathcal{U}$ but not in $S$. It can be written as

	\[
		S^\complement = \{x \;:\; \in \mathcal{U}, x \notin S\}
	\]
\end{boxdefine}

\begin{boxnotation*}{}{}
	The complement of a set can also be written as $\overline{S}$ or $S'$
\end{boxnotation*}

\begin{boxexample}{}{}
	On a six-sided die, what is the compliment of the even sides $\{2,4,6\}$? Answer: the odd sides $\{1,3,5\}$.
\end{boxexample}

Finally, lets talk about what we can do with two sets. We can combine them with a {\bf union}, we can find shared elements with the {\bf intersection}, and we can remove elements with the {\bf set-difference}.

\begin{boxdefine}{Union}{}
	The {\bf union} of two sets $S$ and $T$, denoted as $S \cup T$, is the set of all elements belonging to either set $S$ or set $T$. We can define it as

	\[
		S \cup T = \{x \;:\; x \in S \;\text{OR}\; x \in T \} = \{x \;:\; x \in S \lor x \in T \}
	\]
\end{boxdefine}

\begin{boxexample}{}{}
	$\{a,b,c\} \cup \{1,2,a\} = \{1,2,a,b,c\}$
\end{boxexample}

If the sets $A$ and $B$ are disjoint ($A \cap B = \phi$), we call their union ($A \cup B$) a {\bf disjoint union}.

\begin{boxdefine}{Intersection}{}
	The {\bf intersection} of two sets $S$ and $T$, denoted by $S\cap T$, is the set of all elements belonging to both set $S$ and set $T$. We can define it as

	\[
		S \cap T = \{ x : x \in S \;\text{AND}\; x \in T\} = \{ x : x \in S \land x \in T\}
	\]
\end{boxdefine}

\begin{boxexample}{}{}
	The intersection between even numbers and prime numbers is ${2}$, because 2 is the only number that is even and prime.
\end{boxexample}

\begin{boxdefine}{Set-Difference}{}
	The {\bf set-difference} of two sets $S$ and $T$, denoted by $S \setminus T$, is the set of all elements belonging to $S$, but not $T$. We can define it as

	\[
		S \setminus T = \{x : x \in S \;\text{AND}\; x \notin T\} = \{x : x \in S \land x \notin T\}
	\]
\end{boxdefine}

\begin{boxnotation*}{}{}
	The set-different symbol ``$S \setminus T$'' is also written as ``$S-T$''.
\end{boxnotation*}

\begin{boxexample}{}{}
	$\{1,2,3\} \setminus \{3\} = \{1,2\}$.
\end{boxexample}