\documentclass[main.tex]{subfiles}
\begin{document}
\chapter{Functions and Relations}

\epigraph{They don't think it be like it is, but it do.}{Oscar Gamble}

\minitoc

\section{Introduction}

Sometimes in life you will have two (or more) things that are somehow related. Sometimes people make these relations for you. Sometimes you will be given two things and you have to come up with the relation. Our goal in this section is to give you the tools to solve these problems life throws at you. And remember, when life gets you down, just chuck some lemons back at him.

This chapter begins studying relations, then builds up to define functions.

\section{Relations}

% Relations are a formal way for us to map elements together in a similar fashion to functions. Unlike functions, however, a relation simply gives the \textit{relationship}. Sometimes you can construct a function that yields the relation. Other times, a function that would represent the relation would not be a function by definition.

Relations are a formal way for us to map elements together -- although this is not apparent at first. You may already be familiar with functions, \textit{mappings}, from high-school mathematics courses. We will build to functions using relations.

Relations simply give us a \textit{relation}ship between elements. We begin with a formal definition, then dive into relation classification.

\begin{defn}[Relation\index{Relation}]
	For sets \(A\) and \(B\), a relation \(R\) is any subset of the cross product of \(A\) and \(B\): \[R \subseteq \{(a,b) \in A \times B\}\]

	To specify that two elements are related under a relation \(R\) we say \(xRy\) which means \((x,y) \in R\)
\end{defn}

Here are a few examples:

\begin{example}
	An \textit{equality} relation \(\{(a,b) \mid a = b\} \subseteq \R \times \R\). This is a set of pairs where, for any given pair both elements in the pair are equal.
\end{example}

\begin{example}
	A \textit{less-than} relation \(\{(a,b) \mid a < b\} \subseteq \N \times \N\). This is a set of pairs where, for any given pair the first element is strictly less-than the second element.
\end{example}

\begin{example}
	The following is a relation, with \(A = \{2,4\}\) and \(B = \{6,8,10\}\): \(R = \{(x,y) \in A \times B \mid \frac{y}{x} \in \Z\} = \{(2,6), (2,10), (4,8)\}\)
\end{example}

\begin{rem}
	We will only consider relations of sets with themselves. We \textit{can} define relations between different sets, however this will make the following definitions significantly more challenging to define.
\end{rem}

\begin{defn}[Reflexive\index{Relation!Reflexive}]
	A relation \(R\) on a set \(A\) is reflexive if \[(\forall a \in A)[(a,a) \in R]\]
\end{defn}

\begin{defn}[Symmetric\index{Relation!Symmetric}]
	A relation \(R\) on a set \(A\) is symmetric if \[(\forall a,b \in A)[(a,b) \in R \Rightarrow (b,a) \in R]\]
\end{defn}

\begin{defn}[Transitive\index{Relation!Transitive}]
	A relation \(R\) on a set \(A\) is transitive if \[(\forall a,b,c \in A)[(a,b) \in R \land (b,c) \in R \Rightarrow (a,c) \in R]\]
\end{defn}

You will typically be asked to classify whether a relation satisfies the above three properties.

\exsol{
	Classify the following:

	\begin{center}
		\begin{tabular}{c|c|c|c}
			Relation & refl.\ & symm.\ & trans.\ \\
			\hline
			\(\{(1,1), (1,2), (2,1)\} \subseteq \{1,2\}^2\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
			\(\{(1,1), (1,2), (2,2)\} \subseteq \{1,2\}^2\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
			\(\{(x,y) \in \N \times \N \mid \frac{y}{x} \in \Z\}\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
			\(\{(x,y) \in \R^{\geq 1} \times \R^{\geq 1} \mid x < y^2\}\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
			\(\{(x,y) \in \R \times \R \mid (x=y) \lor (x=-y)\}\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
		\end{tabular}
	\end{center}
}{
	\(\blacklozenge\) indicates YES, \(\lozenge\) indicates NO

	\begin{center}
		\begin{tabular}{c|c|c|c}
			Relation & refl.\ & symm.\ & trans.\ \\
			\hline
			\(\{(1,1), (1,2), (2,1)\} \subseteq \{1,2\}^2\) & \(\lozenge\) & \(\blacklozenge\) & \(\lozenge\) \\
			\(\{(1,1), (1,2), (2,2)\} \subseteq \{1,2\}^2\) & \(\blacklozenge\) & \(\lozenge\) & \(\blacklozenge\) \\
			\(\{(x,y) \in \N \times \N \mid \frac{y}{x} \in \Z\}\) & \(\lozenge\) & \(\lozenge\) & \(\blacklozenge\) \\
			\(\{(x,y) \in \R^{\geq 1} \times \R^{\geq 1} \mid x < y^2\}\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
			\(\{(x,y) \in \R \times \R \mid (x=y) \lor (x=-y)\}\) & \(\blacklozenge\) & \(\blacklozenge\) & \(\blacklozenge\) \\
		\end{tabular}
	\end{center}
}

Oftentimes in computer science and mathematics we care about relations that satisfy all three properties. We even give these relations a fancy name, \textit{equality relation}.

\begin{defn}[Equivalence Relation\index{Relation!Equivalence Relation}]
	A relation that is reflexive, symmetric, and transitive
\end{defn}

The equivalence relation gives us a notion of \textit{equality}. If a relation \(R\) is an equivalence relation, then if \(xRy\) then we can think of \(x\) being ``equal'' to \(y\). The equals sign \(=\) gives us an equivalence relation on sets of numbers. In linear algebra, an isomorphism \(\cong\) is an equivalence relation on vector spaces. The mod operator \(\equiv\) gives us an equivalence relation on \(\Z\). As a real-life application, modular arithmetic is used in cryptography!

Here are some other cool definitions that probably will not be tested.

\begin{defn}[Antisymmetric\index{Relation!Antisymmetric}]
	A relation \(R\) on a set \(A\) is antisymmetric if \[(\forall a,b \in A)[(a,b) \in R \land (b,a) \in R \Rightarrow a = b]\]
\end{defn}

\begin{defn}[Composite of a Relation]
	If \(R \subseteq A \times B\) and \(S \subseteq B \times C\) are both relations, then the \textit{composite} of \(R\) and \(S\) is \[S \circ R = \{(a,c) \mid a \in A \land c \in C \land (b \in B \Rightarrow (a,b) \in R \land (b,c) \in S)\}\]
\end{defn}

\begin{defn}[Power of a Relation]
	If \(R \subseteq A \times A\) then for \(n \in \Z^+\) the \textit{power} \(R^n = R^{n-1} \circ R\) where \(R^1 = R\)
\end{defn}

\begin{thm}
	\label{relation-transitive-theorem}
	\(R \subseteq A \times A\) is transitive iff \(R^n \subseteq R\)
\end{thm}

The proof of this theorem is left as an exercise.

We have been discussing binary relations -- relations between two sets -- however we can define the notion of an \(n\)-ary relation. An \(n\)-ary relation is a subset of the cross product of \(n\) sets.

\begin{example}
	Here is a trinary (or \textit{ternary}) relation: \[R = \{(1,a,*), (2,a,+), (1,b,+)\} \subseteq \{1,2,3\} \times \{a,b,c,d\} \times \{*,+\}\]
\end{example}

The notions of reflexivity, symmetry, and transitivity also abstract to \(n\)-ary relations, as does an equivalence relation. This is outside the scope of this course.

\section{Functions}

Now that we understand relations, we can define a function.

\begin{defn}[Function\index{Function}]
	A relation \(R \subseteq C \times D\) that satisfies the following properties:
	\begin{enumerate}
		\item \((\forall c \in C)(\exists d \in D)[(c,d) \in R]\)
		\item \((\forall c \in C)(\forall d_1,d_2 \in D)[((c,d_1) \in R \land (c,d_2) \in R) \Rightarrow d_1 = d_2]\)
	\end{enumerate}

	If we recall the \textit{exists unique} symbol \(\exists!\), then we can combine the two properties above into one:
	\begin{enumerate}
		\item \((\forall c \in C)(\exists! d \in D)[(c,d) \in R]\)
	\end{enumerate}
\end{defn}

You may be familiar with the following less-formal definition.

\begin{defn}[Function\index{Function}]
	An explicit rule, or set of rules, that can be applied to a specified input to yield a specified output. There are two rules that all functions must follow:
	\begin{enumerate}
		\item Every input must be mapped to an output
		\item No input can map to more than one output
	\end{enumerate}
\end{defn}

Basically, functions give us a \textit{mapping} between sets -- a relation! For any relation \(R \subseteq C \times D\), we let the first set \(C\) be the function \textit{input} and the second set \(D\) be the function \textit{output}. If a pairing \((c,d) \in R\), then the function representing the relation \(R\) means that the function input \(c\) yields the output \(d\).

\exsol{
	Determine whether the following relations are functions.

	\begin{center}
		\begin{tabular}{c|c|c}
			Relation & is a func.\ & is not a func.\ \\
			\hline
			\(\{(x,y) \in \R \times \R^{\geq 0} \mid (x=y) \lor (x=-y)\}\) & \(\vartriangle\) & \(\vartriangle\) \\
			\(\{(x,y) \in \N \times \N \mid \frac{y}{x} \in \Z\}\) & \(\vartriangle\) & \(\vartriangle\) \\
			\(\{(x,y) \in \N \times \{1\} \mid \frac{x}{y} \in \Z\}\) & \(\vartriangle\) & \(\vartriangle\) \\
			\(\{(x,y) \in \N \times \{1\} \mid x < y\}\) & \(\vartriangle\) & \(\vartriangle\) \\
			\(\{(x,y) \in \N \times \{0\} \mid x \geq y\}\) & \(\vartriangle\) & \(\vartriangle\) \\
		\end{tabular}
	\end{center}
}{
	\(\blacktriangle\) indicates YES, \(\vartriangle\) indicates NO

	\begin{center}
		\begin{tabular}{c|c|c}
			Relation & is a func.\ & is not a func.\ \\
			\hline
			\(\{(x,y) \in \R \times \R^{\geq 0} \mid (x=y) \lor (x=-y)\}\) & \(\blacktriangle\) & \(\vartriangle\) \\
			\(\{(x,y) \in \N \times \N \mid \frac{y}{x} \in \Z\}\) & \(\vartriangle\) & \(\blacktriangle\) \\
			\(\{(x,y) \in \N \times \{1\} \mid \frac{x}{y} \in \Z\}\) & \(\blacktriangle\) & \(\vartriangle\) \\
			\(\{(x,y) \in \N \times \{1\} \mid x < y\}\) & \(\vartriangle\) & \(\blacktriangle\) \\
			\(\{(x,y) \in \N \times \{0\} \mid x \geq y\}\) & \(\blacktriangle\) & \(\vartriangle\) \\
		\end{tabular}
	\end{center}
}

Every function is a relation, but not every relation is a function. For any function, you can always write out the \((x,f(x))\) pairings -- this is by definition a relation. On the other hand, the above examples show that not every relation is a function.

The above relations are contrived examples. Typically we define functions on sets of numbers (we will restrict it to subsets of \(\R\)). We use the following notation to denote a function \(f\): \[f(x) = \text{some rules}\] and we pair the input/output sets as follows: \[f : D \rightarrow R\]

We might also notate functions by saying \(f : D \rightarrow R\) given by \(f : d \mapsto r\).

\begin{defn}[Domain\index{Function!Domain}]
	The set of inputs to a function
\end{defn}

\begin{defn}[Range\index{Function!Range}]
	The set containing all outputs to a function. This may also be called the \textit{co-domain}
\end{defn}

\begin{defn}[Image\index{Function!Image}]
	The set of all outputs to a function. This may be a subset of the function's range
\end{defn}

Let's look at some basic examples. First are some graphs of functions. Hopefully you learned how to plot 2-dimensional functions in previous math classes.

\begin{example}
	Here is a graph of the function \(f(x) = x\) where \(f : \R \rightarrow \R\)

	\begin{center}
		\begin{tikzpicture}
		% axes
		%\draw[latex-] (-4.5,0) -- (4.5,0) ;
		\draw[] (-3.5,0) -- (3.5,0) ;
		%\draw[latex-] (0,-4.5) -- (0,4.5) ;
		\draw[] (0,-3.5) -- (0,3.5) ;
		% ticks
		\foreach \x in  {-3,-2,-1,1,2,3} % x-axis
			\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);
		\foreach \x in  {-3,-2,-1,1,2,3} % y-axis
			\draw[shift={(0,\x)},color=black] (3pt,0pt) -- (-3pt,0pt);

		% func
		\draw[latex-,color=red] (-3,-3) -- (3,3) ;
		\draw[-latex,color=red] (-3,-3) -- (3,3) ;
		\end{tikzpicture}
	\end{center}
\end{example}

\begin{example}
	Here is a graph of the function \[f(x) = \begin{cases} x, & x > 2 \\ 1, & -2 \leq x \leq 2 \\ -x, & x < -2 \end{cases}\] where \(f : \R \rightarrow \R\)

	\begin{center}
		\begin{tikzpicture}
		% axes
		%\draw[latex-] (-4.5,0) -- (4.5,0) ;
		\draw[] (-3.5,0) -- (3.5,0) ;
		%\draw[latex-] (0,-4.5) -- (0,4.5) ;
		\draw[] (0,-1.5) -- (0,3.5) ;
		% ticks
		\foreach \x in  {-3,-2,-1,1,2,3} % x-axis
		\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);
		\foreach \x in  {-1,1,2,3} % y-axis
		\draw[shift={(0,\x)},color=black] (3pt,0pt) -- (-3pt,0pt);

		% func
		\draw[-latex,color=red] (2.05,2.05) -- (3,3) ;
		\draw[color=red] (-2,1) -- (2,1) ;
		\draw[latex-,color=red] (-3,3) -- (-2.05,2.05) ;
		\draw[color=red] (2,2) circle[radius=2pt];
		\fill[color=red] (2,1) circle[radius=2pt];
		\fill[color=red] (-2,1) circle[radius=2pt];
		\draw[color=red] (-2,2) circle[radius=2pt];
		\end{tikzpicture}
	\end{center}
\end{example}

Next we present examples of classifying whether a function is a function. To do this, recall the two requirements:
\begin{enumerate}
	\item Every input must be mapped to an output
	\item No input can map to more than one output
\end{enumerate}
You should check both of these requirements. For the first, recall that to dis-prove a universal statement you need a single counter-example. Thus, search the domain and make sure each input maps to an output

\exsol{
	Which of the following are functions? For the non-functions, change them such that they become functions.

	\begin{enumerate}[label=(\alph*)]
		\item \(a(x) = x\) where \(a : \Z \rightarrow \R\)
		\item \(b(x) = \sqrt{x}\) where \(b : \Z \rightarrow \R\)
		\item \(c(y) = \log_2(y)\) where \(c : \N \rightarrow \Z\)
		\item \(d(y) = y^2\) where \(d : \Z \rightarrow \N\)
		\item \(e(z) = \begin{cases} z + 1 & z \geq 0 \\ z - 1 & z \leq 0 \end{cases}\) where \(e : \R \rightarrow \Z\)
	\end{enumerate}
}{
	\begin{enumerate}[label=(\alph*)]
		\item Function
		\item Not a function -- \(b\) does not work for any negative input, however the domain is set to \(\Z\). Simply change the domain to \(\N\)
		\item Not a function -- \(c(3) \not\in \Z\) thus not every input is mapped to an output. Change the co-domain to positive real numbers
		\item Function
		\item Not a function -- (1) the input \(z = 0\) has two different outputs \(e(0) = 1\) and \(e(0) = -1\), (2) any real number that is not an integer does not have a mapping in the range (ex: \(e(1.5) = 1.5 + 1 = 2.5 \not\in \Z\)). Change one of the cases to not include 0, then either change the range to \(\R\) or change the domain to \(\Z\)
	\end{enumerate}
}

There exist 3 properties that any function can have.

\begin{defn}[Injection (One-to-One)\index{Function!Injective}]
	A function where each co-domain (range) value is mapped to by at most one value in the domain. In other words, each value in the range has either 0 or 1 pointers from the domain. Formally, \(f : D \mapsto R\) is injective iff \[(\forall x_1,x_2 \in D)[f(x_1) = f(x_2) \Rightarrow x_1 = x_2]\]
	The horizontal line test can be used to see if a function is injective
\end{defn}

\begin{defn}[Surjection (Onto)\index{Function!Surjective}]
	A function where each co-domain (range) value is mapped to by at least one value in the domain. In other words, there are no un-mapped values in the range. Formally, \(f : D \mapsto R\) is surjective iff \[(\forall y \in R)(\exists x \in D)[y = f(x)]\]
\end{defn}

\begin{defn}[Bijection (One-to-One Correspondence)\index{Function!Bijective}]
	A function that is both an injection and a surjection. Every input is mapped to exactly one unique output
\end{defn}

You should know how to identify these properties in functions. To test injectivity, perform the horizontal line test -- run a horizontal line vertically along the graph of the function; the function is injective if the horizontal line never touches more than one point. To test surjectivity, ask yourself if you can ``get to'' every output element. To be bijective, you must both be injective and surjective. A function can be neither injective nor surjective, but still be a function.

We can represent a function by drawing ovals representing the domain and range, drawing points in each oval representing the elements in each set, and drawing arrows between the points representing the function mapping. In this way, we present a visual guide to the above definitions.

\begin{figure}[H]
	\centering
	\begin{tabular}{ c l | l }
		& Injection & Not Injection \\
		\rotatebox[origin=l]{90}{Surjection} &%
		\begin{tikzpicture}
		\draw (0,0) ellipse (1cm and 2cm);
		\draw (3,0) ellipse (1cm and 2cm);
		\fill (0,1) circle[radius=2pt];
		\fill (0,0) circle[radius=2pt];
		\fill (0,-1) circle[radius=2pt];
		\fill (3,1) circle[radius=2pt];
		\fill (3,0) circle[radius=2pt];
		\fill (3,-1) circle[radius=2pt];
		\draw[-latex,color=red] (0.1,1) -- (2.9,1) ;
		\draw[-latex,color=red] (0.1,0) -- (2.9,0) ;
		\draw[-latex,color=red] (0.1,-1) -- (2.9,-1) ;
		\end{tikzpicture}%
		&%
		\begin{tikzpicture}
		\draw (0,0) ellipse (1cm and 2cm);
		\draw (3,0) ellipse (1cm and 2cm);
		\fill (0,1) circle[radius=2pt];
		\fill (0,0) circle[radius=2pt];
		\fill (0,-1) circle[radius=2pt];
		\fill (3,1) circle[radius=2pt];
		\fill (3,0) circle[radius=2pt];
		\draw[-latex,color=red] (0.1,1) -- (2.9,1) ;
		\draw[-latex,color=red] (0.1,0) -- (2.9,0) ;
		\draw[-latex,color=red] (0.1,-1) -- (2.9,0) ;
		\end{tikzpicture}%
		\\
		\midrule
		\rotatebox[origin=l]{90}{Not Surjection} &%
		\begin{tikzpicture}
		\draw (0,0) ellipse (1cm and 2cm);
		\draw (3,0) ellipse (1cm and 2cm);
		\fill (0,1) circle[radius=2pt];
		\fill (0,0) circle[radius=2pt];
		\fill (3,1) circle[radius=2pt];
		\fill (3,0) circle[radius=2pt];
		\fill (3,-1) circle[radius=2pt];
		\draw[-latex,color=red] (0.1,1) -- (2.9,1) ;
		\draw[-latex,color=red] (0.1,0) -- (2.9,0) ;
		\end{tikzpicture}%
		&%
		\begin{tikzpicture}
		\draw (0,0) ellipse (1cm and 2cm);
		\draw (3,0) ellipse (1cm and 2cm);
		\fill (0,1) circle[radius=2pt];
		\fill (0,0) circle[radius=2pt];
		\fill (0,-1) circle[radius=2pt];
		\fill (3,1) circle[radius=2pt];
		\fill (3,0) circle[radius=2pt];
		\fill (3,-1) circle[radius=2pt];
		\draw[-latex,color=red] (0.1,1) -- (2.9,1) ;
		\draw[-latex,color=red] (0.1,0) -- (2.9,0) ;
		\draw[-latex,color=red] (0.1,-1) -- (2.9,0) ;
		\end{tikzpicture}%
		\\
	\end{tabular}
	\caption{Injection and Surjection}
\end{figure}

\exsol{
	Classify the following:

	\begin{center}
	\begin{tabular}{lc|c|c|c|c}
		& & func.\ & inj.\ & surj.\ & bij.\ \\
		\hline
		\(f(x) = x\) & \(f : \N \rightarrow \Z\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
		\(g(x) = x\) & \(g : \Z \rightarrow \Z\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
		\(h(x) = x^2\) & \(h : \R \rightarrow \N\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
		\(i(x) = \floor{x^2}\) & \(i : \R \rightarrow \N\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
		\(j(x) = x^3-x\) & \(j : \Z \rightarrow \R\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
	\end{tabular}
	\end{center}
}{
	\(\blacklozenge\) indicates YES, \(\lozenge\) indicates NO

	\begin{center}
	\begin{tabular}{lc|c|c|c|c}
		& & func.\ & inj.\ & surj.\ & bij.\ \\
		\hline
		\(f(x) = x\) & \(f : \N \rightarrow \Z\) & \(\blacklozenge\) & \(\blacklozenge\) & \(\lozenge\) & \(\lozenge\) \\
		\(g(x) = x\) & \(g : \Z \rightarrow \Z\) & \(\blacklozenge\) & \(\blacklozenge\) & \(\blacklozenge\) & \(\blacklozenge\) \\
		\(h(x) = x^2\) & \(h : \R \rightarrow \N\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
		\(i(x) = \floor{x^2}\) & \(i : \R \rightarrow \N\) & \(\blacklozenge\) & \(\lozenge\) & \(\blacklozenge\) & \(\lozenge\) \\
		\(j(x) = x^3-x\) & \(j : \Z \rightarrow \R\) & \(\blacklozenge\) & \(\lozenge\) & \(\lozenge\) & \(\lozenge\) \\
	\end{tabular}
	\end{center}
}

Hash tables are an important application of functions in computer science. A hash table is a data structure; it is a fancy array. A hash table has an internal array, and a hash function. This hash function maps table elements (some set of objects) to indices (\(\N\)). The modulus of the hash function output, with respect to the table size, is taken as the input object's key. This is where the object is stored in the table. Ideally, you want your hash function to be an injection on the set of indices.

\begin{thm}
	A bijection \(f : A \rightarrow B\) is invertible, and the inverse, denoted \(f^{-1} : B \rightarrow A\), is also a bijection.
\end{thm}

\begin{proof}
	First note that \textit{inverse} simply means that \(f^{-1}(f(a)) = a\). With this in mind, then we note that \(f^{-1}(b) = a\) such that \(f(a) = b\). Then since \(f\) is a bijection, \(b\) is the one-and-only output that \(a\) maps to. Then \(a\) is the one-and-only output that \(b\) maps to in \(f^{-1}\). So it is a bijection.

	We can show this more formally by showing \(f^{-1}\) is an injection and a surjection.

	\textbf{1-1}: Let \(f^{-1}(b_1) = f^{-1}(b_2)\), then \(f(f^{-1}(b_1)) = f(f^{-1}(b_2)) \Rightarrow b_1 = b_2\).

	\textbf{onto}: Let \(f^{-1}(b) = a\), then \(b = f(a)\) which exists because \(f\) is a bijection.

	You should prove on your own that indeed \(f^{-1}\) is a function!
\end{proof}

\begin{thm}
	The composition of bijective functions is a bijection.
\end{thm}

\begin{proof}
	Let \(f,g\) be the bijections \(f : A \mapsto B\) and \(g : B \mapsto C\). Then by the previous theorem \(f,g\) have inverses which are also bijections.

	Define \(h : A \mapsto C\) given by \(h : a \mapsto g(f(a)) = (g \circ f)(a)\).

	\textbf{1-1}: Let \(h(a_1) = h(a_2)\), then \(g(f(a_1)) = g(f(a_2)) \Rightarrow f^{-1}(g^{-1}(g(f(a_1)))) = f^{-1}(g^{-1}(g(f(a_2)))) \Rightarrow a_1 = a_2\)

	\textbf{onto}: Let \(h(a) = c\), then \(g(f(a)) = c\) so \(a = f^{-1}(g^{-1}(c))\) exists.
\end{proof}

\section{Sequences}

Sequences make up an interesting sub-part of functions. In calculus, we build up continuous functions as limits of sequences. This is discrete mathematics, however, so sequences will be of more interest to us.

\begin{defn}[Sequence\index{Sequence}]
	An ordered list of numbers, each one associated with a specific \textit{position} (index)
\end{defn}

For the purposes of this class, you can think of a sequence as a mapping between the natural numbers and the associated index in the sequence. Sometimes you can express a sequence as a closed-form function, and sometimes a sequence can have multiple interpretations. Let's look at a few examples to get the notation down.

\begin{example}
	The following three notations describe the same sequence:

	\begin{enumerate}[label=(\alph*)]
		\item \(1, 3, 7, 15, 31, \dots\)
		\item \(a_n = \begin{cases} 1 &,\ n = 1 \\ 2a_{n-1} + 1 &,\ n > 1 \end{cases},\ n \in \Z^+\)
		\item \(f(n) = 2^n - 1\) where \(f : \Z^+ \mapsto \Z^+\)
	\end{enumerate}
\end{example}

We have explicit names for the notation types in the previous example.

\begin{defn}[General Form\index{Sequence!General Form}]
	A written list of the elements of a sequence, in order, typically with \(\dots\) at the end (and at the beginning, if your sequence elements start in the middle -- typically this does not happen). Sometimes mathematicians put curly braces \(\{\}\) around their general-form sequences -- try not to get this confused with a set! Other times mathematicians will give you a rule, written in plain English, for generating sequence terms
\end{defn}

\begin{defn}[Recursive Form\index{Sequence!Recursive Form}]
	An explicit formula that depends on previous elements in the sequence, and necessarily has one or more base cases. Typically this is expressed as \[x_n = \begin{cases} \text{base case(s)} &, n \in \{\text{some set}\} \\ \text{some formula dependent on } n &, n \not\in \{\text{the previous set}\} \end{cases}\]
\end{defn}

\begin{defn}[Closed Form\index{Sequence!Closed Form}]
	An explicit formula to generate a specific term in a sequence. This formula is solely dependent on a given index. Typically this is expressed as \[y_k = \text{some formula dependent on } k\]
\end{defn}

\begin{rem}
	Sequence numbers do not need to be in any particular order, nor do they necessarily have to fit some sort of function. Sequences elements can also be any sort of number (we will restrict to \(\R\)).
\end{rem}

\begin{example}
	Some sequences:
	\[1, 0, -1, 0, 1, 0, -1, 0, \dots\]
	\[8, 6, 4, 2, 8, 6, 4, 2, 8, \dots\]
	\[0, 0.5, 0.333, 0.25, 0.2, 0.125, \dots\]
\end{example}

Sometimes we will ask you to generate terms of a sequence. This is a relatively straightforward task so long as you avoid simple arithmetic errors.

\exsol{
	Generate the first 7 terms of the following sequences:

	\begin{enumerate}[label=(\alph*)]
		\item The \(n\)th term is its index tripled (start your indices at 0)
		\item \(f_k = \begin{cases} 1 &, k = 1,2 \\ f_{k-1} + f_{k-2} &, k \geq 3 \end{cases}\)
		\item \(l_1 = l_2 = l_3 = 1\) and \(l_i = l_{i-1} + l_{i-2} + l_{i-3}\)
	\end{enumerate}
}{
	We will use \(\{\}\) notation here:

	\begin{enumerate}[label=(\alph*)]
		\item A simple rule (one that we could express in closed form) -- \(\{0, 3, 6, 9, 12, 15, 18, \dots\}\)
		\item This is the Fibonacci sequence -- \(\{1, 1, 2, 3, 5, 8, 13, \dots\}\)
		\item In this case, the index is implied to be \(\in \Z^+\) -- \(\{1, 1, 1, 3, 5, 9, 17, \dots\}\)
	\end{enumerate}
}

Other times we will ask you to find a closed form for a sequence given in general or recursive form. Typically we ask this about general forms, however occasionally you will see a recursive form. This task is akin to pattern-matching and sometimes requires much more critical thinking. Sometimes sequences can have multiple answers -- any correct answer suffices. Answers that are unreasonable -- i.e.\ answers that only fit the provided sequence digits -- will receive a good chuckle along with zero marks.

\exsol{
	Find closed-forms for the following sequences:

	\begin{enumerate}[label=(\alph*)]
		\item \(\{1, 4, 9, 16, 25, \dots\}\)
		\item \(\{3, 6, 9, 12, 15, \dots\}\)
		\item \(\{1, 4, 9, 6, 5, 6, 9, \dots\}\)
		\item \(\{1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, \dots\}\)
		\item \(\{2, 5, 8, 0, 3, 6, 9, \dots\}\)
	\end{enumerate}
}{
	We attempt to explain the patterns that led us to the solutions:

	\begin{enumerate}[label=(\alph*)]
		\item The pattern here is squares. Each sequence number is the square of its index
		\item \(f(n) = 3(n+1)\) where \(f : \N \rightarrow \Z\)
		\item Hopefully you still had the first sequence in mind -- squares modulo 10
		\item Notice a pattern with the indices -- specifically look at the index where there is a switch. Index 1 starts the 1s, index 4 starts the 2s, index 9 starts the 3s, index 16 starts the 4s. What is the relationship between these numbers? Take the square root of the index. Then realize that square roots maintain inequalities -- \(1 = \sqrt{1} < \{\sqrt{2}, \sqrt{3}\} < \sqrt{4} = 2\), so look towards floors and ceilings to yield integers. Thus, \(f(n) = \floor{\sqrt{n}}\) where \(f : \Z^+ \rightarrow \Z^+\)
		\item This one can have multiple answers. One solution: \(f(n) = 3n-1 \Mod{11}\) (taking least positive remainder) where \(f : \Z^+ \rightarrow \Z^+\). Typically if you see a somewhat circular sequence, especially if there is a zero, then consider applying a modulus. Start at the zero and look forward until the sequence reverts. In this case, we have \(0,3,6,9\) which is simply \(f(n) = 3n\). Then play around with the start point and the modulus
	\end{enumerate}
}

Finding a closed-form function that matches a sequence is difficult. It requires a deep understanding of how all mathematical operators work, and requires a knack for finding patterns. There are multiple methods for estimating functions, including polynomial interpolation, piece-wise functions, floor-ceiling interpolation, and least-squares. These methods usually yield a result significantly more complicated than finding a simpler function. However, there are merits to using these methods as a starting-point.

\subsection{Series}

You may remember series from a calculus course. Series are closely related to sequences.

\begin{defn}[Series\index{Series}]
	The sum of all terms in a sequence
\end{defn}

\begin{example}
	The Geometric Series (\(r \neq 1\)) \[a + ar + ar^2 + \cdots ar^{n-1} = \sum_{i=0}^{n-1} ar^i\]
	which has the solution \[= a(\frac{1-r^n}{1-r})\]
\end{example}

Typically we try to understand the limiting behavior of infinite series. There are a number of tests you can use to study this behavior, however this topic is typically taught in Calculus II classes. We will thus omit this topic here, however we do recommend at least understanding the idea behind limits. We will come back to infinite objects when we discuss countability in chapter \ref{chapter:countability}.

\exsol{
	Determine whether the series \(\sum_{i=1}^{\infty}\frac{1}{3^{i-1}}\) converges or diverges.
}{
	From the geometric series formula above, \(\sum_{i=1}^{n} \frac{1}{3^{i-1}} = \sum_{i=0}^{n-1} \frac{1}{3^i} = \sum_{i=0}^{n-1} \frac{1}{3}^i = \frac{1-\frac{1}{3}^n}{1 - \frac{1}{3}} = \frac{3}{2}(1-\frac{1}{3^n})\). Then we can take the limit as \(n \rightarrow \infty\), which \(= \frac{3}{2}(1 - 0) = \frac{3}{2}\)
}

\section{Summary}

\begin{itemize}
	\item Relations give us a formal way to write out a mathematical relationship between elements in sets.
	\item Functions are a special kind of binary relation that satisfy \((\forall c \in C)(\exists! d \in D)[(c,d) \in R]\). A function \(f : C \mapsto D\) is then described by the input/output pairs \((c,d) \in R\).
	\item Sequences have three forms: general, recursive, and closed.
\end{itemize}

\section{Practice}

\begin{enumerate}
	\item Write out the \textit{greater than} relation \(R \subseteq \{1,2,3\} \times \{-1,1,3,5\}\)
	\item Classify the following relations as reflexive, symmetric, and/or transitive:
	\begin{enumerate}
		\item \(\{(x,y) \in \N \times \N \mid x \leq \sqrt{y}\}\)
		\item \(\{(x,y) \in \N \times \N \mid x \equiv y^2 \Mod{4}\}\)
		\item \(\{(x,y) \in \N \times \N \mid \sqrt{x} \leq y\}\)
	\end{enumerate}
	\item Prove Theorem \ref{relation-transitive-theorem}.
	\item Determine whether the following relations are functions:
	\begin{enumerate}
		\item \(\{(x,y) \in \Z^- \times \Z^+ \mid x < y\}\)
		\item \(\{(x,y) \in \Z^+ \times \Z^- \mid x < y\}\)
		\item \(\{(x,y) \in \R   \times \R   \mid y = x + 2\}\)
	\end{enumerate}
	\item
	Why is the relation \(\{(x,y) \in \Z \times \Z \mid x \equiv y \Mod{m}\}\) \textbf{not} a function?
	What about \(\{(x,y) \in \Z \times \Z \mid x \ \% \ m\}\) (where we take the least non-negative residue; the remainder)?
	\item Determine whether the following are functions. If they are, then classify them as injective, surjective, and/or bijective.
	\begin{enumerate}
		\item \(f : \Z \rightarrow \Z\) given by \(f : z \mapsto z \ \%\ 37\)
		\item \(f : \Q \rightarrow \Q\) given by \(f : q \mapsto \sqrt{q}\)
		\item \(f : \R \rightarrow \Z\) given by \(f : x \mapsto \floor{x}\)
	\end{enumerate}
	\item Suppose a function \(f : X \rightarrow Y\) is injective and \(h : X \rightarrow X\) is bijective. Is the function \(g : X \rightarrow Y\) given by \(g(x) = f(h(x))\) also injective? What if \(h\) is only surjective? What if \(h\) is only injective? % is this a stupid question?
	\item Find closed-forms, or simple rules, that describe the following sequences:
	\begin{enumerate}
		\item \(\{-3,-1,1,3,5,\cdots\}\) % odds, shifted back
		\item \(\{-1, 1, 7, 25, 79, \cdots\}\) % 3^i - 2
		\item \(\{2,3,5,0,4,\cdots\}\) % primes mod 7
	\end{enumerate}
	% todo
\end{enumerate}

\end{document}


\documentclass[20pt]{article} % USenglish for autoref
\usepackage[paper=screen, centering, papersize=20cm]{geometry}
\usepackage{cmap}		% to search and copy ligatures
\usepackage[utf8]{inputenc}	% for Linux computer and Mac
%\usepackage[latin1]{inputenc}	% für Windows computer
\usepackage[T1]{fontenc}	% to search for ligatures in the pdf
\usepackage[USenglish]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{enumerate}
\usepackage{amsthm}
\usepackage{listings}
\usepackage{graphicx}
\usepackage[]{algorithm2e}
%\usepackage{stmaryrd}		% for \mapsfrom
\usepackage{aliascnt}		% for Aliascounter so that autoref gives the thms the right names
\usepackage[pdfborder={0 0 0}]{hyperref}   % if you want to have math environment in captions you have to use \texorpdfstring{$x^2$}{x2}; without frame around the links
\usepackage[figure]{hypcap}		% to make autoref link to figures and not the captions of figures
%\usepackage{paralist}		% for compactitem
\usepackage{mathtools}		% for \coloneqq
\usepackage[]{todonotes}	% use option disable to disable all the todos
\usepackage{float}
\usepackage{lipsum}
\theoremstyle{break}
\newtheorem{definition}{Definition}[section]
\newtheorem{exa}[definition]{Example}
\newtheorem{cor}[definition]{Corollary}
\newtheorem{lem}[definition]{Lemma}
\newtheorem{conj}[definition]{Conjecture}
\newtheorem{quest}[definition]{Research question}
\newtheorem{thm}[definition]{Theorem}
\newtheorem{prop}[definition]{Proposition}
\newtheorem{rem}[definition]{Remark}

\renewcommand{\labelenumi}{(\roman{enumi})} % roman numbers in enumerations

\usepackage{graphicx,tikz,pgfplots} % Allows including images

\usetikzlibrary{calc,decorations.markings}
\usetikzlibrary{shapes,snakes}

\DeclareMathOperator{\Aff}{Aff}
\DeclareMathOperator{\der}{der}
\DeclareMathOperator{\Trans}{Trans}
\DeclareMathOperator{\dir}{dir}
\DeclareMathOperator{\Sing}{Sing}
\DeclareMathOperator{\Stab}{Stab}
\DeclareMathOperator{\id}{id}
\DeclareMathOperator{\im}{im}
\DeclareMathOperator{\NN}{\mathbb{N}}

%For L Cal and L Twidle:
\newcommand{\LC}{\mathcal{L}}
\newcommand{\LT}{\widetilde{\mathcal{L}}}

%For X bar
\newcommand{\XB}{\overline{X}}

%For the real and complex numbers
\newcommand{\RR}{\mathbb{R}}
\newcommand{\CC}{\mathbb{C}}

\newcommand{\remark}[2][]{\todo[color=green!50, #1]{#2}}

\title{Notes on Linear Difference and Differential Equations and Taylor Series}

\begin{document}
\maketitle

\section{Linear Difference Equations}

A homogeneous linear difference equation is an iterative relationship:
\[z_{n+k}+a_{k-1}z_{n+k-1}+\dots+a_0z_n=0\]

The general solution of a homogeneous linear difference equation can be obtained as follows:
\begin{itemize}
\item Firstly, define the characteristic polynomial $\chi(z)=z^k+a_{k-1}z^{k-1}+\dots+a_0$.
\item Let $\lambda_1, \dots, \lambda_l$ be its distinct roots, $m_1, \dots, m_l$ their multiplicities (hence $\sum_im_i=k$).
\item Then, the general solution can be written as

\[z_n=\sum_ip_i(n)\lambda_i^n\]

Where $p_i(n)$ is any polynomial of degree no more than $m_i-1$.
\end{itemize}

\section{Linear Differential Equations}

Similarly, a homogeneous linear differential equation is
\[y^{(k)}+a_{k-1}y^{(k-1)}+\dots+a_1y'+a_0y=0\]

The general solution of a homogeneous linear differential equation is as follows:
\begin{itemize}
\item Firstly, define the characteristic polynomial $\chi(z)=z^k+a_{k-1}z^{k-1}+\dots+a_0$.
\item Let $\lambda_1, \dots, \lambda_l$ be its distinct roots, $m_1, \dots, m_l$ their multiplicities (hence $\sum_im_i=k$).
\item Then, the general solution can be written as

\[y(t)=\sum_ip_i(t)e^{\lambda_i t}\]

Where $p_i(n)$ is any polynomial of degree no more than $m_i-1$.
\end{itemize}

\section{Taylor Series}

If $f\in C^{k+1}$, then the Taylor series of $f$ at $a$, with Lagrange remainder, is

\[f(x)=f(a)+\sum_{j=1}^{k}{f^{(j)}(a)(x-a)^j\over j!}+{f^{(j+1)}(c)(x-a)^{j+1}\over(j+1)!}\]

Where $c$ is in the closed interval between $x$ and $a$.

\end{document}


\documentclass[11pt, a4paper, twoside]{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage[margin=0.8in]{geometry}
\usepackage{listings}
\usepackage{float}
\usepackage{fancyhdr}
\usepackage{indentfirst}
\usepackage[inline]{enumitem}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{array}
\usepackage{minted}
\usemintedstyle{vs}
\usepackage[belowskip=0pt,aboveskip=0pt,font=small,labelfont=small]{caption}
\captionsetup{width=\linewidth}
\setlength\intextsep{0pt}
\graphicspath{{Plots/}}
\setlist[itemize]{noitemsep, topsep=0pt}
\fancyhead[RO,LE]{EE2703: Assignment 6}
\fancyhead[LO,RE]{Akilesh Kannan}
\cfoot{\thepage}

\title{EE2703: Assignment 6}
\author{Akilesh Kannan (EE18B122)}
\date{\today}

\pagestyle{fancy}

\begin{document}
\maketitle

\section{Introduction}
    We analyse LTI systems in continuous time using Laplace transforms to find the output of the system to a given input with the help of a python library, namely \texttt{scipy.signal} toolbox.

\section{Time Response of Spring Oscillator}
    Our goal is to find the response of a spring oscillator, governed by the equation:
    \begin{equation*}
        \ddot{x} + 2.25x = f(t)
    \end{equation*}

    where,
    \begin{gather*}
        x(t) = \text{Displacement of spring}\\
        f(t) = \text{Force applied on the spring}
    \end{gather*}

    We consider the case that the force applied on the spring is given by:
    \begin{equation*}
        f(t) = e^{-at}cos(\omega t)u(t)
    \end{equation*}

    We shall do a case by case analysis for the following values of a and $\omega$:

    \begin{center}
        \begin{tabularx}{0.4\textwidth} {
            | >{\centering\arraybackslash}X
            || >{\centering\arraybackslash}X | }
             \hline
             a ($sec^{-1}$)&$\omega$ ($rad\ sec^{-1}$)  \\
             \hline
             \hline
             0.5&1.5  \\
             \hline
             0.05&1.5 \\
             \hline
             0.05&1.4 \\
             \hline
             0.05&1.45 \\
             \hline
             0.05&1.5 \\
             \hline
             0.05&1.55 \\
             \hline
             0.05&1.6 \\
             \hline
        \end{tabularx}
    \end{center}
    \subsection{Power of Laplace transforms}
        The Laplace transform of $f(t) = e^{-at}cos(\omega t)u(t)$ is given as:
        \begin{equation*}
            \mathcal{L}\{f(t)\} = \frac{s+a}{(s+a)^2 + \omega^2}
        \end{equation*}

        From the property of Laplace transforms, we know:
        \begin{gather*}
            x(t) \longleftrightarrow \mathcal{X}(s)\\
            \implies \dot{x}(t) \longleftrightarrow \ s\mathcal{X}(s) - x(0^-)\\
            \implies \ddot{x}(t) \longleftrightarrow \ s^2\mathcal{X}(s) - sx(0^-)-\dot{x}(0^-)
        \end{gather*}

        From the above equations, we get, for $a = 0.5$ and $\omega = 1.5$:
        \begin{equation*}
            \mathcal{F}(s) = \mathcal{L}\{f(t)\} = \frac{s+0.5}{(s+0.5)^2+2.25}
        \end{equation*}

        So, the equation of the spring oscillator can be written as:
        \begin{equation*}
            s^2\mathcal{X}(s) - sx(0^-)-\dot{x}(0^-) + 2.25\mathcal{X}(s) = \frac{s+0.5}{(s+0.5)^2+2.25}
        \end{equation*}

        Given that the ICs $x(0)$ and $\dot{x}(0)$ are 0, we get:
        \begin{equation*}
            s^2\mathcal{X}(s) + 2.25\mathcal{X}(s) = \frac{s+0.5}{(s+0.5)^2+2.25}
        \end{equation*}

        or,
        \begin{equation*}
            \mathcal{X}(s) = \frac{s+0.5}{((s+0.5)^2+2.25)(s^2+2.25)}
        \end{equation*}

        Using \texttt{scipy.signal.impulse} to find the x(t), and plotting it (for $0<t<50 s$), we get:
        \begin{figure}[H]
            \centering
            \includegraphics[scale=0.7]{Fig 1.png}
            \caption{$x(t)$ for $a=0.5$ and $\omega=1.5$}
            \label{fig:Fig1}
        \end{figure}

        If we use a smaller decay of $a=0.05$, then we get:
        \begin{figure}[H]
            \centering
            \includegraphics[scale=0.7]{Fig 2.png}
            \caption{$x(t)$ for $a=0.05$ and $\omega=1.5$}
            \label{fig:Fig2}
        \end{figure}

    \subsection{Response for different frequencies}
        Modeling the system as a LTI system and computing the response for various frequencies, we get:
        \begin{figure}[H]
            \centering
            \includegraphics[scale=0.7]{Fig 3.png}
            \caption{$x(t)$ for $a=0.05$ and varying $\omega$}
            \label{fig:Fig3}
        \end{figure}

        From the given equation, we can see that the natural response of the system has the frequency $\omega= 1.5\ rad/s$.
        Thus, as expected, the maximum amplitude of oscillation is obtained when the frequency of $f(t)$ is $1.5\ rad/s$, as a case of resonance.
\section{Coupled Spring Problem}
    The coupled equations we are interested in solving are:
    \begin{gather*}
        \ddot{x} + (x-y) = 0\\
        \ddot{y} + 2(y-x) = 0
    \end{gather*}

    Substituting for $y$ from the $1^{\text{st}}$ equation, into the $2^{\text{nd}}$, we get a $4^{\text{th}}$ order differential equation in $x$:
    \begin{equation*}
        \ddddot{x} + 3\ddot{x} = 0
    \end{equation*}

    Given the ICs $x(0)=1$ and $\dot{x}(0)=y(0)=\dot{y}(0)=0$, we can write the above differential equation in the Laplace domain as:
    \begin{gather*}
        s^4\mathcal{X}(s)-s^3 + 3(s^2\mathcal{X}(s)-s)=0\\
        \implies \mathcal{X}(s) = \frac{s^2+3}{s^3+3s}\\
        \implies \mathcal{Y}(s) = \frac{2}{s^3+3s}
    \end{gather*}

    Solving for $x(t)$ and $y(t)$ is now very simple - use \texttt{scipy.signal.impulse} with the above $\mathcal{X}(s)$ and $\mathcal{Y}(s)$. We get the following graph for $x(t)$ and $y(t)$.
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.7]{Fig 4.png}
        \caption{$x(t)$ and $y(t)$ for the coupled spring problem}
        \label{fig:Fig4}
    \end{figure}

    We can see that $x(t)$ and $y(t)$ are sinusoids of the same frequency, but of different phase and magnitude.
\section{Two-port Network}
    The transfer function of the given two-port network can be written as:
    \begin{equation*}
        \frac{V_o(s)}{V_i(s)} = \mathcal{H}(s) = \frac{10^6}{s^2+100s+10^6}
    \end{equation*}

    The Bode magnitude and phase plots can be found using the method \texttt{scipy.signal.bode()}. The plots are shown below:
    \begin{figure}[H]
        \centering
        \setlength\tabcolsep{2pt}
        \begin{tabular}{cc}
            \includegraphics[scale=0.5]{Fig 5(a).png} &
            \includegraphics[scale=0.5]{Fig 5(b).png}\\
        \end{tabular}
        \caption{Bode Plots of the RLC Network's Transfer function}
    \end{figure}

    Now, when the input to the network, $v_i(t)$ is $(cos(10^3t)-cos(10^6t))u(t)$, the output is given by:
    \begin{equation*}
        V_o(s) = V_i(s)\mathcal{H}(s)
    \end{equation*}

    Since we have already found out $\mathcal{H}(s)$ and $V_i(s)$ can be easily found by using a lookup table (or by substituting $a=0$ in the equations used before), finding $v_o(t)$ is a simple task, thanks to \texttt{scipy.signal.lsim}. Plotting the obtained $v_o(t)$, we get:
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.7]{Fig 6(a).png}
        \caption{$v_o(t)$ of the RLC network, when $v_i(t) = (cos(10^3t)-cos(10^6t))u(t)$}
        \label{fig:Fig6a}
    \end{figure}

    We can see it to be varying as a sinusoid of frequency approximately 160 Hz, which is expected, as the RLC network acts as a low pass filter - it allows low frequencies to pass through unchanged, while damping high frequencies to huge extent. We can see this in the graph. If we zoom in Figure (\ref{fig:Fig6a}), we can see the high frequency signal:
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.7]{Fig 6(c).png}
        \caption{High frequency signal in the output}
        \label{fig:Fig6c}
    \end{figure}

    We observe that the peak-to-peak amplitude of this high frequency variation is very less, approximately 0.02 V, compared to the initial 2 V. This is expected as we get a gain of -40dB at $\omega=10^6\ rad/sec$ from the bode plot, which corresponds to gain factor of 0.01.

    Another peculiarity of Figure (\ref{fig:Fig6a}) is the initial variation. When zoomed in, for $0<t<30\ us$, we get:
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.7]{Fig 6(b).png}
        \caption{Initial transients}
        \label{fig:Fig6b}
    \end{figure}

    This is due to application of the step input, i.e., the input is suddenly turned on at $t=0$.
\section{Conclusion}
To conclude, we analysed the solution of various continuous time LTI systems using Laplace transforms with help of \texttt{scipy.signal} toolbox.
\end{document}


% Created 2020-03-20 vie 16:05
% Intended LaTeX compiler: pdflatex
\documentclass[presentation,aspectratio=1610]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{khpreamble}
\usepackage{pgfplots}
\usepackage{pdfpages}
\usepgfplotslibrary{groupplots}
\usetheme{default}
\author{Kjartan Halvorsen}
\date{\today}
\title{Pneumatic tank system}
\hypersetup{
 pdfauthor={Kjartan Halvorsen},
 pdftitle={Pneumatic tank system},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.3.6)},
 pdflang={English}}
\begin{document}

\maketitle

\section{Intro}
\label{sec:org8bfd10f}
\begin{frame}[label={sec:org5f5b8a7}]{Lab experiment}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/tank-lab-setup.png}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgadadc1a}]{Simulink simulation}
\begin{center}
\includegraphics[width=0.8\linewidth]{../../figures/tank-simscape-model.png}
\end{center}
\end{frame}

\section{Modeling}
\label{sec:orgfdff3e1}
\begin{frame}[label={sec:org8513ecf}]{Pneumatic elements}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/tank-lab-setup.png}
\end{center}
\end{frame}

\begin{frame}[label={sec:org19a4887}]{Valve}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/proportional-directional-control-valve-concept.png}
\end{center}
\begin{LATEX}
  \tiny From ''An improved nonlinear modelling and identification
methodology of a servo-pneumatic actuating system with
complex internal design for high-accuracy motion control
applications'' Simulation Modelling Practice and Theory 2017
\end{LATEX}
\end{frame}

\begin{frame}[label={sec:org1059008}]{Valve, contd}
\begin{center}
\includegraphics[width=0.7\linewidth]{../../figures/valve-volt-opening.png}
\end{center}
\end{frame}

\begin{frame}[label={sec:org402cb77}]{Experiment}
\begin{center}
\includegraphics[width=0.65\linewidth]{../../figures/tank-lab-setup.png}
\includegraphics[width=0.35\linewidth]{../../figures/square-wave-pressure-valve.png}
\end{center}
\end{frame}

\begin{frame}[label={sec:org382e73f}]{Result}
\begin{center}
\includegraphics[width=0.7\linewidth]{../../figures/tank-response-scope.png}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgdb8f573}]{Simscape}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/simscape.png}
\end{center}
\end{frame}

\begin{frame}[label={sec:org2b5a140}]{Simulation model}
\begin{center}
\includegraphics[width=0.8\linewidth]{../../figures/tank-simscape-model.png}
\end{center}
\end{frame}
\end{document}


\documentclass{article}
\usepackage{graphicx}

\graphicspath{{images/}}
\begin{document}

\title{A0--MinMax Algorithm Analysis}
\author{Michael Fulton}
\date{February 3, 2015}

\maketitle

\section{Recursive Analysis}

	The function which relates cost functions to number of inputs for my recursive algorithm is as follows:

\[
	C(N)=
	\begin{cases}
		2C(\frac{N}{2})+2 \mbox{ if} N < 2 \\
		1 \mbox{ if}   N = 2 \\
		0 \mbox{ if} \leq 1
	\end{cases}
\]

\[f(n) = \begin{cases} n/2 \mbox{if } n \equiv 0 \\
(3n +1)/2  \mbox{if } n \equiv 1. \end{cases}\]

\section{Iterative Analysis}

\newpage
\section{Plots}
\includegraphics[scale =0.6]{plot.png}
\centering

\end{document}


\documentclass[../main]{subfiles}
\pagestyle{fancy}

\begin{document}

\chapter{Ideals}
\label{apx.ideals}
\thispagestyle{fancy}

\defi{
  Let $I$ be an ideal on a nonempty set $Z$. Let
  \begin{enumerate}
    \item $I^{+} := \mathcal{P}(Z) \setminus I$,
    \item for $a,b \in I^{+}$ let $a \sim_{I} b$ iff $a \Delta b \in I$,
    \item $\mathcal{P}(Z)/I:=\P(Z)/{\sim_I}$ is the Boolean algebra
      with subset inclusion modulo $\sim_I$.
  \end{enumerate}

  We call $\P(Z)/I$ the \index{Ideal!Forcing associated to an ideal}
  \emph{associated forcing} to $I$.
}

\defi{
  If $I$ is an ideal on a cardinal $\kappa$ and
  $g\subset\P(\kappa)/I$ $V$-generic then $g$ is a $V$-ultrafilter on
  $\kappa$ in $V[g]$, so that we may take the \index{Ideal!Generic ultrapower by an ideal}\index{Generic
    ultrapower} \emph{generic ultrapower} $\ult(V,g)$.
}

\prop{
  Let $I$ be an ideal on a cardinal $\kappa$. Then..
  \begin{enumerate}
    \item if $I$ is $\kappa$-complete then so is any generic ultrafilter;
    \item if $I$ is normal then so is any generic ultrafilter.$\hfill\dashv$
    \end{enumerate}
    \todo{define $\kappa$-complete and normal for an ideal. Add reference.}
}

\begin{definition}
  Let $\lambda$ be any cardinal. Then an ideal $I$ on a cardinal
  $\kappa$ is...
  \begin{itemize}
  \item \index{Precipitous ideal}\index{Ideal!Precipitous ideal} \emph{precipitous} if the generic
    ultrapower is wellfounded;
  \item \index{Saturated ideal}\index{Ideal!Saturated ideal} \emph{$\lambda$-saturated}
    if the associated forcing has the $\lambda$-chain condition;
  \item \index{Dense ideal}\index{Ideal!Dense ideal} \emph{$\lambda$-dense} if the
    associated forcing has a dense subset of size
    $\lambda$.$\hfill\dashv$
  \end{itemize}
\end{definition}

Note that $\lambda$-dense trivially implies
$\lambda^+$-saturated. We'll need the following facts about
$\omega_2$-saturated ideals on $\omega_1$:

\begin{proposition}\todo{Parts (ii)-(v) is Example 4.29 in Foreman's
    handbook chapter. Perhaps include the proof.}
  \label{prop.ideal}
  Let $I$ be an $\omega_2$-saturated ideal on $\omega_1$. Then $I$ is
  precipitous, and letting $j \colon V\to M$ be the generic ultrapower map,
  it holds that
  \begin{enumerate}
    \item $M$ is closed under $\omega$-sequences in $V[g]$;
    \item $j(\omega_1^V)=\omega_2^V=\omega_1^{V[g]}$;
    \item $j(\omega_2^V)\in(\omega_2^V,\omega_3^V)$;
    \item $j$ is continuous at $\omega_2^V$;
    \item $j(\omega_n^V)=\omega_n^V=\omega_{n-1}^{V[g]}$ for all
      $n\in[3,\omega]$.$\hfill\dashv$
  \end{enumerate}
\end{proposition}

As for the density, we in particular need the following fact:

\qprop{ Let $I$ be an $\omega_1$-dense ideal on $\omega_1$. Then the
  associated forcing is forcing equivalent to $\col(\omega,\omega_1)$,
  so in particular it's homogeneous.
}

\proof{
  \cite[Proposition~10.20]{Kanamori}.
}

\end{document}


\XtoCBlock{uI}
\label{block:uI}
\begin{figure}[H]\includegraphics{uI}\end{figure}

\begin{XtoCtabular}{Inports}
In & Control error input\tabularnewline
\hline
Init & Value which is loaded at initialization function call\tabularnewline
\hline
Enable & Enable == 0: Deactivation of block; Out is set to 0.

Enable 0->1: Preload of integral part.

Enable == 1: Activation of block\tabularnewline
\hline
\end{XtoCtabular}

\begin{XtoCtabular}{Outports}
Out & Integrator output\tabularnewline
\hline
\end{XtoCtabular}

\begin{XtoCtabular}{Mask Parameters}
Ki & Integral Factor\tabularnewline
\hline
ts\_fact & Multiplication factor of base sampling time (in integer format)\tabularnewline
\hline
\end{XtoCtabular}

\subsubsection*{Description:}
Integrator for angle signals:

    G(s) = Ki/s = 1/(Ti*s)

% include optional documentation file
\InputIfFileExists{\XcHomePath/Library/Control/Doc/uI_Info.tex}{\vspace{1ex}}{}

\subsubsection*{Implementations:}
\begin{tabular}{l l}
\textbf{FiP8} & 8 Bit Fixed Point Implementation\tabularnewline
\textbf{FiP16} & 16 Bit Fixed Point Implementation\tabularnewline
\textbf{FiP32} & 32 Bit Fixed Point Implementation\tabularnewline
\textbf{Float32} & 32 Bit Floating Point Implementation\tabularnewline
\textbf{Float64} & 64 Bit Floating Point Implementation\tabularnewline
\end{tabular}

\XtoCImplementation{FiP8}
\index{Block ID!3376}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP8 \tabularnewline
\textbf{ID} & 3376 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & uI\_FiP8.c \tabularnewline
\textbf{H filename} & uI\_FiP8.h \tabularnewline
\end{tabular}
\vspace{1ex}

8 Bit Fixed Point Implementation

\begin{XtoCtabular}{Controller Parameters}
b0 & Integral coefficient\tabularnewline
\hline
sfr & Shift factor for I coefficient b0\tabularnewline
\hline
i\_old & Integrator value from previous cycle\tabularnewline
\hline
enable\_old & Enable value of previous cycle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int8          *In;
     int8          *Init;
     int8          *Enable;
     int8          Out;
     int8          b0;
     int8          sfr;
     int16         i_old;
     int8          enable_old;
} UI_FIP8;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_uI_FiP8.tex}{}{}
\fi
\XtoCImplementation{FiP16}
\index{Block ID!3377}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP16 \tabularnewline
\textbf{ID} & 3377 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & uI\_FiP16.c \tabularnewline
\textbf{H filename} & uI\_FiP16.h \tabularnewline
\end{tabular}
\vspace{1ex}

