\section*{Problem 3: Dual Coordinate Ascent}

Consider the problem
\begin{equation}
  \min_w L\left(x\right),~\text{where}~
  L\left(x\right) = \sum_{i=1}^n\left(w \cdot x_i - y_i\right)^2 + \lambda \left\lVert
    w
  \right\rVert^2.
  \label{eqn:3problem}
\end{equation}

\begin{enumerate}
\item
  Show that the solution for Equation \ref{eqn:3problem} is obtained for weights
  \begin{align}
    w^* &= \left(X^\intercal X +\lambda I\right)^{-1}X^\intercal Y \label{eqn:3w_star} \\
    &= \frac{1}{\lambda}X^\intercal\alpha^*, \label{eqn:3w_star2}
  \end{align}
  where $\alpha^* = \left(I + XX^\intercal/\lambda\right)^{-1}$.

  \subsection*{Solution}
  \begin{proof}
    We can take the derivative of $L$ in Equation \ref{eqn:3problem}
    directly. Note that $D\left(x \mapsto Ax\right)\left(x\right) = A$ and
    $D\left(x \mapsto x^\intercal x\right)\left(x\right) = 2x^\intercal.$
    Therefore by the chain rule,
    \begin{equation}
      D\left(x \mapsto \left(Ax\right)^\intercal\left(Ax\right)\right)(x)
      = 2 x^\intercal A^\intercal A.
    \end{equation}

    We can reformulate Equation \ref{eqn:3problem} as a function of $w$
    \begin{align}
      l_{X,y}\left(w\right)
      &= \left(Aw - y\right)^\intercal\left(Aw - y\right) + \lambda w^\intercal w \nonumber\\
      &=
        \left(Aw\right)^\intercal\left(Aw\right)
        - 2y^\intercal Aw + y^\intercal y
        + \lambda w^\intercal w.
        \label{eqn:3problem_new}
    \end{align}

    Taking the derivative, we have that
    \begin{equation}
      D\left(l_{X,y}\right)(w)
      = 2 w^\intercal X^\intercal X - 2 y^\intercal X + 2\lambda w^\intercal.
      \label{eqn:3derivative}
    \end{equation}

    Setting Equation \ref{eqn:3derivative} to $0$ and solving for $w$, we have
    \begin{align*}
      0 &= 2 w^\intercal X^\intercal X - 2 y^\intercal X + 2w^\intercal \\
      w^\intercal\left(X^\intercal X  + \lambda I\right)
        &= y^\intercal X \\
      \left(X^\intercal X  + \lambda I\right)w
        &= X^\intercal y \\
      w &= \left(X^\intercal X  + \lambda I\right)^{-1}X^\intercal y.
    \end{align*}

    Since Equation \ref{eqn:3problem_new} is a quadractic form, the problem is
    convex, and
    \begin{equation*}
      w^* = \left(X^\intercal X +\lambda I\right)^{-1}X^\intercal Y
    \end{equation*}
    minimizes Equation \ref{eqn:3problem}.

    Now, note that
    \begin{align*}
      \left(X^\intercal X + \lambda I\right)X^\intercal
      &= X^\intercal XX^\intercal + \lambda X^\intercal
        = X^\intercal\left(XX^\intercal + \lambda I\right).
    \end{align*}
    Multiplying on the left by $\left(X^\intercal X + \lambda I\right)^{-1}$ and on the right
    by $\left(XX^\intercal + \lambda I\right)^{-1}$, we have that
    \begin{align*}
      X^\intercal\left(XX^\intercal + \lambda I\right)^{-1} =
      \left(X^\intercal X + \lambda I\right)^{-1}X^\intercal.
    \end{align*}

    Substituting this into Equation \ref{eqn:3w_star}, we obtain
    \begin{align*}
      w^*
      &= X^\intercal\left(XX^\intercal + \lambda I\right)^{-1}y \\
      &= X^\intercal\left(\lambda\left(I + \frac{XX^\intercal}{\lambda}\right)\right)^{-1}y \\
      &= \frac{1}{\lambda}X^\intercal\left(I + \frac{XX^\intercal}{\lambda}\right)^{-1}y,
    \end{align*}
    which gives us the desired result.
  \end{proof}

  If $\lambda = 0$, in general, this is not true since
  $XX^\intercal + \lambda I$ may not be invertable when $n > d$. However, if
  $d \geq n$, and $\operatorname{rank}\left(X\right) \geq n$, Equation
  \ref{eqn:3w_star2} may still be well-defined.
\item Define
  \begin{equation}
    G\left(\alpha_1,\alpha_2,\ldots,\alpha_n\right) = \frac{1}{2}\alpha^\intercal\left(
      I + XX^\intercal/\lambda\right)\alpha - Y^\intercal \alpha.
    \label{eqn:3g}
  \end{equation}

  Start with $\alpha = 0$. Choose coordinate $i$ randomly, and update
  \begin{equation}
    \alpha_i = \argmin_z G\left(
      \alpha_1,\ldots,\alpha_{i-1},z,\alpha_{i+1},\ldots,\alpha_n
    \right).
  \end{equation}

  Show that the solution to the inner optimization problem for $\alpha_i$ is:
  \begin{equation}
    \alpha_i = \frac{y_i - \frac{1}{\lambda}\left(\sum_{j \neq i} \alpha_j x_j\right) \cdot x_i}
    {1 + \lVert x_i \rVert^2/\lambda}.
    \label{eqn:3update}
  \end{equation}

  \subsection*{Solution}
  \begin{proof}
    We can take the partial derivative of Equation \ref{eqn:3g} directly to obtain
    \begin{align}
      \frac{\partial{G}}{\partial{\alpha_i}}
      &= \alpha_i + \frac{1}{\lambda}\left(\alpha^\intercal X X^\intercal\right)_i - y_i
        \nonumber\\
      &= \alpha_i + \frac{1}{\lambda}\left(
        \alpha_i\left\lVert x_i\right\rVert^2 +
        \sum_{j \neq i} \alpha_j \left(x_j \cdot x_i \right)
        \right) - y_i. \label{eqn:3g_deriv}
    \end{align}

    Setting Equation \ref{eqn:3g_deriv} to $0$, solving for $\alpha_i$, and
    taking advantage of convexity, we find
    \begin{equation}
      \alpha_i = \frac{y_i - \left(\sum_{j \neq i}\alpha_j x_j \right) \cdot x_i}{
        1 + \lVert x_i \rVert^2/\lambda}
  \end{equation}
  minimizes Equation \ref{eqn:3g} as a function of $\alpha_i$, and solves the
  inner optimization problem.
\end{proof}

\item What is the computational complexity of this update, as it is stated?

  \subsection*{Solution}
  The complexity of updating $\alpha_i$ with Equation \ref{eqn:3update} is
  $O(nd)$ since we need to iterate over the $n$ rows of $X$, and take the
  $d$-dimensional dot product of each row with $x_i$.
\item What is the computational complexity of one stochastic gradient descent update?
  \subsection*{Solution}

  The complexity of one stochastic gradient descent update is $O(d)$. We
  computed the derivative in Equation \ref{eqn:3derivative} for the full matrix
  $X$. In stochastic gradient descent we'd replace $X$ by a vector by randomly
  sampling a row from $X$. Then, to compute the gradient we have to do some dot
  products along with scalar operations.
\item Now consider the procedure.
  \begin{itemize}
  \item Start with $\alpha = 0$, $w = \frac{1}{\lambda}X^\intercal \alpha = 0.$
  \item Choose coordinate $i$ randomly and perform the following update:
    \begin{itemize}
    \item Compute the differences:
      \begin{equation}
        \Delta \alpha_i = \frac{\left(y_i - w \cdot x_i\right) - \alpha_i}{
          1 + \left\lVert x_i\right\rVert^2 / \lambda}
        \label{eqn:3alpha_delta}
      \end{equation}
    \item Update the parameters as follows:
      \begin{align}
        \alpha_i &\leftarrow \alpha_i + \Delta\alpha_i \nonumber\\
        w &\leftarrow w + \frac{\Delta \alpha_i}{\lambda}x_i.
            \label{eqn:3update_new}
      \end{align}
    \end{itemize}
  \end{itemize}
  Prove that the update rule in Equation \ref{eqn:3update_new} is valid.

  \subsection*{Solution}
  \begin{proof}
    Let $\alpha^\prime$ and $w^\prime$ be the result of updating coordinate $i$
    of $\alpha$. Assume that $w = \frac{1}{\lambda}X^\intercal\alpha$. This is
    true when $\alpha = 0$. We will show that this invariant holds as $\alpha$
    is updated.

    To see that, the update rule for $w$ is valid, we can rewrite
    \begin{equation}
      w = \frac{\alpha_1}{\lambda}x_1 + \cdots + \frac{\alpha_i}{\lambda}x_i + \cdots + \frac{\alpha_n}{\lambda}x_n,
    \end{equation}
    so
    \begin{align*}
      w^\prime
      &= w + \frac{\Delta\alpha_i}{\lambda}x_i \\
      &= \frac{\alpha_1}{\lambda}x_1 + \cdots + \frac{\alpha_i + \Delta\alpha_i}{\lambda}x_i + \cdots + \frac{\alpha_n}{\lambda}x_n \\
      &= \frac{\alpha_1}{\lambda}x_1 + \cdots + \frac{\alpha^\prime_i}{\lambda}x_i + \cdots + \frac{\alpha_n}{\lambda}x_n \\
      &= \frac{1}{\lambda}X^\intercal \alpha^\prime.
    \end{align*}
    Thus, the $w$ update is valid.

    To see that the $\alpha$ update is valid, we show that Equations
    \ref{eqn:3update} and \ref{eqn:3update_new} are equivalent. Both algorithms
    initiate $\alpha = 0$, so they are equivalent at the initial step.

    By using the definition $w = \frac{1}{\lambda}X^\intercal \alpha$,
    \begin{align*}
      \alpha_i^\prime
      &= \alpha_i + \Delta \alpha_i \\
      &= \frac{\left(y_i - w \cdot x_i\right) - \alpha_i}{
        1 + \left\lVert x_i\right\rVert^2 / \lambda} +
        \frac{\alpha_i + \alpha_i\left\lVert x_i\right\rVert^2/\lambda}{1 + \left\lVert x_i\right\rVert^2 / \lambda} \\
      &=\frac{1}{1 + \left\lVert x_i\right\rVert^2 / \lambda}
        \left(
        y_i - \frac{1}{\lambda}\left(\sum_{j \neq i} \alpha_jx_j\right) \cdot x_i
        - \frac{1}{\lambda}\alpha_i \left\lVert x_i \right\rVert^2
        + \frac{1}{\lambda}\alpha_i \left\lVert x_i \right\rVert^2
        \right) \\
      &= \frac{y_i - \frac{1}{\lambda}\left(\sum_{j \neq i} \alpha_j x_j\right) \cdot x_i}{1 + \left\lVert x_i\right\rVert^2 / \lambda},
    \end{align*}
    so both update rules are equivalent.
  \end{proof}

\item What is the computation complexity of the update defined by Equations
  \ref{eqn:3alpha_delta} and \ref{eqn:3update_new}?

  \subsection*{Solution}

  The computation complexity is $O(d)$. Computing the dot product when computing
  $\Delta\alpha_i$ and updating $w$ are both $O(d)$ operations. Everywhere else,
  we do scalar operations.

  This is much faster than the $O(nd)$ update for Equation \ref{eqn:3update}.
\end{enumerate}


\section{Bayesian Model for Cubic Splines}
Notice that the above definition can be easily extended to a Bayesian
problem: If we define a distribution for $f$, say $dP(f)$ then we may
consider the average risk
\[
\int_f  \E [ || \hat{g} - f ||^2 | f] \, dP(f)
\]
as a criterion.

This follows section 3.6 in the book by Hastie and Tibshirani.

The cubic smoothing spline can be derived from a number of Bayesian
models for smoothing. The details are hard, but can be found in a book
by Wahba.

Here we will demonstrate a fairly simple example.

Remember we can write any natural cubic spline as
\[
g(x) = \bB(x) \bg{\theta}.
\]
Consider the following Bayesian set-up:

Model assumptions: Assume the data $\by$ follow a Gaussian distribution $N(\bB
\bg{\theta}, \sigma^2 {\mathbf I}_n)$.

Prior assumptions: Assume $\bg{\theta}$ follows a multivariate
Gaussian prior distribution with mean $0$ and variance
$\sigma^2/\lambda  \bg{\Omega}^{-1}$.

