\part{Limits and Continuity}

\chapter{Basic Limits}

\section{Basic Limits}

Suppose we have a function $ y = f(x) $. The limit of $ f(x) $ as $ x $ approaches the number $ a $, denoted $ \lim\limits_{x \rightarrow a}f(x) $. \\

For example, suppose we wanted to determine $ \lim\limits_{x \rightarrow 3}x^2 $. \\

\begin{table}[H]
	\centering
	\begin{tabular}{|c|c|} \hline
		\textbf{$ x $ approaches 3 from the left} & \textbf{$ x $ approaches 3 from the right} \\ \hline
		$ f(2.9) = 8.41 $                         & $ f(3.1) = 9.61 $                          \\ \hline
		$ f(2.99) = 8.9401 $                      & $ f(3.01) = 9.0601 $                       \\ \hline
		$ f(2.999) = 8.994001 $                   & $ f(3.001) = 9.006001 $                    \\ \hline
	\end{tabular}
\end{table}

No matter which way we approach from, as $ x $ gets close 3, we can see that $ x^2 $ gets very close to 9. \\

\begin{exercise}\nonumber
	Find  $ \lim\limits_{x \rightarrow 0}f(x) $ where \\
	\begin{align}
		f(x) = \begin{cases}
			cos(x) & x \ne 0 \\
			-1     & x = 0
		\end{cases}
	\end{align}

	\begin{figure}[H]
		\centering
		\begin{tikzpicture}
			\begin{axis}[
					xlabel={$ x $}, ylabel={$ y $},
					xmin=-2*pi, xmax=2*pi,
					ymin=-1.5, ymax=1.5,
					xtick={-6.28319, -3.14159, 0, 3.14159, 6.28319},
					xticklabels={$-2\pi$, $-\pi$, $0$, $\pi$, $2\pi$},
					line width=1pt,
					axis lines=center,
				]
				\addplot[smooth,domain=-2*pi:-0.15, red!70]{cos(deg(x))};
				\addplot[smooth,domain=0.15:2*pi, red!70]{cos(deg(x))};
			\end{axis}
			\node at (3.43,4.72) [red,circle,inner sep=1pt]{$\circ$};
		\end{tikzpicture}
	\end{figure}

	\begin{gather*}
		\lim\limits_{x \rightarrow 0}f(x) = 1 \\
		f(0) = -1
	\end{gather*}
\end{exercise}

\section{Law of Limits}

\begin{theorem}[Law of Limits]
	\begin{align}
		 & \lim\limits_{x \rightarrow a}(f+g)(x) = \lim\limits_{x \rightarrow a}f(x) + \lim\limits_{x \rightarrow a}g(x)                          \\
		 & \lim\limits_{x \rightarrow a}(f-g)(x) = \lim\limits_{x \rightarrow a}f(x) - \lim\limits_{x \rightarrow a}g(x)                          \\
		 & \lim\limits_{x \rightarrow a}(fg)(x) = \lim\limits_{x \rightarrow a}f(x) \cdot \lim\limits_{x \rightarrow a}g(x)                       \\
		 & \lim\limits_{x \rightarrow a}\left({f \over g}\right)(x) = {\lim\limits_{x \rightarrow a}f(x) \over \lim\limits_{x \rightarrow a}g(x)}
	\end{align}
\end{theorem}

\begin{exercise}\nonumber
	Evaluate $ \lim\limits_{x \rightarrow 0}xsin\left({1 \over x}\right) $. \\
	\begin{gather*}
		-1 \le  sin\left({1 \over x}\right) \le 1 \\
		-x \le xsin\left({1 \over x}\right) \le x \\
		\lim\limits_{x \rightarrow 0}-x \le \lim\limits_{x \rightarrow 0}xsin\left({1 \over x}\right) \le \lim\limits_{x \rightarrow 0}x \\
		\therefore \lim\limits_{x \rightarrow 0}xsin\left({1 \over x}\right) = 0
	\end{gather*}
\end{exercise}

\section{One-sided Limits}

$ x $ can approach the value a in two ways: \\

\begin{itemize}
	\item
	      left-hand limit: $ \lim\limits_{x \rightarrow a^-}f(x) $ \\

	\item
	      right-hand limit: $ \lim\limits_{x \rightarrow a^+}f(x) $ \\
\end{itemize}

We can tighten our definition of the limit of a function $ f(x) $ as $ x $ approaches $ a $. If the left-hand and right-hand limits of $ f(x) $ are both equal to a number $ L $, then we say that $ \lim\limits_{x \rightarrow a}f(x) $ exists and is equal to $ L $. \\

\begin{exercise}\nonumber
	Consider the piecewise function given by \\
	\begin{align}
		f(x) = \begin{cases}
			x          & x < 1 \\
			-1         & x = 1 \\
			\sqrt{x-1} & x > 1
		\end{cases}
	\end{align}

	(a) $ \lim\limits_{x \rightarrow 1^-}f(x) = 1 $ \\

	(b) $ \lim\limits_{x \rightarrow 1^+}f(x) = 0 $ \\

	(c) $ f(1) = -1 $ \\

	(d) Does $ \lim\limits_{x \rightarrow 1}f(x) $ exist? No.
\end{exercise}

\chapter{Continuity}

\section{Continuity}

It seems like sometimes we can evaluate a limit by just plugging in, but sometimes we can't! \\

\begin{exercise}\nonumber
	Evaluate $ \lim\limits_{x \rightarrow 1}\left(ln(\sqrt{x}) + {1 \over x}\right) $. \\
	\begin{align}
		 & \lim\limits_{x \rightarrow 1}\left(ln(\sqrt{x}) + {1 \over x}\right) \\
		 & = ln\left(\sqrt{x} + {1 \over x}\right)                              \\
		 & = ln\left(\sqrt{1}\right) + {1 \over 1}                              \\
		 & = 0 + 1                                                              \\
		 & = 1
	\end{align}
\end{exercise}

A function is continuous at a point a if each of the following conditions hold: \\

\begin{enumerate}
	\item
	      $ \lim\limits_{x \rightarrow a}f(x) $ exists \\

	\item
	      $ f(a) $ exists \\

	\item
	      $ \lim\limits_{x \rightarrow a}f(x) = f(a) $ \\
\end{enumerate}

\section{Discontinuity}

Kinds of discontinuities: \\

\begin{figure}[H]
	\centering
	\begin{tikzpicture}[scale=0.7]
		\draw[->] (-5,0) -- (4,0) node[right] {$ x $};
		\draw[->] (0,-4) -- (0,4) node[above] {$ y $};
		\draw[very thick,color=red,domain=-4.5:-1.3] plot (\x,{1 / (\x + 1)});
		\draw[very thick,color=red,domain=-0.7:3] plot (\x,{1 / (\x + 1)});
		\draw[very thick,densely dashed,color=red] (-1,4)--(-1,-4);
	\end{tikzpicture}
	\caption{essential}
\end{figure}

\begin{figure}[H]
	\centering
	\begin{tikzpicture}
		\begin{axis}[
				xlabel={$ x $}, ylabel={$ y $},
				xmin=-2*pi, xmax=2*pi,
				ymin=-1.5, ymax=1.5,
				xtick={-6.28319, -3.14159, 0, 3.14159, 6.28319},
				xticklabels={$-2\pi$, $-\pi$, $0$, $\pi$, $2\pi$},
				line width=1pt,
				axis lines=center,
			]
			\addplot[smooth,domain=-2*pi:1.07, red!70]{sin(deg(x))};
			\addplot[smooth,domain=1.2:2*pi, red!70]{sin(deg(x))};
			\node at (742,240) [red,circle,inner sep=1pt]{$\circ$};
		\end{axis}
	\end{tikzpicture}
	\caption{removable}
\end{figure}

\begin{figure}[H]
	\centering
	\begin{tikzpicture}[scale=0.5,yscale=0.3]
		\draw[->] (-10,0) -- (6,0) node[right] {$ x $};
		\draw[->] (0,-5) -- (0,20) node[above] {$ y $};
		\draw[very thick,color=red,domain=-9:-2.1] plot (\x,{0.5 * \x + 3});
		\draw[very thick,color=red,domain=-2:2] plot (\x,{0});
		\draw[very thick,color=red,domain=2.1:4] plot (\x,{\x^2 - 1});
		\foreach \point in {(-2,0),(2,0)} {
				\node at \point [red,circle,fill,inner sep=1.5pt]{};
			}
		\foreach \point in {(-2,2),(2,3)} {
				\node at \point [red,circle,inner sep=1.5pt]{$\circ$};
			}
	\end{tikzpicture}
	\caption{jump}
\end{figure}

So far, we know that evaluating $ \lim\limits_{x \rightarrow a}f(x) $ is easy if $ f(x) $ is continuous at $ a $. But what if it isn't? The limit may still exist. \\

\begin{exercise}\nonumber
	Evaluate the following limits. \\

	(a) $ \lim\limits_{x \rightarrow 3^+}{1 \over 3-x} = -\infty $ \\

	(b) $ \lim\limits_{x \rightarrow 3^-}{1 \over 3-x} = \infty $ \\

	(c) $ \lim\limits_{x \rightarrow 0^+}ln(x) = -\infty $ \\

	(d) $ \lim\limits_{x \rightarrow -{\pi \over 2}^+}sec(x) = \infty $
\end{exercise}

A function doesn't have to go to $ \pm \infty $ at a discontinuity though. Consider, for instance, evaluating $ \lim\limits_{x \rightarrow 3}{x^2-x-6 \over x-3} $. Plugging in $ x = 3 $ gives us "$ 0 / 0 $", which is known as an indeterminate form. \\

A good first step is to factor where possible. Once we cancel the factor $ x - 3 $ on top and bottom, this function is just the function $ f(x) = x + 2 $, but with a hole at $ x = 3 $. The hole is there because we cannot ignore that the original form of the function had issues at $ x = 3 $. \\

\begin{exercise}\nonumber
	Evaluate the following limits. \\

	(a)
	\begin{align}
		 & \lim\limits_{x \rightarrow 3}{x^2-x-6 \over x-3}      \\
		 & = \lim\limits_{x \rightarrow 3}{(x-3)(x+2) \over x-3} \\
		 & = \lim\limits_{x \rightarrow 3}{x+2}                  \\
		 & = 5
	\end{align}

	\begin{figure}[H]
		\centering
		\begin{tikzpicture}[scale=0.3]
			\draw[->] (-6,0) -- (6,0) node[right] {$ x $};
			\draw[->] (0,-4) -- (0,8) node[above] {$ y $};
			\draw[very thick,color=red,domain=-5:2.85] plot (\x,{\x + 2});
			\draw[very thick,color=red,domain=3.2:5] plot (\x,{\x + 2});
			\node at (3,5) [red,circle,inner sep=1.5pt]{$\circ$};
		\end{tikzpicture}
	\end{figure}

	(b)
	\begin{align}
		 & \lim\limits_{x \rightarrow -1}{x^3 - 11x^2 + 8x + 20 \over x + 1} \\
		 & = \lim\limits_{x \rightarrow -1}{(x+1)(x^2-12x+20) \over x + 1}   \\
		 & = (-1)^2 - 12(-1) + 20                                            \\
		 & = 33
	\end{align}
	\\

	(c)
	\begin{align}
		 & \lim\limits_{x \rightarrow 4}{\sqrt{x+5} - 3 \over x-4}                                              \\
		 & = \lim\limits_{x \rightarrow 4}{({{\sqrt{x+5}-3} \over {x-4}})({{\sqrt{x+5}+3} \over \sqrt{x+5}+3})} \\
		 & = \lim\limits_{x \rightarrow 4}{x-4 \over (x-4)(\sqrt{x+5}+3)}                                       \\
		 & = {1 \over \sqrt{9} + 3}                                                                             \\
		 & = {1 \over 6}
	\end{align}
	\\

	(d)
	\begin{align}
		 & \lim\limits_{x \rightarrow 2}{{1 \over x-4} + {1 \over 2} \over x-2}                     \\
		 & = \lim\limits_{x \rightarrow 2}{{{2+(x-4)} \over {2(x-4)}} \over x-2}                    \\
		 & = \lim\limits_{x \rightarrow 2}{\left({x-2 \over 2x-4}\right)\left({1 \over x-2}\right)} \\
		 & = \lim\limits_{x \rightarrow 2}{1 \over 2(x-4)}                                          \\
		 & = {1 \over 2(-2)}                                                                        \\
		 & = -{1 \over 4}
	\end{align}
\end{exercise}

\chapter{The Fundamental Sine Limit}

\section{The Fundamental Sine Limit}

Consider the following limit:

$$
	\lim\limits_{x \rightarrow 0}{sin(x) \over x}
$$

We can quickly see that this is a "$ 0 / 0 $" limit, and therefore is indeterminate. We can't factor anything or otherwise simplify the expression to get rid of the issue. \\