16 Bit Fixed Point Implementation

\begin{XtoCtabular}{Controller Parameters}
b0 & Integral coefficient\tabularnewline
\hline
sfr & Shift factor for I coefficient b0\tabularnewline
\hline
i\_old & Integrator value from previous cycle\tabularnewline
\hline
enable\_old & Enable value of previous cycle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int16         *In;
     int16         *Init;
     int8          *Enable;
     int16         Out;
     int16         b0;
     int8          sfr;
     int32         i_old;
     int8          enable_old;
} UI_FIP16;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_uI_FiP16.tex}{}{}
\fi
\XtoCImplementation{FiP32}
\index{Block ID!3378}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP32 \tabularnewline
\textbf{ID} & 3378 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & uI\_FiP32.c \tabularnewline
\textbf{H filename} & uI\_FiP32.h \tabularnewline
\end{tabular}
\vspace{1ex}

32 Bit Fixed Point Implementation

\begin{XtoCtabular}{Controller Parameters}
b0 & Integral coefficient\tabularnewline
\hline
sfr & Shift factor for I coefficient b0\tabularnewline
\hline
i\_old & Integrator value from previous cycle\tabularnewline
\hline
enable\_old & Enable value of previous cycle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int32         *In;
     int32         *Init;
     int8          *Enable;
     int32         Out;
     int32         b0;
     int8          sfr;
     int64         i_old;
     int8          enable_old;
} UI_FIP32;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_uI_FiP32.tex}{}{}
\fi
\XtoCImplementation{Float32}
\index{Block ID!3379}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & Float32 \tabularnewline
\textbf{ID} & 3379 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & uI\_Float32.c \tabularnewline
\textbf{H filename} & uI\_Float32.h \tabularnewline
\end{tabular}
\vspace{1ex}