it follows that the posterior distribution of $\bg{\theta}$ is
multivariate Gaussian with mean
\[
\E(\bg{\theta}|\by) = \bB(\bB'\bB +  \lambda \bg{\Omega})^{-1} \bB'\by
\]
which is the natural smoothing spline estimate of $\bg{\theta}$.


\section*{Principal Component Analysis}
$\mathbf{X} \in \mathbb{R}^{D \times N}$. $N$ observations, $K$ rank.\\
1. Empirical Mean: $\overline{\mathbf{x}} = \frac{1}{N} \sum_{n=1}^N \mathbf{x}_n$.\\
2. Center Data: $\overline{\mathbf{X}} = \mathbf{X} - [\overline{\mathbf{x}}, \ldots, \overline{\mathbf{x}}] = \mathbf{X} - \mathbf{M}$.\\
3. Cov.: $\boldsymbol{\Sigma} = \frac{1}{N	} \sum_{n=1}^N (\mathbf{x}_n - \overline{\mathbf{x}}) (\mathbf{x}_n - \overline{\mathbf{x}})^\top = \frac{1}{N} \overline{\mathbf{X}}\overline{\mathbf{X}}^\top$.\\
4. Eigenvalue Decomposition: $\boldsymbol{\Sigma} = \mathbf{U} \boldsymbol{\Lambda} \mathbf{U}^\top$.\\
5. Select $K < D$, only keep $\mathbf{U}_K, \boldsymbol{\lambda}_K$.\\
6. Transform data onto new Basis: $\overline{\mathbf{Z}}_K = \mathbf{U}_K^\top \overline{\mathbf{X}}$.\\
7. Reconstruct to original Basis: $\tilde{\overline{\mathbf{X}}} = \mathbf{U}_k \overline{\mathbf{Z}}_K$.\\
8. Reverse centering: $\tilde{\mathbf{X}} = \tilde{\overline{\mathbf{X}}} + \mathbf{M}$.\\
For compression save $\mathbf{U}_k, \overline{\mathbf{Z}}_K, \overline{\mathbf{x}}$.\\
$\mathbf{U}_k \in \mathbb{R}^{D \times K}, \boldsymbol{\Sigma} \in \mathbb{R}^{D \times D}, \overline{\mathbf{Z}}_K \in \mathbb{R}^{K \times N}, \overline{\mathbf{X}} \in \mathbb{R}^{D \times N}$

\subsection*{Reconstruction Error Exercise}
$\tilde {\mathbf{X}} = \mathbf{U}_K \mathbf{U}_K^\top \bar {\mathbf{X}}$, the error is $\frac{1}{N} \sum_{i=1}^N ||\tilde x_i - \bar x_i||_2^2$ \\
$=\frac{1}{N}||\tilde{\mathbf{X}}-\bar{\mathbf{X}}||_F^2 = \frac{1}{N}||(\mathbf{U}_K \mathbf{U}_K^\top - \mathbf{I}_d)\bar{\mathbf{X}}||_F^2$ \\
$= \frac{1}{N} \text{trace}((\mathbf{U}_K \mathbf{U}_K^\top - \mathbf{I}_d)\bar{\mathbf{X}} \bar{\mathbf{X}}^\top (\mathbf{U}_K \mathbf{U}_K^\top - \mathbf{I}_d)^\top)$ \\
$=\text{trace}((\mathbf{U}_K \mathbf{U}_K^\top - \mathbf{I}_d) \Sigma (\mathbf{U}_K \mathbf{U}_K^\top - \mathbf{I}_d))$ \ $\textcolor{gray}{\Sigma = \mathbf{U}\Lambda \mathbf{U}^\top}$ \\
$=\text{trace}((\mathbf{U}_K \mathbf{U}_K^\top  \mathbf{U} -  \mathbf{U})\Lambda ( \mathbf{U}^\top \mathbf{U}_K \mathbf{U}_K^\top -  \mathbf{U}^\top))$ \\
$=\text{trace}(([\mathbf{U}_K ;\mathbf{0}] -  \mathbf{U})\Lambda ([\mathbf{U}_K; \mathbf{0}] -  \mathbf{U}^\top)) $ \\
$=\text{trace}(\sum_{i=K+1}^D \lambda_i u_i u_i^\top) = \sum_{i=K+1}^D \lambda_i \cdot \text{trace}(u_i u_i^\top)
$ \\
$=\sum_{i=K+1}^D \lambda_i$ since $\text{trace}(u_i u_i^\top) = ||u_i||_2^2 = 1$
\subsection*{Iterative View}
Residual $r_i$: $x_i - \tilde{x}_i = I - uu^T  x_i$\\
Cov of $r$:  $\frac{1}{n} \sum_{i=1}^n (I-uu^T)x_i x_i^T (I-uu^T)^T =$ \\
$(I-uu^T) \Sigma (I-uu^T)^T = \Sigma - 2\Sigma u u^T + u u^T \Sigma u u ^T = \Sigma - \lambda uu^T$ \\
1. Find principal eigenvector of $(\Sigma - \lambda u u^T)$\\
2. Which is the second eigenvector of $\Sigma$\\
3. Iterating to get $d$ principal eigenvector of $\Sigma$

\subsection*{Power Method}
Power iteration: $v_{t+1} = \frac{Av_t}{||Av_t||}$, $\lim_{t \rightarrow \infty} v_t = u_1$\\
Assuming $\langle u_1, v_0 \rangle \not = 0$ and $|\lambda_1| > |\lambda_j| (\forall j \geq 2)$
Then $\lambda_1 = \lim_{t\to \infty}||\mathbf{Av}_t||/||\mathbf{v}_t||$


\section{Linear Groups}
In this section, let $\mathbb F=\mathbb R$ or $\mathbb C$.
Let $M_{n\times n}(\mathbb F)$ be the set of $n\times n$ matrices with entries in $\mathbb F$.
Matrix multiplication then gives us a binary operation on $M_{n\times n}(\mathbb F)$.
$I_n$ is certainly an identiy element of this operation, so $M_{n\times n}(\mathbb F)$ is a monoid under this operation.
\begin{proposition}
    An $n\times n$ matrix is invertible iff its determinant is nonzero.
\end{proposition}
\begin{proof}
    In Vectors \& Matrices.
\end{proof}
\begin{definition}
    The set of $n\times n$ matrix with entries in $\mathbb F$ which has inverses, written as $\operatorname{GL}_n(\mathbb F)$, is a group under matrix multiplication.
    Equivalently, by the preceding proposition, $\operatorname{GL}_n(\mathbb F)$ consists of all $n\times n$ matrices with nonzero determinant.
\end{definition}
The map $\det:\operatorname{GL}_n(\mathbb F)\to\mathbb F^\times=(\mathbb F\setminus\{0\},\times,1)$ is a (surjective) group homomorphism since $\det(AB)=\det(A)\det(B)$.
\begin{definition}
    The kernel of $\det$ is called the special linear group $\operatorname{SL}_n(\mathbb F)$, which consists of all $n\times n$ matrices $M$ with $\det M=1$.
\end{definition}
So $\operatorname{SL}_n(\mathbb F)\unlhd\operatorname{GL}_n(\mathbb F)$.
By Theorem \ref{1_isom_thm}, we have $\operatorname{GL}_n(\mathbb F)/\operatorname{SL}_n(\mathbb F)\cong\mathbb F^\times$.\\
The group $\operatorname{GL}_n(\mathbb F)$ acts on $\mathbb F^n$ by $M\star x=Mx$ (here $x$ is written as column vector).
This corresponds to a homomorphism $\rho:\operatorname{GL}_n(\mathbb F)\to\operatorname{Sym}(\mathbb F^n)$.
Note that $\rho$ is injective by considering the action of a matrix on the standard basis.
Also, the image of $\rho$, which is isomorphic to $\operatorname{GL}_n(\mathbb F)$ by Theorem \ref{1_isom_thm}, is precisely the set of invertible linear maps $\mathbb F^n\to\mathbb F^n$.
\begin{proposition}
    If $A$ is a $n\times n$ matrix represents a linear transformaton $\alpha:\mathbb F^n\to\mathbb F^n$ in the standard basis $\{e_i\}$.
    If we have another basis $\{f_i\}$, then in the new basis, $\alpha$ is represented by $P^{-1}AP$ where $P$ is the (invertible) matrix with entries determined by the linear combination of $f_j$ by $\{e_i\}$.
    That is
    $$f_j=\sum_{i=1}^nP_{ij}e_i$$
    Group theoretically, the group $\operatorname{GL}_n(\mathbb F)$ can act on the set of all $n\times n$ matrices, so the orbit of $A$ under this action is all matrices in the form $P^{-1}AP$, that is, the matrices that actually represents the ``same'' linear transformation but in different basis.
\end{proposition}
\begin{proof}
    It is easy to check that conjugating by invertible matrix is indeed an action, and the formula is just verification.
\end{proof}
\begin{example}
    1. Every complex matrix is conjugate to a matrix in the Jordan normal form.
    For $2$-dimensional matrices, any complex $2\times 2$ matrix is conjugate to one of
    $$
    \begin{pmatrix}
        \lambda_1&0\\
        0&\lambda_2
    \end{pmatrix},\lambda_1\neq\lambda_2;
    \begin{pmatrix}
        \lambda&0\\
        0&\lambda
    \end{pmatrix};
    \begin{pmatrix}
        \lambda&1\\
        0&\lambda
    \end{pmatrix}
    $$
    One can see easily by looking at eigenvalues that no two of them are conjugate to each other.
    Also, for different value of $\lambda$, in the latter two cases, any two matrices of the same type are not conjugate to each other either.
    In the first, case, $\operatorname{diag}(\lambda_1,\lambda_2),\operatorname{diag}(\mu_1,\mu_2)\iff \{\lambda_1,\lambda_2\}=\{\mu_1,\mu_2\}$.\\
    Now we consider the stabilisers of them.
    Consider an invertible matrix
    $\left(\begin{smallmatrix}
        a&b\\
        c&d
    \end{smallmatrix}\right)$.
    Then a matrix of the first type is stabilised by it iff $b=c=0$, and every invertible matrix stabilises a matrix of the second type.
    For the third type, if this matrix does stabilise a matrix of that kind, then we need $c=0,a=d$, so the stabilisers are the matrices of the form
    $\left(\begin{smallmatrix}
        a&b\\
        0&a
    \end{smallmatrix}\right)$.\\
    2. Consider Mobius transformations $f(z)=\frac{az+b}{cz+d},f'(z)=\frac{a'z+b'}{c'z+d'}$, then $f\circ f'=\frac{a''z+b''}{c''z+d''}$ where we have
    $$\begin{pmatrix}
        a&b\\
        c&d
    \end{pmatrix}
    \begin{pmatrix}
        a'&b'\\
        c'&d'
    \end{pmatrix}
    =
    \begin{pmatrix}
        a''&b''\\
        c''&d''
    \end{pmatrix}$$
    which implies a homomorphism $\phi:\operatorname{SL}_2(\mathbb C)\to\mathcal{M}$.
    This homorphism is surjective since multiplying all of $a,b,c,d$ by a nonzero complex number does not change the Mobius transformation.
    How about the kernel of $\phi$?
    Suppose
    $$\phi\left(\begin{pmatrix}
        a&b\\
        c&d
    \end{pmatrix}\right)=\operatorname{id}$$
    So $az+b=(cz+d)z$ which has to be true for all $z\in\mathbb C$, hence $c=0,d=a,b=0$.
    This implies that the matrix is either $I$ or $-I$.
    By Theorem \ref{1_isom_thm},
    $$\operatorname{PSL}_2(\mathbb C)=\operatorname{SL}_2(\mathbb C)/\{\pm I\}\cong\mathcal M$$
\end{example}
\begin{definition}
    The $n^{th}$ orthogonal group is defined by
    $$\operatorname{O}(n)=\{P\in\operatorname{GL}_n(\mathbb R):PP^\top=I\}$$
\end{definition}
Note that $PP^\top=I\iff P^\top P=I$.
This is a group since
$$\forall P,Q\in\operatorname{O}(n),(PQ^{-1})(PQ^{-1})^\top=(PQ^\top)(PQ^\top)^\top=PQ^\top QP^\top=I$$
therefore $PQ^{-1}\in \operatorname{O}(n)$.
Also $I\in \operatorname{O}(n)$, hence $\operatorname{O}(n)\neq\varnothing$, so indeed $\operatorname{O}(n)\le \operatorname{GL}_n(\mathbb R)$.\\
In addition, the columns of an orthogonal matrix forms an orthonormal basis for $\mathbb R^n$, and the converse is also true.
\begin{lemma}
    Let $P\in\operatorname{GL}_n(\mathbb R)$, then $P\in \operatorname{O}(n)\iff\forall v,w\in\mathbb R^n, (Pv)\cdot(Pw)\iff v\cdot w$.
\end{lemma}
\begin{proof}
    Trivial.
\end{proof}
\begin{corollary}
    Any orthogonal matrix preserves lengths and angles.
\end{corollary}
\begin{proof}
    Immediate.
\end{proof}
Note that $\det (A^\top)=\det (A)$, so $\forall P\in\operatorname{O}(n),\det P=\pm 1$.
\begin{definition}
    The $n^{th}$ special orthorgonal group $\operatorname{SO}(n)$ consists of orthogonal matrices with determinant $1$.
\end{definition}
Or equivalently, $\operatorname{SO}(n)=\ker(\det|_{\operatorname{O}(n)})$, so immediately we have $\operatorname{SO}(n)\unlhd\operatorname{O}(n)$.\\
Typical examples of non-special orthogonal matrices are refections.
For an unit vector $a\in\mathbb R^n$, we can consider the reflection $R_a:v\mapsto v-2(v\cdot a)a$.
This is obviously linear and can be geometrically interpreted as reflection.
So if we choose a basis for $\mathbb R^n$ which consists of $a$ and an orthonormal basis for the subspace $a^\perp=\{v\in\mathbb R^n:v\perp a\}$, then the union of them gives an orthonormal basis for $\mathbb R^n$, which induces the orthogonal matrix representing the reflection.
Alternatively we can evaluate to get $R_a(v)\cdot R_a(w)=v\cdot w$ for every $v,w\in\mathbb R^n$.
But by its form in our specially chosen basis, we have $\det R_a=-1$, so $R_a\in \operatorname{O}(n)\setminus\operatorname{SO}(n)$.
\begin{lemma}
    $$\operatorname{SO}(2)=\left\{\begin{pmatrix}
        \cos\theta&-\sin\theta\\
        \sin\theta&\cos\theta
    \end{pmatrix}:\theta\in\mathbb R\right\}$$
\end{lemma}
\begin{proof}
    Consider any $A=\left(\begin{smallmatrix}
        a&b\\
        c&d
    \end{smallmatrix}\right)\in\operatorname{SO}(2)$, then since we have $AA^\top=I$, $a=d,b=-c$.
    Then $1=ad-bc=a^2+b^2$, so $a,b\in [-1,1]$, so we can write $a=\cos\theta$, consequently $b=-\sin\theta$ (the sign does not matter since we can always do $\theta\mapsto -\theta$).
    The lemma follows.
\end{proof}
Note that for $A\in\operatorname{O}(2)\setminus\operatorname{SO}(2)$, we have $a=-d,b=c$ and $a^2+c^2=1$, therefore
$$\operatorname{O}(2)\setminus\operatorname{SO}(2)=\left\{\begin{pmatrix}
    \cos\phi&\sin\phi\\
    \sin\phi&-\cos\phi
\end{pmatrix}:\phi\in\mathbb R\right\}=\begin{pmatrix}
    1&0\\
    0&-1
\end{pmatrix}\operatorname{SO}(2)$$
One immediately have the following corollaries.
\begin{corollary}
    $\operatorname{O}(2)\setminus\operatorname{SO}(2)$ consists of reflections.
\end{corollary}
\begin{corollary}\label{two_reflections}
    Everything in $\operatorname{O}(2)$ is a product of at most $2$ reflections.
\end{corollary}
\begin{remark}
    Corollary \ref{two_reflections} can be generalized to $\mathbb R^n$ by induction (with, of course, the replacement of $2$ by $n$).
\end{remark}
We proceed to analyze the rotations and reflections in $\mathbb R^3$.
\begin{theorem}
    Let $A\in \operatorname{SO}(3)$, then there is an unit vector $v\in\mathbb R^3$ such that $Av=v$.
\end{theorem}
\begin{proof}
    Suffice to show that $A$ has eigenvalue $1$.
    Indeed, $\det(A-I)=\det(A^\top-I)=\det A\det (A^\top-I)=\det(I-A)=(-1)^3\det(A-I)=-\det(A-I)$, hence $\det(A-I)=0$.
\end{proof}
In fact, we can generalize $3$ to any $2n+1$ for $n\in\mathbb N$ using exactly the same way.
\begin{corollary}\label{SO3_conj}
    Every $A\in\operatorname{SO}(3)$ is conjugate to a matrix in the form
    $$\begin{pmatrix}
        1&0&0\\
        0&\cos\theta&-\sin\theta\\
        0&\sin\theta&\cos\theta
    \end{pmatrix}$$
\end{corollary}
\begin{proof}
    By the theorem there is some unit vector $f_1\in\mathbb R^3$ such that $Af_1=f_1$.
    And choose an orthonormal basis $f_2,f_3$ of $f_1^\perp$, so that $f_1,f_2,f_3$ is an orthonormal basis of $\mathbb R^3$.
    Then for $i=2,3$, we have $(Af_i)\cdot f_1=(Af_i)\cdot (Af_1)=f_i\cdot f_1=0$.
    So $Af_i$ is a linear combination of $f_2,f_3$ only.
    Hence in this new basis, the matrix will look like
    $$A'=\begin{pmatrix}
        1&0&0\\
        0&a&b\\
        0&c&d
    \end{pmatrix}$$
    By computing $A'A'^\top=I$, we find
    $$\begin{pmatrix}
        a&b\\
        c&d
    \end{pmatrix}=\begin{pmatrix}
        \cos\theta&-\sin\theta\\
        \sin\theta&\cos\theta
    \end{pmatrix}$$
    for some $\theta$.
    The result follows.
\end{proof}
Note that we can manipulate the change-of-basis matrix to make it special orthogonal.
\begin{corollary}
    Every element in $\operatorname{O}(3)$ is the composition of at most $3$ reflections.
\end{corollary}
\begin{proof}
    Every element in $\operatorname{SO}(3)$ is the composition of two reflections by observing
    $$\begin{pmatrix}
        1&0&0\\
        0&\cos\theta&-\sin\theta\\
        0&\sin\theta&\cos\theta
    \end{pmatrix}=\begin{pmatrix}
        1&0&0\\
        0&1&0\\
        0&0&-1
    \end{pmatrix}\begin{pmatrix}
        1&0&0\\
        0&\cos(-\theta)&\sin(-\theta)\\
        0&\sin(-\theta)&-\cos(-\theta)
    \end{pmatrix}$$
    and using Corollary \ref{SO3_conj}.
    Now choose any reflection $R$, then $\operatorname{O}(3)\setminus\operatorname{SO}(3)=R\operatorname{SO}(3)$, therefore every other element in $\operatorname{O}(3)$ is a composition of at most $3$ reflections.
\end{proof}


\section{Limits and Continuity}\label{sec:LimitsContinuityPartialDifferentiation}

To develop calculus for functions of one variable, we needed to make
sense of the concept of a limit, which was used in the definition of a
continuous function and the derivative of a function. Limits involving
functions of two variables can be considerably more difficult to deal
with; fortunately, most of the functions we encounter are fairly easy
to understand.

The potential difficulty is largely due to the fact that there are
many ways to ``approach'' a point in the $x$-$y$ plane. If we want to
say that $\ds\lim_{(x,y)\to(a,b)}f(x,y)=L$, we need to capture the
idea that as $(x,y)$ gets close to $(a,b)$ then $f(x,y)$ gets close to
$L$. For functions of one variable, $f(x)$, there are only two ways
that $x$ can approach $a$: from the left or right. But there are an
infinite number of ways to approach $(a,b)$: along any one of an
infinite number of straight lines, or even along a curved path in the $x,y$-plane. We might hope that it's
really not so bad---suppose, for example, that along every possible
line through $(a,b)$ the value of $f(x,y)$ gets close to $L$; surely
this means that ``$f(x,y)$ approaches $L$ as $(x,y)$ approaches
$(a,b)$''. Sadly, no.

\begin{figure}[H]
%\texonly
\centerline{
\vbox{\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <3truecm,3truecm>
\setplotarea x from 0 to 1.1, y from 0 to 1.1
\put {\hbox{\epsfxsize12cm\epsfbox{images/weird_limit.eps}}} at 0 0
\endpicture}}
%\endtexonly
\caption{$\ds f(x,y)={xy^2\over x^2+y^4}$}
\label{fig:weird limit}
\end{figure}

\begin{example}{Weird Limit}{weird limit}
Analyze $f(x,y)=xy^2/(x^2+y^4)$.
\end{example}
\begin{solution}
When $x=0$ or $y=0$, $f(x,y)$ is 0, so the limit of $f(x,y)$ approaching the
origin along either the $x$ or $y$ axis is 0. Moreover, along the line
$y=mx$, $f(x,y)=m^2x^3/(x^2+m^4x^4)$. As $x$ approaches 0 this expression
approaches 0 as well. So along every line through the origin $f(x,y)$
approaches 0. Now suppose we approach the origin along $x=y^2$. Then
$$f(x,y)={y^2y^2\over y^4+y^4}={y^4\over2y^4}={1\over2},$$
so the limit is $1/2$. Looking at Figure~\ref{fig:weird limit}, it
is apparent that there is a ridge above $x=y^2$. Approaching the
origin along a straight line, we go over the ridge and then drop down
toward 0, but approaching along the ridge the height is a constant
$1/2$.
\end{solution}

Fortunately, we can define the concept of limit without needing to
specify how a particular point is approached---indeed, in
Definition~\ref{def:limitprecise}, we didn't need the concept of
``approach.'' Roughly, that definition says that when $x$ is
close to $a$ then $f(x)$ is close to $L$; there is no mention of
``how'' we get close to $a$. We can adapt that definition to two
variables quite easily:

\begin{definition}{Limit of a Multivariate Function}{limit}
Suppose $f(x,y)$ is a function. We say that
$$\lim_{(x,y)\to
  (a,b)}f(x,y)=L$$
 if for every $\epsilon>0$ there is a $\delta > 0$ so that
  whenever $0 < \sqrt{(x-a)^2+(y-b)^2} < \delta$, $|f(x,y)-L|<\epsilon$.\index{limit!multivariate function}
\end{definition}

This says that we can make $|f(x,y)-L|<\epsilon$, no matter how small
$\epsilon$ is, by making the distance from $(x,y)$ to $(a,b)$ ``small
enough''.

\begin{example}{Multivariate Limit}{multilimitone}
Show that $\ds \lim_{(x,y)\to(0,0)}{3x^2y\over
	x^2+y^2}=0$.
\end{example}
\begin{solution}
Suppose $\epsilon>0$. Then
$$\left|{3x^2y\over x^2+y^2}\right|={x^2\over x^2+y^2}3|y|.$$
Note that $x^2/(x^2+y^2)\le1$ and
$|y|=\sqrt{y^2}\le\sqrt{x^2+y^2}<\delta$. So
$${x^2\over x^2+y^2}3|y|<1\cdot 3\cdot \delta.$$
We want to force this to be less than $\epsilon$ by picking $\delta$
``small enough.'' If we choose $\delta=\epsilon/3$ then
$$\left|{3x^2y\over x^2+y^2}\right|< 1\cdot 3\cdot{\epsilon\over3}=
\epsilon.$$
\end{solution}

Recall that a function $f(x)$ is continuous at $x=a$ if
$\ds\lim_{x\to a}f(x)=f(a)$. We can say exactly the same thing about
a function of two variables: $f(x,y)$ is continuous at $(a,b)$ if
$\ds\lim_{(x,y)\to (a,b)}f(x,y)=f(a,b)$.

The function $f(x,y)=3x^2y/(x^2+y^2)$ is not continuous at $(0,0)$,
because $f(0,0)$ is not defined. However, we know that
$\ds \lim_{(x,y)\to(0,0)}f(x,y)=0$, so we can make a continuous function,
by extending the definition of $f$ so that $f(0,0)=0$.
This surface is shown in Figure~\ref{fig:removable discontinuity}.

\begin{figure}[H]
%\texonly
\centerline{
\vbox{\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <3truecm,3truecm>
\setplotarea x from 0 to 1.1, y from 0 to 1.1
\put {\hbox{\epsfxsize8cm\epsfbox{images/removable_discont.eps}}} at 0 0
\endpicture}}
%\endtexonly
\caption{$\ds f(x,y)={3x^2y\over x^2+y^2}$}
\label{fig:removable discontinuity}
\end{figure}

Note that we cannot extend the definition of the function in Example~\ref{exa:weird limit}
to create a continuous function, since the limit does not exist as we approach $(0,0)$.

Fortunately, the functions we will be working with will usually be
continuous almost everywhere.
As with single variable functions, two classes of common functions are
particularly useful and easy to describe. A
polynomial\index{polynomial!of two variables} in two
variables is a sum of terms of the form $ax^my^n$, where $a$ is a real
number and $m$ and $n$ are non-negative integers. A rational
function\index{function!of two variables}
is a quotient of polynomials.

\begin{theorem}{Continuity of Functions}{ContFunctTheorem}
Polynomials are continuous everywhere. Rational functions are
continuous everywhere they are defined.
\end{theorem}

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

\begin{enumialphparenastyle}

Determine whether each limit exists. If it does, find the limit
and prove that it is the limit; if it does not, explain how you know.

\begin{ex}
$\ds\lim_{(x,y)\to(0,0)}{x^2\over x^2+y^2}$
\begin{sol}
No limit; use $x=0$ and $y=0$.
\end{sol}
\end{ex}

\begin{ex}
$\ds\lim_{(x,y)\to(0,0)}{xy\over x^2+y^2}$
\begin{sol}
No limit; use $x=0$ and $x=y$.
\end{sol}
\end{ex}

\begin{ex}
$\ds\lim_{(x,y)\to(0,0)}{xy\over 2x^2+y^2}$
\begin{sol}
No limit; use $x=0$ and $x=y$.
\end{sol}
\end{ex}

\begin{ex}
$\ds\lim_{(x,y)\to(0,0)}{x^4-y^4\over x^2+y^2}$
\begin{sol}
Limit is zero.
\end{sol}
\end{ex}

\begin{ex}
$\ds\lim_{(x,y)\to(0,0)}{\sin(x^2+y^2)\over x^2+y^2}$
\begin{sol}
Limit is 1.
\end{sol}
\end{ex}

\begin{ex}
$\ds\lim_{(x,y)\to(0,0)}{xy\over \sqrt{2x^2+y^2}}$
\begin{sol}
Limit is zero.
\end{sol}
\end{ex}

\begin{ex}
$\ds\lim_{(x,y)\to(0,0)} {e^{-x^2-y^2}-1\over x^2+y^2}$
\begin{sol}
Limit is $-1$.
\end{sol}
\end{ex}

\begin{ex}
$\ds\lim_{(x,y)\to(0,0)}{x^3+y^3\over x^2+y^2}$
\begin{sol}
Limit is zero.
\end{sol}
\end{ex}

\begin{ex}
$\ds\lim_{(x,y)\to(0,0)}{x^2 + \sin^2 y\over 2x^2+y^2}$
\begin{sol}
No limit; use $x=0$ and $y=0$.
\end{sol}
\end{ex}

\begin{ex}
$\ds\lim_{(x,y)\to(1,0)}{(x-1)^2\ln x\over(x-1)^2+y^2}$
\begin{sol}
Limit is zero.
\end{sol}
\end{ex}

\begin{ex}
$\ds\lim_{(x,y)\to(1,-1)}{3x+4y}$
\begin{sol}
Limit is $-1$.
\end{sol}
\end{ex}

\begin{ex}
$\ds\lim_{(x,y)\to(0,0)}{4x^2y\over x^2+y^2}$
\begin{sol}
Limit is zero.
\end{sol}
\end{ex}

\begin{ex}
Does the function $\ds f(x,y)={x-y\over 1+x+y}$
have any discontinuities?  What about
$\ds f(x,y)={x-y\over 1+x^2+y^2}$?  Explain.
\end{ex}

\end{enumialphparenastyle}


\chapter{Elementary Principles}
\section{Mechanics of a Particle}
\textbf{Linear Momentum}\\
Let $\textbf{r}$ be the radius vector of a particle from some given origin and $\textbf{v}$ its vector velocity.
$$\textbf{v}=\frac{d\textbf{r}}{dt}$$
The linear momentum $\textbf{p}$ of the particle is
$$\textbf{p}=m\textbf{v}$$
Newton's second law of motion  states that there exist frames of reference in which the motion of the particle is described by the differential equation
$$\textbf{F}=\frac{d\textbf{p}}{dt}\equiv\dot{\textbf{p}}$$
$$\textbf{F}=\frac{d}{dt}(m\textbf{v})$$
If the mass of particle is constant
$$\textbf{F}=m\frac{d\textbf{v}}{dt}=m\textbf{a}$$
Where $\textbf{a}$ is the vector acceleration of the particle
The equation of motion is thus a differential equation of second order, assuming $\textbf{F}$ does not depend on higher-order derivatives.\\\\
\textbf{\textit{Conservation theorem for the Linear Momentum of a Particle: If the total force $\vec{F}$ is zero, then $\dot{\vec{p}}$, is conserved.}}\vspace{0.5cm}
\\\\
\textbf{Angular Momentum}\vspace{0.2cm}\\
The angular momentum of the particle about point $O$, denoted by $\textbf{L}$, is defined as
$$\textbf{L}=\textbf{r}\times\textbf{p}$$
where $\textbf{r}$ is the radius vector from $O$ to the particle. The moment of force or torque about $O$
 $$\textbf{N}=\textbf{r}\times\textbf{F}$$
 $$\textbf{r}\times\textbf{F}=\textbf{N}=\textbf{r}\times\frac{d}{dt}(mv)$$
 using the vector identity
 $$\frac{d}{dt}(\textbf{r}\times m\textbf{v})\textbf{v}\times m\textbf{v}+=\textbf{r}\times\frac{d}{dt}(m\textbf{v})$$
$$\textbf{N}=\frac{d}{dt}(\textbf{r}\times m\textbf{v})=\frac{d\textbf{L}}{dt}\equiv \dot{\textbf{L}}$$
Note that both $\textbf{N}$ and $\textbf{L}$ depend on the point $O$ about which the moments are taken.\\
\textit{Conservation Theorem for the Angular Momentum of a Particle: If the total torque, $\textbf{N}$,is zero then $\dot{\textbf{L}}=0$, and the angular momentum $\textbf{L}$ is conserved. }\\\\
\textbf{Energy}\\
Next consider the work done by the external force $\textbf{F}$ upon the particle in going from point 1 to point 2. \\
This work is
$$W_{12}=\int\limits_{1}^{2}\textbf{F}\cdot d\textbf{s}$$
$$\int\textbf{F}\cdot d\textbf{s}=m\int\frac{d\textbf{v}}{dt}\cdot\textbf{v}dt=\frac{m}{2}\int\frac{d}{dt}(v^2)dt$$
$$W_{12}=\frac{m}{2}(v_2^2-v_1^2)$$
The scalar quantity $mv^2/2$ is called the kinetic energy of the particle and is denoted by $T$. so that the work done is equal to the change in the kinetic energy.
\begin{equation}
W_{12} =T_2-T_1\label{EP-01}
\end{equation}
if the force field is such that the work $W_{12}$ is the same for any physically possible path between points 1 and 2, then the force (and the system) is said to be conservative.\\
or
$$\oint\textbf{F}\cdot\textbf{s}=0 d$$
$\textbf{F}$ be the gradient of some scalar function of position
$$\textbf{F}=-\nabla V(\textbf{r})$$
where $V$ is called the potential energy.
$$\textbf{F}\cdot d\textbf{s}=-dV$$
or
$$F_s=-\frac{\partial V}{\partial s}$$
We can add to $V$ any quantity constant in space, without affecting  the results. Hence the zero level of $V$ is arnitrary.\\
For a consetvative system, the work done by the forces is
\begin{equation}
W_{12}=V_1-V_2\label{EP-02}
\end{equation}
Combining Eq.(\ref{EP-01}) with Eq.(\ref{EP-02}),we have the result
$$T_1+V_1=T_2+V_2$$
\textbf{\textit{Energy Conservation Theorem for a Particle: If the forces acting on a particle are consetvative, then the total energy of the particle, $T+V$,is conserved}}\\
\begin{note}
	The force applied to to a particle may in some circumstances be given by the gradient of a scalar function that depends explicity on both the position of the particle and the time. However, the work done on the particle when it travels a distance $ds$.
	$$\textbf{F}\cdot{d\textbf{s}=\frac{\partial V}{\partial s}ds}$$
	is then no longer the total change in $-V$ during the displacement, since $V$ also changes explicitly with time as the particle moves. Hence, the work done as the particle goes from point 1 to point 2 is no longer the difference in the function $V$ between those points. While a total  energy $T+V$ may still be defined, it is not consetved during the course of the particle's motion.
\end{note}
\section{Mechanics of a system of Particle}
When considering a system of particles, we must distinguish between the external forces acting on the particles due to source outside the system and internal forces on some particle $i$ due to all other particles in the system.\\\\
\textbf{Linear Momentum}\\
Equation of motion for the $i^{th}$ particle
$$\vec{F}_i^{(e)}+\sum\limits_{j}\vec{F}_{ji}=\vec{\dot{p}_i}$$
$\vec{F}_i^{(e)}$- external force\\
$\vec{F}_{ji}$- internal force on the $i^{th}$ particle due to the $j^{th}$ particle.\\\\
Assume $\vec{F}_{ij}$ , like ${F}_i^{(e)}$ obey Newton's third law  ie. $\vec{F}_{ij}= - \vec{F}_{ji}$,\\
this assumption some times referred to as \textit{The weak law of action and reaction}.\\
$$\therefore \text{ we get }\quad \frac{d^2}{dt^2}\sum\limits_{i}m_ir_i=\sum\limits_{i}{F}_i^{(e)}+\sum\limits_{i,j ,\ i\neq j}F_{ji}$$
$$\frac{d^2}{dt^2}\sum\limits_{i}m_ir_i=\sum\limits_{i}{F}_i^{(e)}$$
Let's define $\vec{R}$ as the average of radii vectors of the particles weighted in proportion to their mass.
$$\vec{R}=\frac{\sum m_i \vec{r}_i}{\sum m_i}=\frac{\sum m_i \vec{r}_i}{M}$$
vector $\vec{R}$ defines a point known as the centre of mass
$$M\frac{d^2 R}{dt^2}=\sum\limits_{i}\vec{F}_i^{(e)}\equiv \vec{F}^{(e)}$$
The centre of mass moves as if the total external force were acting on the entire mass of the system concentrated at the centre of mass. The total limear momentum of system.
$$\vec{P}=\sum m_1\frac{d\vec{r_i}}{dt}=M\frac{d\vec{R}}{dt}$$
ie. total mass of the system times the velocity of the center of mass\\
conservation for linear momentum of a system of particles: if the total external force is zero, the total linear momentum is conserved.\\\\
\textbf{Angular Momentum}\\
\begin{align*}
\sum\limits_{i}(\vec{r_i}\times\dot{P_1})&=\sum\limits_{i}\frac{d}{dt}(\vec{r_i}\times\vec{P_i})=\dot{L}\\
&=\sum\limits_{i}\vec{r_i}\times\vec{F}_i^{(e)}+\sum\limits_{i,j}\vec{r_i}\times\vec{F}_{ji}\\
\vec{r_i}-\vec{r_j}&=\vec{r_{ij}}\\
\therefore \text{ RHS of equation becomes },\quad\vec{r_{ij}}\times \vec{F_{ji}}
\end{align*}
The internal forces between two particles. in addition to being equal and opposite, also lie along the line going the particles (This condition known as the strong law of action and reaction.)\\
Then all cross products vanish,\\
$$\frac{d\vec{L}}{dt}=\vec{N}^{(e)}$$\\
The time derivative of angular momentum thus equal to the moment of the external force about the given point.\\\\
Conservation of total angular momentum: $L$ \textit{is constant in time if the applied (external) torque is zero.} \\\\\begin{note}
	This is a vector theorem,
	$\therefore$ $L_z$ will be conserved if $N_z^{(e)}$ is zero, even if $N_x^{(e)}$ and $N_y^{(e)}$ are not zero.\\
\end{note}
\textbf{To represent $\vec{L}$ in terms of centre of mass,}\\
\begin{figure}[H]
	\centering
	\includegraphics[height=4cm,width=5cm]{EP-02}
\end{figure}
Let $R$ be the radius vector from $O$ to the centre of mass to the $i^{th}$ particle\\
\begin{align*}
\vec{r_1}&=\vec{r^\prime_1}+\vec{R}
\intertext{and}
\vec{v_1}&=\vec{v^\prime_1}+\vec{\mathrm{v}}
\intertext{where}
\mathrm{v}&=\frac{dR}{dt}\text{velocity of CM relative to $O$ }
\intertext{and}
v^\prime_1&=\frac{dr^\prime}{dt}
\end{align*}
is the velocity of the $i^{th}$ particle relative to centre of mass of the system\\
$\therefore$ total angular momentum
$$\vec{L}=\vec{R}\times M\vec{\mathrm{v}}+\sum\limits_{i}r^\prime_i\times p^\prime_i$$
Total angular momentum about a point $O$ is the angular momentum of motion concentrated at the centre of mass, plus the angular momentum of motion about the centre of mass.\\
\begin{note}
	$L$ depends on origin $O$ through the vector $\vec{R}$
	only if the centre of mass is at rest with respect to $O$ will the angular momentum be independent of point of reference.
\end{note}
\textbf{Energy Equation}\\
Work done by all forces in moving a system from an initial configuration $1$ to a final configuration $2$
\begin{align*}
W_{12}&=\sum\limits_{i}\int\limits_{1}^{2}\vec{F}_i\cdot d\vec{s}_i=\sum\limits_{i}\int\limits_{1}^{2}F_i^{(e)}\cdot d s_i+\sum\limits_{i,j ,i+j}\ \int\limits_{1}^{2}
F_{ji}\cdot ds_i
\intertext{by equation of motion}
\sum\limits_{i}\int\limits_{1}^{2}\vec{F}_i\cdot d\vec{s}_i&=\sum\limits_{i}\int\limits_{1}^{2}m_i \vec{\dot{v}_i}\cdot\vec{v}_i dt=\sum\limits_{i}\int\limits_{1}^{2}r(\frac{1}{2}m_iv_i^2)\\
\therefore W_{12}&=T_2-T_1
\intertext{	Where $T$, the total kinetic energy of system}
T=\frac{1}{2} \sum\limits_{i}m v_i^2
\intertext{Transformation to centre of mass coordinates}
T=\frac{1}{2} Mv^2+\frac{1}{2}\sum m_i {v^\prime}_i^2
\end{align*}
ie. kinetic energy obtained if all mass were concentrated at the centre of mass $+$
 kinetic energy of motion about the centre of mass. When external forces are derivable in terms of the gradient of a potential the first term can be written as
  \begin{align*}
 \sum\limits_{i}\int\limits_{1}^{2}\vec{F}_i^{(e)}\cdot d {s}_i&=-\sum\limits_{i}\int\limits_{1}^{2}\nabla _i v_i\cdot d \vec{s}=-\sum\limits_{i}-v_i \Bigr |_{1}^{2}
 \intertext{If the internal forces are also conservative.}
 v_{ij}&=v_{ij}(|r_i-r_j|)
 \intertext{When forces are all conservative}
 \sum\limits_{i,j ,i+j}\ \int\limits_{1}^{2}
 F_{ji}\cdot ds_i&=-\sum\limits_{i}\int\limits_{1}^{2}(\nabla _i v_{i j}\cdot d s_i+\nabla_j v_{ij}\cdot ds_i)\\
 \vec{r}_{ij}&=(r_i-r_j), \nabla_{i j}\text{ gradient with respect to }r_{i j}\\
  \text{ Then}{ \nabla_i} v_{ij}&={\nabla_{i j}} v_{ij} =-\nabla_j v_{ij}\\
 ds_i-ds_i&=dr_i-dr_i=dr_{ij}
\intertext{$\therefore $ total work arising from internal force}
&=\frac{-1}{2}\sum\limits_{i,j ,i+j}\ \int\limits_{1}^{2}\nabla_{i j} \nabla_{i j}\cdot d\vec{r}_{ij}=\frac{-1}{2}\sum\limits_{i,j ,i+j} v_{ij}\Bigr |_{1}^{2}
 \end{align*}
 $\therefore$ The external and internal force are both derivable from potentials so it is possible to define a total potential energy $v$ of the system.
 $$v=\sum\limits_{i}v_i+\frac{1}{2}\sum\limits_{i,j ,i+j}v_{ij}$$
\begin{itemize}
	\item For rigid body internal potential is constant
	\item  In rigid body internal forces do no work.
\end{itemize}
\section{Constraints}
We have
$$ m_i\ddot{\mathbf{r}}_1=\textbf{F}_1^{(e)}+\sum\limits_{j}\textbf{F}_{ji}$$
All problems in mechanics be reduced to solving a set of differential equation. But this view is over simplified even from a purely physicals standpoint \\
eg:it is necessary to take constraints\\\\
While solving a problem along with this we have to take consideration of constraints that limits the motion of the system.\\
Constraints are classified in various ways.\\\\
\begin{itemize}
\item \textit{Holonomic Constraints}: Constraint equations are integrable if the conditions of constraint can be expressed aas equarions connecting the coordinates of the particles (and possible the time) having the form
	$$ f(\textbf{r}_1,\textbf{r}_2, \textbf{r}_3,.....t)=0$$
	Then constraints are holonomic
	eg:rigid body $(\textbf{r}_i-\textbf{r}_j)^2-c_{ij}^2=0$
	a particle constrained to move along any curve or on a given surface equation\\
\item \textit{Non Holonomic }:
Constraint which are not expressible as above are non holonomic constraint.
Non-holonomic constraints include nonintegrable differential constraints,constraint conditions involve higher order derivatives, or may in the form of inequality.\\
eg:Aparticle placed on the surface of a sphere, walls of a gas container\\
\item \textit{Rheonomous Constraints}:
Rheonomous constraints are which the constraint equation contain time as an explicit variable.\\
\item \textit{Scleronomous}:
Constraints which the constraint equations are not ecplicity dependent on time.\\
\end{itemize}
\textbf{Because of Constraints}:\\
1) The coordinates $r_i$ are no longer all independent since they are connected by the equation of constraint hence equation of motion are not all independent.\\
2)The forces of constraint are unknown and must be obtained from solution.\\
In case of holonomic constraints the first difficulty is solved by introduction of generalized coordinates.\\\\
\section{Generalized Coordinates}
\begin{itemize}
	\item A system of N-particles, free from constraints has $3N$ independent coordinates or degree of freedom.
	\item If the exist holonomic constraints expressed in $k$ equations in the form:\\
	$f(r_1,r_2.....t)=0$\\
	we use these equations to eliminate $k$ of the $3N$ coordinates
	\item We left with $3N-k$ independent coordinates and system said to have $3N-k$ degrees of freedom.
	\item Elimination of dependend variable is done by introduction of new $3N-k$ , independent variables $q_1,q_2.....q_{3N-k}$ in terms of which the old cordinates ate expressed by equation of form