\begin{figure}[H]
	\centering
	\begin{tikzpicture}[scale=1]
		\begin{axis}[
				grid=both,
				xmin=-4,
				xmax=4,
				ymin=-2,
				ymax=2,
				xlabel=$x$,
				ylabel=$y$,
				axis lines=center,
				>=stealth
			]
			\addplot[
				domain=-4:4,
				red,
				ultra thick,
				samples=100,
			] plot[smooth] {sin(deg(x))/x};
		\end{axis}
	\end{tikzpicture}
	\caption{$ sin(x) \over x $}
\end{figure}

It is pretty clear from the graph that \\

\begin{theorem}[The Fundamental Sine Limit]
	\begin{align}
		\lim\limits_{x \rightarrow 0}{sin(x) \over x} = \lim\limits_{x \rightarrow 0}{x \over sin(x)} = 1
	\end{align}
\end{theorem}

\begin{exercise}\nonumber
	Evaluate the following limits. \\

	(a)
	\begin{align}
		 & \lim\limits_{t \rightarrow 0}{5sin(t) \over 2t}                     \\
		 & = \lim\limits_{t \rightarrow 0}{{5 \over 2} \cdot {sin(t) \over t}} \\
		 & = {5 \over 2}
	\end{align}
	\\

	(b)
	\begin{align}
		 & \lim\limits_{\theta \rightarrow 0}{\theta \over sin(4\theta)}                 \\
		 & = \lim\limits_{\theta \rightarrow 0}{4\theta \over 4sin(4\theta)}             \\
		 & = \lim\limits_{\theta \rightarrow 0}{{1 \over 4}{4\theta \over sin(4\theta)}} \\
		 & = {1 \over 4}
	\end{align}
	\\

	(c)
	\begin{align}
		 & \lim\limits_{x \rightarrow -1}{sin^2(x+1) \over (x+1)^2}                          \\
		 & = \lim\limits_{x \rightarrow -1}{{sin(x+1) \over x+1} \cdot {sin(x+1) \over x+1}} \\
		 & = 1
	\end{align}
	\\

	(d)
	\begin{align}
		 & \lim\limits_{x \rightarrow 0}{sin(3x) \over sin(2x)}                                          \\
		 & = \lim\limits_{x \rightarrow 0}{{sin(3x) \over 3} \cdot {2x \over sin(2x)} \cdot {3 \over 2}} \\
		 & = {3 \over 2}
	\end{align}
	\\

	(e)
	\begin{align}
		 & \lim\limits_{\theta \rightarrow 0}{100\theta \over tan(3\theta)}                                                 \\
		 & = \lim\limits_{\theta \rightarrow 0}{10\theta \cdot {cos(3\theta) \over sin(3\theta)}}                           \\
		 & = \lim\limits_{\theta \rightarrow 0}{{3\theta \over sin(3\theta)} \cdot {1 \over 3} \cdot 10 \cdot cos(3\theta)} \\
		 & = 1 \cdot {10 \over 3} \cdot 1                                                                                \\
		 & = {10 \over 3}
	\end{align}
	\\

	(f)
	\begin{align}
		 & \lim\limits_{h \rightarrow 0}{cos(h)-1 \over h}                                                        \\
		 & = \lim\limits_{h \rightarrow 0}{\left({cos(h)-1 \over h}\right)\left({cos(h)+1 \over cos(h)+1}\right)} \\
		 & = \lim\limits_{h \rightarrow 0}{cos^2h - 1 \over h(cos(h) + 1)}                                        \\
		 & = \lim\limits_{h \rightarrow 0}{-sin^2h \over h\left(cos(h) + 1\right)}                                \\
		 & = \lim\limits_{h \rightarrow 0}{-{sin(h) \over h} \cdot {sin(h) \over cos(h) + 1}}                     \\
		 & = -1 \cdot {0 \over 1+1}                                                                               \\
		 & = 0
	\end{align}
\end{exercise}

\chapter{Limits Approaching $ \pm \infty $}

\section{Limits Approaching $ \pm \infty $}

Sometimes, we will be interested in determining what happens as $ x $ gets really big, either in the positive or negative direction. \\

\begin{exercise}\nonumber
	Evaluate the following limits. \\

	(a) $ \lim\limits_{x \rightarrow \infty}{1 \over x} = 0 $ \\

	(b) $ \lim\limits_{x \rightarrow -\infty}{1 \over x} = 0 $ \\

	(c) $ \lim\limits_{x \rightarrow -\infty}{401 \over x^{203}} = 0 $ \\

	(d) $ \lim\limits_{x \rightarrow -\infty}{-238 \over 23x^{1/4}} = 0 $
\end{exercise}

For problems like $ \lim\limits_{x \rightarrow \infty}{2x^2 + 3x + 1 \over x^2 - 10x + 100} $, a good first step is to divide the top and the bottom by the highest power of $ x $ appearing in the denominator. \\

\begin{exercise}\nonumber
	Evaluate the following limits. \\

	(a)
	\begin{align}
		 & \lim\limits_{x \rightarrow \infty}{2x^2 + 3x + 1 \over x^2 - 10x + 100}                                            \\
		 & = \lim\limits_{x \rightarrow \infty}{\left({2x^2+3x+1 \over x^2-10x+100}\right)\left({1/x^2 \over 1/x^2}\right)}   \\
		 & = \lim\limits_{x \rightarrow \infty}{\left({2x^2+3x+1 \over x^2-10x+100}\right)\left({1/x^2 \over 1/x^2}\right)}   \\
		 & = \lim\limits_{x \rightarrow \infty}{{2 + {3 \over x} + {1 \over x^2}} \over {1 - {10 \over x} + {100 \over x^2}}} \\
		 & = {2 \over 1}                                                                                \\
		 & = 2
	\end{align}
	\\

	(b)
	\begin{align}
		 & \lim\limits_{x \rightarrow -\infty}{3 - 2x^3 \over 1 + x + x^2}                                            \\
		 & = \lim\limits_{x \rightarrow -\infty}{\left({3-2x^3 \over 1+x+x^2}\right)\left({1/x^2 \over 1/x^2}\right)} \\
		 & = \lim\limits_{x \rightarrow -\infty}{{3 \over x^2} - 2x \over {{1 \over x^2} + {1 \over x} + 1}}          \\
		 & = \lim\limits_{x \rightarrow -\infty}{-2x \over 1}                                                         \\
		 & = \infty
	\end{align}
	\\

	(c)
	\begin{align}
		 & \lim\limits_{x \rightarrow \infty}{\sqrt{x^2 - x + 1}}                                                                    \\
		 & = \lim\limits_{x \rightarrow \infty}{\left(\sqrt{x^2-x+1}-x\right)\left({\sqrt{x^2-x+1}+x \over \sqrt{x^2-x+1}+x}\right)} \\
		 & = \lim\limits_{x \rightarrow \infty}{(x^2-x+1)-x^2 \over \sqrt{x^2-x+1}+x}                                                \\
		 & = \lim\limits_{x \rightarrow \infty}{x+1 \over \sqrt{x^2-x+1}+x}                                                          \\
		 & = \lim\limits_{x \rightarrow \infty}{\left({x+1 \over \sqrt{x^2-x+1}+x}\right)\left({1/x \over 1/x}\right)}               \\
		 & = \lim\limits_{x \rightarrow \infty}{-1 + {1 \over x} \over {\sqrt{x^2-x+1} \over x} + 1}                                 \\
		 & = \lim\limits_{x \rightarrow \infty}{-1 + {1 \over x} \over \sqrt{{x^2-x+1} \over x^2} + 1}                               \\
		 & = \lim\limits_{x \rightarrow \infty}{-1 \over \sqrt{1} + 1}                                                               \\
		 & = -{1 \over 2}
	\end{align}
	\\

	(d)
	\begin{align}
		 & \lim\limits_{t \rightarrow \infty}{\sqrt{3t^2 + 2t + 5} + t} \\
		 & = \infty
	\end{align}
\end{exercise}


% Start of header.
\documentclass{article}
\usepackage{amsthm}
\usepackage{caption}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\newcommand{\I}[0]{\mathbb{I}}
\newcommand{\Q}[0]{\mathbb{Q}}
\newcommand{\Z}[0]{\mathbb{Z}}
\newcommand{\C}[0]{\mathbb{C}}
\newcommand{\F}[0]{\mathbb{F}}
\newcommand{\R}[0]{\mathbb{R}}
\newcommand{\N}[0]{\mathbb{N}}
\newcommand{\unif}[0]{\mathcal{U}}
\newcommand{\Tor}[0]{\operatorname{Tor}}
\newcommand{\ra}[0]{\rightarrow}
\newcommand{\rr}[0]{\Rightarrow}
\newcommand{\llrr}[0]{\Leftrightarrow}
\newcommand{\subq}[0]{\subseteq}
\newcommand{\sub}[0]{\subset}
\newcommand{\nsubq}[0]{\not\subseteq}
\newcommand{\nsub}[0]{\not\subset}
\newcommand{\sep}[0]{\hbox{ }}
\newcommand{\inv}[0]{^{\raisebox{.2ex,$\scriptscriptstyle-1$}}}
\newcommand{\thnew}[0]{^{\text{th}}}
\newcommand{\ifnew}[0]{\text{ if }}
\newcommand{\elsenew}[0]{\text{ else }}
\newcommand{\for}[0]{\text{ for }}

\setlength{\jot}{8pt}
% End of header
% Start of body.

\begin{document}

\section{Expected value of kth element of an ordered sequence of elements from some interval}

\subsection{Question:}

Let each ordered sequence of $n$ unique elements $X_1, \dots, X_n$ from the interval $(a, b)$ be equally probable. What is the expected value of the element $X_k$?

\subsection{Answer:}

Consider random variables $Y_1, \dots, Y_n$ sampled from a uniform  distribution over $(a, b)$ such that no two $Y_i$ are equal. First, we show that any ordered sequence made from $Y_1, \dots, Y_n$ is equally probable.

Let $X_1 = Y_{i_1}, \dots, X_n = Y_{i_n}$ be the ordered sequence made from our random variables $Y_1, \dots, Y_n$. Then we see the probability density function, $f$, at a specific random sequence is

$$f_X([X_1, \dots, X_n]) = \sum_{Y_1, \dots, Y_n \in \text{ Permuations}(X_1, \dots, X_n)} f_{Y_1, \dots, Y_n}(Y_1, \dots, Y_n) $$
$$= \sum_{Y_1, \dots, Y_n \in \text{ Permuations}(X_1, \dots, X_n)} f_{Y_1}(Y_1)\cdots f_{Y_n}(Y_n)$$

Note that the value of a uniform probability density does not change if we introduce a finite number of holes (as the integral over the pdf does not change in value), thus:

$$
f_{Y_i}(Y_i) =
\begin{cases}
\frac{1}{b-a},  & \text{if $Y_i \in (a, b)$ and $Y_i \neq Y_j$ for $j \neq i$} \\
0, & \text{otherwise}
\end{cases}
$$