32 Bit Floating Point Implementation

\begin{XtoCtabular}{Controller Parameters}
b0 & Integral coefficient\tabularnewline
\hline
i\_old & Integrator value from previous cycle\tabularnewline
\hline
enable\_old & Enable value of previous cycle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     float32       *In;
     float32       *Init;
     int8          *Enable;
     float32       Out;
     float32       b0;
     float32       i_old;
     int8          enable_old;
} UI_FLOAT32;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_uI_Float32.tex}{}{}
\fi
\XtoCImplementation{Float64}
\index{Block ID!3380}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & Float64 \tabularnewline
\textbf{ID} & 3380 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & uI\_Float64.c \tabularnewline
\textbf{H filename} & uI\_Float64.h \tabularnewline
\end{tabular}
\vspace{1ex}

64 Bit Floating Point Implementation

\begin{XtoCtabular}{Controller Parameters}
b0 & Integral coefficient\tabularnewline
\hline
i\_old & Integrator value from previous cycle\tabularnewline
\hline
enable\_old & Enable value of previous cycle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     float64       *In;
     float64       *Init;
     int8          *Enable;
     float64       Out;
     float64       b0;
     float64       i_old;
     int8          enable_old;
} UI_FLOAT64;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_uI_Float64.tex}{}{}
\fi