\begin{align*}
	\textbf{r}_1=\textbf{r}_1(q_1,q_2&.......q_{3N-k}, t)\\
	&....\\
	&....\\
	\textbf{r}_N=\textbf{r}_N(q_1,q_2&.......q_{3N-k}, t)
\end{align*}
	These contain constrainst implicitly\\
	Generalized coordinates ate different from conventional orthogonal position coordinates. All sorts of quantities  may be involved to serve as generalized coordinates
	\begin{note}
		If the constraint is non-holonomic the equations expressing the constraint cannot be used to eliminate the dependent coordinates.
	\end{note}
	eg: Object rolling on a rough surface without slipping.
\end{itemize}
\section{D'Alembert's Principle and Lagrange's Equations}
\textit{\textbf{Virtual Displacement}}:\\A change in the configuration of the system as the result of any arbitrary infinitesimal chabge of the coordinates $\delta \textbf{r}_i$ consistent with the forces and constraints imposed on the system at the given instant $t$.\\\\
\textbf{Principle of Virtual Work}\\\\
For a system in rquilibrium\\
total fore on each particle $\textbf{F}_i=0$\\
Then virtual work of $\textbf{F}_i$ in desplacement $\delta \textbf{F}_i$
\begin{equation}
\sum\limits_{i}\textbf{F}_i\cdot\delta \textbf{r}_i=0\label{EP-01}
\end{equation}
\begin{align*}
\intertext{Let}
\textbf{F}_i=\textbf{F}_i^{(a)}+\textbf{f}_i\hspace{3cm}\textbf{F}_i^{(a)}\rightarrow \text{applied force}\\
\textbf{f}_i\rightarrow \text{force of constraint}\\
\end{align*}
$\therefore (\ref{EP-01}) $ \quad becomes \quad $\sum_{i} \mathbf{F}_{i}^{(a)} \cdot \delta \mathbf{r}_{i}+\sum_{i} \mathbf{f}_{i} \cdot \delta \mathbf{r}_{i}=0$ \\\\
We now restrict ourselves to systems for which the net virtual work of constraints is zero.We therefore have as the condition for equilibrium of a system that the virtual work of the applied forces vanishes.
\begin{equation}
\sum_{i} \mathbf{F}_{i}^{(a)} \cdot \delta \mathbf{r}_{i}=0\label{EP-02}
\end{equation}
This is often called \textit{principle of virtual work}\\\\
\textbf{D'Alembert's Principle}\\
Equation (\ref{EP-02})  only deals with statics. We want a condition involving general motion of the system\\
The equation of motion
$$\textbf{F}_i=\dot{\textbf{P}_i}$$
$$\implies \textbf{F}_i-\dot{\textbf{P}_i}=0$$
\textit{The particle in the system will be in equilibrium under a force equal to actual force plus a "reversed effective force"} $-\dot{\textbf{P}_i}$\\
$\therefore$ equation (\ref{EP-01}) becomes
\begin{equation}
\sum\limits_{i}(\textbf{F}_i-\dot{\textbf{P}_i})\cdot\delta \textbf{r}_i=0 \label{EP-05}
\end{equation}
resolving it
$$\sum\limits_{i}(\textbf{F}_i^{(a)}-\dot{\textbf{P}_i})\cdot\delta \textbf{r}_i+\sum\limits_{i}\textbf{f}_i\cdot\delta \textbf{r}_i=0$$
for systems for which virtual work of the forces of constraint vanishes
\begin{equation}
\sum\limits_{i}(\textbf{F}_i^{(a)}-\dot{\textbf{P}_i})\cdot\delta\label{EP-06} \textbf{r}_i=0
\end{equation}
Which is called \textit{D'Alembert's principle}\\\\
\textbf{Generalized Force}\\
The translation from $r_i$ to $q_j$ language starts from the transformation equations
\begin{equation}
\textbf{r}_i=\textbf{r}_i(q_1,q_2,...q_n,t)\label{EP-07}
\end{equation}
velocity $V_i$ in terms of $\dot{q}_k$
\begin{equation}
\mathbf{v}_{i} \equiv \frac{d \mathbf{r}_{i}}{d t}=\sum_{k} \frac{\partial \mathbf{r}_{i}}{\partial q_{k}} \dot{q}_{k}+\frac{\partial \mathbf{r}_{i}}{\partial t}\label{EP-08}
\end{equation}
similarly, the arbitrary virtual displacement $\delta r_i$ can be connected with the virtual displacement $\delta q_1$ by
\begin{equation}
\delta \mathbf{r}_{i}=\sum_{j} \frac{\partial \mathbf{r}_{i}}{\partial q_{j}} \delta q_{j}
\end{equation}
In terms of generalized coordinates the virtual work of $\textbf{F}_i$ becomes
\begin{equation}
\sum_{i} \mathbf{F}_{i} \cdot \delta \mathbf{r}_{i}=\sum_{i, j} \mathbf{F}_{i} \cdot \frac{\partial \mathbf{r}_{i}}{\partial q_{j}} \delta q_{j}=\sum_{j} Q_{j} \delta q_{j}\label{EP-10}
\end{equation}
Where $Q_j$ are called the component of the \textit{generalized force}
\begin{equation}
Q_{j}=\sum_{i} \mathbf{F}_{i} \cdot \frac{\partial \mathbf{r}_{i}}{\partial q_{j}}\label{EP-11}
\end{equation}
\textit{Just as $q$'s need not have the diamensions of length, so the $Q$'s do not necessarily have the diamensions of force, but $Q_j\delta q_j$ must always have the diamensions of work.}
\begin{note}
	No variation of time, $\delta t$ is involved here since a virtual displacement by definition considers only displacements of the coordinates.
\end{note}
\textbf{D'Alembert's Principle in generalized Coordinates}
\begin{align*}
\sum\limits_{i}(r_i^{(a)}-\dot{P_1}_i)\cdot\delta r_i&=0\text{ \quad is the D'Alembert's Principle}\\
\sum_{i} \dot{\mathbf{p}}_{i} \cdot \delta \mathbf{r}_{i}&=\sum_{i} m_{i} \ddot{\mathbf{r}}_{i} \cdot \delta \mathbf{r}_{i}\\
&=\sum_{i, j} m_{i} \ddot{\mathbf{r}}_{i} \cdot \frac{\partial \mathbf{r}_{i}}{\partial q_{j}} \delta q_{j}
\end{align*}
Now consider the relation
\begin{equation}
\sum_{i} m_{i} \ddot{\mathbf{r}}_{i} \cdot \frac{\partial \mathbf{r}_{i}}{\partial q_{j}}=\sum_{i}\left[\frac{d}{d t}\left(m_{i} \dot{\mathbf{r}}_{i} \cdot \frac{\partial \mathbf{r}_{i}}{\partial q_{j}}\right)-m_{i} \dot{\mathbf{r}}_{i} \cdot \frac{d}{d t}\left(\frac{\partial \mathbf{r}_{i}}{\partial q_{j}}\right)\right]\label{EP-12}
\end{equation}
interchanging differentiation with respect to $t$ and $q_j$
\begin{align*}
 \frac{d}{d t}\left(\frac{\partial \mathbf{r}_{i}}{\partial q_{j}}\right) &=\frac{\partial \dot{\mathbf{r}}_{i}}{\partial q_{j}}=\sum_{k} \frac{\partial^{2} \mathbf{r}_{i}}{\partial q_{j} \partial q_{k}} \dot{q}_{k}+\frac{\partial^{2} \mathbf{r}_{i}}{\partial q_{j} \partial t}, \\
  &=\frac{\partial \mathbf{v}_{i}}{\partial q_{j}}
 \end{align*}
  we have from equation (\ref{EP-08})
\begin{equation}
\frac{\partial \mathbf{v}_{i}}{\partial \dot{q}_{j}}=\frac{\partial \mathbf{r}_{i}}{\partial q_{j}}\label{EP-13}
\end{equation}
substitution of these in equation (\ref{EP-12})
\begin{align*}
&\sum_{i} m_{i} \ddot{\mathbf{r}}_{i} \cdot \frac{\partial \mathbf{r}_{i}}{\partial q_{j}}=\sum_{i}\left[\frac{d}{d t}\left(m_{i} \mathbf{v}_{i} \cdot \frac{\partial \mathbf{v}_{i}}{\partial \dot{q}_{j}}\right)-m_{i} \mathbf{v}_{i} \cdot \frac{\partial \mathbf{v}_{i}}{\partial q_{j}}\right]
\intertext{second term on the LHS of equation (\ref{EP-06}) can be explanded into}
&\sum_{j}\left\{\frac{d}{d t}\left[\frac{\partial}{\partial \dot{q} j}\left(\sum_{i} \frac{1}{2} m_{i} v_{i}^{2}\right)\right]-\frac{\partial}{\partial q_{j}}\left(\sum_{i} \frac{1}{2} m_{i} v_{i}^{2}\right)-Q_{j}\right\} \delta q_{j}\\
&\sum_{i} \frac{1}{2} m_{i} v_{i}^{2}=T\quad \text{ie. system kinetic energy}
\end{align*}
$\therefore$ D'Alembert's principle becomes
\begin{equation}
\sum_{j}\left\{\left[\frac{d}{d t}\left(\frac{\partial T}{\partial \dot{q}_{j}}\right)-\frac{\partial T}{\partial q_{j}}\right]-Q_{j}\right\} \delta q_{j}=0\label{EP-14}
\end{equation}\\\\
\textbf{Lagrange's Equation}\\
Any virtual displacement $\delta q_j$ is independent of $\delta q_k.$\\
$\therefore$ only way to hold equation \ref{EP-14} is for the individual coeffitients to vanish.
\begin{equation}
\frac{d}{d t}\left(\frac{\partial T}{\partial \dot{q}_{j}}\right)-\frac{\partial T}{\partial q_{j}}=Q_{j}\label{EP-15}
\end{equation}
There are $n$ such equations in all, When forces are derivable from scalar potential $V$
$$\mathbf{F}_{i}=-\nabla_{i} V$$
Then the generalized forces can be written as
$$Q_{j}=\sum_{i} \mathbf{F}_{i} \cdot \frac{\partial \mathbf{r}_{i}}{\partial q_{j}}=-\sum_{i} \nabla_{i} V \cdot \frac{\partial \mathbf{r}_{i}}{\partial q_{j}}$$
Which is partial derivative of $-V(r_1,r_2...r_3,t)$ with respect to $q_j$
\begin{equation}
Q_{j}=-\frac{\partial V}{\partial q_{j}} .\label{EP-16}
\end{equation}
$\therefore$ equation \ref{EP-15} can be rewritten as
\begin{equation}
\frac{d}{d t}\left(\frac{\partial T}{\partial \dot{q}_{j}}\right)-\frac{\partial (T-V)}{\partial q_{j}}=0
\end{equation}
Potential $V$ does not depend on the generalized velocities \\
$\therefore$ we can write
$$\frac{d}{d t}\left( \frac{\partial (T-V)}{\partial \dot{q}_{j}} \right)  -\frac{\partial (T-V)}{\partial q_{j}}=0$$
defining the Lagrangian $L$,as $L=T-V$\\
$\therefore$ equation \ref{EP-15} becomes
$$\frac{d}{d t}\left(\frac{\partial L}{\partial \dot{q}_{j}}\right)-\frac{\partial L}{\partial q_{j}}=0$$
expression refered to as \textit{"Lagrange's equations"}
\begin{note}
	If $L(q,\dot{q},t)$ is an approximate Lagrangian and $F(q,t)$ is any differentiable function of the generalized coordinates and time, then
	$$L^{\prime}(q, \dot{q}, t)=L(q, \dot{q}, t)+\frac{d F}{d t}$$
\end{note}
is a Lagrangian also resulting in the same equation of motion.\\
\section{Formulation of Lagrangian}
Lagrangian in different coordinate system:
Lagrangian of a system is defined as $L=T\left(q_{i}, \dot{q}_{i}, t\right)-V\left(q_{i}, \dot{q}_{i}, t\right)$, where $T$ and $V$ are functions of generalized coordinates, generalized velocities and time $t$.\\
\textbf{How to write a Lagrangian}\\\\
\textbf{Step 1:}\\
(a) Draw a Cartesian coordinate system with a suitable origin.
(b) Then disturb the system arbitrarily and fix the coordinates of individual particle with respect to origin.
(c) Write down kinetic energy and potential energy as discussed below.
If a system constitutes $p$ number of particle and position vector of the $p^{\text {th }}$ particle is given by $\vec{r}_{p}=x_{p} \hat{i}+y_{p} \hat{j}+z_{p} \hat{k}$, then kinetic energy of the system is given by
$$
T=\frac{1}{2} \sum_{P} m_{p}\left[\left(\dot{x}_{p}\right)^{2}+\left(\dot{y}_{p}\right)^{2}+\left(\dot{z}_{p}\right)^{2}\right] \text { and }
$$
potential energy is given by
$$
V=\sum_{p} V_{p}\left(x_{p}, y_{p}, z_{p}, \dot{x}_{p}, \dot{y}_{p}, \dot{z}_{p}\right)
$$
Lagrangian of the system is given by $L=T-V$\\\\
\textbf{Step 2:}\\
(a) Find the degree of freedom with the formula $\mathrm{DOF}=3 N-k$ as discussed in previous chapter.\\
(b) Number of degree of freedom is equivalent to minimum number of independent motion.\\
(c) Identify the independent motion with the help of symmetry of problem.\\
(d) Transform the Lagrangian of the system in suitable symmetry.\\
\begin{exercise}
If kinetic energy and potential energy is given by $T=\frac{1}{2} m\left(\dot{x}^{2}+\dot{y}^{2}\right)$ and $V=-m g y$ respectively.
	 \begin{tasks}(1)
		\task[\textbf{a.}] Write down Lagrangian of the system.
		\task[\textbf{b.}] Identify generalized coordinate and generalized velocity.
		\task[\textbf{c.}] Identify cyclic coordinate and discuss conservation of momentum.
		\task[\textbf{d.}]  Discuss equation of motion.
	\end{tasks}
\end{exercise}
\begin{answer}
	\begin{align*}
\intertext{(a)} L=T-V \Rightarrow \frac{1}{2} m\left(\dot{x}^{2}+\dot{y}^{2}\right)&-(-m g y) \Rightarrow \frac{1}{2} m\left(\dot{x}^{2}+\dot{y}^{2}\right)+m g y\\
\intertext{(b)} \text{ Generalized coordinate }q_{1}&=x, q_{2}=y\text{ generalized velocity} \dot{q}_{1}=\dot{x}, \dot{q}_{2}=\dot{y}
\intertext{(c)} \left(\frac{\partial L}{\partial q_{1}}\right)&=\left(\frac{\partial L}{\partial x}\right)=0, \text{so it is a cyclic coordinate}\\ \text{Then} \left(\frac{\partial L}{\partial \dot{x}}\right)&=p_{x}=m \dot{x}, \intertext{Identified as linear momentum in $x$ direction is constant of motion.}
\left(\frac{\partial L}{\partial y}\right)&=m g \neq 0,\text{ so it is not cyclic coordinate}
\intertext{(d)} \frac{d}{d t}\left(\frac{\partial L}{\partial \dot{x}}\right)-\left(\frac{\partial L}{\partial x}\right)&=0 \\ \frac{d}{d t} m \dot{x}-0&=0 \Rightarrow m \dot{x}=c, \intertext{which is exactly explained in section (b)}
\frac{d}{d t}\left(\frac{\partial L}{\partial \dot{y}}\right)-\left(\frac{\partial L}{\partial y}\right)&=0 \\ \frac{d}{d t} m \dot{y}-m g&=0 \Rightarrow m \ddot{y}-m g=0
	\end{align*}
\end{answer}
\begin{exercise}
	1. Apply Lagrange's equation to find the equation of motion of a particle in space using \\
	(a). Cartesian coordinates. \\
	(b). plane polar coordinates.
\end{exercise}
\begin{answer}
	\begin{align*}
	\intertext{ (a).\quad The generalized forces needed are $F_{x}, F_{y}$, and $F_{z}$. Then}
	T &=\frac{1}{2} m\left(\dot{x}^{2}+\dot{y}^{2}+\dot{z}^{2}\right), \\ \frac{\partial T}{\partial x} &=\frac{\partial T}{\partial y}=\frac{\partial T}{\partial z}=0, \\ \frac{\partial T}{\partial \dot{x}}=m \dot{x}, & \frac{\partial T}{\partial \dot{y}}=m \dot{y}, \quad \frac{\partial T}{\partial \dot{z}}=m \dot{z},
	\intertext{ \quad and the equations of motion are}
	\frac{d}{d t}(m \dot{x})&=F_{x}, \quad \frac{d}{d t}(m \dot{y})=F_{y}, \quad \frac{d}{d t}(m \dot{z})=F_{z}
	\end{align*}
	\begin{align*}
	\intertext{(b).\quad Here we must express $T$ in terms of $\dot{r}$ and $\dot{\theta}$. The transformation equations are}
	x&=r \cos \theta\\
	y&=r \sin \theta
	\intertext{the velocities are given by}
	\dot{x}&=\dot{r} \cos \theta-r \dot{\theta} \sin \theta,\\
	\dot{y}&=\dot{r} \sin \theta+r \dot{\theta} \cos \theta\\
	\text{The kinetic energy }T&=\frac{1}{2} m\left(\dot{x}^{2}+\dot{y}^{2}\right)\text{ then reduces formally to}\\
	T&=\frac{1}{2} m\left[\dot{r}^{2}+(r \dot{\theta})^{2}\right]
	\intertext{ the plane polar components of the velocity are $\dot{r}$ along $\mathbf{r}$, and $r \dot{\theta}$ along the direction perpendicular to $r$, denoted by the unit vector $\hat{\theta}$. Hence, the square of the velocity expressed in polar coordinates is simply $\dot{r}^{2}+(r \dot{\theta})^{2}$.}
	\intertext{the components of the gencralized force are}
	Q_{r}&=\mathbf{F} \cdot \frac{\partial \mathbf{r}}{\partial r}=\mathbf{F} \cdot \hat{\mathbf{r}}=F_{r}\\
	Q_{\theta}&=\mathbf{F} \cdot \frac{\partial \mathbf{r}}{\partial \theta}=\mathbf{F} \cdot r \hat{\mathbf{\theta}}=r {F}_{\theta} .
	\intertext{ There are two generalized coordinates, and therefore two Lagrange equations. The derivatives occurring in the $r$ equation are}
	\frac{\partial T}{\partial r}&=m r \dot{\theta}^{2}, \quad \frac{\partial T}{\partial \dot{r}}=m \dot{r}, \quad \frac{d}{d t}\left(\frac{\partial T}{\partial \dot{r}}\right)=m \ddot{r},\\
	m \ddot{r}-m r \dot{\theta}^{2}&=F_{r},\\
	\frac{\partial T}{\partial \theta}=0, \quad \frac{\partial T}{\partial \dot{\theta}}=m r^{2} \dot{\theta},& \quad \frac{d}{d t}\left(m r^{2} \dot{\theta}\right)=m r^{2} \ddot{\theta}+2 m r \dot{r} \dot{\theta}.\\
	\frac{d}{d t}\left(m r^{2} \dot{\theta}\right)&=m r^{2} \ddot{\theta}+2 m r \dot{\theta} \dot{\theta}=r F_{\theta} .\\
	\text{where angular momentum }L&=m r^{2} \theta\text{ and torque }N^{(e)}=r F_{\theta}.
	\end{align*}
\end{answer}
\begin{exercise}
	Atwood's machine \\
	\begin{figure}[H]
		\centering
		\includegraphics[height=4.7cm,width=4cm]{EP-04}
	\end{figure}
\end{exercise}
\begin{answer}
	\begin{align*}
	\intertext{A conservative system with holonomic, scleronomous constraint  (the pulley is assumed frictionless and massless). There is only one independent coordinate $x$, the position of the other weight being determined by the constraint that the length of the rope between them is $l$. The potential energy is}
	V&=-M_{1} g x-M_{2} g(l-x)
	\intertext{while the kinetic energy is}
	T&=\frac{1}{2}\left(M_{1}+M_{2}\right) \dot{x}^{2}
	\intertext{Lagrangian has the form}
	L&=T-V=\frac{1}{2}\left(M_{1}+M_{2}\right) \dot{x}^{2}+M_{1} g x+M_{2} g(l-x)
	\intertext{equation of motion}
	\frac{\partial L}{\partial  x}&=\left(M_{1}-M_{2}\right) g\\
	\frac{\partial  L}{\partial  x}&=\left(M_{1}+M_{2}\right) \dot{x}
	\intertext{$\mathrm{so}$}
	\left(M_{1}+M_{2}\right) \ddot{x}&=\left(M_{1}-M_{2}\right) g,\\
	\ddot{x}&=\frac{M_{1}-M_{2}}{M_{1}+M_{2}} g,\\
	\end{align*}
\end{answer}
\begin{exercise}
	A bead (or ring ) sliding on a uniformly rotating wire in a force-free space. The wire is straight, and is rotated uniformly about some fixed axis perpendicular to the wire.
\end{exercise}
\begin{answer}
	\begin{align*}
	\intertext{This is an example of constraint being time dependent, with the rotationaxis along $z$ and the wire in the $xy$ plane.}
	x&=r \cos \omega t \hspace{3cm}(\omega=\text { angular velocity of rotation }) \\
	y&=r \sin \omega t \hspace{3cm}(r=\text{distance along wire from rotation axis} )
	\intertext{constraint can be expressed as $\dot{\theta}=\omega$, so}
	T&=\frac{1}{2} m\left(\dot{r}^{2}+r^{2} \omega^{2}\right)
	\intertext{The equation of motion is then}
	m \ddot{r}&-m r^{2}=0\\
	\ddot{r}&=r \omega^{2}
	\intertext{which is the familiar simple harmonic oscillator equation with a change of sign. The solution $r=e^{\omega t}$ for a bead initially at rest on the wire shows that the bead moves exponentially outwards.The angular momentum, $L=m r^{2} \omega=m \omega r_{0}^{2} e^{2 \omega t}$, provides the force $F=N / r$, which produces the constraint force, $F=2 m r_{0} \omega^{2} e^{\omega t}$, acting perpendicular to the wire and the axis of rotation.}
	\end{align*}
\end{answer}

Classical Mechanics  -CSIR/chapter/Elementary Principles.tex


\chapter{Differential Equation}
\section{Homogeneous linear first-order}
The homogeneous linear first-order differential equations have the form:
\begin{align*}
   f'(t) + p(t)f(t) = 0
\end{align*}
Homogeneous is because one side of the equation is zero.
You can rewrite the expression above to have \(f(x)\) separated
\begin{gather}
   f'(t)  = -p(t)f(t) \\
   f'(t) \frac{1}{f(t)} = -p(t) \label{homodiff}
\end{gather}
Now all the \(f(t)\) terms are on the left hand side.

Note the following differentiation:
\begin{gather}
   (\ln f(t))' = \frac{1}{f(t)} f'(t)
\end{gather}
It can be used to help integrate \ref{homodiff}.
\begin{gather*}
   \int f'(t) \frac{1}{f(t)} = \int -p(t) \\
   \ln(|f(t)|) + C_1  =- P(t) + C_2 \\
   \ln(|f(t)|)  = -P(t) + \hat{C}
\end{gather*}
You can combine \(C_1\) and \(C_2\) to a single constant \(\hat{C}\), because they
both are constants.
Since the domain of \( \ln \) is \((0, \inf]\) you have to take the absolute value of \(f(t)\).
To get rid of \(\ln\) raise both side to \( e \). To compensate for the absolute value
you have to take \(\pm\) of \(e\).
\begin{gather}
   |f(t)| = e^{-P(t) + \hat{C}} \\
   f(t) = \pm e^{-P(t) + \hat{C}} \\
   f(t) =  e^{-P(t)} C \label{homogeneral}
\end{gather}
The expression \(e^{\hat{C}}\) is a constant, so it can be replaced by \(C\), which constant
be \(\pm\).

The expression \ref{homogeneral} is the general solution for homogeneous first order
linear differential equations. Any linear combination of the general is a valid solution
to the differential equation.
\subsubsection{Notation}
The notation of differential equations can be simplified by:
\begin{gather*}
   f(t) = y\\
   f(t)' = y'\\
\end{gather*}
\begin{example}
   \begin{gather*}
      y' + sin(x + 2)y = 0
   \end{gather*}
   The general solution is:
   \begin{gather*}
      p(t) = sin(x + 2) \\
      P(t) = - cos(x + 2)\\
      y =  e^{-p(t)} C \\
      y =  e^{cos(x + 2)} C \\
   \end{gather*}
   Following functions are valid solutions to the homogeneous equation:
   \begin{gather*}
      y =  2e^{cos(x + 2)} \\
      y =  2e^{cos(x + 2)} +  3e^{cos(x + 2)} \\
      y = 0 \\
   \end{gather*}
\end{example}
\subsubsection{Non-homogenous}
\begin{gather}\label{inhomo}
   y' + p(t)y = s(t)
\end{gather}
Recall the product rule which states
\begin{gather}
   (f \cdot g) = f' g + f g'
\end{gather}
If we multiply \ref{inhomo} by an unknown function \(\mu(t)\) called the \textbf{integrating factor} we get the following expression:
\begin{gather}\label{intfactor}
   \mu(t) y' + \mu(t) p(t)y = \mu(t) s(t)
\end{gather}
we have an expression that looks like the product rule, if we suppose that
\begin{gather}\label{intfactorcondition}
   \mu(t)' =  \mu(t) p(t) \\
   \mu(t) = \int \mu(t) p(t)
\end{gather}
Integrating \ref{intfactor} by using the product formula in reverse we get:
\begin{gather}
   \int \mu(t) \cdot y' + \mu(t)\cdot p(t) \cdot y  dt = \int \mu(t) \cdot s(t) dt \\
   \mu (t) y = \int \mu(td) s(t) dt \\
   y = \frac{1}{\mu(t)}\int \mu(t) s(t)  \label{inhomosol}
\end{gather}
The function that satisfies \ref{intfactorcondition} is
\begin{gather}
   \mu(t) = C(t) e^{P(t)}\\
   \mu(t)' = C(t)' e^{P(t)} + C(t) \cdot p(t) \cdot e^{P(t)}
\end{gather}
We can chose \(C(t) = 1\)
\begin{gather}
   \mu(t)  = e^{P(t)} \label{mufunction}\\
   \mu(t)' = p(t)e^{P(t)} = p(t)\mu(t)\\
\end{gather}
Setting \ref{mufunction} in \ref{inhomosol} we get the solution for the differential equation.
\begin{equation}
   y = e^{-P(t)}\int e^{P(t)} s(t)dt
\end{equation}
In general integrating the expression above yield the following expression
\begin{equation}
   y = y_p + y_h
\end{equation}
Where \(y_h\) is the solution to the homogenous equation \(y' + p(t)y = 0\). The term
\(y_p\) is called a particular solution and it is one of the solutions to the nonhomogeneous equation.
\begin{example}
   \begin{gather*}
      y' + 2y = t \\
      p(t) = 2\\
      P(t) = 2t\\
      s(t) = t \\
      y = e^{-2t}\int e^{2t} t dt\\
      y = e^{-2t}\left( \frac{1}{2} e^{2t} \cdot t - \frac{1}{4}e^{2t} + C \right)\\
      y = \frac{1}{2} t- \frac{1}{4} + C e^{-2t}
   \end{gather*}
   We see that  \(Ce^{-2t}\) is the solution to the homogeneous equation.
   \(y_p = \frac{1}{2} t- \frac{1}{4}\) is one of the solution to the nonhomogeneous equation.
\end{example}
\begin{matlab}
   \apilink{dsolve}{https://www.mathworks.com/help/symbolic/dsolve.html}
   \begin{lstlisting}
   >> syms y(t)
   >> eqn = diff(y, t) + 2*y == t
   >> dsolve(eqn)
   ans =
      t/2 + (C1*exp(-2*t))/4 - 1/4
  \end{lstlisting}
   Note: C1 / 4 is still a constant C.
\end{matlab}
\section{Linear first-oder system of differential equations}
\subsubsection{The exponential map}
The exponential map is defined by
\begin{equation}
   P_n(t) = e^{At}
\end{equation}
where \(A\) is a square matrix with dimension \(n \times n\) (see also \ref{sec:matrixexponent}).
If a is diagonalizabe then:
\begin{equation}
   P_n(t) = Ue^{Dt} U^{-1}
\end{equation}
The derivate is given by:
\begin{equation}
   P_n(t) \dt = A e^{At}
\end{equation}
\subsubsection{System of differential equations}
A system off differential equations contains a set of unkown function \(x_1(t), x_2(t) \cdots x_n(t) \) denoted by \(x_1, x_2 \cdots x_n\). The derivate of a funciton \(x_i\) (with respext to to \(t\)) is denoted by
\(x_i'\).
\subsection{Homogenous case}
A homogenous linear first-order system has the form:
\begin{equation*}
   \begin{split}
      x_1' &+ p_{11}x_1 + p_{12} x_2 \cdots p_{1n}x_{1n} = 0 \\
      x_2' &+ p_{21}x_1 + p_{22} x_2 \cdots p_{1n}x_{2n} = 0 \\
      &\vdots \\
      x_3' &+ p_{n1}x_1 + p_{n2} x_2 \cdots p_{1n}x_{nn}= 0 \\
   \end{split}
\end{equation*}
The terms \(p_{ij}\) are called coefficient functions (so written out the are like \(p_{ij}(t)\)) they only depend on t.
The derivates can be expressed as a column vector:
\begin{equation*}
   \bm{x'} = \begin{bmatrix}
      x_1' \\ x_2' \\ \vdots \\ x_n'
   \end{bmatrix}
\end{equation*}
The coefficient functions can be expressed as a square matrix \(A^{n \times n}\)
\begin{equation}
   \bm{p} = \begin{bmatrix}
      p_{11} & p_{12} & \cdots & p_{1n} \\
      p_{21} & p_{22} & \cdots & p_{2n} \\
      \vdots &        &        &        \\
      p_{n1} & p_{n2} & \cdots & p_{nn}
   \end{bmatrix}
\end{equation}
The functions can be written as a column vector
\begin{equation}
   \bm{x} = \begin{bmatrix}
      x_1 & x_2 & \cdots & x_n
   \end{bmatrix}
\end{equation}
The wholse system can be written in matrix form:
\begin{equation}
   \bm{x'} = \bm{p} \bm{x}
\end{equation}
The solution is given by (same formula as \ref{homogeneral} applied to matrices):
\begin{equation}
   \bm{x} = M_p(t) \bm{c}
\end{equation}
where \(\bm{c}\) is a column vector of constants.
\begin{example}
   \begin{gather*}
      x_1' = 1 x_1 + 4 x_2 \\
      x_2' = 3x_2 + 2 x_2 \\
   \end{gather*}
   Which can be rewritten as:
   \begin{gather*}
      \bm{x'} = \bm{p} \bm{x} \\
      \begin{bmatrix}
         x_1' \\ x_2'
      \end{bmatrix} = \begin{bmatrix}
         1 & 4 \\ 2 & 4
      \end{bmatrix} \begin{bmatrix}
         x_1 \\ x_2
      \end{bmatrix}
   \end{gather*}
   The eigenvalues and eigenvectors are:
   \begin{gather*}
      \lambda_1 = -2, v_1 = \begin{bmatrix}
         \frac{-4}{3} \\ 1
      \end{bmatrix}
      \lambda_2 = 5, v_1 = \begin{bmatrix}
         1 \\ 1
      \end{bmatrix}
   \end{gather*}
   Calculating \(M_p(t)\)
   \begin{gather*}
      M_p(t) = \begin{bmatrix}
         \frac{-4}{3} & 1 \\ 1 & 1
      \end{bmatrix}
      \begin{bmatrix}
         e^{-2t} & 0 \\ 0 & e^{5t}
      \end{bmatrix} \frac{1}{7}\begin{bmatrix}
         -3 & 3 \\ 4 & 4
      \end{bmatrix} = \frac{1}{7 }\begin{bmatrix}
         4 e^{-2t} + 3e^{5t}  & 4e^{5t} - 4e^{-2t}   \\
         -3 e^{-2t} + 3e^{5t} & 3 e^{-2t} + 4 e^{5t}
      \end{bmatrix}
   \end{gather*}
   The solution is (note the factor \(\frac{1}{7}\) can be ignored because \(\frac{c}{7}\) ist still a constant):
   \begin{gather}
      \bm{x} =  M_p(t) \bm{c} \\
      x_1(t) = c_1 \left( 4 e^{-2t} + 3e^{5t} \right) + c_2 \left(4e^{5t} - 4e^{-2t} \right) \\
      x_2(t) = c_1 \left(-3 e^{-2t} + 3e^{5t} \right) + c_2 \left(3 e^{-2t} + 4 e^{5t} \right)
   \end{gather}
   The constans can be simplified by setting \(c_a = (3c_1 + 4c_2)\) and \( c_b = 3(c_2 - c_1) \):
   \begin{align*}
      x_1(t) & = c_1 4 e^{-2t} + c_1 3e^{5t} + c_2 4 e^{5t} - c_2 4e^{-2t} = -4(c_2 - c_1)e^{-2t}  +  (3c_1 + 4c_2)e^{5t}    \\
             & = c_a e^{5t} - \frac{4}{3} c_b e^{-2t}                                                                        \\
      x_2(t) & = c_1 (-3) e^{-2t} + c_1 3e^{5t}  + c_2 3 e^{-2t} + c_2 4 e^{5t} = 3(c_2 - c_1) e^{-2t} + (3c_1 + 4c_2)e^{5t} \\
             & = c_b e^{-2t} + c_a e^{5t}
   \end{align*}
   \begin{matlab}
      \begin{lstlisting}
         >> syms x1(t) x2(t)
         >> ode1 = diff(x1) == 1*x1 + 4*x2
         >> ode2 = diff(x2) == 3*x1 + 2 *x2
         >> S = dsolve([ode1 ; ode2])
         >> S.x1
            ans =
            C1*exp(5*t) - (4*C2*exp(-2*t))/3
         >> S.x2
            ans =
            C2*exp(-2*t) + C1*exp(5*t)
      \end{lstlisting}
   \end{matlab}
\end{example}
A more simplified solution is:
\begin{equation}
   \bm{x} = \sum_{i=1}^{n} c_i \bm{v_i} e^{\lambda_i t}
\end{equation}


\documentclass[../main.tex]{subfiles}

\begin{document}
%
\chapter{Implicit Differentiation and Inverses}
%--------------------------------------------------
\section{Implicit Differentiation}
%
We know:
\[ \frac{d}{dx} x^a = ax^{a-1} \quad \mid \quad a \in \mathbb{Z} \]
We will now extend this formula to cover $\mathbb{Q}$ as well:
\[
    a = \frac{m}{n} \rightarrow y = x^{\frac{m}{n}}
    \quad \mid \quad
    m, n \in \mathbb{Z}
\]
We can start computing the derivative using the chain rule:%
%
\begin{align*}
    y^n                                                 & = x^n                             \\
    \frac{d}{dx} \left( y^n \right)                     & = \frac{d}{dx} \left( x^m \right) \\
    \frac{d}{dy} \left( y^n \right) \cdot \frac{dy}{dx} & = mx^{m - 1}                      \\
    \frac{dy}{dx} \left( ny^{n - 1} \right)             & = mx^{m - 1}
\end{align*}
%
We finally have an expression for $y'$:
%
\begin{align*}
    \frac{dy}{dx}               & = \frac{mx^{m - 1}}{ny^{n - 1}}                                              \\
                                & = \frac{m}{n} \cdot \frac{x^{m - 1}}{y^{n - 1}}                              \\
                                & = \frac{m}{n} \cdot \frac{x^{m - 1}}{\left( x^{\frac{m}{n}} \right)^{n - 1}} \\
                                & = ax^{m - 1 - \frac{m}{n}(n - 1)}                                            \\
                                & = ax^{a - 1}                                                                 \\
    \therefore \frac{d}{dx} x^a & = ax^{a - 1} \quad \mid \quad a \in \mathbb{Q}
\end{align*}
%
\begin{exmp}
    The equation of a unit circle is:
    \[ y^2 = 1 - x^2 \]
    This can be rewritten as:
    \[ y = \left( 1 - x^2 \right)^{\frac{1}{2}} \]
    We can compute the derivative using the chain rule:
    %
    \begin{align*}
        \frac{dy}{dx} & = \frac{1}{2} \left( 1 - x^2 \right)^{\frac{1}{2} - 1} \cdot (-2x) \\
                      & = \frac{-x}{\left( 1 - x^2 \right)^{\frac{1}{2}}}                  \\
                      & = - \frac{x}{y}
    \end{align*}
    %
    However, we can do the same thing using \emph{implicit differentiation}:
    %
    \begin{align*}
        x^2 + y^2                                                         & = 1                \\
        \frac{d}{dx} \left( x^2 + y^2 \right)                             & = \frac{d}{dx} (1) \\
        \frac{d}{dx} \left( x^2 \right) + \frac{d}{dx} \left( y^2 \right) & = 0                \\
        2x + \frac{d}{dy} \left( y^2 \right) \cdot \frac{dy}{dx}          & = 0                \\
        2x + 2y y'                                                        & = 0                \\
        y'                                                                & = - \frac{x}{y}
    \end{align*}
    %
\end{exmp}
%
\begin{exmp}
    In the following case, it is not so easy to solve for $y$:
    \[ y^3 +xy^2 + 1 = 0 \]
    We will need to use implicit differentiation to find the derivative:
    %
    \begin{align*}
        3y^2y' + y^2 + 2xyy' + 0     & = 0                        \\
        y' \left( 3y^2 + 2xy \right) & = -y^2                     \\
        y'                           & = - \frac{y^2}{3y^2 + 2xy}
    \end{align*}
    %
\end{exmp}
%--------------------------------------------------
\section{Inverses}
%
If $y = f(x)$ and $g(y) = x$, we call $g$ the \emph{inverse} of $f$,
denoted $f^{-1}$:
\[ x = g(y) = f^{-1}(y) \]
Now, we will use implicit differentiation to find the derivative
of the inverse function:
%
\begin{align*}
    y                                                         & = f(x)                    \\
    f^{-1}(y)                                                 & = x                       \\
    \frac{d}{dx} \left( f^{-1}(y) \right)                     & = \frac{d}{dx}(x)         \\
    \frac{d}{dy} \left( f^{-1}(y) \right) \cdot \frac{dy}{dx} & = 1                       \\
    \frac{d}{dy} \left( f^{-1}(y) \right)                     & = \frac{1}{\frac{dy}{dx}} \\
\end{align*}
%
\begin{exmp}
    The derivative of $y = \tan^{-1}(x)$:
    %
    \begin{align*}
        \tan y                                                 & = x                                  \\
        \frac{d}{dx} \left( \tan y \right)                     & = \frac{d}{dx}(x)                    \\
        \frac{d}{dy} \left( \tan y \right) \cdot \frac{dy}{dx} & = 1                                  \\
        \left( \csc^2 y \right) \cdot \frac{dy}{dx}            & = 1                                  \\
        \frac{dy}{dx}                                          & = \cos^2 y                           \\
                                                               & = \cos^2 \left( \tan^{-1}(x) \right)
    \end{align*}
    %
    This form is messy but we can use geometry to simplify.
    %
    \begin{figure}[ht]
        \centering
        \begin{tikzpicture}[scale = 0.5 \textwidth / 2cm]
            \coordinate (A) at (0,0);
            \coordinate (B) at (2,0);
            \coordinate (C) at (0,1);
            \draw
            (A)
            --
            node[below = 5pt]
                {$x$}
            (B)
            --
            node
            [
            sloped,
            above = 5pt
            ]
            {$\left( 1 + x^2 \right)^{\frac{1}{2}}$}
            (C)
            --
            node[left = 5pt]
                {$1$}
            cycle
            pic
                [
                    draw = green!50!black,
                    fill = green!25,
                    angle radius = 1cm,
                    "$y$",
                    -stealth
                ]
                {angle = A--C--B}
            pic
                [
                    draw = black,
                    angle radius = 0.5cm
                ]
                {right angle = B--A--C};
        \end{tikzpicture}
        \caption
        {
            Triangle with angles and lengths corresponding to
            those in the example illustrating differentiation
            using the inverse function
        }
        \label{fig:simplificationTriangle}
    \end{figure}

    In the triangle in Figure \ref{fig:simplificationTriangle},
    $\tan y = x \Rightarrow y = \tan^{-1}(x)$. From this, we can find:
    \begin{align*}
        \cos y                                   & = \frac{1}{\sqrt{1 + x^2}}                  \\
        \left( \cos y \right)^2                  & = \left( \frac{1}{\sqrt{1 + x^2}} \right)^2 \\
        \cos^2 y                                 & = \frac{1}{1 + x^2}                         \\
        \frac{dy}{dx}                            & = \frac{1}{1 + x^2}                         \\
        \frac{d}{dx} \left( \tan^{-1}(x) \right) & = \frac{1}{1 + x^2}
    \end{align*}
    %
\end{exmp}
%
\subsection{Graphing}
%
Suppose $y = f(x)$ and $g(y) = f^{-1}(y) = x$.

To graph $f$ and $g$ together, we need to write $g$ as a function of $x$.
If $g(x) = y$, then $x = f(y)$. What we have done is trade the variables
$x$ and $y$. This is illustrated in Figure \ref{fig:inverseTangent}:
\begin{figure}[ht]
    \centering
    \begin{tikzpicture}
        [
            scale = \textwidth / 11cm,
            smooth,
            > = latex
        ]

        % Axes
        \draw[<->]  (-5,0) -- (5,0) node[right] {$x$};
        \draw[<->]  (0,-5) -- (0,5) node[above] {$y$};
        % Origin
        \node at (0,0) [below right = 2pt] {$0$};

        % Horizontal Aymptotes
        \draw
        [
            densely dotted,
            blue
        ]
        (-5,1.57)  -- (5,1.57);
        \draw
        [
            densely dotted,
            blue
        ]
        (-5,-1.57) -- (5,-1.57);
        % Vertical Asymptotes
        \draw[densely dotted] (1.57,-5)  -- (1.57,5);
        \draw[densely dotted] (-1.57,-5) -- (-1.57,5);

        % Ticks:
        \draw (-1.57,-2pt) -- (-1.57,2pt);
        \draw (1.57,-2pt)  -- (1.57,2pt);
        \draw (-2pt,-1.57) -- (2pt,-1.57);
        \draw (-2pt,1.57)  -- (2pt,1.57);

        % y = ±τ/4
        \node at (0,1.57)
        [
            right = 2pt,
            fill = white
        ]
        {$\frac{\tau}{4}$};
        \node at (0,-1.57)
        [
            right = 2pt,
            fill = white
        ]
        {$- \frac{\tau}{4}$};

        % x = ±τ/4
        \node at (-1.57,0)
        [
            below = 2pt,
            fill = white
        ]
        {$ - \frac{\tau}{4}$};
        \node at (1.57,0)
        [
            below = 2pt,
            fill = white
        ]
        {$ \frac{\tau}{4}$};

        % y = x
        \draw
        [
            gray,
            densely dashed
        ]
        plot[domain = -4 : 4]
        (\x,\x)
        node
            [
                right = 2pt,
                fill = white
            ]
            {$y = x$};

        % y = tan(x)
        \draw
        plot[domain = -1.37 : 1.37]
        (\x,{tan(\x r)}) % \x r means to convert '\x' from degrees to _r_adians:
        node
            [
                right = 2pt,
                fill = white
            ]
            {$y = \tan(x)$};

        % y = arctan(x)
        \draw[blue]
        plot[domain = -4.9 : 4.9]
        (\x,{rad(atan(\x))})
        node[below]
            {$y = \tan^{-1}(x)$};

        % Origin Mark:
        \filldraw[black] (0,0) circle [radius=1pt];
    \end{tikzpicture}
    \caption
    {
        You can think about $f^{-1}$ as the graph of $f$ reflected
        about the line $y = x$
    }
    \label{fig:inverseTangent}
\end{figure}
\end{document}


\subsection{Coordinate vectors}

For example, the basis of $xy$ plane can be:

\[\mathfrak{B}=\left\{\begin{bmatrix} 1\\1\\0\\ \end{bmatrix}, \begin{bmatrix}1\\-1\\0\\ \end{bmatrix}\right\}\]

To form $\begin{bmatrix}1\\ 0\\0\end{bmatrix}$ with this basis, can do $\begin{bmatrix} 1\\0\\0\\ \end{bmatrix}=\frac{1}{2}\begin{bmatrix} 1\\1\\0\\ \end{bmatrix}+\frac{1}{2}\begin{bmatrix}1\\-1\\0\\ \end{bmatrix}$.
The coefficients used form the following vector:

\[\begin{bmatrix}\frac{1}{2}\\ \frac{1}{2}\end{bmatrix}\]

Known as \textbf{$\mathfrak{B}$-coordinate vector}.
Notation:

\[\left[\begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix} \right]_\mathfrak{B}=\begin{bmatrix} \frac{1}{2} \\ \frac{1}{2} \\ \end{bmatrix} \]

\begin{framed}
\noindent
Generally, given $\mathfrak{B}=\left\{ \tb v_1, \tb v_2, \tb v_3, \dots, \tb v_m \right\}\subset \mathbb{R}^n$
is linearly independent, then $[\tb{v}_i]_{\mathfrak{B}}=\tb{e}_i\in\R^m$.
\end{framed}

This is because row-reducing the matrix of $\mathfrak{B}$ gives $\mathrm{rref}(A)$ where $A$ is this matrix. Given the same
$\mathfrak{B}$, can find the components of $\tb{w}$:

\[\left[\tb w \right]_\mathfrak{B}=\begin{bmatrix}c_1\\c_2\\ \vdots\\ c_m\end{bmatrix}\]
\[\tb{w}=c_{1} \tb{v}_{1}+c_{2} \tb{v}_{2}+\cdots+c_{m} \tb{v}_{m}\]

Thus,

\[\tb w = \begin{bmatrix}|&|&\dots&|\\ \tb v_1 &\tb v_2 & \dots &\tb v_m\\  |&|&\dots&| \end{bmatrix}\begin{bmatrix}c_1\\c_2\\\vdots\\c_m\end{bmatrix}\]

Matrix is called change of basis matrix $S$. A standard basis is given as $\tb{e}_1,\tb{e}_2,\cdots$. A nonstandard basis is not of this form.

\subsection{B-matrix}

If $A$ is $n\times n$ and $T(\tb{x})=A\tb{x}$ where $T:\R^n\rightarrow \R^n$, then there exists a matrix $B$ such that
$\left[T(\tb x)\right]_\mathfrak{B}=B\left[\tb x\right]_\mathfrak{B}$. This is called the $\mathfrak{B}-matrix$.
If $\tb{v}_i\in \mathfrak{B}$, then $[\tb{v}_i]_\mathfrak{B}=\tb{e}_i$.\\

\noindent
This means that
$\left[T(\tb v_i)\right]_\mathfrak{B}=B\left[\tb v_i \right]_\mathfrak{B}=B\tb e_i$, so
\textbf{the $i^{\mathrm{th}}$column of $B$ must be $[T(\tb{v}_i)]_\mathfrak{B}$}.\\

\noindent
Multiple ways to calculate $\mathfrak{B}$-matrix of $T$, considering $T$ to be a projection
onto $y=\frac{x}{3}$:

\includegraphics[scale=0.5]{CommutativeDiagram2.jpg}

Means that multiple ways to get to $[T(\tb{x})]_\mathfrak{B}$.
When following $\tb{x}$ and going right and down:

\[S^{-1}\left(A\tb x\right)=S^{-1}A\tb x=\left[T(\tb x) \right]_\mathfrak{B}\]

Going down and right:

\[B\left(S^{-1}\tb x\right)=BS^{-1} \tb x=\left[T(\tb x )\right]_\mathfrak{B}\]

Thus,

\begin{align*}
S^{-1}A=BS^{-1}\\
\boxed{S^{-1}AS=B}
\end{align*}

If this is satisfied, then $A$ is similar to $B$ or $A\sim B$.


\begin{verbatim}
\subsection{Sine functions}
For given $x \in [0, 2\pi]$ with step size $\pi/12$, we can obtain the
evaluations of \eqref{eq:y1}, \eqref{eq:y2}, \eqref{eq:y3} at $x$
(see Table \ref{tab:sin}), and the corresponding plot (see Figure \ref{fig:sin}).
\begin{align}
y_1 & = \sin(x/2) \label{eq:y1}
y_2 & = \sin(x)   \label{eq:y2}
y_3 & = \sin(2x)  \label{eq:y3}
\end{align}
\input{../src/sin.tex}
\begin{figure}[!hbtp]
    \centering
    \includegraphics[width=0.75\textwidth]{../src/lab_05_plot.pdf}
    \caption{Sine functions}
    \label{fig:sin}
\end{figure}
\end{verbatim}


\subsection{Functions}
\begin{definition}
    A function $f$ is a rule between a pair of sets, denoted $f: D \to C$, that assigns values from the first set, the domain $D$, to the second set, the codomain $C$.
\end{definition}

We call the subset of the codomain $C$ that constitutes all values $f$ can actually attain the range $R \subseteq C$.
Note that when we draw a graph of a function, all we are doing is drawing all ordered pairs $\{(x, f(x)) \mid x \in D\}$.

\begin{example}
    Find the domain of the following function:
    \begin{equation*}
        f(x) = \frac{1}{(1 - x)\sqrt{5 - x^2}}
    \end{equation*}
\end{example}

\begin{answer}
    We know that $\frac{n}{0}$ is undefined for all $n \in \mathbb{R}$ and $\sqrt{x}$ is only defined for $x \geq 0$.
    The first condition applies to the first term in the denominator and both conditions apply to the second, giving us
    \begin{equation*}
        (1 - x) \neq 0 \text{ and } 5 - x^2 > 0
    \end{equation*}
    The first condition implies $x \neq 1$ while the second implies $|x| < \sqrt{5}$.
    Putting these together, we find that the domain is
    \begin{equation*}
        \{x \mid x \neq 1, |x| < \sqrt{5}\} \text{ or } (-\sqrt{5}, 1) \cup (1, \sqrt{5})
    \end{equation*}
\end{answer}

We can also compose two functions, such that the ouput of one function is the input of another:
\begin{equation*}
    (f \circ g)(x) = f(g(x)).
\end{equation*}

\begin{definition}
    A function $g$ is called an inverse function of $f$ if $f(g(x)) = x$ for all x in the domain of g and $g(f(x))$ for all x in the domain of f.
    We write this as $g = f^{-1}$.
\end{definition}

One common algorithm for finding an inverse function is to set $y = f(x)$, substitute all $x$'s for $y$'s, and then solve for y.
\begin{example}
    Find the inverse function of
    \begin{equation*}
        f(x) = \frac{5x + 2}{4x - 3}.
    \end{equation*}
\end{example}
\begin{answer}
    We first make the substitutions to set up the algorithm:
    \begin{equation*}
        y = \frac{5x + 2}{4x - 3} \text{ followed by }
        x = \frac{5y + 2}{4y - 3}
    \end{equation*}
    After multiplying both sides by the denominator and simplifying, we have
    \begin{equation*}
        \implies 4xy - 3x = -5y - 2 \\
        \implies y = f^{-1}(x) = \frac{3x - 2}{4x + 5}.
    \end{equation*}
\end{answer}

We say that a function $f$ is even if it satisfies $f(-x) = f(x)$ for all $x \in D$.
Likewise, we say that a function $f$ is odd if it satisfies $f(-x) = -f(x)$ for all $x \in D$.
Geometrically, we can see that the graph of an even function is symmetric with respect to the $y$-axis, while the graph of an odd function is symmetric with respect to the origin.

\begin{example}
    Is $f(x) = 2x - x^2$ even, odd, or neither?
\end{example}
\begin{answer}
    \begin{equation*}
        f(-x) = 2(-x) - (-x)^2 = -2x - x^2
    \end{equation*}
    Since $f(-x) \neq f(x)$ and $f(-x) \neq -f(x)$, the function is neither even nor odd.
\end{answer}


\subsection{Definition}

If:

\(c=a^b\)

Then

\(log_ac=b\)

Product rule:

\(a=c^{log_ca }\)

\(b=c^{log_cb }\)

So:

\(ab=c^{log_cab }\)

But also:

\(ab=c^{log_ca }c^{log_cb }\)

\(ab=c^{log_ca + log_cb }\)

So:

\(log_ca+log_cb=log_cab\)

\subsection{Power rule}

\(a=b^{log_ba}\)

So:

\(a^c=b^{log_ba^c}\)

And separately:

\(a^c=(b^{log_ba})^c\)

\(a^c=(b^{clog_ba})\)

So:

\(clog_ba=log_ba^c\)


\subsection{Tuples}

We can get a list of sets in an order. A 2-tuple is an ordered pair:

\((a, b)\)

We can write an ordered pair of \(a\) and \(b\) as:

\(\{\{a\},\{a,b\}\}\)

Ordred pair definition, and tuple

\((a,b)=(c,d) \leftrightarrow (a=c\land b=d)\)

This is the characteristic property.


\section{State Machines}

\frame{
{Part 3: State Machines}

\tableofcontents[currentsection,hideallsubsections, firstsection=2, sections={2-4}]
}

\subsection{Definition}

\begin{frame}{What are state machines?}

  State machines are used to represent "step-by-step" processes. They contain:
  \begin{itemize}
    \item A description of each possible state in the machine;
    \item How the machine transition from one state to another;
  \end{itemize}\bigskip

  State machines are often used to describe algorithms, programs, logic circuit, decision processes, etc.\bigskip

  State machines are a \structure{formal description} that can be used to prove the correctness of an algorithm.
\end{frame}

\begin{frame}[t]{Example of a State Machine}

  \begin{columns}
    \column{0.4\textwidth}
    State machine for counting from 0 to 99:
    \begin{itemize}
      \item {\bf States:} 0 to 99, overflow.
      \item {\bf Start State:} 0
      \item {\bf Transitions:}\\
        $i \to i+1$ if $i < 99$\\
        $99 \to$ overflow\\
        overflow $\to$ overflow\\
    \end{itemize}\bigskip

    Note how we can represent the State Machine many different ways.

    \column{0.6\textwidth}
    \includegraphics[width=1\textwidth]{../img/statemachine}
  \end{columns}
\end{frame}

\subsection{State Machines for Proofs}
\begin{frame}{State Machine for Proofs}{Robot 1.0}

  Imagine a robot moving forwards and backwards on a street. The robot has two speeds:
  \begin{itemize}
    \item The robot can move exactly {\bf five squares} forwards.
    \item The robot can move exactly {\bf three squares} backwards.
  \end{itemize}
  \bigskip

  If the robot starts from position 0, is it possible for it to reach position 4?
\end{frame}

\begin{frame}{State Machine for Proofs}{Robot 1.1}
  Imagine a robot moving forwards and backwards on a street. The robot has two speeds:
  \begin{itemize}
    \item The robot can move exactly {\bf nine squares} forwards.
    \item The robot can move exactly {\bf three squares} backwards.
  \end{itemize}
  \bigskip

  If the robot starts from position 0, is it possible for it to reach position 4?
\end{frame}

\begin{frame}{State Machine for Proofs}{Preserved Invariants}

  \structure{Preserved Invariants} are propositions that are always true, after {\bf any} transition of the state machine. We can use preserved invariants to prove which squares the robots can reach.

  \begin{block}{Robot 1.0}
    The position of robot 1.0 is always: $s_0 + 5a - 3b$
  \end{block}
  \begin{block}{Robot 1.1}
    The position of robot 1.1 is always: $s_0 + 9a - 3b$
    \begin{itemize}
      \item $s_0 + 9a - 3b = s_0 + 3(3a-b)$
      \item The position of robot 1.1 is always $s_0$ plus a multiple of 3; (\structure{Preserved Invariant})
      \item So it is impossible for robot 1.1 to reach 4 from 0.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}{Induction with Preserved Invariants}

  Preserved Invariants can be used together with inductions to prove things about state machines:\bigskip

  \begin{itemize}
    \item Prove that $P(s)$ is a preserved invariant. This means that if $P(s)$ is true for some state $s$, then it will continue to be true after any transition.\medskip

    \item Prove that $P(s)$ is true for the initial state, $s_0$.\medskip

    \item Conclude that $P(s)$ is always true for the entire state machine.
  \end{itemize}\bigskip

  If $P(s)$ is a "correctness condition" of an algorithm, this method can be used to prove that an algorithm is correct.
\end{frame}

\begin{frame}{State Machine for Proofs}{Robot 2.0}

  Robot 2.0 can move on the diagonals of $\mathbb{Z}^2$: (+1,
  +1), (-1,-1), (+1,-1), (-1,+1). Starting from (0,0), is it possible for the robot to reach position (1,0)?

  \begin{center}
    \includegraphics[width=0.4\textwidth]{../img/diag_robot}
  \end{center}

  \alert{QUIZ}: Try to prove this by yourself first!

\end{frame}

\begin{frame}{State Machine for Proofs}{Robot 2.0 -- Solution}

  We can show that a \structure{preserved invariant} of robot 2.0 is that the sum of its coordinates is always even (or always odd):\bigskip

  \begin{itemize}
  \item P(0,0) is true (0+0 is even).
  \item The steps of the robot are:
    \begin{itemize}
    \item $+1+1 = +2$: even + 2 is still even;
    \item $-1-1 = -2$: even - 2 is still even;
    \item $+1-1 = 0$: even + 0 is still even;
    \item $-1+1 = 0$: even + 0 is still even;
    \end{itemize}
  \end{itemize}
  \bigskip

  So we can see that the parity of the position is a \structure{preserved invariant}. Because the parities of (0,0) and (1,0) are different, it is impossible for robot 2.0 to go from (0,0) to (1,0).
\end{frame}

\begin{frame}{State Machines for Proofs}{Fast Exponentiation}

  This is the end for this lecture. I highly recommend that you watch lecture video 1.9.1 from MIT OCW for a final example with the Fast Exponentiation algorithm.\bigskip

  Summary of the third part: To prove that an algorithm is correct, we need to show that:
  \begin{itemize}
    \item Prove that if the algorihm is in a correct state, it will always stay in the correct state (preserved invariant);
    \item Prove that the algorithm can reach the correct state from the initial position;
    \item Prove that the algorithm stops at some point (not an infinite loop).
    \begin{itemize}
      \item We haven't talked about this part yet, but you can prove this by showing that some variable in the state machine is always decreasing.
    \end{itemize}
  \end{itemize}
\end{frame}