\textbf{NOTE}: \emph{I think I can rewrite it this way, because all I'm doing is reorganizing the points of the composite $Y$ pdf into groups that produce the same ordered sequence. So when I integrate over the pdf of the ordered sequences, it's integrating over the same region as the composite $Y$ pdf.} \textbf{But the multiplier of $n!$ can't be right or the pdf for $f_X$ will integrate to greater than 1!}

Therefore we can rewrite $f_X$:

$$f_X([X_1, \dots, X_n]) = \sum_{Y_1, \dots, Y_n \in \text{ Permuations}(X_1, \dots, X_n)}  \frac{1}{(b-a)^n}$$
$$=\frac{n!}{(b-a)^n}$$
\end{document}
% End of body.


\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage{enumitem}

\newcommand{\fromlectures}{{\\ \color{blue} \hspace*{\fill}(from lecture slides)} \\}
\newcommand{\bydefn}{{\\ \color{blue} \hspace*{\fill}(by definition)} \\}
\newcommand{\given}{{\\ \color{blue} \hspace*{\fill}(given)} \\}
\newcommand{\rtp}{{\\ \color{blue} \hspace*{\fill}(required to prove)} \\}

\newcommand{\f}[1]{o_{#1}x_{#1}y_{#1}z_{#1}}
\newcommand{\rx}[1]{\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & cos(#1) & -sin(#1) & 0 \\ 0 & sin(#1) & cos(#1) & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}}
\newcommand{\rz}[1]{\begin{bmatrix} cos(#1) & -sin(#1) & 0 & 0 \\ sin(#1) & cos(#1) & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}}
\newcommand{\iden}{\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}}
\newcommand{\trans}[3]{\begin{bmatrix} 1 & 0 & 0 & #1 \\ 0 & 1 & 0 & #2 \\ 0 & 0 & 1 & #3 \\ 0 & 0 & 0 & 1 \end{bmatrix}}

\title{CSci 5551 - HW4}
\author{Yashasvi Sriram Patkuri\\patku001@umn.edu}

\begin{document}
\maketitle
\pagebreak

\section{}
A three link three joint planar robot with link lengths $l_1, l_2, l_3$ respectively is considered.
\given

\begin{figure}[h]
  \centering
  \includegraphics[width=0.6\textwidth]{q1.png}
  \caption{Three link three joint robot}
  \label{fig:q1.1}
\end{figure}
Assigning frames according to DH convention we have,
\fromlectures
\begin{figure}[h]
  \centering
  \begin{tikzpicture}[x=1cm, y=1cm, z=-0.6cm]
    % F0
    \draw [->]       (0, 0) -- (2, 0) node [right] {$x_0$};
    \draw            (0, 0) circle (0.15cm);
    \draw [fill]     (0, 0) circle (0.07cm) node [left] {$z_0$};
    % F1
    \draw [->]       (4, 2) -- (5.7888, 2.8944) node [right] {$x_1$};
    \draw            (4, 2) circle (0.15cm);
    \draw [fill]     (4, 2) circle (0.07cm) node [left] {$z_1$};
    % F2
    \draw [->]       (6, 5) -- (7.414, 6.414) node [right] {$x_2$};
    \draw            (6, 5) circle (0.15cm);
    \draw [fill]     (6, 5) circle (0.07cm) node [left] {$z_2$};
    % F3
    \draw [->]       (9, 5) -- (11, 5) node [right] {$x_3$};
    \draw            (9, 5) circle (0.15cm);
    \draw [fill]     (9, 5) circle (0.07cm) node [left] {$z_3$};
  \end{tikzpicture}
  \caption{DH frame assignment}
  \label{fig:q1.2}
\end{figure}

\begin{enumerate}[nolistsep]
  \item The z-axes are chosen along the axes of rotation and out of paper for the end-effector.
  \item The choice of $x_0$ is free so it is chosen to be parallel to ground for simplicity.
  \item There is a unique common normal b/w $z_0$ and $z_1$, along which $x_1$ is chosen.
  \item $x_2$ and $x_3$ are chosen in the same way.
\end{enumerate}

\subsubsection*{DH parameters}
The DH parameters for the frames shown in Figure \ref{fig:q1.2} are as follows
\begin{center}
\begin{tabular}{ c | c c c c }
 \hline
 $F_i \to F_j$ & $\theta$ & d & r & $\alpha$ \\
 \hline
 $0 \to 1$ & $\theta_1$ & 0 & $l_1$ & $0^{\circ}$ \\
 $1 \to 2$ & $\theta_2$ & 0 & $l_2$ & $0^{\circ}$ \\
 $2 \to 3$ & $\theta_3$ & 0 & $l_3$ & $0^{\circ}$ \\
 \hline
\end{tabular}
\end{center}

\subsubsection*{Finding transformation $T_{01}, T_{02}, T_{03}$}
Given frame i and frame j with DH parameters [ $\theta$, d, r, $\alpha$ ] the transformation matrix is
\[
  T_{i,j} \equiv Rot_{z,\theta} * Trans_{z, d} * Trans_{x, r} * Rot_{x, \alpha}
\]
\fromlectures
Consider $T_{01}$
\[
  T_{01} \equiv Rot_{z,\theta_1} * Trans_{z, 0} * Trans_{x, l_1} * Rot_{x, 0^{\circ}}
\]
\[
  T_{01} \equiv \rz{\theta_1} * \trans{0}{0}{0} * \trans{l_1}{0}{0} * \rx{0^{\circ}}
\]
\[
  T_{01} \equiv \rz{\theta_1} * \iden * \trans{l_1}{0}{0} * \iden
\]
\[
  T_{01} \equiv \rz{\theta_1} * \trans{l_1}{0}{0}
\]
\[
  T_{01} \equiv
  \begin{bmatrix} c_1 & -s_1 & 0 & l_1c_1 \\ s_1 & c_1 & 0 & l_1s_1 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Where $c_1 \equiv cos(\theta_1), s_1 \equiv sin(\theta_1)$.
$T_{12}, T_{23}$ have the same DH-parameter structure with changed variables, therefore we can use the final form of $T_{01}$ for them.
\[
  T_{12} \equiv
  \begin{bmatrix} c_2 & -s_2 & 0 & l_2c_2 \\ s_2 & c_2 & 0 & l_2s_2 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{23} \equiv
  \begin{bmatrix} c_3 & -s_3 & 0 & l_3c_3 \\ s_3 & c_3 & 0 & l_3s_3 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
But
\[
  T_{ij} \equiv T_{ik} * T_{kj}
\]
\fromlectures
Therefore,
\[
  T_{02} \equiv T_{01} * T_{12}
\]
\[
  T_{02} \equiv
  \begin{bmatrix} c_1 & -s_1 & 0 & l_1c_1 \\ s_1 & c_1 & 0 & l_1s_1 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix} c_2 & -s_2 & 0 & l_2c_2 \\ s_2 & c_2 & 0 & l_2s_2 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{02} \equiv
  \begin{bmatrix} c_1c_2 - s_1s_2 & -(s_1c_2 + c_1s_2) & 0 & l_1c_1 + l_2(c_1c_2 - s_1s_2) \\ s_1c_2 + c_1s_2 & c_1c_2 - s_1s_2 & 0 & l_1s_1 + l_2(s_1c_2 + c_1s_2) \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{02} \equiv
  \begin{bmatrix} c_{12} & -s_{12} & 0 & l_1c_1 + l_2c_{12} \\ s_{12} & c_{12} & 0 & l_1s_1 + l_2s_{12} \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Where $c_{12} \equiv cos(\theta_1 + \theta_2), s_{12} \equiv sin(\theta_1 + \theta_2)$.
Consider $T_{03}$,
\[
  T_{03} \equiv T_{02} * T_{23}
\]
\[
  T_{03} \equiv
  \begin{bmatrix} c_{12} & -s_{12} & 0 & l_1c_1 + l_2c_{12} \\ s_{12} & c_{12} & 0 & l_1s_1 + l_2s_{12} \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix} c_3 & -s_3 & 0 & l_3c_3 \\ s_3 & c_3 & 0 & l_3s_3 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{03} \equiv
  \begin{bmatrix}
    c_{12}c_{3} - s_{12}s_{3} & -(s_{12}c_{3} + c_{12}s_{3}) & 0 & l_1c_1 + l_2c_{12} + l_3(c_{12}c_{3} - s_{12}s_{3})\\
    s_{12}c_{3} + c_{12}s_{3} & c_{12}c_{3} - s_{12}s_{3}    & 0 & l_1s_1 + l_2s_{12} + l_3(s_{12}c_{3} + c_{12}s_{3})\\
    0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
\[
  T_{03} \equiv
  \begin{bmatrix}
    c_{123} & -s_{123} & 0 & l_1c_1 + l_2c_{12} + l_3c_{123}\\
    s_{123} & c_{123}  & 0 & l_1s_1 + l_2s_{12} + l_3s_{123}\\
    0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]

\subsubsection*{Finding Jacobian}
For a 3R robot, the Jacobian of pose of end effector w.r.t frame 0 as a function of joint variables
\[
  J \equiv
  \begin{bmatrix} z_0^0 \times a_{03}^0 & z_1^0 \times a_{13}^0 & z_2^0 \times a_{23}^0 \\ z_0^0 & z_1^0 & z_2^0 \end{bmatrix}
\]
where
\[
  a_{i3}^{0} \equiv a_{3}^{0} - a_{i}^{0}
\]
where $a_3^0$ is the origin of frame 3 in frame 0 and $a_i^0$ is origin of frame i in frame 0.
$z_i^0$ can be obtained by the first three number of third column of $T_{0i}$.
\fromlectures
\[
  z_0^{0} \equiv z_1^{0} \equiv z_2^{0} \equiv z_3^{0} \equiv \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix}
\]
Consider $a_{3}^{0}$,
\[
  a_3^0 \equiv T_{03} * O_3
\]
\[
  a_3^0 \equiv
  \begin{bmatrix}
    c_{123} & -s_{123} & 0 & l_1c_1 + l_2c_{12} + l_3c_{123}\\
    s_{123} & c_{123}  & 0 & l_1s_1 + l_2s_{12} + l_3s_{123}\\
    0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
  *
  \begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \end{bmatrix}
\]
\[
  a_3^0 \equiv
  \begin{bmatrix}
    l_1c_1 + l_2c_{12} + l_3c_{123}\\
    l_1s_1 + l_2s_{12} + l_3s_{123}\\
    0 \\
    1
  \end{bmatrix}
\]
Consider $a_{2}^{0}$,
\[
  a_2^0 \equiv T_{02} * O_2
\]
\[
  a_2^0 \equiv
  \begin{bmatrix} c_{12} & -s_{12} & 0 & l_1c_1 + l_2c_{12} \\ s_{12} & c_{12} & 0 & l_1s_1 + l_2s_{12} \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \end{bmatrix}
\]
\[
  a_2^0 \equiv
  \begin{bmatrix}
    l_1c_1 + l_2c_{12}\\
    l_1s_1 + l_2s_{12}\\
    0 \\
    1
  \end{bmatrix}
\]
Consider $a_{1}^{0}$,
\[
  a_1^0 \equiv T_{01} * O_1
\]
\[
  a_1^0 \equiv
  \begin{bmatrix} c_1 & -s_1 & 0 & l_1c_1 \\ s_1 & c_1 & 0 & l_1s_1 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \end{bmatrix}
\]
\[
  a_1^0 \equiv
  \begin{bmatrix}
    l_1c_1\\
    l_1s_1\\
    0 \\
    1
  \end{bmatrix}
\]
And,
\[
  a_0^0 \equiv \begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \end{bmatrix}
\]
Removing the homogeneous fourth coordinate we have,
\[
  a_0^0 \equiv
  \begin{bmatrix} 0 \\ 0 \\ 0  \end{bmatrix}
\]
\[
  a_1^0 \equiv
  \begin{bmatrix}
    l_1c_1\\
    l_1s_1\\
    0
  \end{bmatrix}
\]
\[
  a_2^0 \equiv
  \begin{bmatrix}
    l_1c_1 + l_2c_{12}\\
    l_1s_1 + l_2s_{12}\\
    0
  \end{bmatrix}
\]
\[
  a_3^0 \equiv
  \begin{bmatrix}
    l_1c_1 + l_2c_{12} + l_3c_{123}\\
    l_1s_1 + l_2s_{12} + l_3s_{123}\\
    0
  \end{bmatrix}
\]
Consider $a_{03}^0$,
\[
  a_{03}^0 \equiv a_3^0 - a_0^0
\]
\[
  a_{03}^0 \equiv
  \begin{bmatrix}
    l_1c_1 + l_2c_{12} + l_3c_{123}\\
    l_1s_1 + l_2s_{12} + l_3s_{123}\\
    0
  \end{bmatrix}
  -
  \begin{bmatrix} 0 \\ 0 \\ 0  \end{bmatrix}
\]
\[
  a_{03}^0 \equiv
  \begin{bmatrix}
    l_1c_1 + l_2c_{12} + l_3c_{123}\\
    l_1s_1 + l_2s_{12} + l_3s_{123}\\
    0
  \end{bmatrix}
\]
Consider $a_{13}^0$,
\[
  a_{13}^0 \equiv a_3^0 - a_1^0
\]
\[
  a_{13}^0 \equiv
  \begin{bmatrix}
    l_1c_1 + l_2c_{12} + l_3c_{123}\\
    l_1s_1 + l_2s_{12} + l_3s_{123}\\
    0
  \end{bmatrix}
  -
  \begin{bmatrix}
    l_1c_1\\
    l_1s_1\\
    0
  \end{bmatrix}
\]
\[
  a_{13}^0 \equiv
  \begin{bmatrix}
    l_2c_{12} + l_3c_{123}\\
    l_2s_{12} + l_3s_{123}\\
    0
  \end{bmatrix}
\]
Consider $a_{23}^0$,
\[
  a_{23}^0 \equiv a_3^0 - a_2^0
\]
\[
  a_{23}^0 \equiv
  \begin{bmatrix}
    l_1c_1 + l_2c_{12} + l_3c_{123}\\
    l_1s_1 + l_2s_{12} + l_3s_{123}\\
    0
  \end{bmatrix}
  -
  \begin{bmatrix}
    l_1c_1 + l_2c_{12}\\
    l_1s_1 + l_2s_{12}\\
    0
  \end{bmatrix}
\]
\[
  a_{23}^0 \equiv
  \begin{bmatrix}
    l_3c_{123}\\
    l_3s_{123}\\
    0
  \end{bmatrix}
\]
Consider the cross product of the form,
\[
  \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} \times \begin{bmatrix} a \\ b \\ 0 \end{bmatrix}
  \equiv
  \begin{bmatrix} -b \\ a \\ 0 \end{bmatrix}
\]
Finally,
\[
  z_0^0 \times a_{03}^0 \equiv
  \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} \times
  \begin{bmatrix}
    l_1c_1 + l_2c_{12} + l_3c_{123}\\
    l_1s_1 + l_2s_{12} + l_3s_{123}\\
    0
  \end{bmatrix}
  \equiv
  \begin{bmatrix}
    -(l_1s_1 + l_2s_{12} + l_3s_{123})\\
    l_1c_1 + l_2c_{12} + l_3c_{123}\\
    0
  \end{bmatrix}
\]
\[
  z_1^0 \times a_{13}^0 \equiv
  \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} \times
  \begin{bmatrix}
    l_2c_{12} + l_3c_{123}\\
    l_2s_{12} + l_3s_{123}\\
    0
  \end{bmatrix}
  \equiv
  \begin{bmatrix}
    -(l_2s_{12} + l_3s_{123})\\
    l_2c_{12} + l_3c_{123}\\
    0
  \end{bmatrix}
\]
\[
  z_2^0 \times a_{23}^0 \equiv
  \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} \times
  \begin{bmatrix}
    l_3c_{123}\\
    l_3s_{123}\\
    0
  \end{bmatrix}
  \equiv
  \begin{bmatrix}
    -(l_3s_{123})\\
    l_3c_{123}\\
    0
  \end{bmatrix}
\]
Using these to build the Jacobian,
\[
  J \equiv
  \begin{bmatrix}
    -(l_1s_1 + l_2s_{12} + l_3s_{123}) & -(l_2s_{12} + l_3s_{123}) & -(l_3s_{123})\\
    l_1c_1 + l_2c_{12} + l_3c_{123} & l_2c_{12} + l_3c_{123} & l_3c_{123}\\
    0 & 0 & 0 \\
    0 & 0 & 0 \\
    0 & 0 & 0 \\
    1 & 1 & 1 \\
  \end{bmatrix}
\]

\subsubsection*{Finding $v$ and $\omega$ of $O_c$}
The $v$ and $\omega$ of $O_c$ are not dependent on the last joint.
Consider a manipulator identical to the original one for the first one and half link.
The new manipulator will have its end-effector at $O_c$.
The required quantities can be found by repeating the same process of finding Jacobian as done for the original manipulator.

Assigning frames for the new manipulator according to DH convention we have,
\fromlectures
\begin{figure}[h]
  \centering
  \begin{tikzpicture}[x=1cm, y=1cm, z=-0.6cm]
    % F0
    \draw [->]       (0, 0) -- (2, 0) node [right] {$x_0$};
    \draw            (0, 0) circle (0.15cm);
    \draw [fill]     (0, 0) circle (0.07cm) node [left] {$z_0$};
    % F1
    \draw [->]       (4, 2) -- (5.7888, 2.8944) node [right] {$x_1$};
    \draw            (4, 2) circle (0.15cm);
    \draw [fill]     (4, 2) circle (0.07cm) node [left] {$z_1$};
    % F2
    \draw [->]       (5, 4) -- (6.414, 5.414) node [right] {$x_2$};
    \draw            (5, 4) circle (0.15cm);
    \draw [fill]     (5, 4) circle (0.07cm) node [left] {$z_2$};
  \end{tikzpicture}
  \caption{DH frame assignment}
  \label{fig:q1.3}
\end{figure}

\begin{enumerate}[nolistsep]
  \item The z-axes are chosen along the axes of rotation and out of paper for the end-effector.
  \item The choice of $x_0$ is free so it is chosen to be parallel to ground for simplicity.
  \item There is a unique common normal b/w $z_0$ and $z_1$, along which $x_1$ is chosen.
  \item $x_2$ is chosen in the same way.
\end{enumerate}

\subsubsection*{DH parameters}
The DH parameters for the frames shown in Figure \ref{fig:q1.3} are as follows
\begin{center}
\begin{tabular}{ c | c c c c }
 \hline
 $F_i \to F_j$ & $\theta$ & d & r & $\alpha$ \\
 \hline
 $0 \to 1$ & $\theta_1$ & 0 & $l_1$ & $0^{\circ}$ \\
 $1 \to 2$ & $\theta_2$ & 0 & $l_2 / 2$ & $0^{\circ}$ \\
 \hline
\end{tabular}
\end{center}

\subsubsection*{Finding transformation $T_{01}, T_{02}$}
Given frame i and frame j with DH parameters [ $\theta$, d, r, $\alpha$ ] the transformation matrix is
\[
  T_{i,j} \equiv Rot_{z,\theta} * Trans_{z, d} * Trans_{x, r} * Rot_{x, \alpha}
\]
\fromlectures
Consider $T_{01}$
\[
  T_{01} \equiv Rot_{z,\theta_1} * Trans_{z, 0} * Trans_{x, l_1} * Rot_{x, 0^{\circ}}
\]
\[
  T_{01} \equiv \rz{\theta_1} * \trans{0}{0}{0} * \trans{l_1}{0}{0} * \rx{0^{\circ}}
\]
\[
  T_{01} \equiv \rz{\theta_1} * \iden * \trans{l_1}{0}{0} * \iden
\]
\[
  T_{01} \equiv \rz{\theta_1} * \trans{l_1}{0}{0}
\]
\[
  T_{01} \equiv
  \begin{bmatrix} c_1 & -s_1 & 0 & l_1c_1 \\ s_1 & c_1 & 0 & l_1s_1 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Where $c_1 \equiv cos(\theta_1), s_1 \equiv sin(\theta_1)$.
$T_{12}$ has the same DH-parameter structure with changed variables, therefore we can use the final form of $T_{01}$ for it.
\[
  T_{12} \equiv
  \begin{bmatrix} c_2 & -s_2 & 0 & \frac{l_2}{2}c_2 \\ s_2 & c_2 & 0 & \frac{l_2}{2}s_2 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
But
\[
  T_{ij} \equiv T_{ik} * T_{kj}
\]
\fromlectures
Therefore,
\[
  T_{02} \equiv T_{01} * T_{12}
\]
\[
  T_{02} \equiv
  \begin{bmatrix} c_1 & -s_1 & 0 & l_1c_1 \\ s_1 & c_1 & 0 & l_1s_1 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix} c_2 & -s_2 & 0 & \frac{l_2}{2}c_2 \\ s_2 & c_2 & 0 & \frac{l_2}{2}s_2 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{02} \equiv
  \begin{bmatrix} c_1c_2 - s_1s_2 & -(s_1c_2 + c_1s_2) & 0 & l_1c_1 + \frac{l_2}{2}(c_1c_2 - s_1s_2) \\ s_1c_2 + c_1s_2 & c_1c_2 - s_1s_2 & 0 & l_1s_1 + \frac{l_2}{2}(s_1c_2 + c_1s_2) \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{02} \equiv
  \begin{bmatrix} c_{12} & -s_{12} & 0 & l_1c_1 + \frac{l_2}{2}c_{12} \\ s_{12} & c_{12} & 0 & l_1s_1 + \frac{l_2}{2}s_{12} \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Where $c_{12} \equiv cos(\theta_1 + \theta_2), s_{12} \equiv sin(\theta_1 + \theta_2)$.

\subsubsection*{Finding Jacobian}
For a 2R robot, the Jacobian of pose of end effector w.r.t frame 0 as a function of joint variables
\[
  J \equiv
  \begin{bmatrix} z_0^0 \times a_{02}^0 & z_1^0 \times a_{12}^0\\ z_0^0 & z_1^0 \end{bmatrix}
\]
where
\[
  a_{i2}^{0} \equiv a_{2}^{0} - a_{i}^{0}
\]
where $a_2^0$ is the origin of frame 2 in frame 0 and $a_i^0$ is origin of frame i in frame 0.
$z_i^0$ can be obtained by the first three number of third column of $T_{0i}$.
\fromlectures
\[
  z_0^{0} \equiv z_1^{0} \equiv z_2^{0} \equiv \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix}
\]
Consider $a_{2}^{0}$,
\[
  a_2^0 \equiv T_{02} * O_2
\]
\[
  a_2^0 \equiv
  \begin{bmatrix}
    c_{12} & -s_{12} & 0 & l_1c_1 + \frac{l_2}{2}c_{12}\\
    s_{12} & c_{12} & 0 & l_1s_1 + \frac{l_2}{2}s_{12} \\
    0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
  *
  \begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \end{bmatrix}
\]
\[
  a_2^0 \equiv
  \begin{bmatrix}
    l_1c_1 + \frac{l_2}{2}c_{12}\\
    l_1s_1 + \frac{l_2}{2}s_{12} \\
    0 \\
    1
  \end{bmatrix}
\]
Consider $a_{1}^{0}$,
\[
  a_1^0 \equiv T_{01} * O_1
\]
\[
  a_1^0 \equiv
  \begin{bmatrix} c_1 & -s_1 & 0 & l_1c_1 \\ s_1 & c_1 & 0 & l_1s_1 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \end{bmatrix}
\]
\[
  a_1^0 \equiv
  \begin{bmatrix}
    l_1c_1\\
    l_1s_1\\
    0 \\
    1
  \end{bmatrix}
\]
And,
\[
  a_0^0 \equiv \begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \end{bmatrix}
\]
Removing the homogeneous fourth coordinate we have,
\[
  a_0^0 \equiv
  \begin{bmatrix} 0 \\ 0 \\ 0  \end{bmatrix}
\]
\[
  a_1^0 \equiv
  \begin{bmatrix}
    l_1c_1\\
    l_1s_1\\
    0
  \end{bmatrix}
\]
\[
  a_2^0 \equiv
  \begin{bmatrix}
    l_1c_1 + \frac{l_2}{2}c_{12}\\
    l_1s_1 + \frac{l_2}{2}s_{12}\\
    0
  \end{bmatrix}
\]
Consider $a_{02}^0$,
\[
  a_{02}^0 \equiv a_2^0 - a_0^0
\]
\[
  a_{02}^0 \equiv
  \begin{bmatrix}
    l_1c_1 + \frac{l_2}{2}c_{12}\\
    l_1s_1 + \frac{l_2}{2}s_{12}\\
    0
  \end{bmatrix}
  -
  \begin{bmatrix} 0 \\ 0 \\ 0  \end{bmatrix}
\]
\[
  a_{02}^0 \equiv
  \begin{bmatrix}
    l_1c_1 + \frac{l_2}{2}c_{12}\\
    l_1s_1 + \frac{l_2}{2}s_{12}\\
    0
  \end{bmatrix}
\]
Consider $a_{12}^0$,
\[
  a_{12}^0 \equiv a_2^0 - a_1^0
\]
\[
  a_{12}^0 \equiv
  \begin{bmatrix}
    l_1c_1 + \frac{l_2}{2}c_{12}\\
    l_1s_1 + \frac{l_2}{2}s_{12}\\
    0
  \end{bmatrix}
  -
  \begin{bmatrix}
    l_1c_1\\
    l_1s_1\\
    0
  \end{bmatrix}
\]
\[
  a_{12}^0 \equiv
  \begin{bmatrix}
    \frac{l_2}{2}c_{12}\\
    \frac{l_2}{2}s_{12}\\
    0
  \end{bmatrix}
\]
Consider the cross product of the form,
\[
  \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} \times \begin{bmatrix} a \\ b \\ 0 \end{bmatrix}
  \equiv
  \begin{bmatrix} -b \\ a \\ 0 \end{bmatrix}
\]
Finally,
\[
  z_0^0 \times a_{02}^0 \equiv
  \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} \times
  \begin{bmatrix}
    l_1c_1 + \frac{l_2}{2}c_{12}\\
    l_1s_1 + \frac{l_2}{2}s_{12}\\
    0
  \end{bmatrix}
  \equiv
  \begin{bmatrix}
    -(l_1s_1 + \frac{l_2}{2}s_{12})\\
    l_1c_1 + \frac{l_2}{2}c_{12}\\
    0
  \end{bmatrix}
\]
\[
  z_1^0 \times a_{12}^0 \equiv
  \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} \times
  \begin{bmatrix}
    \frac{l_2}{2}c_{12}\\
    \frac{l_2}{2}s_{12}\\
    0
  \end{bmatrix}
  \equiv
  \begin{bmatrix}
    -(\frac{l_2}{2}s_{12})\\
    \frac{l_2}{2}c_{12}\\
    0
  \end{bmatrix}
\]
Using these to build the Jacobian,
\[
  J \equiv
  \begin{bmatrix}
    -(l_1s_1 + \frac{l_2}{2}s_{12}) & -(\frac{l_2}{2}s_{12})\\
    l_1c_1 + \frac{l_2}{2}c_{12} & \frac{l_2}{2}c_{12}\\
    0 & 0 \\
    0 & 0 \\
    0 & 0 \\
    1 & 1 \\
  \end{bmatrix}
\]
Finally,
\[
  \begin{bmatrix} v \\ \omega \end{bmatrix}
  \equiv
  J *
  \begin{bmatrix} \frac{d\theta_1}{dt} \\ \frac{d\theta_2}{dt} \end{bmatrix}
\]
\[
  \begin{bmatrix} v \\ \omega \end{bmatrix}
  \equiv
  \begin{bmatrix}
    -(l_1s_1 + \frac{l_2}{2}s_{12}) & -(\frac{l_2}{2}s_{12})\\
    l_1c_1 + \frac{l_2}{2}c_{12} & \frac{l_2}{2}c_{12}\\
    0 & 0 \\
    0 & 0 \\
    0 & 0 \\
    1 & 1 \\
  \end{bmatrix}
  *
  \begin{bmatrix} \frac{d\theta_1}{dt} \\ \frac{d\theta_2}{dt} \end{bmatrix}
\]
\[
  \begin{bmatrix} v \\ \omega \end{bmatrix}
  \equiv
  \begin{bmatrix}
    -(l_1s_1 + \frac{l_2}{2}s_{12}) * \frac{d\theta_1}{dt} - (\frac{l_2}{2}s_{12}) * \frac{d\theta_2}{dt} \\
    l_1c_1 + \frac{l_2}{2}c_{12} * \frac{d\theta_1}{dt} + \frac{l_2}{2}c_{12} * \frac{d\theta_2}{dt}\\
    0 \\
    0 \\
    0 \\
    \frac{d\theta_1}{dt} + \frac{d\theta_2}{dt}\\
  \end{bmatrix}
\]
Therefore,
\[
  v
  \equiv
  \begin{bmatrix}
    -(l_1s_1 + \frac{l_2}{2}s_{12}) * \frac{d\theta_1}{dt} - \frac{l_2}{2}s_{12} * \frac{d\theta_2}{dt} \\
    (l_1c_1 + \frac{l_2}{2}c_{12}) * \frac{d\theta_1}{dt} + \frac{l_2}{2}c_{12} * \frac{d\theta_2}{dt}\\
    0 \\
  \end{bmatrix},
  \omega
  \equiv
  \begin{bmatrix}
    0 \\
    0 \\
    \frac{d\theta_1}{dt} + \frac{d\theta_2}{dt}\\
  \end{bmatrix}
\]

\pagebreak

\section{}
A three link three joint robot with link lengths $l_1, l_2, l_3$ respectively is considered.
\given
\begin{figure}[h]
  \centering
  \includegraphics[width=0.6\textwidth]{q2.png}
  \caption{Three link three joint robot}
  \label{fig:q2.1}
\end{figure}
\begin{figure}[h]
  \centering
  \begin{tikzpicture}[x=1cm, y=1cm, z=-0.6cm]
    \draw [dashed, red]       (-3, 0) -- (-3, 3) node [right] {$l_1$};
    \draw [dashed, red]       (0, 5) -- (3, 5) node [right] {$l_2$};
    \draw [dashed, red]       (3, 4) -- (6, 4) node [right] {$l_3$};
    % F0
    \draw [->]       (0, 0) -- (0, 1) node [right] {$z_0$};
    \draw            (0, 0) circle (0.15cm);
    \draw [fill]     (0, 0) circle (0.07cm) node [left] {$x_0$};
    % F1
    \draw [->]       (0, 3) -- (1, 3) node [right] {$x_1$};
    \draw            (0, 3) circle (0.15cm);
    \draw [fill]     (0, 3) circle (0.07cm) node [left] {$z_1$};
    % F2
    \draw [->]       (3, 3) -- (4, 3) node [right] {$x_2$};
    \draw            (3, 3) circle (0.15cm);
    \draw [fill]     (3, 3) circle (0.07cm) node [left] {$z_2$};
    % F3
    \draw [->]       (6, 3) -- (7, 3) node [right] {$x_3$};
    \draw            (6, 3) circle (0.15cm);
    \draw [fill]     (6, 3) circle (0.07cm) node [left] {$z_3$};
  \end{tikzpicture}
  \caption{DH frame assignment}
  \label{fig:q2.2}
\end{figure}

\subsubsection*{DH parameters}
The DH parameters for the frames shown in Figure \ref{fig:q2.2} are as follows
\begin{center}
\begin{tabular}{ c | c c c c }
 \hline
 $F_i \to F_j$ & $\theta$ & d & r & $\alpha$ \\
 \hline
 $0 \to 1$ & $\theta_1$ & $l_1$ & $ 0 $ & $90^{\circ}$ \\
 $1 \to 2$ & $\theta_2$ & 0     & $l_2$ & $0^{\circ}$ \\
 $2 \to 3$ & $\theta_3$ & 0     & $l_3$ & $0^{\circ}$ \\
 \hline
\end{tabular}
\end{center}

\subsubsection*{Finding transformation $T_{01}, T_{02}, T_{03}$}
Given frame i and frame j with DH parameters [ $\theta$, d, r, $\alpha$ ] the transformation matrix is
\[
  T_{i,j} \equiv Rot_{z,\theta} * Trans_{z, d} * Trans_{x, r} * Rot_{x, \alpha}
\]
\fromlectures
Consider $T_{01}$
\[
  T_{01} \equiv Rot_{z,\theta_1} * Trans_{z, l_1} * Trans_{x, 0} * Rot_{x, 90^{\circ}}
\]
\[
  T_{01} \equiv Rot_{z,\theta_1} * Trans_{z, l_1} * \iden * Rot_{x, 90^{\circ}}
\]
\[
  T_{01} \equiv Rot_{z,\theta_1} * Trans_{z, l_1} * Rot_{x, 90^{\circ}}
\]
\[
  T_{01} \equiv \rz{\theta_1} * \trans{0}{0}{l_1} * \rx{90^{\circ}}
\]
\[
  T_{01} \equiv
  \begin{bmatrix}
    c_1 & -s_1 & 0 & 0 \\
    s_1 & c_1 & 0 & 0 \\
    0 & 0 & 1 & l_1 \\
    0 & 0 & 0 & 1
  \end{bmatrix} * \rx{90^{\circ}}
\]
where $c_i \equiv cos(\theta_i), s_i \equiv sin(\theta_i)$.
\[
  T_{01} \equiv
  \begin{bmatrix}
    c_1 & 0 & s_1 & 0 \\
    s_1 & 0 & -c_1 & 0 \\
    0 & 1 & 0 & l_1 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
Consider $T_{12}$
\[
  T_{12} \equiv Rot_{z,\theta_2} * Trans_{z, 0} * Trans_{x, l_2} * Rot_{x, 0^{\circ}}
\]
\[
  T_{12} \equiv \rz{\theta_2} * \iden * \trans{l_2}{0}{0} * \rx{0^{\circ}}
\]
\[
  T_{12} \equiv \rz{\theta_2} * \iden * \trans{l_2}{0}{0} * \iden
\]
\[
  T_{12} \equiv \rz{\theta_2} * \trans{l_2}{0}{0}
\]
\[
  T_{12} \equiv
  \begin{bmatrix}
    c_2 & -s_2 & 0 & l_2c_2 \\
    s_2 & c_2 & 0 & l_2s_2 \\
    0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
$T_{23}$ has same DH structure as $T_{12}$, therefore we can use the final form of the latter. Therefore,
\[
  T_{23} \equiv
  \begin{bmatrix}
    c_3 & -s_3 & 0 & l_3c_3 \\
    s_3 & c_3 & 0 & l_3s_3 \\
    0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
But
\[
  T_{ij} \equiv T_{ik} * T_{kj}
\]
\fromlectures
Therefore,
\[
  T_{02} \equiv T_{01} * T_{12}
\]
\[
  T_{02} \equiv
  \begin{bmatrix}
    c_1 & 0 & s_1 & 0 \\
    s_1 & 0 & -c_1 & 0 \\
    0 & 1 & 0 & l_1 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
  *
  \begin{bmatrix}
    c_2 & -s_2 & 0 & l_2c_2 \\
    s_2 & c_2 & 0 & l_2s_2 \\
    0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
\[
  T_{02} \equiv
  \begin{bmatrix}
    c_1c_2 & -c_1s_2 & s_1 & l_2c_1c_2 \\
    s_1c_2 & -s_1s_2 & -c_1 & l_2s_1c_2 \\
    s_2 & c_2 & 0 & l_2s_2 + l_1 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
\[
  T_{03} \equiv T_{02} * T_{23}
\]
\[
  T_{03} \equiv
  \begin{bmatrix}
    c_1c_2 & -c_1s_2 & s_1 & l_2c_1c_2 \\
    s_1c_2 & -s_1s_2 & -c_1 & l_2s_1c_2 \\
    s_2 & c_2 & 0 & l_2s_2 + l_1 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
  *
  \begin{bmatrix}
    c_3 & -s_3 & 0 & l_3c_3 \\
    s_3 & c_3 & 0 & l_3s_3 \\
    0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
\[
  T_{03} \equiv
  \begin{bmatrix}
    c_1(c_2c_3 - s_2s_3) & -c_1(s_2c_3 + c_2s_3) & s_1  & c_1l_2c_2 + c_1l_3(c_2c_3 - s_2s_3) \\
    s_2(c_2c_3 - s_2s_3) & -s_1(s_2c_3 + c_2s_3) & -c_1 & s_1l_2c_2 + s_1l_3(c_2c_3 - s_2s_3) \\
    s_2c_3 + c_2s_3      & c_2c_3 - s_2s_3       & 0    & l_2s_2 + l_3(s_2c_3 + c_2s_3) + l_1 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
\[
  T_{03} \equiv
  \begin{bmatrix}
    c_1c_{23} & -c_1s_{23} & s_1  & l_2c_1c_2 + l_3c_1c_{23} \\
    s_1c_{23} & -s_1s_{23} & -c_1 & l_2s_1c_2 + l_3s_1c_{23} \\
    s_{23}    & c_{23}     & 0    & l_1 + l_2s_2 + l_3s_{23} \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]

\subsubsection*{Finding Jacobian}
For a 3R robot, the Jacobian of pose of end effector w.r.t frame 0 as a function of joint variables
\[
  J \equiv
  \begin{bmatrix} z_0^0 \times a_{03}^0 & z_1^0 \times a_{13}^0 & z_2^0 \times a_{23}^0 \\ z_0^0 & z_1^0 & z_2^0 \end{bmatrix}
\]
where
\[
  a_{i3}^{0} \equiv a_{3}^{0} - a_{i}^{0}
\]
where $a_3^0$ is the origin of frame 3 in frame 0 and $a_i^0$ is origin of frame i in frame 0.
$z_i^0$ can be obtained by the first three number of third column of $T_{0i}$.
\fromlectures
\[
  z_0^{0} \equiv \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix}
\]
\[
  z_1^{0} \equiv z_2^{0} \equiv z_3^{0} \equiv \begin{bmatrix} s_1 \\ -c_1 \\ 0 \end{bmatrix}
\]
Consider $a_{3}^{0}$,
\[
  a_3^0 \equiv T_{03} * O_3
\]
\[
  a_3^0 \equiv
  \begin{bmatrix}
    c_1c_{23} & -c_1s_{23} & s_1  & l_2c_1c_2 + l_3c_1c_{23} \\
    s_1c_{23} & -s_1s_{23} & -c_1 & l_2s_1c_2 + l_3s_1c_{23} \\
    s_{23}    & c_{23}     & 0    & l_1 + l_2s_2 + l_3s_{23} \\
    0 & 0 & 0 & 1
  \end{bmatrix}
  *
  \begin{bmatrix}
    0 \\
    0 \\
    0 \\
    1
  \end{bmatrix}
\]
\[
  a_3^0 \equiv
  \begin{bmatrix}
    l_2c_1c_2 + l_3c_1c_{23} \\
    l_2s_1c_2 + l_3s_1c_{23} \\
    l_1 + l_2s_2 + l_3s_{23} \\
    1
  \end{bmatrix}
\]
Consider $a_{2}^{0}$,
\[
  a_2^0 \equiv T_{02} * O_2
\]
\[
  a_2^0 \equiv
  \begin{bmatrix}
    c_1c_2 & -c_1s_2 & s_1 & l_2c_1c_2 \\
    s_1c_2 & -s_1s_2 & -c_1 & l_2s_1c_2 \\
    s_2 & c_2 & 0 & l_2s_2 + l_1 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
  *
  \begin{bmatrix}
    0 \\
    0 \\
    0 \\
    1
  \end{bmatrix}
\]
\[
  a_2^0 \equiv
  \begin{bmatrix}
    l_2c_1c_2 \\
    l_2s_1c_2 \\
    l_2s_2 + l_1 \\
    1
  \end{bmatrix}
\]
Consider $a_{1}^{0}$,
\[
  a_1^0 \equiv T_{01} * O_1
\]
\[
  a_1^0 \equiv
  \begin{bmatrix}
    c_1 & 0 & s_1 & 0 \\
    s_1 & 0 & -c_1 & 0 \\
    0 & 1 & 0 & l_1 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
  *
  \begin{bmatrix}
    0 \\
    0 \\
    0 \\
    1
  \end{bmatrix}
\]
\[
  a_1^0 \equiv
  \begin{bmatrix}
    0 \\
    0 \\
    l_1 \\
    1
  \end{bmatrix}
\]
Removing homogeneous coordinate we have,
\[
  a_3^0 \equiv
  \begin{bmatrix}
    l_2c_1c_2 + l_3c_1c_{23} \\
    l_2s_1c_2 + l_3s_1c_{23} \\
    l_1 + l_2s_2 + l_3s_{23}
  \end{bmatrix}
  a_2^0 \equiv
  \begin{bmatrix}
    l_2c_1c_2 \\
    l_2s_1c_2 \\
    l_2s_2 + l_1
  \end{bmatrix}
  a_1^0 \equiv
  \begin{bmatrix}
    0 \\
    0 \\
    l_1
  \end{bmatrix}
  a_0^0 \equiv
  \begin{bmatrix}
    0 \\
    0 \\
    0
  \end{bmatrix}
\]
Consider $a_{03}^0$,
\[
  a_{03}^0 \equiv a_3^0 - a_0^0
\]
\[
  a_{03}^0 \equiv
  \begin{bmatrix}
    l_2c_1c_2 + l_3c_1c_{23} \\
    l_2s_1c_2 + l_3s_1c_{23} \\
    l_1 + l_2s_2 + l_3s_{23}
  \end{bmatrix}
  -
  \begin{bmatrix}
    0 \\
    0 \\
    0
  \end{bmatrix}
\]
\[
  a_{03}^0 \equiv
  \begin{bmatrix}
    l_2c_1c_2 + l_3c_1c_{23} \\
    l_2s_1c_2 + l_3s_1c_{23} \\
    l_1 + l_2s_2 + l_3s_{23}
  \end{bmatrix}
\]
Consider $a_{13}^0$,
\[
  a_{13}^0 \equiv a_3^0 - a_1^0
\]
\[
  a_{13}^0 \equiv
  \begin{bmatrix}
    l_2c_1c_2 + l_3c_1c_{23} \\
    l_2s_1c_2 + l_3s_1c_{23} \\
    l_1 + l_2s_2 + l_3s_{23}
  \end{bmatrix}
  -
  \begin{bmatrix}
    0 \\
    0 \\
    l_1
  \end{bmatrix}
\]
\[
  a_{13}^0 \equiv
  \begin{bmatrix}
    l_2c_1c_2 + l_3c_1c_{23} \\
    l_2s_1c_2 + l_3s_1c_{23} \\
    l_2s_2 + l_3s_{23}
  \end{bmatrix}
\]
Consider $a_{23}^0$,
\[
  a_{23}^0 \equiv a_3^0 - a_2^0
\]
\[
  a_{23}^0 \equiv
  \begin{bmatrix}
    l_2c_1c_2 + l_3c_1c_{23} \\
    l_2s_1c_2 + l_3s_1c_{23} \\
    l_1 + l_2s_2 + l_3s_{23}
  \end{bmatrix}
  -
  \begin{bmatrix}
    l_2c_1c_2 \\
    l_2s_1c_2 \\
    l_2s_2 + l_1
  \end{bmatrix}
\]
\[
  a_{23}^0 \equiv
  \begin{bmatrix}
    l_3c_1c_{23} \\
    l_3s_1c_{23} \\
    l_3s_{23}
  \end{bmatrix}
\]
The first 3x3 block of J is
\[
  J_{11} \equiv
  \begin{bmatrix} z_0^0 \times a_{03}^0 & z_1^0 \times a_{13}^0 & z_2^0 \times a_{23}^0\end{bmatrix}
\]
\[
  J_{11} \equiv
  \begin{bmatrix}
    \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix}
    \times
    \begin{bmatrix}
      l_2c_1c_2 + l_3c_1c_{23} \\
      l_2s_1c_2 + l_3s_1c_{23} \\
      l_1 + l_2s_2 + l_3s_{23}
    \end{bmatrix}
    &
    \begin{bmatrix} s_1 \\ -c_1 \\ 0 \end{bmatrix}
    \times
    \begin{bmatrix}
      l_2c_1c_2 + l_3c_1c_{23} \\
      l_2s_1c_2 + l_3s_1c_{23} \\
      l_2s_2 + l_3s_{23}
    \end{bmatrix}
    &
    \begin{bmatrix} s_1 \\ -c_1 \\ 0 \end{bmatrix}
    \times
    \begin{bmatrix}
      l_3c_1c_{23} \\
      l_3s_1c_{23} \\
      l_3s_{23}
    \end{bmatrix}
  \end{bmatrix}
\]
Consider the cross product of the form,
\[
  \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} \times \begin{bmatrix} a \\ b \\ 0 \end{bmatrix}
  \equiv
  \begin{bmatrix} -b \\ a \\ 0 \end{bmatrix}
\]
Therefore simplifying first column,
\[
  J_{11} \equiv
  \begin{bmatrix}
    \begin{bmatrix}
      -l_2s_1c_2 - l_3s_1c_{23} \\
      l_2c_1c_2 + l_3c_1c_{23} \\
      0
    \end{bmatrix}
    &
    \begin{bmatrix} s_1 \\ -c_1 \\ 0 \end{bmatrix}
    \times
    \begin{bmatrix}
      l_2c_1c_2 + l_3c_1c_{23} \\
      l_2s_1c_2 + l_3s_1c_{23} \\
      l_2s_2 + l_3s_{23}
    \end{bmatrix}
    &
    \begin{bmatrix} s_1 \\ -c_1 \\ 0 \end{bmatrix}
    \times
    \begin{bmatrix}
      l_3c_1c_{23} \\
      l_3s_1c_{23} \\
      l_3s_{23}
    \end{bmatrix}
  \end{bmatrix}
\]
Simplifying second and third columns,
\[
  J_{11} \equiv
  \begin{bmatrix}
    \begin{bmatrix}
      -l_2s_1c_2 - l_3s_1c_{23} \\
      l_2c_1c_2 + l_3c_1c_{23} \\
      0
    \end{bmatrix}
    &
    \begin{bmatrix}
      -l_2c_1s_2 - l_3c_1s_{23} \\
      -l_2s_1s_2 - l_3s_1s_{23} \\
      l_2c_2 + l_3c_{23} (s_1^2 + c_1^2)
    \end{bmatrix}
    &
    \begin{bmatrix}
      -l_3c_1s_{23}\\
      -l_3s_1s_{23}\\
      (c_1^2 + s_1^2)l_3c_{23}
    \end{bmatrix}
  \end{bmatrix}
\]
But
\[
  c_1^2 + s_1^2 \equiv 1
\]
\[
  J_{11} \equiv
  \begin{bmatrix}
    \begin{bmatrix}
      -l_2s_1c_2 - l_3s_1c_{23} \\
      l_2c_1c_2 + l_3c_1c_{23} \\
      0
    \end{bmatrix}
    &
    \begin{bmatrix}
      -l_2c_1s_2 - l_3c_1s_{23} \\
      -l_2s_1s_2 - l_3s_1s_{23} \\
      l_2c_2 + l_3c_{23}
    \end{bmatrix}
    &
    \begin{bmatrix}
      -l_3c_1s_{23}\\
      -l_3s_1s_{23}\\
      l_3c_{23}
    \end{bmatrix}
  \end{bmatrix}
\]
\[
  J_{11} \equiv
    \begin{bmatrix}
      -l_2s_1c_2 - l_3s_1c_{23} & -l_2c_1s_2 - l_3c_1s_{23} & -l_3c_1s_{23}\\
      l_2c_1c_2 + l_3c_1c_{23}  & -l_2s_1s_2 - l_3s_1s_{23} & -l_3s_1s_{23}\\
      0                         & l_2c_2 + l_3c_{23}        & l_3c_{23}
    \end{bmatrix}
\]
Hence the required relation is proved (I used $l$'s to represent lengths instead of $a$'s).

\subsubsection*{If $\theta_3 = 0^{\circ}$}
If $\theta_3 = 0^{\circ}$, then $c_{23} = c_2, s_{23} = s_2$. Therefore,
\[
  J_{11} \equiv
    \begin{bmatrix}
      -l_2s_1c_2 - l_3s_1c_{2} & -l_2c_1s_2 - l_3c_1s_{2} & -l_3c_1s_{2}\\
      l_2c_1c_2 + l_3c_1c_{2}  & -l_2s_1s_2 - l_3s_1s_{2} & -l_3s_1s_{2}\\
      0                         & l_2c_2 + l_3c_{2}        & l_3c_{2}
    \end{bmatrix}
\]
Taking common terms out,
\[
  J_{11} \equiv
    \begin{bmatrix}
      -(l_2 + l_3)s_1c_2 & (l_2 + l_3)(-c_1s_2) & l_3(-c_1s_{2})\\
      (l_2 + l_3)c_1c_2  & (l_2 + l_3)(-s_1s_2) & l_3(-s_1s_{2})\\
      0                  & (l_2 + l_3)c_2        & l_3c_{2}
    \end{bmatrix}
\]
Observe the second and third columns are linearly dependent (non-zero multiples of $[-c_1s_2, -s_1s_2, c_2]^T$). When two columns of a matrix are linearly dependent then its determinant $\equiv 0$ or the matrix loses rank. Therefore $J_{11}$ loses rank i.e. $\theta_3 = 0$ describes a family of singular configurations for $J_{11}$.

\pagebreak

\section{}
Two frames $F_0$ and $F_1$ are related by transformation H i.e.
\[
  p^0 \equiv H * p^1
\]
\[
  H \equiv
  \begin{bmatrix} 0 & -1 & 0 & 1 \\ 1 & 0 & 0 & -1 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\given
Let
\[
  p^0 \equiv (x^0, y^0, z^0), p^1 \equiv (x^1, y^1, z^1)
\]
Then
\[
  \begin{bmatrix} x^0 \\ y^0 \\ z^0 \\ 1\end{bmatrix} \equiv
  \begin{bmatrix} 0 & -1 & 0 & 1 \\ 1 & 0 & 0 & -1 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix} x^1 \\ y^1 \\ z^1 \\ 1\end{bmatrix}
\]
\[
  \begin{bmatrix} x^0 \\ y^0 \\ z^0 \\ 1\end{bmatrix} \equiv
  \begin{bmatrix} -y^1 + 1 \\ x^1 - 1 \\ z^1 \\ 1\end{bmatrix}
\]
Therefore
\[
  \begin{bmatrix} x^0 \\ y^0 \\ z^0\end{bmatrix} \equiv
  \begin{bmatrix} -y^1 + 1 \\ x^1 - 1 \\ z^1\end{bmatrix}
\]
Taking derivative w.r.t. time on both sides
\[
  \frac{d}{dt}\begin{bmatrix} x^0 \\ y^0 \\ z^0\end{bmatrix} \equiv
  \frac{d}{dt}\begin{bmatrix} -y^1 + 1 \\ x^1 - 1 \\ z^1\end{bmatrix}
\]
As $\frac{dx^0}{dt} \equiv v_x^0$ and so on...,
\[
  \begin{bmatrix} v_x^0 \\ v_y^0 \\ v_z^0\end{bmatrix} \equiv
  \begin{bmatrix} -v_y^1 + 0 \\ v_x^1 - 0 \\ v_z^1\end{bmatrix}
\]
\[
  \begin{bmatrix} v_x^0 \\ v_y^0 \\ v_z^0\end{bmatrix} \equiv
  \begin{bmatrix} -v_y^1 \\ v_x^1 \\ v_z^1\end{bmatrix}
\]
Given $v^1(t) = (3, 1, 0)$, using the above identity we have
\[
  \begin{bmatrix} v_x^0 \\ v_y^0 \\ v_z^0\end{bmatrix} \equiv
  \begin{bmatrix} -1 \\ 3 \\ 0\end{bmatrix}
\]
Therefore the velocity w.r.t frame 0 $v^0(t) = (-1, 3, 0) \frac{m}{s}$

\pagebreak

\section{}
A three link cylindrical robot is considered.
\given

\begin{figure}[h]
  \centering
  \includegraphics[width=0.47\textwidth]{q4.png}
  \caption{Three link three joint robot}
  \label{fig:q4.1}
\end{figure}
It's frame assignment is shown in Figure \ref{fig:q4.2}. Let the length of link 1 be $l_1$ and the joint variables be $q_1, q_2, q_3$ respectively.
\begin{figure}[h]
  \centering
  \begin{tikzpicture}[x=1cm, y=1cm, z=-0.6cm]
    \draw [dashed, red]       (-3, 0) -- (-3, 3) node [right] {$l_1$};
    \draw [dashed, red]       (-2, 3) -- (-2, 6) node [right] {$q_2$};
    \draw [dashed, red]       (0, 5) -- (3, 5) node [right] {$q_3$};
    % F0
    \draw [->]       (0, 0) -- (0, 1) node [right] {$z_0$};
    \draw            (0, 0) circle (0.15cm);
    \draw [fill]     (0, 0) circle (0.07cm) node [left] {$x_0$};
    % F1
    \draw [->]       (0, 3) -- (0, 4) node [right] {$z_1$};
    \draw            (0, 3) circle (0.15cm);
    \draw [fill]     (0, 3) circle (0.07cm) node [left] {$x_1$};
    % F2
    \draw [->]       (0, 6) -- (1, 6) node [right] {$z_2$};
    \draw            (0, 6) circle (0.15cm);
    \draw [fill]     (0, 6) circle (0.07cm) node [left] {$x_2$};
    % F3
    \draw [->]       (3, 6) -- (4, 6) node [right] {$z_3$};
    \draw            (3, 6) circle (0.15cm);
    \draw [fill]     (3, 6) circle (0.07cm) node [left] {$x_3$};
  \end{tikzpicture}
  \caption{DH frame assignment}
  \label{fig:q4.2}
\end{figure}

\subsubsection*{DH parameters}
The DH parameters for the frames shown in Figure \ref{fig:q4.2} are as follows
\begin{center}
\begin{tabular}{ c | c c c c }
 \hline
 $F_i \to F_j$ & $\theta$ & d & r & $\alpha$ \\
 \hline
 $0 \to 1$ & $q_1$ & $l_1$ & $0$ & $0^{\circ}$ \\
 $1 \to 2$ & $0$   & $q_2$ & $0$ & $-90^{\circ}$ \\
 $2 \to 3$ & $0$   & $q_3$ & $0$ & $0^{\circ}$ \\
 \hline
\end{tabular}
\end{center}

\subsubsection*{Finding transformation $T_{01}, T_{02}, T_{03}$}
Given frame i and frame j with DH parameters [ $\theta$, d, r, $\alpha$ ] the transformation matrix is
\[
  T_{i,j} \equiv Rot_{z,\theta} * Trans_{z, d} * Trans_{x, r} * Rot_{x, \alpha}
\]
\fromlectures
Consider $T_{01}$
\[
  T_{01} \equiv Rot_{z,q_1} * Trans_{z, l_1} * Trans_{x, 0} * Rot_{x, 0^{\circ}}
\]
\[
  T_{01} \equiv \rz{q_1} * \trans{0}{0}{l_1} * \trans{0}{0}{0} * \rx{0^{\circ}}
\]
\[
  T_{01} \equiv \rz{q_1} * \trans{0}{0}{l_1} * \iden * \iden
\]
\[
  T_{01} \equiv \rz{q_1} * \trans{0}{0}{l_1}
\]
\[
  T_{01} \equiv
  \begin{bmatrix}
    cq_1 & -sq_1 & 0 & 0 \\
    sq_1 & cq_1 & 0 & 0 \\
    0 & 0 & 1 & l_1 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
Where $cq_1 \equiv cos(q_1), sq_1 \equiv sin(q_1)$.
Consider $T_{12}$
\[
  T_{12} \equiv Rot_{z, 0} * Trans_{z, q_2} * Trans_{x, 0} * Rot_{x, -90^{\circ}}
\]
\[
  T_{12} \equiv \rz{0} * \trans{0}{0}{q_2} * \trans{0}{0}{0} * \rx{-90^{\circ}}
\]
\[
  T_{12} \equiv \iden * \trans{0}{0}{q_2} * \trans{0}{0}{0} * \rx{-90^{\circ}}
\]
\[
  T_{12} \equiv \trans{0}{0}{q_2} * \rx{-90^{\circ}}
\]
\[
  T_{12} \equiv \trans{0}{0}{q_2}
  *
  \begin{bmatrix}
    1 & 0 & 0 & 0 \\
    0 & 0 & 1 & 0 \\
    0 & -1 & 0 & 0 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
\[
  T_{12} \equiv
  \begin{bmatrix}
    1 & 0 & 0 & 0 \\
    0 & 0 & 1 & 0 \\
    0 & -1 & 0 & q_2 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
Consider $T_{23}$
\[
  T_{23} \equiv Rot_{z, 0} * Trans_{z, q_3} * Trans_{x, 0} * Rot_{x, 0^{\circ}}
\]
\[
  T_{23} \equiv \rz{0} * \trans{0}{0}{q_3} * \trans{0}{0}{0} * \rx{0^{\circ}}
\]
\[
  T_{23} \equiv \iden * \trans{0}{0}{q_3} * \trans{0}{0}{0} * \iden
\]
\[
  T_{23} \equiv \trans{0}{0}{q_3}
\]
But
\[
  T_{ij} \equiv T_{ik} * T_{kj}
\]
\fromlectures
Therefore,
\[
  T_{02} \equiv T_{01} * T_{12}
\]
\[
  T_{02} \equiv
  \begin{bmatrix}
    cq_1 & -sq_1 & 0 & 0 \\
    sq_1 & cq_1 & 0 & 0 \\
    0 & 0 & 1 & l_1 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
  *
  \begin{bmatrix}
    1 & 0 & 0 & 0 \\
    0 & 0 & 1 & 0 \\
    0 & -1 & 0 & q_2 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
\[
  T_{02} \equiv
  \begin{bmatrix}
    cq_1 & 0 & -sq_1 & 0 \\
    sq_1 & 0 & cq_1 & 0 \\
    0 & -1 & 0 & l_1 + q_2 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
\[
  T_{03} \equiv T_{02} * T_{23}
\]
\[
  T_{03} \equiv
  \begin{bmatrix}
    cq_1 & 0 & -sq_1 & 0 \\
    sq_1 & 0 & cq_1 & 0 \\
    0 & -1 & 0 & l_1 + q_2 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
  *
  \trans{0}{0}{q_3}
\]
\[
  T_{03} \equiv
  \begin{bmatrix}
    cq_1 & 0 & -sq_1 & -q_3sq_1 \\
    sq_1 & 0 &  cq_1 &  q_3cq_1 \\
    0 & -1 & 0 & l_1 + q_2 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]

\subsubsection*{Finding Jacobian}
For an RPP robot, the Jacobian of pose of end effector w.r.t frame 0 as a function of joint variables
\[
  J \equiv
  \begin{bmatrix}
    z_0^0 \times a_{03}^0 & z_1^0 & z_2^0 \\
    z_0^0 & 0 & 0
  \end{bmatrix}
\]
where
\[
  a_{i3}^{0} \equiv a_{3}^{0} - a_{i}^{0}
\]
where $a_3^0$ is the origin of frame 3 in frame 0 and $a_i^0$ is origin of frame i in frame 0.
$z_i^0$ can be obtained by the first three number of third column of $T_{0i}$.
\fromlectures
\[
  z_0^0 \equiv z_1^0 \equiv \begin{bmatrix}
    0 \\
    0 \\
    1
  \end{bmatrix}
\]
\[
  z_2^0 \equiv
  \begin{bmatrix}
    -sq_1 \\
     cq_1 \\
     0
  \end{bmatrix}
\]
Consider $a_3^0$,
\[
  a_3^0 \equiv T_{03} * O_3
\]
\[
  a_3^0 \equiv
  \begin{bmatrix}
    cq_1 & 0 & -sq_1 & -q_3sq_1 \\
    sq_1 & 0 &  cq_1 &  q_3cq_1 \\
    0 & -1 & 0 & l_1 + q_2 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
  *
  \begin{bmatrix}
    0 \\
    0 \\
    0 \\
    1
  \end{bmatrix}
\]
\[
  a_3^0 \equiv
  \begin{bmatrix}
    -q_3sq_1 \\
    q_3cq_1 \\
    l_1 + q_2 \\
    1
  \end{bmatrix}
\]
Removing the homogeneous coordinate,
\[
  a_3^0 \equiv
  \begin{bmatrix}
    -q_3sq_1 \\
    q_3cq_1 \\
    l_1 + q_2
  \end{bmatrix}
\]
Consider $a_0^0$,
\[
  a_0^0 \equiv \begin{bmatrix}
    0 \\
    0 \\
    0
  \end{bmatrix}
\]
Therefore,
\[
  a_{03}^0 \equiv
  \begin{bmatrix}
    -q_3sq_1 \\
    q_3cq_1 \\
    l_1 + q_2
  \end{bmatrix}
\]
Consider
\[
  z_0^0 \times a_{03}^0 \equiv
  \begin{bmatrix}
    0 \\
    0 \\
    1
  \end{bmatrix}
  \times
  \begin{bmatrix}
    -q_3sq_1 \\
    q_3cq_1 \\
    l_1 + q_2
  \end{bmatrix}
  \equiv
  \begin{bmatrix}
    -q_3cq_1 \\
    -q_3sq_1 \\
    0
  \end{bmatrix}
\]
Using these quantities to build the Jacobian,
\[
  J \equiv
  \begin{bmatrix}
    z_0^0 \times a_{03}^0 & z_1^0 & z_2^0 \\
    z_0^0 & 0 & 0
  \end{bmatrix}
\]
\[
  J \equiv
  \begin{bmatrix}
    -q_3cq_1 & 0 & -sq_1 \\
    -q_3sq_1 & 0 &  cq_1 \\
    0        & 1 &  0 \\
    0 & 0 & 0 \\
    0 & 0 & 0 \\
    1 & 0 & 0
  \end{bmatrix}
\]

\subsubsection*{Inspecting for singularities}
Consider $J_\omega$,
\[
  J_\omega \equiv
  \begin{bmatrix}
    0 & 0 & 0 \\
    0 & 0 & 0 \\
    1 & 0 & 0
  \end{bmatrix}
\]
$J_\omega$ does not depend on configuration therefore it cannot lose rank with configuration.
Consider $J_v$,
\[
  J_v \equiv
  \begin{bmatrix}
    -q_3cq_1 & 0 & -sq_1 \\
    -q_3sq_1 & 0 &  cq_1 \\
    0        & 1 &  0
  \end{bmatrix}
\]
Consider its determinant (using the third row),
\[
  det(J_v) \equiv
  -(-q_3cq_1^2 - q_3sq_1^2)
\]
\[
  det(J_v) \equiv
  q_3(cq_1^2 + sq_1^2)
\]
But $cos(\theta)^2 + sin(\theta)^2 \equiv 1, \forall \theta \in R$ therefore,
\[
  det(J_v) \equiv q_3
\]
As (the length of third link) $q_3 > 0$ then determinant$(J_v) > 0 \implies$ $J_v$ does not lose rank for any configuration.

Combining above two results, there are no configurations where $J$ loses rank i.e. there are no singular configurations for the Jacobian $J$.
Hence proved.

\end{document}


\chapter{Einfügen von Quellcode}
\label{chap:code}

\section{Beispiel für einen Programmcode}

\if 1\mintedinstall
\subsection{Beispiel Minted}
	\begin{minted}[xleftmargin=20pt,linenos]{python}
	import numpy as np

	def incmatrix(genl1,genl2):
		m = len(genl1)
		n = len(genl2)
		M = None #to become the incidence matrix
		VT = np.zeros((n*m,1), int)  #dummy variable

		#compute the bitwise xor matrix
		M1 = bitxormatrix(genl1)
		M2 = np.triu(bitxormatrix(genl2),1)

		for i in range(m-1):
			for j in range(i+1, m):
				[r,c] = np.where(M2 == M1[i,j])
				for k in range(len(r)):
					VT[(i)*n + r[k]] = 1;
					VT[(i)*n + c[k]] = 1;
					VT[(j)*n + r[k]] = 1;
					VT[(j)*n + c[k]] = 1;

					if M is None:
						M = np.copy(VT)
					else:
						M = np.concatenate((M, VT), 1)

					VT = np.zeros((n*m,1), int)

		return M
	\end{minted}
\fi

\subsection{Beispiel listings}
\begin{lstlisting}[language=Python]
import numpy as np

def incmatrix(genl1,genl2):
	m = len(genl1)
	n = len(genl2)
	M = None #to become the incidence matrix
	VT = np.zeros((n*m,1), int)  #dummy variable

	#compute the bitwise xor matrix
	M1 = bitxormatrix(genl1)
	M2 = np.triu(bitxormatrix(genl2),1)

	for i in range(m-1):
		for j in range(i+1, m):
			[r,c] = np.where(M2 == M1[i,j])
			for k in range(len(r)):
				VT[(i)*n + r[k]] = 1;
				VT[(i)*n + c[k]] = 1;
				VT[(j)*n + r[k]] = 1;
				VT[(j)*n + c[k]] = 1;

				if M is None:
					M = np.copy(VT)
				else:
					M = np.concatenate((M, VT), 1)

				VT = np.zeros((n*m,1), int)

	return M
\end{lstlisting}


\documentclass[12pt, a4paper]{article}
\usepackage[margin=1.25in]{geometry}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{float}
\usepackage{listings}
\usepackage{caption}
\usepackage{physics}
\usepackage{mathrsfs}
\usepackage[shortlabels]{enumitem}

\setlength\parindent{0pt}
\newcommand{\code}{\lstinline[basicstyle=\small]}
\lstset{
    language=Python,
    basicstyle=\scriptsize
}

\title{EE2703: Applied Programming Lab \\ \Large Assignment 8: The Digital Fourier Transform}
\author{Soham Roy \\ \normalsize EE20B130}
\date{\today}
\begin{document}

\maketitle % Insert the title, author and date

\section{Introduction}
The goal of this assignment is to analyse signals in the frequency domain. The Discrete Fourier Transform (DFT)
is a powerful tool for this purpose, and has been calculated using the Fast Fourier Transform (FFT) Algorithm.
The Continuous Time Fourier Transform (CTFT) of a gaussian has also been approximated using the FFT. For this,
we use the \code{numpy} library.

\section{Subquestions}

\subsection{Work through the Examples}

\subsubsection{Random Data}
The Fourier Transform of a random signal has been evaluated, and then the Inverse Fourier Transform of
the result is evaluated. The maximum absolute error of the result and the original signal varies, but usually
has an order of magnitude of -16.
\begin{lstlisting}
    x = np.random.rand(100)
    X = fft(x)
    y = ifft(X)
    np.c_[x, y]
    print("Maximum Absolute Error for Random Data: ", np.abs(x - y).max())
\end{lstlisting}

\subsubsection{Spectrum of $\sin(5t)$}
We begin with the following rudimentary code:
\begin{lstlisting}
    x = np.linspace(0, 2 * np.pi, 128)
    y = np.sin(5 * x)
    Y = fft(y)

    plt.figure()
    plt.subplot(2, 1, 1)
    plt.title("Spectrum of $\sin(5t)$ without Phase Wrapping")
    plt.ylabel("$|Y|$")
    plt.plot(np.abs(Y), lw=2)
    plt.grid(True)

    plt.subplot(2, 1, 2)
    plt.xlabel("$\omega$")
    plt.ylabel("Phase of $Y$")
    plt.plot(np.unwrap(np.angle(Y)), lw=2)
    plt.grid(True)
\end{lstlisting}
To obtain:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.8]{eg1.png}
\end{figure}
The frequencies we sample must exclude $2\pi$, as it is equivalent to 0. The phase plot must also be shifted
to go from $-\pi$ to $\pi$. For this, a helper function \code{plotter()} has also been written.
\begin{lstlisting}
    def plotter(w, Y, title, lim, out, xlabel, ylabels=("$|Y|$", r"$\angle Y$")):
        plt.figure(figsize=(8, 10))
        plt.subplot(2, 1, 1)
        plt.title(title, size=16)
        plt.ylabel(ylabels[0], size=14)
        plt.plot(w, abs(Y), lw=2)
        plt.xlim(-lim, lim)
        plt.grid(True)

        plt.subplot(2, 1, 2)
        plt.xlabel(xlabel, size=14)
        plt.ylabel(ylabels[1], size=14)
        ii = np.where(abs(Y) > 1e-3)
        plt.plot(w[ii], np.unwrap(np.angle(Y[ii])), "go", lw=2)
        plt.xlim(-lim, lim)
        plt.grid(True)

        plt.savefig("Assignment_08/LaTeX/" + out)

    x = np.linspace(0, 2 * np.pi, 128, endpoint=False)
    w = np.linspace(-64, 64, 128, endpoint=False)

    Y = fftshift(fft(np.sin(5 * x))) / 128
    plotter(w, Y, "Spectrum of $\sin(5t)$", 10, "eg2", "$k$")
\end{lstlisting}
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{eg2.png}
\end{figure}

\subsubsection{Spectrum of Amplitude Modulated Wave}
The signal to be considered is:
\begin{equation*}
    f(t) = (1 + 0.1\cos(t))\cos(10t)
\end{equation*}
The same helper function has been used as such:
\begin{lstlisting}
    Y = fftshift(fft((1 + 0.1 * np.cos(x)) * np.cos(10 * x))) / 128
    plotter(w, Y, "Spectrum of $(1 + 0.1\cos(t))\cdot\cos(10t)$", 15, "eg3")
\end{lstlisting}
This gives us:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{eg3.png}
\end{figure}
The number of samples used is not enough to resolve all the peaks, hence the time vector and the number of
samples have both been increased such that the sampling frequency remains constant.
\begin{lstlisting}
    x = np.linspace(-4 * np.pi, 4 * np.pi, 512, endpoint=False)
    w = np.linspace(-64, 64, 512, endpoint=False)

    Y = fftshift(fft((1 + 0.1 * np.cos(x)) * np.cos(10 * x))) / 512
    plotter(w, Y, "Spectrum of $(1 + 0.1\cos(t))\cdot\cos(10t)$", 15, "eg4")
\end{lstlisting}
Thus, we obtain:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{eg4.png}
\end{figure}

\subsection{Generate the Spectrum of $\sin^3(t)$ and $\cos^3(t)$}
\subsubsection{$\sin^3(t)$}
$\sin^3(t)$ can be expressed as a sum of sine waves as:
\begin{equation*}
    \sin^3(t) = \frac{3}{4}\sin(t) - \frac{1}{4}\sin(3t)
\end{equation*}
Thus, 2 peaks are expected: at 1 and 3, with phases being of magnitude $= \frac{\pi}{2}$.
\begin{lstlisting}
    Y = fftshift(fft(np.sin(x) ** 3)) / 512
    plotter(w, Y, "Spectrum of $\sin^3(t)$", 15, "q2a")
\end{lstlisting}
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{q2a.png}
\end{figure}

\subsubsection{$\cos^3(t)$}
$\cos^3(t)$ can be expressed as a sum of cosine waves as:
\begin{equation*}
    \cos^3(t) = \frac{3}{4}\cos(t) + \frac{1}{4}\cos(3t)
\end{equation*}
Thus, 2 peaks are expected: at 1 and 3, with phases $= 0$.
\begin{lstlisting}
    Y = fftshift(fft(np.cos(x) ** 3)) / 512
    plotter(w, Y, "Spectrum of $\cos^3(t)$", 15, "q2b")
\end{lstlisting}
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{q2b.png}
\end{figure}

\subsection{Frequency Modulated Wave: $\cos(20t + 5\cos(t))$}
The same helper function is invoked as follows:
\begin{lstlisting}
    Y = fftshift(fft(np.cos(20 * x + 5 * np.cos(x)))) / 512
    plotter(w, Y, "Spectrum of $\cos(20t + 5\cos(t))$", 30, "q3")
\end{lstlisting}
This gives us:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{q3.png}
\end{figure}
It can be seen that many more peaks are present, and that single peaks no longer carry a majority of the energy.
Thus, the signal is phase modulated.

\subsection{The Gaussian}
We know that the Fourier Transform of a signal is defined as:
\begin{equation*}
    X(\omega) = \frac{1}{2\pi} \int_{-\infty}^{\infty} x(t) e^{-j\omega t} dt
\end{equation*}
We also know that a Gaussian tends to 0 for large magnitudes of $t$. Thus, for some window size $T$,
we can approximate its Transform as:
\begin{equation*}
    X(\omega) = \frac{1}{2\pi} \int_{-T/2}^{T/2} x(t) e^{-j\omega t} dt
\end{equation*}
Approximating the integral to a Reimann summation of $N$ terms, we get:
\begin{equation*}
    X(\omega) \approx \frac{T}{2\pi N} \sum_{n = -N/2}^{N/2 - 1} x(nT/N) e^{-j\omega nT / N}
\end{equation*}
Where $T / N$ is the time step. Then, let $\omega = 2\pi k / T$:
\begin{equation*}
    X(2\pi k / T) \approx \frac{T}{2\pi N} \sum_{n = -N/2}^{N/2 - 1} x(nT/N) e^{-j2\pi kn/N}
\end{equation*}
We observe that the summation is the Discrete Fourier Transform (DFT) of the signal. Thus, we get:
\begin{equation*}
    X(2\pi k / T) \approx \frac{T}{2\pi N} DFT\{x(nT/N)\}
\end{equation*}
We can improve the accuracy of our obtained approximation by choosing a larger window size while keeping the
sampling frequency constant. We do this iteratively until our error is below a certain threshold.

We compare our approximation to the actual Continuous Time Fourier Transform (CTFT) of the signal:
\begin{equation*}
    \mathscr{F}(e^{-\frac{t^2}{2}}) = \frac{1}{\sqrt{2\pi}}e^{-\frac{\omega^2}{2}}
\end{equation*}

For this, we use the following code:
\begin{lstlisting}
    T = 2 * np.pi
    N = 128
    iter_n = 0
    error = None
    threshold = 1e-6  # 6 decimals of precision

    while error is None or error > threshold:
        t = np.linspace(-T / 2, T / 2, N, endpoint=False)
        w = np.linspace(-np.pi, np.pi, N, endpoint=False) * N / T
        y = np.exp(-0.5 * t ** 2)
        Y = fftshift(fft(ifftshift(y))) * T / (2 * np.pi * N)

        Y_true = np.exp(-0.5 * w ** 2) / np.sqrt(2 * np.pi)
        error = np.sum(np.abs(Y - Y_true))

        T *= 2
        N *= 2
        iter_n += 1
        print(f"Iteration {iter_n}:   Total Error = {error:.2e}")

    T /= 2
    N /= 2

    print(f"Samples = {int(N)},   Time Period = {int(T / np.pi)} pi")

    plotter(w, Y, "Spectrum of Approximated Gaussian", 5, "q4a")
    plotter(w, Y_true, "Spectrum of True Gaussian", 5, "q4b")
\end{lstlisting}
This gives us the graphs:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{q4a.png}
\end{figure}
Here, we also have the following output:
\begin{lstlisting}
    Iteration 1:   Total Error = 7.19e-03
    Iteration 2:   Total Error = 5.35e-09
    Samples = 256,   Time Period = 4 pi
\end{lstlisting}
Thus, we can say that our approximation converged to an accuracy of 6 decimals very fast.
\pagebreak

To compare, the Continuous Time Fourier Transform results in the following graph:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{q4b.png}
\end{figure}

\section{Conclusion}
We have calculated the DFT of various signals using the FFT Algorithm. We started with a random signal,
then sinusoids, combinations of sinusoids, and finally a Gaussian.

\end{document}


\section{Methods}
\label{sec:methods}
To test our hypothesis, we constructed a simple, two-layer recurrent neural network (based on Aungiers, 20184). For data, we used publically available pricing data from the AWS spot market. Effectiveness was measured by how well our network could predict the next spot price.
We developed two workflows, one as a baseline and one as a test case (illustrated to the right). The RNN was constructed using Keras with a Tensorflow back-end and the workflows were completed using this implementation within a Python notebook. Each workflow was run 10 times in order to ensure accuracy and reproducibility.
The baseline workflow simply trains our neural network from N different initialization and then choses the trained model with the lowest loss after K epochs. This is effectively the same as retraining a number of times to find a trained model with the best parametrization.
Our experimental workflow begins in the same manner as the baseline workflow. However, during the training (for example, after K/2 epochs), optimization is halted and only the top T networks are selected. We then merge the weights of the two networks using some predefined function, an illustration of which is pictured to the right. We then re-spawn N networks with slight random mutations between them and continue training until we have reached K epochs.

\begin{figure}[h!]
\begin{subfigure}[h]{0.49\linewidth}
\includegraphics[width=\linewidth]{figures/simple.png}
\caption{Control, no cross-over.}
\end{subfigure}
\hfill
\begin{subfigure}[h]{0.49\linewidth}
\includegraphics[width=\linewidth]{figures/workflow.png}
\caption{Test, with cross-over.}
\end{subfigure}%
\caption{Generalized control and test workflows used to evaluate parameter merging.}
\end{figure}

\begin{figure}[h!]
\includegraphics[width=\linewidth]{figures/merge.png}
\caption{Illustration of merging network weights}
\end{figure}