\newpage
\section{Assumptions that can be satisfied with current technology}
\label{sec:satisfiable_assumptions}

\subsection{Key length}
We assume that the key lengths are sufficient to prevent brute force attacks.

\subsection{Keys are randomly generated}
We assume that the keys are randomly generated by using high quality randomness that is suitable for cryptographic operations. Therefore  we assume that the keys can not be predicted or guessed.

\subsection{ID-card keys are generated in the card}
We assume that the private keys of the ID-card never leave the card and are generated inside of the chip.

\subsection{ID-card private keys do not leak}
We assume that the private keys of the ID-card are protected by special hardware and can not be extracted and leaked.

\subsection{ID-card keys can not be deleted}
We assume that an attacker is not able to delete or regenerate the key pairs of an ID-card.

\subsection{Only strong cryptosystems with sufficient key lengths are used}
We assume that only strong and standardized cryptosystems are used, which can not be broken with classical computers. E.g., we assume that it is not possible to deduce the private key from the public key in case the key pair was randomly generated.

What cryptosystems can be considered strong in near term (10 years)? In order to answer this question, we rely on the ECRYPT CSA 2018 report~\cite{ECRYPT-CSA}. An excerpt from the Table 4.6 of the report is presented in Table~\ref{tab:keylengths}.

\begin{table}[ht]
    \begin{center}
        \caption{ECRYPT CSA recommended key lengths}
        \label{tab:keylengths}
        \begin{tabular}{|c|c|}
            \hline
            Cryptosystem    & Minimal output/key length\\
            \hline
            \hline
            Symmetric (AES) & 256 \\
            RSA             & 3072\\
            ECC             & 256\\
            Hash function   & 256\\
            \hline
        \end{tabular}
    \end{center}
\end{table}

Concerning padding, ECRYPT CSA report~\cite{ECRYPT-CSA} recommends using RSA-PSS instead of PKCS\#1 v1.5 for new deployments of RSA signature scheme. PSS scheme is also standardised for use in JSON Web Algorithms RFC7518~\cite{RFC7518}. Note that the standard only supports the schemes utilising SHA2-256, SHA2-384 and SHA2-512 hash functions.

In case of elliptic curve signature schemes, there is no need for a special padding. Care must be taken that the bit lengths of the hash function output and ECC signature input match. RFC7518~\cite{RFC7518} specifies three pairs of ECDSA signature scheme curves and hash functions:
\begin{itemize}
    \item ECDSA using P-256 and SHA2-256,
    \item ECDSA using P-384 and SHA2-384, and
    \item ECDSA using P-521 and SHA2-512.
\end{itemize}

\subsection{Quantum computers are not available}
We assume that large quantum computers, which could break the modern asymmetric cryptosystems are not available to the attacker. In 2019 Quantum Threat Timeline Report\footnote{\url{https://globalriskinstitute.org/publications/quantum-threat-timeline/}} was published and it contains estimates by 22 experts on the field. Half of them estimated that  a quantum computer capable of breaking RSA-2048 will be built within 15 years\footnote{\url{https://quantumcomputingreport.com/our-take/how-many-years-until-a-quantum-computer-can-break-rsa-2048/}}.

\subsection{Attacker with superuser access has complete access}
In case an attacker has infected client's device and has superuser access, we assume that the attacker has complete access. Thus, the attacker can choose what to draw on the screen, which values to send to the card reader and the ability to read the keystrokes of the user.

\subsection{Collision resistance property can not be broken}
We assume that the attacker is not able to break the collision resistance property of the cryptographic hash function that is used for signing.

\subsection{Second preimage resistance property can not be broken}
We assume that the attacker is not able to break the second preimage resistance property of the cryptographic hash function that is used for signing.

\subsection{The authentication challenge can not be guessed or predicted}
We assume that the authentication challenge is generated randomly and has sufficient length / entropy.

\subsection{Session cookie is not predictable}
We assume that the session cookie has sufficient entropy and is not predictable.

\subsection{Communication channel is protected by TLS}
The communication channel is secured using TLS 1.2 or a newer TLS version. We do not consider attacks against such channels to be in the scope of this analysis as currently these attacks are not feasible without either compromising the client or the server.

\subsection{Secondary channel to inform the user about card use}
At the first sight this seems easy -- just send the user an email, SMS, push notification or alike. On the other hand, there may be some scenarios where this may be discouraged (most notably I-voting where strong evidence of signature device use can be used for vote selling). In case informing the user would be mandatory, the service would have to be centralized as all service providers would not be able to set up the secondary channel. However, by having a centralized service, the privacy of the user becomes an issue. Therefore, the architecture of the functionality has to be well thought through before a decision is made to implement it.

\newpage
\section{Assumptions that can not be satisfied with current technology}
\label{sec:unsatisfiable_assumptions}

\subsection{Card readers with PIN pad and trusted preview}

Even the officially recommended smart card reader with PIN-firewalled PIN pad (Gemalto IDBridge CT710\footnote{\url{https://gemcard.ro/wp-content/uploads/2016/11/Gemalto_IDBridgeCT700_CT710_brochure.pdf}}) seems to be unavailable in Estonia. Requesting a trusted preview panel seems even more infeasible. Thus, the first step towards not having to trust the computing device would be to make the PIN-firewalled PIN pads available to the end-users. By using PIN firewalled smart card readers the complexity of an attack would increase. As a next step, it should be researched whether smart card readers with trusted preview are available on the market and how they could be integrated to the current software ecosystem.

\subsection{Token Binding}
Token Binding (or an equivalent technology) would mitigate a lot of attacks. However, it does not seem to be universally available across all the browsers and platforms.

\subsection{Browser extension can access details of TLS connection}
Some of the proposed countermeasures for the MITM attack relay on the possibility of accessing the public key of the service provider. Currently only Firefox allows an extension to query information about the TLS connection (e.g., the public key of the service provider for the current session).

\subsection{Using separate key pairs for authentication, encryption and authorization}
\label{subsec:separatekeypairs}
Currently, the Estonian ID-card contains two key pairs, out of which the second key pair is only used for issuing legally binding signatures. However, the first key pair is used for multiple functionalities like authentication, encryption/decryption, logging into services, opening VPN connections, etc. This is not the best design as mixed usage scenarios may cause unexpected vulnerabilities.

When the ID-card owner uses his/her card for a longer-period service, say to log onto a terminal, the card has to be in the reader while the user is logged in, and only when the card is removed from the reader, the user is logged out. The current Open eID architecture relies on mutually authenticated TLS. Therefore, Open eID design allows the user to enter PIN1 only once during the authentication phase, after which the card's authentication security environment is left open. Such design allows to use mutually authenticated TLS without constantly asking the user to enter the PIN1. The downside of this design is that other applications running on the same device could also use the first key pair while the authentication security environment on the card is open (in the above example, during the whole period of being logged onto the terminal).

However, the Web eID architecture does not rely on mutually authenticated TLS channels. Thus, it is no longer necessary to keep the card's authentication security environment open after the authentication has been completed. Closing the access to the authentication security environment would prevent other applications from using the private key from the first key pair. Still, access to the card's authentication security environment can not be simply reset, since this can interfere with other legitimate applications that are using that security environment at the same time.

It is an open question to find out how many users would be negatively affected when access to card's authentication security environment would be reset by Web eID. In the long run, it is advised to issue separate key pairs for separate functionalities like authentication, encryption and authorization. With separate key pairs it would be easier to avoid problems arising from different security requirements of different usage scenarios.


\subsection{Dynamic Time Warping}

\begin{frame}{Dynamic Time Warping}
    \begin{itemize}
        \item A widely used and robust distance measure for time series

        \item \textit{"Allowing similar shapes to match even if they are out of phase in the time axis"}
            \cite{keogh2002exact}
    \end{itemize}
\end{frame}

\begin{frame}<handout:0>{Dynamic Time Warping}{Example}
    \begin{center}
        \resizebox {\textwidth} {!} {
            \begin{tabular}{cc}
                \resizebox* {!} {0.3\textwidth} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            xmin=0,
                            xmax=47,
                            xlabel=time,
                            ylabel=acceleration,
                            width=\axisdefaultwidth,
                            height=0.7*\axisdefaultheight,
                            reverse legend,
                            legend pos=south east]
                            \addplot[red, thick, mark=none] table {../data/fig/dynamictimewarping/q.dat};
                            \addlegendentry{Q}
                            \addplot[blue, thick, mark=none] table {../data/fig/dynamictimewarping/c.dat};
                            \addlegendentry{C}
                        \end{axis}
                    \end{tikzpicture}
                } & \quad
                \resizebox* {!} {0.3\textwidth} {
                    \begin{tabular}[b]{ll}
                        \begin{turn}{90}
                            \begin{tikzpicture}
                                \begin{axis}[
                                    xmin=0,
                                    xmax=47,
                                    ymin=-100,
                                    ymax=0,
                                    hide x axis,
                                    hide y axis,
                                    width=\axisdefaultwidth,
                                    height=0.7*\axisdefaultheight]
                                    \addplot[red, ultra thick, mark=none] table {../data/fig/dynamictimewarping/q.dat};
                                \end{axis}
                            \end{tikzpicture}
                        \end{turn} \hspace*{3em} &
                        \begin{tikzpicture}
                            \begin{axis}[
                                enlargelimits=false,
                                ymin=0,
                                ymax=47,
                                hide x axis,
                                hide y axis,
                                width=\axisdefaultwidth,
                                height=\axisdefaultwidth,
                                colorbar,
                                colormap/viridis high res]
                                \addplot[matrix plot*,
                                    mesh/cols=48,
                                    point meta=explicit] table[meta=C] {../data/fig/dynamictimewarping/matrix.dat};
                            \end{axis}
                        \end{tikzpicture} \\
                        &
                        \\[1em]
                        &
                        \begin{tikzpicture}
                            \begin{axis}[
                                xmin=0,
                                xmax=47,
                                ymin=-100,
                                ymax=0,
                                hide x axis,
                                hide y axis,
                                width=\axisdefaultwidth,
                                height=0.7*\axisdefaultheight]
                                \addplot[blue, ultra thick, mark=none] table {../data/fig/dynamictimewarping/c.dat};
                            \end{axis}
                        \end{tikzpicture}
                    \end{tabular}
                }
            \end{tabular}
        }
    \end{center}
\end{frame}

\begin{frame}{Dynamic Time Warping}{Example}
    \begin{center}
        \resizebox {\textwidth} {!} {
            \begin{tabular}{cc}
                \resizebox* {!} {0.3\textwidth} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            xmin=0,
                            xmax=47,
                            xlabel=time,
                            ylabel=acceleration,
                            width=\axisdefaultwidth,
                            height=0.7*\axisdefaultheight,
                            reverse legend,
                            legend pos=south east]
                            \addplot[gray, quiver={u=\thisrow{u}, v=\thisrow{v}}] table {../data/fig/dynamictimewarping/path.dat};
                            \addplot[red, thick, mark=none] table {../data/fig/dynamictimewarping/q.dat};
                            \addlegendentry{Q}
                            \addplot[blue, thick, mark=none] table {../data/fig/dynamictimewarping/c.dat};
                            \addlegendentry{C}
                        \end{axis}
                    \end{tikzpicture}
                } & \quad
                \resizebox* {!} {0.3\textwidth} {
                    \begin{tabular}[b]{ll}
                        \begin{turn}{90}
                            \begin{tikzpicture}
                                \begin{axis}[
                                    xmin=0,
                                    xmax=47,
                                    ymin=-100,
                                    ymax=0,
                                    hide x axis,
                                    hide y axis,
                                    width=\axisdefaultwidth,
                                    height=0.7*\axisdefaultheight]
                                    \addplot[red, ultra thick, mark=none] table {../data/fig/dynamictimewarping/q.dat};
                                \end{axis}
                            \end{tikzpicture}
                        \end{turn} \hspace*{3em} &
                        \begin{tikzpicture}
                            \begin{axis}[
                                enlargelimits=false,
                                ymin=0,
                                ymax=47,
                                hide x axis,
                                hide y axis,
                                width=\axisdefaultwidth,
                                height=\axisdefaultwidth,
                                colorbar,
                                colormap/viridis high res]
                                \addplot[matrix plot*,
                                    mesh/cols=48,
                                    point meta=explicit] table[meta=C] {../data/fig/dynamictimewarping/matrix.dat};
                                \addplot[white, ultra thick, mark=*, mark size=1] table {../data/fig/dynamictimewarping/matrix_path.dat};
                            \end{axis}
                        \end{tikzpicture} \\
                        &
                        \\[1em]
                        &
                        \begin{tikzpicture}
                            \begin{axis}[
                                xmin=0,
                                xmax=47,
                                ymin=-100,
                                ymax=0,
                                hide x axis,
                                hide y axis,
                                width=\axisdefaultwidth,
                                height=0.7*\axisdefaultheight]
                                \addplot[blue, ultra thick, mark=none] table {../data/fig/dynamictimewarping/c.dat};
                            \end{axis}
                        \end{tikzpicture}
                    \end{tabular}
                }
            \end{tabular}
        }
    \end{center}
\end{frame}

\input{background/dynamic_time_warping/sakoe-chiba_band.tex}
\input{background/dynamic_time_warping/time_series_normalization.tex}


\documentclass[11pt]{article}

\textheight 22cm
\textwidth  16cm
\hoffset= -0.6in
\voffset= -0.5in
\setlength{\parindent}{0cm}
\setlength{\parskip}{10pt plus 2pt minus 2pt}
\pagenumbering{roman}
\setcounter{page}{-9}

\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
\newcommand{\bd}{\begin{displaymath}}
\newcommand{\ed}{\end{displaymath}}
\begin{document}

\title{Astrophysics II: Laboratory 3}
\author{\Large Binary Stars and Stellar Masses}
\maketitle

\setlength{\parindent}{0.2pt}
\setlength{\parskip}{2ex}

\section{{\bf Objectives:}}

\begin{itemize}
\item Continue learning functionality of MATLAB.\\
	- Visualize data.\\
	- Use visualization in aid of fitting function to data.
\item Use Spectroscopic measurements of a stellar absorption line to determine binary masses.\\
\end{itemize}

\section{{\bf Introduction}}
	Our sun appears to be a rarity in space. Approximately two-thirds of all solar-type field stars are members of binary systems, and recent studies suggest that virtually all stars begin life as members of multiple systems. Consequently, many of the stars you see at night are actually binaries, comprised of two stars gravitationally bound in orbit with one another. These binary systems are important astrophysical laboratories because they allow us to deduce the properties of the constituent stars more accurately than we can with single stars. The physics that governs how stars orbit one another was developed by Newton and Kepler over three hundred years ago, and can be summarized by the equation
\be
	P^{2}=\frac{4\pi^{2}}{G(M_{P}+M_{S})}a^{3}
\ee
where P is the period of orbit, G is the gravitational constant, $M_{P}$ and $M_{S}$ are the masses of the primary and secondary stars respectively, and $a$ is the semi-major axes of the two orbits $a=a_{P}+a_{S}$. In mks units, $G = 6.67 \times 10^{-11}$ but these units are not the units of choice. If masses are measured in solar masses, distances in astronomical units, and periods in years, then the application of Newton's law to the Earth-Sun system gives $4\pi^{2}/G=1$.

Binary stars fall into several categories, depending on their observed properties: optical doubles, visual binaries, composite spectrum binaries, astrometric binaries, spectroscopic binaries, and eclipsing binaries (photometric binaries). Here we focus on the case of spectroscopic binaries in the case where the spectrum from the binary system exhibits a doublet of the same HI absorption line.

Your primary task in this lab is to measure the wavelength of the H$\alpha$ line vs time in a series of stellar spectra. This can be done simply by plotting the spectra in matlab and then calculating roughly by sight the shift in wavelength corresponding to doppler shift in the low velocity limit. We assume here that the components of the binary in question are assumed to follow circular orbits. This is not true for all binaries, but for the present system it is valid. This means that the orbital eccentricity is zero and that the orbital velocities are constant at all times.

\section{{\bf Procedure}}
\begin{enumerate}
\item Open MATLAB.
\item Download the seven data files at www.astro.umd.edu/$\sim$mavara/lab3-121/ and save them into the MATLAB directory.
\item Type the following commands at the matlab command line to load the data in each of these files into matlab:\\
$<<$ load(`binary1.dat')\\
$<<$ load(`binary2.dat')\\
 	etc.....
\item Note that these data files each contain a matrix of two columns, the first signifying wavelength in angstroms and the second a normalized flux.
\item Plot a few of these spectra to get a feel for what they're showing.
\item The primary absorption feature in these plots is a doubling of the HI$\alpha$ line. Notice that there are two strong absorption features in each spectrum and they are of unequal depths. What does that different signify in a normalized spectrum?
\item Use the equation
\be
	\frac{\lambda_{0}}{\lambda}=\frac{v}{c}
\ee
to determine the corresponding radial velocities of both components of the binary system for each spectrum
\item The Spectra, in order, were taken on the Julian Dates given in Table 1.
\begin{table}[ht]
\caption{Dates of Observations}
\centering
\begin{tabular}{c c}
\hline \hline
Filename & Julian Date\\ [0.5ex]
\hline
binary1.dat & 2441578.831 \\
binary2.dat & 2441579.581 \\
binary3.dat & 2441580.742 \\
binary4.dat & 2441581.943 \\
binary5.dat & 2441582.670 \\
binary6.dat & 2441582.982 \\
binary7.dat & 2441583.960 \\
\hline
\end{tabular}
\end{table}
\item Plot the radial velocity from the absorption feature of each contributing star vs date, plotting both sets of data on the same plot.
\item Estimate the period, amplitude, etc, and plot a sin function over the data to confirm your estimations.
\item Note: In general, primary refers to the brighter or more massive star in a binary system.
\item Use our assumptions about this particular stellar binary system and the velocities to measure the semi-major axis of the orbit.
\item Use the simplified (units of stellar mass, etc.) version of Equation 1 to calculate the combined mass.
\item What are the masses of the two components in solar masses?\\
	(Recall, from the definition of center of mass, $a_{S}M_{S} = a_{P}M_{P}$. Your measurement demonstrates the importance of studying binary stars. Virtually everything we know about stellar masses comes from analyzing their motions.)
\end{enumerate}

\section{{\bf Questions}}
{\it Now let's think about the usefulness of what we've learned.}

What is the primary problem with the measurements of masses of the primary and secondary we obtained?

How might we go about solving this problem?

What additional information about the binary system have we acquired with the spectroscopic information?

Can we be sure that the primary absorption features in the spectrum represent the same line? What else can particular lines tell us?

What would the spectrum of a triple system look like?
\\
\\
\\

* Note that this lab has borrowed heavily from Dr. Christopher Palma's Lab 4 of Astro 293, spring 2003.
http://www.astro.psu.edu/$\sim$cpalma/astro293/
\end{document}


\documentclass[11pt,a4paper,fleqn]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[colorlinks=true, citecolor=blue, linkcolor=blue, filecolor=blue,urlcolor=blue]{hyperref}
\hypersetup{
	colorlinks   = true,
	citecolor    = gray
}
\usepackage{wrapfig}

\usepackage{caption}
\captionsetup{format=plain, indent=5pt, font=footnotesize, labelfont=bf}

\setkomafont{disposition}{\scshape\bfseries}

\usepackage{amsmath}
\usepackage{fontawesome}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{bbm}
\usepackage{mathtools}
\usepackage{epsfig}
\usepackage{grffile}
%\usepackage{times}
%\usepackage{babel}
\usepackage{tikz}
\usepackage{paralist}
\usepackage{color}
\usepackage[top=3cm, bottom=2.5cm, left=2.5cm, right=3cm]{geometry}
%\setlength{\mathindent}{1ex}

\usepackage{palatino}
\usepackage{mathpazo}
\setlength\parindent{0pt}

% PGF
\usepackage{pgfplots}
\pgfplotsset{
  compat=newest,
  every axis/.append style={small, minor tick num=3}
}

\usepackage{natbib}
%\usepackage[backend=biber,style=alphabetic,url=false,doi=false]{biblatex}
%\addbibresource{sheet01_biber.bib}
% \addbibresource{/home/coroa/papers/refs.bib}

\newcommand{\id}{\mathbbm{1}}
\newcommand{\NN}{{\mathbbm{N}}}
\newcommand{\ZZ}{{\mathbbm{Z}}}
\newcommand{\RR}{{\mathbbm{R}}}
\newcommand{\CC}{{\mathbbm{C}}}
\renewcommand{\vec}[1]{{\boldsymbol{#1}}}

\renewcommand{\i}{\mathrm{i}}

\newcommand{\expect}[1]{\langle\,#1\,\rangle}
\newcommand{\e}[1]{\ensuremath{\,\mathrm{#1}}}

\renewcommand{\O}{\mc{O}}
\newcommand{\veps}{\varepsilon}
\newcommand{\ud}[1]{\textup{d}#1\,}

\newcommand{\unclear}[1]{\color{green}#1}
\newcommand{\problem}[1]{\color{red}#1}

%=====================================================================
%=====================================================================
\begin{document}

\begin{flushright}
 \textbf{Energy System Modelling }\\
 {\small Karlsruhe Institute of Technology}\\
 {\small Institute for Automation and Applied Informatics}\\
 {\small Summer Term 2020}\\
\end{flushright}

 \vspace{-0.5em}
 \hrulefill
 \vspace{0.3em}

\begin{center}
 \textbf{\textsc{\Large Tutorial II: Network Theory and Power Flow}}\\
 \small Will be worked on in the exercise session on Friday, 5 June 2020.\\[1.5em]
\end{center}

\vspace{-0.5em}
\hrulefill
\vspace{0.8em}

%=====================================================================
\paragraph{Problem II.1 (programming) -- network theory basics \faGroup}~\\
%=====================================================================

\begin{wrapfigure}[10]{r}{0pt}
 \centering
 \begin{tikzpicture}
  [scale=.81,auto=left,every node/.style={circle,fill=gray!20}]
  \node (n0) at (2,14) {0};
  \node (n1) at (2,8)  {1};
  \node (n2) at (0,4)  {2};
  \node (n3) at (3,0) {3};
  \node (n4) at (4,4)  {4};
  \node (n5) at (5,7)  {5};
  \foreach \from/\to in {n0/n1,n1/n2,n1/n4,n1/n5,n2/n4,n2/n3,n3/n4,n4/n5}
  \draw (\from) -- (\to);
 \end{tikzpicture}
 \caption{Simple Network}
 \label{fig:network}
\end{wrapfigure}

Consider the simple network shown ins Figure \ref{fig:network}. Calculate in Python or by hand:

\begin{enumerate}[(a)]
 \item Compile the \textit{nodes list} and the \textit{edge list}.\\~\\
       \textbf{Remark:} While graph-theoretically both lists are unordered sets, let's agree on an ordering now which can serve as basis for the matrices in the following exercises: we sort everything in ascending numerical order, i.e.\ node 1 before node 2 and edge (1,2) before (1,4) before (2,3).
 \item Determine the \textit{order} and the \textit{size} of the network.
 \item Compute the\textit{ adjacency matrix} $A$ and check that it is symmetric.
 \item Find the \textit{degree} $k_n$ of each node $n$ and compute the \textit{average degree} of the network.
 \item Determine the \textit{incidence matrix} $K$ by assuming the links are always directed from smaller-numbered node to larger-numbered node, i.e.\ from node 2 to node 3, instead of from 3 to 2.
 \item Compute the \textit{Laplacian} $L$ of the network using $k_n$ and $A$. Remember that the Laplacian can also be computed as $L=KK^T$ and check that the two definitions agree.
 \item Find the \textit{diameter} of the network by looking at Figure \ref{fig:network}.

\end{enumerate}

\pagebreak
%=====================================================================
\paragraph{Problem II.2 (programming) -- linear power flow \faGroup}~\\
%=====================================================================

If you map the nodes to countries like \texttt{0=DK, 1=DE, 2=CH, 3=IT, 4=AT,5=CZ} the network in Figure \ref{fig:network2} represents a small part of the European electricity network (albeit very simplified). You can find the \textit{power imbalance} time series for the six countries for January 2017 in hourly MW in the file \texttt{imbalance.csv}. They have been derived from physical flows as published by ENTSO-E.\footnote{\url{https://transparency.entsoe.eu/transmission-domain/physicalFlow/show}}\\

The linear power flow is given by
\begin{equation}
 p_i = \sum_j \tilde{L}_{i,j}\theta_j \qquad \text{and} \qquad f_l = \frac{1}{x_l} \sum_i K_{i,l}\theta_i, \qquad \text{where} \qquad \tilde{L}_{i,j}= \sum_l K_{i,l}\frac{1}{x_l} K_{j,l}
\end{equation}
is the weighted Laplacian. For simplicity, we assume identity reactance on all links $x_l = 1$.

\begin{enumerate}[(a)]
 \item Compute the \textit{voltage angles }$\theta_j$ and \textit{flows} $f_l$ for the first hour in the dataset with the convention of $\theta_0 = 0$; i.e.\ the slack bus is at node 0.\\~\\
       \textbf{Remark:} Linear equation systems are solved efficiently using \texttt{numpy.linalg.solve}.
 \item Determine the average flow on each link for 01-2017 and draw it as a directed network.

\end{enumerate}

\begin{figure}[h]
 \centering
 \begin{tikzpicture}
  [scale=.7,auto=left,every node/.style={circle,fill=gray!20}]
  \node (n0) at (2,14) {DK};
  \node (n1) at (2,8)  {DE};
  \node (n2) at (0,4)  {CH};
  \node (n3) at (3,0) {IT};
  \node (n4) at (4,4)  {AT};
  \node (n5) at (5,7)  {CZ};
  \foreach \from/\to in {n0/n1,n1/n2,n1/n4,n1/n5,n2/n4,n2/n3,n3/n4,n4/n5}
  \draw (\from) -- (\to);
 \end{tikzpicture}
 \caption{Simple Network}
 \label{fig:network2}
\end{figure}

%\bibliographystyle{unsrt}
%\bibliography{sheet02}
%\printbibliography

\end{document}