\documentclass{article}

\include{stddefs}
\include{genericdefs}

\newcommand{\red}[1]{\textcolor{red}{\textbf{#1}}}
\newcommand{\green}[1]{\textcolor{green}{\textbf{#1}}}

\begin{document}

\chapterno{1}

\chapter{The language of mathematics}

We need to introduce some formal notation to be able to talk clearly about mathematics and also in a reasonable way
formulate mathematical models of the real world. In modern mathematics the
concept of a set is crucial. It is \url{a bit tricky}{https://en.wikipedia.org/wiki/Set_theory\#Axiomatic_set_theory} to define this precisely.
We will go on and define the basic concepts of what is called
\url{naive set theory}{https://en.wikipedia.org/wiki/Naive_set_theory}.

Mathematics is hard and there are a lot of relevant and good questions
to ask. For example, I do not agree that
% \url{\includegraphics[width="50\%"]{tweetmath.png}}{https://twitter.com/aIeturner/status/1298372968838508546}
\includegraphics[width="50\%"]{tweetmath.png}
is \emph{the dumbest video ive ever seen}. In learning mathematics,
there should be no place for arrogance and putting other people down.

\begin{hideinbutton}{Emergency access}
  For some reason the Twitter account of Mohammed El Mocro %(in the link above)
  has been suspended. Here is a salvaged copy of the original TikTok video that went viral.

\youtube{SqTtWIiS42o}
\end{hideinbutton}

\section{Computer algebra}

We will use the computer algebra system
\url{Sage}{http://www.sagemath.org/} in exploring and experimenting
with mathematics. This means that you will have to write small
commands and code snippets. Sage is built on top of the very wide
spread language
\url{Python}{https://en.wikipedia.org/wiki/Python_(programming_language)}
and you can in fact enter \footnote{Python code}{One may also enter code in several other languages, but I have so far only set the interface up for Sage and Python} in
the Sage input windows in this text. Below is an example of a basic
graphics command in Sage.  Push the Compute button to evaluate.

\begin{sage}
plot(sin(x) + cos(2*x), (x, 0, 2*pi))
\end{sage}

\beginshex
Did you notice that you can edit and enter new commands in the Sage window?
Do the following problems using Sage based on the \url{Sage guided tour}{http://doc.sagemath.org/html/en/tutorial/tour.html}.

\begin{enumerate}[(i)]
\item Consider $f(x) = x \sin(1/x)$. Plot the graph of $f$ from $0$ to $0.1$. Computing $f(0)$ does not make sense. Do you
  see a way of assigning a natural value to $f(0)$ using the graph?
\item Find an approximate solution with four decimals to the equation $\cos(x) = x$.

  \begin{hint}[showhide]
    This is an example of an equation, that can only be solved numerically.
    Try first plotting the graph of $f(x) = x - \cos(x)$ from $0$ to $1$. Then use a
    suitable function from the Sage guide.
    \end{hint}
\item Compute $\pi$ with $100$ decimals.
\end{enumerate}

\endshex

\section{Objects or elements and the symbols $=$ and $\neq$}

Mathematics can be broadly viewed as handling objects precisely
according to a specific system of rules. The first element
of precision is in distinguishing the objects and deciding when
they are the same. This calls for notation. If two objects
$x$ and $y$ are the same, we write $x = y$. If they are different
we write $x\neq y$.

You may laugh here, but identifying objects is really one of the fundamental tasks of mathematics.
It is not always that easy. Even though objects appear different they are the same as
in, for example
$$
\frac{105}{189} = \frac{35}{63}\qquad\text{and}\qquad \sin\left(\frac{\pi}{2}\right) = 1.
$$
The first example above is an identity of fractions (rational numbers). The second is
an identity, which calls for knowledge of the sine function and real numbers. Each of these
identities calls for some rather advanced mathematics.

\beginshex\label{sagecompex1}

\begin{sage}
var('a b')
e = (a+b)^2
e.expand()
\end{sage}

Use the Sage window above to reason
about equality in the quiz below. In each case describe the objects i.e.,
are they numbers, symbols, etc.? Also, please check your computations
by hand with the old fashioned paper and pencil, especially $(a+b)(a-b)$.

\begin{quiz}
\question
Click on the right equalities below.
\answer{T}
$$a + b - 2 b = a - b$$
\answer{F}
$$(a+b)^2 = a^2 + b^2$$
\answer{T}
$$(a + b)(a - b) = a^2 - b^2$$
\answer{T}
$$(a + b)^2 = a^2 + 2 a b +  b^2$$
\answer{F}
$$(a+b)^3 = a^3 + 2 a^2 b + 2 a b^2 + b^3$$
\answer{F}
$$\frac{3}{8} = \frac{5}{13}$$
\answer{F}
$$
\pi = \frac{22}{7}
$$
\answer{T}
$$
\cos^2(\pi) + \sin^2(\pi) = 1
$$
\end{quiz}
\endshex

\beginshex
You know that $(a+ b)^2 = a^2 + 2 a b + b^2$. Use Sage to find a similar identity
for $(a + b)^4$.

\begin{hint}[showhide]
  Go back and look at (the beginning of) Exercise \ref{sagecompex1}.
\end{hint}
\endshex

\section{Sets}

A set is (informally) a collection of distinct objects or \emph{elements}.

\begin{equation*}[emph]
  \text{Two sets are equal if they contain the same elements.}
\end{equation*}

An example of a set could be
the set $\{1,2,3\}$ of natural numbers between $0$ and $4$. Notice that we use the symbol
"$\{$" to start the listing of elements in a set and the symbol "$\}$" to denote the end of the listing.
Notice also that (by our definition of equality between sets), the order of the elements in the listing does not matter i.e.,
$$
\{1, 2, 3\} = \{2, 3, 1\}.
$$
We are also not allowing duplicates like for
example in the listing $\{1, 2, 2, 3, 3, 3\}$ (such a thing is called a \url{multiset}{https://en.m.wikipedia.org/wiki/Multiset}).

An example of a set not involving numbers could be the set of letters
$$
S=\{A, n, e, x, a, m, p, l, c, o, u, d, b, t, h, s, r, i\}
$$
used in this sentence. The number of elements in a set $S$ is called the \emph{cardinality} of the set.
We will denote it by $|S|$.

\beginshex
Give a precise reason as to why the two sets $\{1, 2, 3\}$ and $\{1, 2, 4\}$ are not equal.
Is it possible for a set with $5$ elements to be equal to a set with $7$ elements?
\endshex

Sets may be explored using Sage. This is illustrated in the Sage snippet below.

\begin{code}
X = Set([1, 2, 3])
Y = Set([2, 3, 1])
print("X=Y is ", X==Y)

S = Set(['A','n','e','x','a','m','p','l','c','o','u','d','b','t','h','s','r','i'])
print("S = ", S)
print("The number of elements in S is |S|=", S.cardinality())
\end{code}

\subsection{The empty set}

There is a unique set containing no or zero elements. This set is called the empty set and
is denoted $\emptyset$ i.e.,
$$
\emptyset = \{\}\qquad\text{and}\qquad |\emptyset| = 0.
$$
The empty set and its cardinality may be explored using the Sage code below.

\begin{code}
emptyset = Set([])
print(emptyset.cardinality())
\end{code}

\subsection{Sets of numbers}

A set could also be the natural numbers (yes, I want $0$ as a natural number:
$0$ is very natural, although it came late \url{historically}{https://en.wikipedia.org/wiki/0})
$$
\NN = \{0, 1, 2, 3, \dots\},
$$
or the set of integers
$$
\ZZ = \{\dots, -3, -2, -1, 0, 1, 2, 3, \dots\}.
$$
These sets are called infinite, since they contain infinitely many elements. Even though
the natural numbers seem as easy as one, two three, they contain wonderful and deep
mathematical mysteries, such as the nature and distribution of the prime numbers
$2, 3, 5, 7, 11, 13, 17, \dots$. Also please respect, that the \url{negative numbers}{https://en.m.wikipedia.org/wiki/Negative_number} like
$-3, -1\in \ZZ$ have caused confusion for centuries.

We also have
the set $\QQ$ of rational numbers (fractions) and the set
$\RR$ of real numbers. The real numbers
contains all the possible numbers that we encounter in
this course.

We will not define the
arithmetic operations (like addition and multiplication) on $\ZZ, \QQ$
and $\RR$
formally. I will assume that you know how to add and multiply fractions,
and that you \textbf{do not make mistakes like}
$$
\color{red}
\frac{1}{2} + \frac{2}{3} = \frac{1+2}{2+3}=\frac{3}{5}.
$$
Similarly, I will assume that you know that a rational number stays the
same, when the numerator and denominator is multiplied by the same non-zero
integer. For example,
$$
\frac{1}{2} = \frac{3}{6}\qquad\text{and}\qquad \frac{2}{3} = \frac{4}{6}.
$$
In fact,
$$
\frac{1}{2} + \frac{2}{3} = \frac{3}{6} + \frac{4}{6} = \frac{3 + 4}{6} = \frac{7}{6}.
$$
The computation above says that it is straightforward to add pizza slices of the
same size (one sixth), but that you need to think a bit when adding one half pizza slice and
two pizza slices of size one third.

\begin{quizexercise}[showhide]
  \begin{quiz}
\question
Click on the right equalities below. Do not use Sage (or any computer)!
\answer{F}
$$
\frac{1}{5} + \frac{1}{7} = \frac{1}{35}
$$
\answer{T}
$$
\frac{3}{7} + \frac{4}{7} = 1
$$
\answer{T}
$$
\frac{2}{3} + \frac{3}{2} - 2 = \frac{1}{6}
$$
\answer{F}
$$
\frac{1}{3} + 2 = \frac{8}{3}.
$$
\end{quiz}
\end{quizexercise}

\subsection{The symbols $\in$ and $\notin$}

The symbol $\in$ is ubiquitous in set theory (and mathematics).
It means \emph{belongs to} or \emph{is an element of} as in
$x\in A$, where $x$ is an element and $A$ is a set. The symbol
$\notin$ means is \textbf{not} an element of as in
$x\notin A$ meaning $x$ is not an element of $A$.

\begin{quizexercise}[showhide]
\begin{paraquiz}
  \question
  \box$\in$\box, but \box$\not\in$\box. This exercise actually has \box possible correct solutions
if $\{1, 2, 3\}$ is in the second empty box and $\{4, 5, 6\}$ in the fourth empty box.
  \answer
  $\{1, 2, 3\}$
  \answer
  $\{4, 5, 6\}$
  \answer
  $0$
  \answer
  $1$
  \answer
  $3$
  \answer
  $6$
  \answer
  $7$

  \case{(is 41326)}{T}
  \green{Correct!}
  \case{(is 41526)}{T}
  \green{Correct!}
  \case{(is 41726)}{T}
  \green{Correct!}
  \case{(is 51326)}{T}
  \green{Correct!}
  \case{(is 51426)}{T}
  \green{Correct!}
  \case{(is 51726)}{T}
  \green{Correct!}

  \default
  \red{Nope. Try again!}
\end{paraquiz}
\end{quizexercise}

Belongs to ($\in$) is straightforward in Sage:

\begin{code}
S = Set([1,2,3])
print("S = ", S)
print("The element 1 is in S: ", 1 in S)
print("The element 4 is in S: ", 4 in S)
\end{code}

\subsection{Subsets and the symbols $\subseteq$ and $\not\subseteq$}

If $A$ and $B$ are sets, then $A\subseteq B$ means that
every element of $A$ is also an element of $B$. In this case we say
that \emph{$A$ is a subset of $B$}.

We have
for example that
$$
\NN \subseteq \ZZ.
$$
What does $A\not\subseteq B$ mean? Here we have to be a little
careful. We want this notation to mean that $A$ is \textbf{not} a
subset of $B$. In order for $A\subseteq B$ to be
false, there must exist $x\in A$, such that $x\notin B$. This
is the meaning of $A\not\subseteq B$. For example,
$$
\ZZ\not\subseteq \NN,
$$
since $-1\in \ZZ$ and $-1\notin \NN$.

\begin{quizexercise}[showhide]
\begin{paraquiz}
  \question
  The set \box is not a subset of $A=$\box, simply because \box does not belong to $A$.
  This exercise actually has \box possible correct solutions.
  \answer
  $\{1, 2, 3\}$
  \answer
  $\{-1, 1, 2, 3, 4\}$
  \answer
  $\{-1, 0, 1, 2, 4\}$
  \answer
  $3$
  \answer
  $-1$
  \answer
  $5$
  \answer
  $6$
  \answer
  $0$
  \case{(is 1347)}{T} %13(3)
  \green{Correct!}
  \case{(is 2157)}{T} %21(-1)
  \green{Correct!}
  \case{(is 2347)}{T} %23(3)
  \green{Correct!}
  \case{(is 3287)}{T} %32(0)
  \green{Correct!}
  \case{(is 3157)}{T} %31 (-1)
  \green{Correct!}
  \case{(is 3187)}{T} %31 (0)
  \green{Correct!}
  \default
  \red{Nope. Try again!}
\end{paraquiz}
\end{quizexercise}

Below Sage will list all subsets of the set $\{1, 2, 3\}$. Before pressing
the Compute button, try to write them down on your own.

\begin{sage}
X = Set([1,2,3])
list(X.subsets())
\end{sage}

\begin{quizexercise}[showhide]
\begin{paraquiz}
  \question
  The empty set has \box elements. A set with \box elements has \box subsets. In general a set with
  $n$ elements has \box subsets.
  \answer
  $1$
  \answer
  $0$
  \answer
  $5$
  \answer
  $25$
  \answer
  $32$
  \answer
  $n^2$
  \answer
  $2^n$
  \case{(is 2357)}{T}
  \green{Correct!}
  \default
  \red{Nope. Try again!}
\end{paraquiz}
\end{quizexercise}

It turns out that the empty set $\emptyset$ is a subset of any set. Does this
make sense?

\subsection{Intersections, unions and the symbols $\cap,\,\, \cup$ and $\setminus$}

Suppose that we have two sets $A$ and $B$. Then the \emph{intersection} $A\cap B$ is the
set consisting of the elements in both $A$ and $B$. This is illustrated in the
socalled \url{Venn diagram}{https://en.wikipedia.org/wiki/Venn_diagram} below.

\includegraphics{vennintersection.svg}

The \emph{union} $A\cup B$ is the
set consisting of the elements in $A$ or $B$. To be more precise, an element is in
$A\cup B$ if it is in $A$ or in $B$ (or in both of them):

\includegraphics{vennunion.svg}

Lastly, the
difference $A\setminus B$ (between $A$ and $B$) consists of the elements
in $A$, the are not contained in $B$:

\includegraphics{venndifference.svg}

You should experiment using the Sage window below to get a feeling for these three operations.

\begin{sage}
A = Set([1, 2, 3, 4, 5, 6, 7, 8, 9])
B = Set([7, 8, 9, 10, 11, 12])
print("A =", A)
print("B =", B)
print("The intersection of A and B is ", A.intersection(B))
print("The union of A and B is ", A.union(B))
print("The difference between A and B is ", A.difference(B))
\end{sage}

\beginshex
Given two sets $A$ and $B$, is it true that
$A \cap B = B \cap A$ and $A\cup B = B\cup A$?

What about $A\setminus B = B\setminus A$?

Suppose that $A$ and $B$ are two finite sets. Is it true that
$$
|A\setminus B| = |A| - |B|?
$$
What about
$$
|A\cup B| = |A| + |B|?
$$
Seriously, both formulas are wrong. Can you come up with the correct
version of the formula for $|A \cup B|$?

Use your correct formula to find a formula for
$$
|A\cup B \cup C|
$$
viewing $A\cup B$ as the first set and $C$ as the second set. Here you need
the formula
$$
(A\cup B)\cap C = (A\cap C) \cup (B\cap C).
$$
Why is this formula true?
\endshex

\beginshex
There is one more operation called the symmetric difference between two sets $A$ and $B$. It is
denoted $A\, \Delta\, B$. Experiment in the Sage window below to find out exactly what it does.
Is it true that $A\, \Delta\, B = B\, \Delta\, A$?

\begin{sage}
A = Set([1,2,3,4])
B = Set([4,5,6,7])
print("A =", A)
print("B =", B)
print("The symmetric difference between A and B is ", A.symmetric_difference(B))
\end{sage}
\endshex

\begin{hideinbutton}{CS test on sets}

The following is an excerpt from the infamous \emph{Beredskabsprøve Datalogi}.

\begin{quiz}
\question
Let $X$ and $Y$ denote sets. Which of the following are true?
\answer{T}
$X \cup X = X$
\answer{T}
$X\cap X = X$
\answer{F}
$X\setminus X = X$
\answer{T}
$X\subseteq X\cap X$
\answer{T}
$\emptyset \subseteq X$
\answer{T}
For some sets $X$ and $Y$ we can have
$$
X\cap Y = X\cup Y.
$$
\end{quiz}
\end{hideinbutton}

\subsection{Pairs, triples and tuples}

Given two sets $A$ and $B$ we can form the new set $A\times B$,
which is the set of pairs $(a, b)$, where $a\in A$ and
$b\in B$. For example,
$$
\{1, 2\}\times \{1, 2, 3\} =
\{(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3)\}.
$$
The set $A\times B$ is also called the \url{Cartesian
  product}{https://en.wikipedia.org/wiki/Cartesian_product} of $A$ and
$B$.

\beginshex
Consider two pairs $(a, b)$ and $(c, d)$. What is a natural way of defining
equality between these pairs i.e., $(a, b) = (c, d)$?
\endshex

\begin{sage}
A = Set([1, 2])
B = Set([1, 2, 3])
C = cartesian_product([A, B])
print("A =", A)
print("B =", B)
print("The cartesian product of A and B is ", list(C))
\end{sage}

There is no need to restrict ourselves to tuples. We might as well
consider triples $A\times B\times C$ i.e.,
the set of all $(a, b, c)$, where $A$, $B$ and $C$ are sets, or
for that matter tuples
$$
(a_1, a_2, \dots, a_n)\in A_1\times A_2\times \cdots \times A_n
$$
of any length $n\in \NN$, where $a_1\in A_1, a_2\in A_2,
\dots, a_n\in A_n$. Based on the above example with tuples we have,
\begin{align*}
&\{0\}\times\{1, 2\}\times \{1, 2, 3\} = \\
&\{(0, 1, 1), (0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 2), (0, 2, 3)\}.
\end{align*}

You may check this using the Sage snippet below.

\begin{code}
A = Set([0])
B = Set([1, 2])
C = Set([1, 2, 3])
print("A =", A)
print("B =", B)
print("C =", C)
D = cartesian_product([A, B, C])
print("The cartesian product of A, B and C is ", list(D))
\end{code}

For a given set $A$ and $n\in \NN$ we define $n$-fold cartesian product of $A$ as
$$
A^n = \underbrace{A\times A\times \cdots \times A}_{n\text{ times}}.
$$
\begin{code}
A = Set([1,2])
n = 3
B = cartesian_product([A]*n)
print("A =", A)
print("n =", n)
print("The n-fold cartesian product of A is ", list(B))
\end{code}

\beginshex
Let $A$ and $B$ be two sets. Is $A\times B = B \times A$?

Let $X$ be any set. What is $\emptyset \times X$?

Let $A, B, C$ and $D$ be four sets. Is
$$
(A\times B)\setminus (C\times D) = (A\setminus C)\times (B\setminus D)?
$$
\endshex

\section{Ordering numbers}

Let us be a little rigorous and introduce the (usual) ordering
on our numbers with addition and multiplication using almost full blown
mathematical formalities. First the formal definition for two
integers $x, y\in \ZZ$:

\begin{equation}[emph]\label{ordZ}
x \leq y\qquad \text{ means that }\qquad y - x\in \NN
\end{equation}

Notice that $x = y$ implies that $x\leq y$ (and $y\leq x$).
Along this line we also define $x < y$ if $x \leq y$ and $x\neq y$.

\begin{quizexercise}[showhide]
\begin{orderquiz}
  \question
  Assume that $x, y, z\in \ZZ$ and that $x \leq y$. Then drag and drop the
  elements from the left to the right below to explain that
  $x + z \leq y + z$.
  \answer %1
  By assumption $x\leq y$.
  \answer %2
  This means that $z - x + y\in \NN$
  \answer %3
  This means that $y - x\in \NN$
  \answer %4
  To show that $x + z \leq y + z$, we need to show that
  $(y + z) - (x + z) \in \NN$.
  \answer %5
  But $(y + z) - (x + z) = y + z - x + z$. Therefore,
  \answer %6
  But $(y + z) - (x + z) = y + z - x - z = y - x$. Therefore,
  \answer %7
  $(y + z) - (x + z)\in \NN$, since
  \answer %8
  $y - x \in \NN$
  \expected{6}

  \case{(is 134678)}{T}
  \green{Spot on, my friend.}

  \case{(is 467813)}{T}
  \green{This is right!}

  \case{(is 413678)}{T}
  \green{This is right!}

  \default
  \red{Wrong order. Check the definition of $\leq$ in \eqref{ordZ} once more!}
\end{orderquiz}
\end{quizexercise}

You can
see that this definition agrees with our preconception that
\begin{equation}\label{ordwrong}
\cdots < -3 < -2 < -1 < 0 < 1 < 2 < \cdots
\end{equation}

\begin{exercise}[showhide]
  To be precise, writing $\cdots < -3 < -2 < -1 < 0 < 1 < 2 < \cdots$ is nonsense, since $\leq$ is only defined for two integers in \eqref{ordZ}. How is one supposed to interpret $0 < 1 < 2$ for example? Go ahead and write \eqref{ordwrong} the
  right way. Also, suppose \footnote{that}{As an example, this could be assuming $1 \leq 2$ and $2 \leq 5$ and then
    arguing that $1\leq 5$.}
  $$x \leq y\qquad\text{and}\qquad y\leq z
  $$
  for three integers $x, y, z\in \ZZ$. Argue
  from the definition in \eqref{ordZ} that $x\leq z$.

  How does Python/Sage interpret $-3 < -2 < -1< 0 < 1 < 2$? Find out using the Sage snippet below.

  \begin{code}
print(-3 < -2 < -1 < 0 < 1 < 2)
  \end{code}

  What about $1 < 5 > 3 < 4$?
\end{exercise}

Notice that the integers has huge holes. Given two integers $a, b\in \ZZ$, such
that $a < b$, we cannot always find an integer $c\in \ZZ$ in between $a$ and $b$:
$$
a < c < b.
$$

The rational numbers has the property that they do not have holes. We can
always find an in between number such as $c$ above. But we need a precise way
of comparing rational numbers. A way to explain precisely why for example
$$
\frac{2}{3}\,\, \leq \,\, \frac{5}{7}.
$$
Of course, you can enter the two numbers a on computer and see that
$\frac{2}{3}$ is approximately $0.67$ and $\frac{5}{7}$
is approximately $0.71$, but we aim for the mathematical
precise definition.

A
rational number $\frac{p}{q}$ consists of a numerator $p\in \ZZ$ and a denominator
$q\in \ZZ$ with $q > 0$. We already know the criterion for two rational numbers
$\frac{p}{q}$ and $\frac{p'}{q'}$
to be \footnote{equal}{Technically speaking we are defining a socalled \emph{equivalence relation} identifying the infinitely many ways of writing a rational number into one.}:

\begin{equation*}[emph]
\frac{p}{q}\, =\, \frac{p'}{q'}\qquad \text{ means that }\qquad p q' = p' q\qquad (\text{in }\ZZ).
\end{equation*}

We wish to compare the two rational numbers $\frac{p}{q}$ and $\frac{p'}{q'}$ deciding
precisely how they are ordered:

\begin{equation}[emph]
\frac{p}{q}\, \leq\, \frac{p'}{q'}\qquad \text{ means that }\qquad p q' \leq p' q\qquad (\text{in }\ZZ).
\end{equation}

As for the integers, we also define $x < y$ if $x \leq y$ and $x\neq y$ for
two rational numbers $x$ and $y$.

Using this definition, you can check that $\frac{2}{3} \leq \frac{5}{7}$, since
$$
2\cdot 7 < 3 \cdot 5.
$$

An easy, but surprising,
way of finding a rational number strictly between these two is
adding their numerators and denominators:
$$
\frac{2}{3} < \frac{2 + 5}{3 + 7} < \frac{5}{7}.
$$

We wil try to explain the first inequality in mathematical general terms going through a
rather formal proof consisting of five steps. These steps are
given in the quiz below. Your task is drag from the left and drop them to the right in an order,
so that the proof makes sense.

After that you are supposed, on your own, to write down a precise proof of
the second inequality.

\begin{quizexercise}[showhide]
\begin{orderquiz}
  \question
  Order the arguments below so that they constitute a coherent explanation of the
  statement that if
  $$
  \frac{a}{b} < \frac{c}{d},
  $$
  then
  $$
  \frac{a}{b} < \frac{a + c}{b + d}
  $$

  \answer %2
  By definition this means that $a d < b c$.

 \answer %1
  We are assuming that $\frac{a}{b} < \frac{c}{d}$.

  \answer %4
  For integers $x, y, z$ we know that the rule
  $
  x ( y + z) = x y + x z
  $
  holds. Therefore
  \answer %5
  we need to show that $a b + c d < b a + b c$.

  \answer %6
  Since $a b = b a$ and $a b + a d < a b + b c$ is a consequence of $a d < b c$, we are
  done if we know this is true.
  \answer %7
  However, this is a consequence of our assumption $\frac{a}{b} < \frac{c}{d}$.

\answer %3
  To show that
  $
  \frac{a}{b} < \frac{a+c}{b + d},
  $
  we need to argue that $a (b + d) < b (a + c)$.

  \answer

  we need to show that $a b + a d < b a + b c$.

  \expected{5}

  \case{(is 73856)}{T}
  \green{Spot on, my friend!}

  \case{(contains 4)}{F}
  \red{You have not applied the formula for expanding $x(y + z)$ correctly.}

  \default
  \red{Wrong order.}

\end{orderquiz}
\end{quizexercise}

\beginshex
Similarly to the quiz above, assume that
$$
  \frac{a}{b} < \frac{c}{d}.
  $$
  Write down a precise argument showing that
  $$
  \frac{a + c}{b + d} < \frac{c}{d}.
  $$
  You may seek inspiration in Video \ref{Video:proofexample} for
  how to mix math and words (even though
  it is further ahead).
  \endshex

\beginshex
On \url{Twitter}{https://twitter.com/Ramangupta4/status/1162999733142482945}, Raman Gupta posted the note below

\includegraphics[width="50\%"]{tweet.jpg}

For a natural number $m\in \NN$,
$$
m! = m (m-1) (m-2)\cdot \dots \cdot 2\cdot 1.
$$
For example, $3! = 6$ and $5! = 120$. What is the answer for the
question in the note?
\endshex

The exercise below shows that our trick for finding rational numbers
in between two given rational numbers can be made into a machine for
generating all positive rational numbers!

\beginshex
Can you spot the system in the fractions in the diagram below?
\includegraphics{SternBrocotTree.svg}
Once you see the system, extend the diagram with the next level downwards. Is every
positive fraction present in this diagram if one keeps adding levels?

\begin{hint}[showhide]
Suppose that
$$
\frac{p}{q} < \frac{r}{s}
$$
and $q r - s p = 1$. Then for
$$
\frac{p}{q} <  \frac{p+r}{q+s} < \frac{r}{s},
$$
we have $q (p+r) - (q+s) p = 1$ and $(q+s) r - (p + r) s = 1$. If $\frac{a}{b}$ is
a positive fraction, such that
$$
\frac{p}{q} <  \frac{a}{b} < \frac{r}{s},
$$
show that
$$
a + b = (r+s)(qa−bp)+(p+q)(br−as)\geq p+q+r+s.
$$
\end{hint}
\endshex

\subsection{Subsets of numbers and first elements}\label{subsecfirst}

In a set equipped with an order, it is intuitively clear what a first element should be. For example,
the natural numbers $\NN$ has $0$ as its first element. On the other hand the set $\ZZ$ of
integers does not have a first element (it is ``infinite to the left'').

In fact every non-empty subset $S\subseteq \NN$ has
a first element. This follows from a rather special property of $\NN$: there
can be only finitely many natural numbers smaller than a given one. This is
not true for $\ZZ$. Here there are infinitely many integers smaller than
any integer.

\beginshex
Consider the subset $S$ of $\QQ$ consisting of positive fractions i.e., rational numbers  $>0$.
Does this subset have a first element?
\endshex

\section{Propositional logic}

We have seen quite a few mathematical statements that ended up
being true or false. Such statements are called \emph{propositions}.
Here are two examples of propositions usings sets (in Sage):

\begin{sage}
print(1 in Set([1,2,3]))
A = Set([1, 2, 3])
B = Set([2, 3, 4])
print(A.intersection(B) == Set([2, 3]))
\end{sage}

\beginshex
What exactly are the two propositions in the above Sage window written
up in mathematical terminology? Notice that the symbol == is
a programming construct. It is not used in mathematics notation.
\endshex

Propositions can be combined into
new (compound) propositions. Take for example the propositions

\begin{align*}
&p: \text{it rains}\\
&q: \text{it is cloudy}.
\end{align*}

  Then ($p$ and $q$) is a perfectly good
  new proposition reading \emph{it rains and it is cloudy}. The same goes for (if $p$ then $q$), which reads
  \emph{if it rains then it is cloudy}. The proposition (if $q$ then $p$) reads \emph{if it is cloudy then
    it rains}. This proposition is (clearly) false.

We need some notation to describe these compound propositions:

\begin{equation*}
\begin{array}{ll}
p \land q\qquad\qquad & \qquad\qquad p \text{ and } q\\
\\
p \lor q\qquad\qquad & \qquad\qquad p \text{ or } q\\
\\
p\implies q\qquad\qquad & \qquad\qquad \text{if } p \text{ then } q\\
\\
\neg p\qquad\qquad & \qquad\qquad \text{not } p
\end{array}
\end{equation*}

The compound propositions are either true($t$) or false ($f$) depending on
$p$ and $q$. The dependencies are displayed in the \emph{truth tables} below.

\begin{equation*}[emph]
\def\arraystretch{1.2}
      \begin{array}{c|c|c}
        p & q  & p\land q  \\
        \hline
        t & t  & t    \\
        t & f & f\\
        f & t & f\\
        f & f & f
      \end{array}\qquad
      \begin{array}{c|c|c}
        p & q  & p\lor q  \\
        \hline
        t & t  & t    \\
        t & f & t\\
        f & t & t\\
        f & f & f
      \end{array}
      \qquad
      \begin{array}{c|c|c}
        p & q  & p\implies q  \\
        \hline
        t & t  & t    \\
        t & f & f\\
        f & t & t\\
        f & f & t
      \end{array}\qquad
      \begin{array}{c|c}
        p & \neg p \\
        \hline
        t & f\\
        f & t
      \end{array}
  \end{equation*}

The tables for the compound propositions $p\land q, p\lor q$ and also
$\neg p$ are not too hard to grasp. The table for $p\implies q$
raises a few more questions. Why is $f\implies t$ true?
I will not go into this, but just point out that there are
many explanations available online and,
perhaps more importantly, refer you to the exercise below.

\beginshex
Suppose that we are presented with four cards
\begin{equation}\label{cards}
\boxed{3}\qquad \fcolorbox{black}{red}{\phantom{3}}
\qquad\boxed{4}\qquad \fcolorbox{black}{blue}{\phantom{4}}
\end{equation}
with a (natural) number on the front and the color
\textcolor{blue}{blue} or \textcolor{red}{red} on the back.
In \eqref{cards}, the first and third cards are shown with their fronts facing up and
the second and fourth cards are shown with their backs facing up.

A claim (proposition) is made that if a card has an even number on the front, then it
must have the color \textcolor{blue}{blue} on the back.

Your task is to verify this for the cards above. Of course you can
do this by turning all four cards, but is there a way of checking this
by turning less than four cards?

What if we add the claim, that if a card has the color
\textcolor{blue}{blue} on the back, then
it must have an even number on the front?

\begin{hint}[showhide]
  Find two propositions $p$ and $q$ so that the claim reads
  $p\implies q$.
\end{hint}

\endshex

\beginshex
Explain why Python/Sage thinks that \footnote{the value}{Thanks to Gerth Brodal for pointing this out to me} of
  $$
  1 < 0 < 1/0
  $$
  is False! Notice that you are dividing one by zero in the last "integer" above.
\endshex

In the exercise below you will see for example that
$p\implies q$ is the same as $\neg q \implies \neg p$.

\beginshex\label{trutheq}
Two propositions are considered the same ($=$) if they have the same truth table. Verify, by
filling out and comparing truth tables, that
\begin{enumerate}[(i)]
\item
$\neg (p \land q) = (\neg p) \lor (\neg q)$
\item
$\neg (p \lor q) = (\neg p) \land (\neg q)$
\item
$p \implies q = (\neg q) \implies (\neg p)$
\item
$p \implies q = (\neg p)\lor q$
\end{enumerate}
\endshex

\beginshex
Can you use the setup up in Exercise \ref{trutheq} to verify that
$$
p\land (q \lor r) = (p\land q) \lor (p \land r)
$$
for three propositions $p, q$ and $r$? What about
$
p\lor (q \land r)?
$
\endshex

The notation $p \iff q$ is used frequently. It means that both
$p\implies q$ and $q\implies p$ are true i.e.,
$$
(p\implies q) \land (q\implies p).
$$

% \subsection{Predicates with variables}

% In mathematics it is natural to work with propositions with variables, such as $x > 0$. By itself
% the expression $x>0$ is hopelessly imprecise. We need to specify which numbers we wish to compare:
% natural numbers, rational numbers, $\dots$ i.e., what does $>$ really mean? Once this is done,
% we can substitute a number for $x$ and only then do we get a predicate!

% A precise way of writing, could be: for $x\in \ZZ$ we consider $p(x) = x > 0$.

% \subsection{Subsets defined using predicates}

\subsection{The symbols $\exists, \forall$ and propositions with variables}

In mathematics one usually reasons with propositions with variables.

In order to have a variable $x$, one must first specify to which set
$S$ the variable belongs. For example, the proposition $p(x)$ given by
$$
x^2 > 0,
$$
does not make sense if $x$ is taken from the set of letters in the
English alphabet (not unless you give an interpretation of $x^2$,
$>$ and $0$ in this set). However, if $x\in \ZZ$, then $p(x)$
certainly makes sense. Whether $p(x)$ is true depends on $x$.

For example, $p(0)$ is false, whereas $p(-1)$ is true. This leads us to
the existential and universal quantifiers $\exists$ and $\forall$.
The former reads \emph{there exists} and the latter \emph{for every}.

For example, the proposition
$$
\exists x\in \ZZ: \neg p(x)
$$
is true and so is
$$
\forall x\in \ZZ\setminus\{0\}: p(x).
$$
Notice that the  symbol ":" above means \footnote{"such that"}{Therefore
$\exists x\in \ZZ: \neg p(x)$ reads "there exists $x$ in $\ZZ$, such that
$\neg p(x)$ is true".}.

Also, $\forall x\in \ZZ: p(x)$ is false, because
$\exists x\in \ZZ: \neg p(x)$ is true. In general,
$$
\neg\left(\forall x\in S: p(x)\right) = \exists x\in S: \neg p(x).
$$
So we do not really need the quantifier $\forall$, when we have
$\neg$ and $\exists$, but $\forall$ is convenient and used all
the time.

The quantifiers are important to learn and apply when expressing
mathematical ideas. So is the use of propositions with variables
in writing up subsets: if $S$ is a set, $x$ a variable taking values
in $S$ and $p(x)$ a proposition (making sense in $S$), then
$$
\{x\in S \mid p(x)\}
$$
is the subset of the elements $x\in S$, such that $p(x)$ is true.

For example, if $p(x) = x^2 > 0$, then
$$
\{x\in \ZZ \mid p(x)\} = \ZZ\setminus \{0\}.
$$

\begin{hideinbutton}{More from CS}

The following is yet another excerpt from the infamous \emph{Beredskabsprøve Datalogi}.

\begin{quiz}
\question
Which of the following are true?
\answer{F}
$\forall x\in \NN: x > 2$
\answer{T}
$\exists x\in \NN: x > 2$
\answer{T}
$\forall x\in \emptyset: x = 7$
\answer{F}
$\exists x\in \emptyset: x = 7$
\end{quiz}
\end{hideinbutton}

\subsection{The use of implication ($\implies$) and bi-implication ($\iff$)}

Usually $\implies$ and $\iff$ are applied to link propositions in a logical argument. An example
is
$$
x \leq y \iff x + z \leq y + z
$$
for integers $x, y, z$. To be completely precise, I should here write
$$
\forall x, y, z\in \ZZ: x \leq y \iff x + z \leq y + z,
$$
but one often writes $\forall$ with words as for example \emph{for integers $x, y, z$}.

Here $x \leq y\implies x + z \leq y + z$ is true and similarly
$x + z \leq y + z \implies x \leq y$ (by using the definition (see \eqref{ordZ}) of $\leq$ in $\ZZ$). So the
use of $\iff$ is valid.

However, for $x \geq 0 \implies x^2 \geq 0$ we cannot link the two propositions by $\iff$,
simply because $x^2 \geq 0 \implies x \geq 0$ is false.

\section{What is a mathematical proof?}

Most professional mathematicians rarely think about the precise
definition of a proof. During many years of training they have
assimilated knowledge by experience. Therefore many proofs
seem born out of witchcraft containing several magical
devices.

However, many proofs appearing in
respected mathematical journals, submitted by respected mathematicians, have turned out to contain
errors. Recent developments in automated proof systems
like \url{Coq}{https://en.wikipedia.org/wiki/Coq} show
promise in checking proofs like for example the famous
\url{four color theorem}{https://en.wikipedia.org/wiki/Four_color_theorem}.

Informally a proof of a proposition $q$, consists in arguing that an implication $p\implies q$ is true by first assuming $p$. Usually this is done
not only through one implication $p\implies q$, but through a series
of intermediate implications
$$
p\implies q_1 \implies q_2 \implies q_3 \implies \cdots \implies q_N,
$$
where the last proposition $q_N$ is $q$. If $p$ is true, this
will constitute a proof that $q_N = q$ is true. Just like in \eqref{ordwrong},
there is an imprecision here. Can you tell what it is?

In this section we will illustrate a simple mathematical proof of the
proposition:
$$
\forall n\in \NN: p(n)\implies p(n^2),
$$
where $p(n) = (n\text{ is odd})$ i.e., the square of an odd
natural number has to be odd. This seems true for a first selection
of examples: $3^2=9, 5^2=25, \dots$.

First we need to know what $p(n)$ means. What does it mean
exactly for a number to be odd? This means that it is
not divisible by $2$ or that there exists another
natural number $a$, such that $n = 2 a + 1$. So
$$
p(n) = \exists a\in \NN: n = 2 a + 1.
$$
Therefore we need to show that
$$
\left(\exists a\in \NN: n = 2 a + 1\right) \implies
\left(\exists b\in \NN: n^2 = 2 b + 1\right).
$$
Notice that I had to change $a$ into $b$ in the second proposition above.
The two variables are not the same: $a$ is associated with $n$ and
$b$ is associated with $n^2$.

Let us assume that $n = 2 a + 1$. Now we need to argue that $
n^2 = 2 b + 1$ for some $b\in \NN$. You stare at this for a while
and notice that we should use the assumption $n=2 a + 1$ in
computing $n^2$:
$$
n^2 = (2 a + 1)^2 = (2 a)^2 + 2 (2 a) + 1^2 = 4 a^2 + 4 a + 1 =
2(2 a^2 + 2 a) + 1.
$$
Thus, using our assumption we may conclude that if $n = 2 a + 1$, then
$$
n^2 = 2 b + 1,
$$
where $b=2a^2+ 2 a$. This completes the proof.

The beauty here is that we have verified for all odd natural numbers
that their square is odd. Not just a finite selection like
$3, 7, 11, 13$.

Below I have given a very detailed walk through of the proof above. It
examplifies how to write up the proof mixing words and mathematics. In
many ways a proof is like a detailed argument in a court case, except
that the rules of mathematics are universal. You need the absolute truth.

\begin{video}\label{Video:proofexample}
\youtube{1tewESCAP2k}
\end{video}

\subsection{Proof by contradiction}

A proposition $p$ is either true or false. This seemingly obvious statement
goes by the name of \footnote{the law of excluded middle}{
An application could be in proving the existence of irrational numbers $\alpha$ and $\beta$, such that
$\alpha^\beta$ is rational. The law of excluded middle is here applied to the proposition
\emph{$\sqrt{2}^{\sqrt{2}}$ is rational}.}.

The law of excluded middle can be turned
into a powerful proof technique called \emph{proof by contradiction}.

Suppose we wish to establish that $p$ is true. Then we turn things upside down by
assuming that $p$ is false i.e., that $\neg p$ is true. If we then
by logical deduction can show that
$$
\neg p \implies q,
$$
for some proposition $q$, which is demonstrably false, then $\neg p$ cannot be true (since
true $\implies$ false is false). Therefore $\neg p$
must be false and $p$ must be true. This technique is used all the time!

\beginshex
We will give an example of a proof by contradiction using a previous exercise: show that
the set
$$
S = \{x\in \QQ \mid x > 0\}
$$
does not have a first element. Recall the definition of a first element in
the context of $S$: $x_0\in S$ is a first element if
$$
\forall x\in S: x_0 \leq x.
$$
So if $x_0$ is a first element in $S$, there cannot exist $x_1\in S$, such that
$x_1 < x_0$.

The proof by contradiction  in this case, runs as follows. Assume that $S$
has a first element $x_0 = \frac{p}{q}$. Then using $x_0$ we can form
$$
x_1 = \frac{p}{q+1},
$$
and you \footnote{can check}{Check that $p q < p(q+1)$.} that $x_1\in S$ and $x_1 < x_0$ i.e., $x_0$ is not a first
element. So our assumption that $S$ has a first element immediately leads
to the conclusion that $S$ does not have a first element. Therefore this
assumption has to be false, and $S$ cannot have a first element.
\endshex

\beginshex
Suppose that $q(n) = (n \text{ is even})$. Prove that
$$
\forall n\in \NN: q(n^2) \implies q(n).
$$
Suppose that
$$
\sqrt{2} = \frac{m}{n}.
$$
Show that this implies $2 n^2 = m^2$ and that $m$ and $n$ are even numbers.

Given the above, write up a precise proof that $\sqrt{2}\not\in \QQ$
using proof by contradiction.

You may wonder what is so special about rational numbers. Which property does
$\sqrt{2}$ break? You can explore this by looking at the decimal expansion of
some fractions below.

\begin{sage}
print("1/8 =", (1/8).n(digits=50))
print("3/17 = ", (3/17).n(digits=50))
print("5/7 = ", (5/7).n(digits=50))
print("")
print("sqrt(2) = ", sqrt(2).n(digits=100))
\end{sage}

However, $\sqrt{2}$ is an algebraic number being a root in the
polynomial $x^2 - 2$. In general an algebraic number is a number,
which is a root in a polynomial with coefficients in $\ZZ$.

\endshex

\subsection{Proof by induction}

A precocious Gauss proved the formula
\begin{equation}\label{gaussind}
1 + 2 + \cdots + n = \frac{n(n+1)}{2}
\end{equation}
at the age of seven diplaying remarkable ingenuity for his age. Lesser
mortals usually use induction to prove this formula. Gauss was asked
along with his classmates to compute the sum of all natural numbers
$1, 2, \dots, 100$. Using his formula he quickly came up with the correct
answer $5050$. His classmates had to work for the entire lesson.

Suppose that the formula in \eqref{gaussind} is viewed as a
proposition $p(n)$. To prove the formula we need to prove it for all
natural numbers (you can easily see that $p(1)$ and $p(2)$ are true) i.e.,
we need to prove
$$
\forall n\in \NN: p(n).
$$
An induction proof is a way of proving this statement by showing two things:
\begin{enumerate}[(i)]
\item
  $p(1)$
\item
  $\forall n\in \NN: p(n)\implies p(n+1)$
\end{enumerate}
These two statements ensure that $p(1) \implies p(2)$. Therefore
$p(2)$ must be true, since we assumed $p(1)$ true from the
beginning. Similarly $p(2)\implies p(3)$ ensures that $p(3)$
is true. In fact we have proved $p(n)$ for every $n\in \NN$
using this technique. One can prove this using proof by
contradiction and that every non-empty subset
of $\NN$ has a first element (see subsection \ref{subsecfirst} and below).

\begin{proof}[showhide]
Suppose by contradiction that there exists $n\in \NN$, such that
$p(n)$ is false. Then the subset
$$
S = \{n\in \NN \mid \neg p(n)\}\subseteq \NN
$$
is non-empty. Therefore it has a first element $n_0\in S$.
Here $n_0 > 1$, since $p(1)$ is assumed to be true. So we
know that $p(n_0-1)$ is true and that
$p(n_0-1)\implies p(n_0)$ is true. But the latter
implication is a contradiction, since true implies
false is false.
\end{proof}

Let us see how an induction proof plays out in the above example
with the statement $p(n)$ that
\begin{equation}\label{indant}
1 + 2 + \cdots + n = \frac{n(n+1)}{2}.
\end{equation}
Clearly $p(1)$ is true. We need to prove $p(n)\implies p(n+1)$, so
we assume that $p(n)$ holds i.e., that \eqref{indant} is true.
Then we may add $n+1$ to both sides of \eqref{indant} to get
$$
1 + 2 + \cdots + n + (n+1) = \frac{n(n+1)}{2} + (n+1).
$$
Here the right hand side can be rewritten as
$$
\frac{n(n+1) + 2(n+1)}{2} = \frac{(n+1)(n+2)}{2},
$$
which is exactly what we want. This is the conjectured formula for
the sum of the numbers $1, 2, \dots, n, n+1$. Therefore
we have proved that $p(n)\implies p(n+1)$ and the induction
proof is complete.

\begin{example}
  For a real number $r\neq 1$, the extremely useful formula
  \begin{equation}\label{geoind}
  1 + r + \cdots + r^n = \frac{1 - r^{n+1}}{1-r}
  \end{equation}
  holds. Let us prove this formula by induction. For $n=1$ this amounts to the identity
  $$
  1 + r = \frac{1-r^2}{1-r},
  $$
  which is true since $1-r^2 = (1+r)(1-r)$. We let $p(n)$ denote
  the identity in \eqref{geoind}. We have seen that $p(1)$ is true. The induction step
  consists in proving $p(n)\implies p(n+1)$. We can prove this
  by adding $r^{n+1}$ to the right hand side in \eqref{geoind}:
  $$
  \frac{1 - r^{n+1}}{1-r} + r^{n+1} = \frac{1 - r^{n+1} + (1-r) r^{n+1}}{1-r} = \frac{1 - r^{n+2}}{1-r}.
  $$
\begin{hideinbutton}{Real life application}
    In order to pay for a house you borrow $P$ DKK at an interest of
    $r$ per year. You want to pay off your debt over $N$ years by
    paying a fixed amount each year. How much is the fixed yearly
    amount you need to pay?

    Let us analyze the setup: suppose that the fixed yearly amount
    is $Y$. We will find an equation giving us $Y$ in terms of
    $P, N$ and $r$. Put $q = 1+ r$.

    After one year you owe
    $$
    q P - Y.
    $$
    After two years you owe
    $$
    q(q P - Y) - Y.
    $$
    After three years you owe
    $$
    q ( q ( q P - Y) - Y) - Y.
    $$
    In general after $n$ years you owe
    $$
    q^n P - Y (1 + q + \cdots + q^{n-1}).
    $$
    Since we want to be debt free after $N$ years, the yearly payment will have to satisfy
    $$
    q^N P = Y ( 1 + q + \cdots + q^{N-1}).
    $$
    By the formula \eqref{geoind}, we get
    $$
    q^N P = Y \frac{1-q^N}{1-q}.
    $$
    Here $Y$ can be isolated giving the formula
    $$
    Y = \frac{r P}{1 - \left(\frac{1}{1+r}\right)^N}.
    $$
    With the current (August 2020) interest rate around one percent, you pay a fixed monthly
    amount of around 3200 DKK for borrowing one million DKK over $30$ years.
  \end{hideinbutton}
\end{example}

\beginshex
Prove by induction that the sum of the first $n$ odd numbers is
given by the formula
$$
1 + 3 + \cdots + (2 n - 1) = n^2,
$$
i.e., for $n=5$ we have
$$
1 + 3 + 5 + 7 + 9 = 25.
$$
\endshex

\beginshex
Prove by induction that
$$
1^2 + 2^2 + 3^2 + \cdots + n^2 = \frac{n(n+1)(2n + 1)}{6}.
$$,
\endshex

\beginshex
Prove using the idea of induction that
$$
2^n < n!
$$
for $n\geq 4$.
\endshex

The last exercise related to induction concerns the famous \url{pigeonhole principle}{https://en.wikipedia.org/wiki/Pigeonhole_principle}. The statement itself looks innocent, well almost ridiculous, but it is very \url{powerful}{https://mindyourdecisions.com/blog/2008/11/25/16-fun-applications-of-the-pigeonhole-principle/}. Even the go-to website
\url{mathoverflow}{https://mathoverflow.net/} for research mathematicians has
a quite nice \url{thread}{https://mathoverflow.net/questions/4279/interesting-applications-of-the-pigeonhole-principle}
about this.

\beginshex
Prove the following by induction on $m$: if $n$ items are put into $m$ containers and
$n > m$, then at least one container must contain more than one item.
\endshex

\section{The concept of a function}

A function is a crucial concept in mathematics. In Sage (actually python here) a simple function can be
programmed like

\begin{code}
def f(n): return(n+1)
\end{code}

The code above seems to take a number and returns the number plus one. This (f) is in fact a function
taking as \emph{input} a number and returning as \emph{output} the number plus one. Notice that
we do not even know which numbers we are talking about here. In mathematics we need to have
a more precise notion of a function.

Mathematically a function $f$ takes values from a set $S$ and returns values in a set $T$. In details,
it is denoted $f: S\rightarrow T$ and the value associated with $s\in S$ is denoted $f(s)\in T$.

The above python function could more formally be denoted as $f: \ZZ\rightarrow \ZZ$ with
$f(n) = n+1$ if we are dealing with the integers, but we cannot tell from the code.

\begin{hideinbutton}{Well, to be fair ...}
To be completely fair, it is possible from Python version 3.5 to add type annotations to functions, so that we could write
%\begin{sage}
%def f(n: int) -> int: return(n+1)
%\end{sage}
\begin{code}
def f(n: int) -> int: return(n+1)
\end{code}
in the Python code to state that the function should take values in the integers and return integers.
\end{hideinbutton}

If you want the  super precise mathematical definition of a function, I
will give it here.  A function $f: S\rightarrow T$ is a subset
$f\subseteq S\times T$, such that
$(s, t_1)\in f \land (s, t_2)\in f \implies t_1 = t_2$. In words it states that a
function $f: S\rightarrow T$ is a subset $f$ of $S\times T$, containing pairs
having only one second coordinate for every first coordinate.

The everyday working definition of a
function is more intuitive: a machine taking input from some set
$S$ and giving output in some set $T$. The uniqueness of the output
is encoded in the mathematical definition of a function.

\beginshex
Write down precisely how the truth table for $p\implies q$ may
be expressed in terms of a function $f: S\rightarrow T$. What are the sets $S$ and $T$ in this case?
\endshex

\subsection{Composition of functions}

Given two functions $f: S\rightarrow T$ and $g: U\rightarrow V$, where
$V\subseteq S$, we define a new function $f\circ g: U \rightarrow T$ by
$$
(f\circ g)(u) = f(g(u)).
$$
This notion calls for some reflection. We have a total of four sets
in this definition: $U, V, S$ and $T$ and, not to forget, the condition that
$V\subseteq S$. If this last condition was not satisfied it would be
meaningless to apply the function $f$ to $g(u)$.
I hope the diagram below helps the
understanding.

\includegraphics{compositefunction.svg}

\begin{remark}
  The concept of a function is powerful and underlies functional programming in computer science: every computation can be realized as applying a composition of functions to an argument. This is examplified in the computer language
  \url{Haskell}{https://www.haskell.org/}.
  \end{remark}

\beginshex
Consider $f: \RR\rightarrow \RR^2$ and $g: \RR^2\rightarrow \RR$ given by
\begin{align*}
  f(t) &= (t^2, t^3)\\
  g((x, y)) &= \cos(x y) + x \sin(x + y).
\end{align*}
What is $(g\circ f)(t)$ as a function from $\RR$ to $\RR$ in terms of $t$?
\endshex

\subsection{Neural networks}

Having defined functions and composition of functions, we can deflate
the term (deep) neural network, which is often clouded in
magic and mystery.

A \emph{neural network} is a special case of a function
\begin{equation}\label{neural}
f: A\rightarrow B,
\end{equation}
where $A\subseteq \RR^m$ and $B\subseteq \RR^n$. Neural networks are
often compositions of many intermediate functions called
(hidden) layers.

A function such as \eqref{neural} can
be written
$$
f(x_1, \dots, x_m) = \left(
f_1(x_1, \dots, x_m), \dots, f_n(x_1, \dots, x_m)\right),
$$
where $f_1, \dots, f_n$ are functions $A\rightarrow \RR$.

In a neural
network the functions $f_1, f_2, \dots, f_n$ are viewed as neurons. Depending on their
input they either fire or do not fire a signal. Classically this is
modelled by the \url{perceptron}{https://en.wikipedia.org/wiki/Perceptron},
which is a function $p:\RR^n\rightarrow \RR$ of the form
$$
p(x_1, \dots, x_n) =
\begin{cases}
  1 &\text{if } w_1 x_1 + \cdots + w_n x_n > b\\
  0 &\text{if } w_1 x_1 + \cdots + w_n x_n \leq b
\end{cases}
$$
  for fixed numbers $w_1, \dots, w_n$ (called weights) and a number $b$ (called the threshold).
  If the weighted sum $w_1 x_1 + \cdots + w_n x_n$ is above the threshold, the neuron
  fires (returns the value $1$). If not it does not fire (returns the value $0$).

\includegraphics{perceptron.svg}

  \beginshex
  Consider the three perceptrons $p_1, p_2, p_3: \RR^2\rightarrow \RR$, where
  $$
p_1(x, y) =
\begin{cases}
  1 &\text{if } -x-y > -\frac{3}{2}\\
  0 &\text{if } -x-y \leq -\frac{3}{2}
\end{cases},
\qquad
p_2(x, y) =
\begin{cases}
  1 &\text{if } x + y > \frac{1}{2}\\
  0 &\text{if } x + y \leq \frac{1}{2}
\end{cases},
$$
and
$$
p_3(x, y) =
\begin{cases}
  1 &\text{if } x + y > \frac{3}{2}\\
  0 &\text{if } x + y \leq \frac{3}{2}
\end{cases}.
$$
Let $f(x, y) = p_3 (p_1(x, y), p_2(x, y))$. Then $f$ is
a composite function $f = g\circ h$ of two functions $h: \RR^2\rightarrow \RR^2$
and $g: \RR^2\rightarrow \RR$. Write down these functions.

Compute
$f(0, 0), f(1, 0), f(0, 1)$ and $f(1, 1)$.

Relate the perceptrons $ p_1 $ and $ p_2 $ to the illustration
below. What do you think the red and blue line illustrate?  What does
it mean that a dot is solid compared to hollow? What is special
about points between the red and blue lines?  Try to relate $f(0,0),
f(1,0), f(0,1)$ and $f(1,1)$ to the illustration.

\includegraphics{whkperceptron.svg}

(Illustration courtesy of William Heyman Krill).

\endshex

  \beginshex
  Give weights $w_1, w_2$ and a threshold $b$ for a perceptron $p:\RR^2\rightarrow \RR$ that computes
  the logical and function $\land$ i.e, $p$ must satisfy
  \begin{align*}
    p(0,0) &= 0\\
    p(1, 0) &= 0\\
    p(0,1) &= 0\\
     p(1, 1) &= 1.
  \end{align*}
  Do the same for the logical or function $\lor$.
  \endshex

  The output of one neuron can be used as input for other neurons in a potentially extremely complicated network:

  \includegraphics{deepneural.png}

  The diagram above represents a neural network, which is a function $\RR^8\rightarrow \RR^4$. This function
  is actually a composition (represented by the hidden layers $1$, $2$, $3$ and the output layer):
  $$
  \RR^8\rightarrow \RR^9 \rightarrow \RR^9 \rightarrow \RR^9 \rightarrow \RR^4.
  $$
  All of the nodes above, except the ones in the input layer, represent perceptrons.

  \beginshex
  Is it possible to find a perceptron $p:\RR^2\rightarrow \RR$, such that
    \begin{align*}
    p(0,0) &= 0\\
    p(1, 0) &= 1\\
    p(0,1) &= 1\\
     p(1, 1) &= 0?
  \end{align*}
  What if you are allowed to use a neural network composed as $\RR^2\rightarrow \RR^2\rightarrow \RR$ (one hidden layer)
  \includegraphics{xor.svg}?
  \endshex

  Mathematically there is no reason to use special functions such as perceptrons in each node. One also uses
  a (smooth) version of the perceptron employing the \url{sigmoid function}{https://en.wikipedia.org/wiki/Sigmoid_function}.
  With the notation above, this function is given as
  $$
  \sigma(x_1, \dots, x_n) = \frac{1}{1 + e^{-(w_1 x_1 + \cdots + w_n x_n) - b}},
  $$

\end{document}


\section{Deterministic Variational Approach}
There are two main parts for computing the reconstruction term: propagation of distributions through activations to compute $\tilde{q}(\mathbf{a}^L)$, and evaluation of unparameterized log-likelihood $\mathcal{L}$. In Fig~\ref{fig:feed_arch} we can see the general architecture used to accomplish these tasks.

\begin{figure}[H]
\centering
\includegraphics[scale=.5]{fig/dvi-architect-p1.png}
\caption{Feed-forward architecture for reconstruction term computation.}
\label{fig:feed_arch}
\end{figure}

\noindent\textbf{Moment Propagation.} \\
We can consider the model, $\mathcal{M}$, as a set of layers each containing an non-linear and affine transformation,
$$
\mathcal{M} := \{(\bm{h}^l,\bm{a}^l): \bm{h}^l = f(\bm{a}^{l-1}), \bm{a}^l = \bm{h}^l\bm{W}^l + \bm{b}^l\}_{l=1}^{\mathbb{N}}
$$

where $\{\bm{W},\bm{b}\}\subset\bm{\omega}$ are random variables representing the weights and are assumed independent per layer. $\bm{a}^l$ is argued to be Gaussian under the Central Limit Theorem given a sufficiently large latent space and finite $1^{st}$ and $2^{nd}$ moment since it is formulated as the linear combination of the elements of $\bm{h}^l$. Given that $\bm{a}^l$ is Gaussian we can appoximate the $1^{st}$ and $2^{nd}$ moment,

\begin{equation}
\langle a_i\rangle = \langle h_j\rangle\langle W_{ji}\rangle + \langle b_i\rangle
\end{equation}
$$
\text{Cov}(a_i,a_k) =
$$
\begin{equation}
\langle h_jh_l\rangle\text{Cov}(W_{ji},W_{lk})+ \langle W_{ji}\rangle\text{Cov}(h_j,h_l)\langle W_{lk}\rangle + \text{Cov}(b_i,b_k)
\end{equation}

where $\langle a_i\rangle := \mathbb{E}_{q}[a_i]$ and $h_jW_{ji} = \sum_{j=1}^nh_jW_{ji}$ is called Einstein notation. To reduce approximation, gaussian distributions are considered for the mean and covariance of the weights so that all that is left determine are the moments $\langle h_j\rangle$ and $\langle h_jh_l\rangle$

\begin{equation}
\langle h_j\rangle \propto \int f(\alpha_j)\exp\bigg[-\frac{(\alpha_j-\langle a_j^{l-1}\rangle)^2}{2\Sigma_{jj}^{l-1}}\bigg]d\alpha_j
\end{equation}

\begin{equation}
\langle h_jh_l\rangle \propto \int f(\alpha_j)f(\alpha_l)\exp\bigg[-\frac{1}{2}\zeta^T\Lambda^{-1}\zeta\bigg]d\alpha_jd\alpha_l
\end{equation}

$$
\zeta = \begin{pmatrix} \alpha_j - \langle a_j^{l-1}\rangle\\ \alpha_l - \langle a_l^{l-1}\rangle\\\end{pmatrix}
$$

$$
\Lambda = \begin{pmatrix} \Sigma_{jj}^{l-1} & \Sigma_{jl}^{l-1}\\ \Sigma_{lj}^{l-1} & \Sigma_{ll}^{l-1}\\\end{pmatrix}
$$

 Closed form solutions exist for (4) when considering Heaviside or ReLU non-linearity for $f$. For (5), we can approximate the moment through,

\begin{equation}
\langle h_jh_l\rangle = S_{jl}^{l-1}\bigg\{A(\mu_j^{l-1},\mu_{l}^{l-1},\rho_{jl}^{l-1})+\exp[-Q(\mu_j^{l-1},\mu_l^{l-1},\rho_{jl}^{l-1})]\bigg\}
\end{equation}

where the key idea is that the asymptotes $A$ of the non-linearities as well as the residuals $Q$ in the form of a polynomial provide a good first order approximation of the moment. Fig~\ref{fig:approx} provides a visual representation of this process. Due to CLT these approximations provide sufficient information for us to explicitly determine $\tilde{q}(\bm{a}^L)$ through sequential distribution propagation.

\begin{figure}[H]
\centering
\includegraphics[scale=.5]{fig/activations.png}
\caption{Model activation function approximation.}
\label{fig:approx}
\end{figure}

\noindent\textbf{Log-Likelihood Evaluation.} \\
We can evaluate the expected log-likelihood $\mathbb{E}_{\bm{\omega}\sim q}\big[\log p(y \mid \bm{x},\bm{\omega})\big]$ through directly evaluating $\mathbb{E}_{\bm{a}^L\sim q(\bm{a}^L)}\big[\log p(y \mid \bm{a}^L)\big]$ since $q(y \mid \bm{a}^L)$ is a parameter free transformation.

\section{Empirical Bayes for Variational BNNs}

Considering a $d$-dimensional Gaussian prior, $p(\bm{\omega}) = \mathcal{N}(\mu_p,\Sigma_p)$, and variational distribution, $q = \mathcal{N}(\mu_q,\Sigma_q)$, the KL divergence has the form,

\begin{equation}
\frac{1}{2}\bigg[\log\frac{\det(\Sigma_p)}{\det(\Sigma_q)} - d + \text{tr}(\Sigma_p^{-1}\Sigma_q) + (\mu_p - \mu_q)^T\Sigma_p^{-1}(\mu_p - \mu_q)\bigg]
\end{equation}

Rather than using this directly the authors propose conditioning the prior on a hyper-parameter $\bm{s}$ such that $\bm{\omega}\sim p(\bm{\omega}\mid\bm{s}); \bm{s} \sim p(\bm{s})$, where $\bm{s}$ is distributed according to a inverse gamma distribution and acts as a conjugate prior for the diagonal gaussian variance. Further through partioning the weights $\bm{\omega}$ into sets $\{\lambda\}$ such that an element $s_\lambda$ of $\bm{s}$ can be assigned to each set,
$$
s_\lambda \sim \text{Inv-Gamma}(\alpha,\beta), \quad w_i^\lambda \sim \mathcal{N}(0,s_\lambda)
$$
we can consider solving the MAP optimization problem for the KL divergence,

$$
s^*_\lambda = \argmin_{s_\lambda} KL\bigg[q(\bm{\omega};\bm{\theta})||p(\bm{\omega}^\lambda\mid s_\lambda) - \log p(s_\lambda)\bigg]
$$

This leads to the closed-form solution,
$$
s^*_\lambda = \frac{\text{tr}(\Sigma_q^\lambda+\mu_q^\lambda(\mu_q^\lambda)^T)+2\beta}{\Omega_\lambda + 2\alpha + 2}
$$

where $\Omega_\lambda := |\lambda|$. We can then use $s^*_\lambda$ to determine the diagonal entries of $\Sigma_p$ and solve (1).

\begin{figure}[H]
\centering
\includegraphics[scale=.5]{fig/empirical-bayes.png}
\caption{Test Log-likelihood with tuned prior (orange) and EB (blue).}
\label{fig:emp_bayes}
\end{figure}


% ------------------------------------------------------------------
\documentclass[12 pt]{article}
\newcommand\ignore[1]{}
\usepackage[left]{lineno}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{cancel}
 \usepackage{graphicx}
\usepackage{braket}
\usepackage{authblk}
\usepackage{caption,subcaption}
\usepackage{comment}
\usepackage{enumitem}
\pagestyle{plain}
\pagenumbering{arabic}
\usepackage{color}
\newcommand{\rcb}[1]{\textcolor{blue}{  #1 }}
\newcommand{\rcbfoot}[1]{\textcolor{red}{**\footnote{\textcolor{blue}{ \sc COMMENT REMOVE LATER *** #1 ***}}}}
\pdfpagewidth 8.5 in
\pdfpageheight 11 in
\setlength{\parindent}{0 mm}
\setlength{\parskip}{10pt}
\setlength{\abovedisplayskip}{0 pt}
\setlength{\belowdisplayskip}{0 pt}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[margin = .75 in]{geometry}
\usepackage[pdftex, pdfstartview={FitH}, pdfnewwindow=true, colorlinks=false, pdfpagemode=UseNone]{hyperref}

% Laziness shortcuts

\newcommand\dd{\partial}
\newcommand{\nn}{\nonumber \\}
\newcommand\be{\begin{equation}}
\newcommand\ee{\end{equation}}
\newcommand\bea{\begin{eqnarray}}
\newcommand\eea{\end{eqnarray}}
\newcommand{\<}{\langle}
\renewcommand{\>}{\rangle}
\newcommand\half{ \textstyle {\frac{1}{2}}}
% ------------------------------------------------------------------
\bibliographystyle{unsrt}

% ------------------------------------------------------------------
\begin{document}

\begin{center}
 \Large \bf 2D U(1) Gauge Slab Action
\end{center}

\section{Massless $\phi^4$ Theory}
Consider the 1D central line of a 1D $\phi^4$ theory. One may ask the question "what is the effective action on the central line if we integrate out the action from the non-central lines?"

We shall refer to coordinates on the $x$- plane simply as $x$ and coordinates in the extra dimension as $s$. Hence, a lattice point will in general be given by $(x,s)$. The full action given in terms of the discrete Laplace operator is
\be
S =  \frac{1}{2} \sum_{x,s} (\phi(x+a,s) - \phi(x,s))^2 + \frac{1}{2}\sum_{x}\sum_{s=-L_{s}/2}^{L_s/2-1}(\phi(x,s+a) - \phi(x,s))^2
\ee
so we are asking the question

\section{Non-compact Gaussian Action}

Here is the 2d $L^2$ action  with $L_s + 1$  slices: $s = 0, \pm 1, \cdots L_s/2$.
%
\be
S =  \frac{1}{4} \sum_{x,s}   \sum_{\mu,\nu} F_{\mu \nu}(x,s) F_{\mu
  \nu}(x,s)  + \frac{1}{2} \sum_{\mu,\nu} E_\mu(x,s) E_\mu(x,s)
\ee
where
\be
F_{\mu \nu}(x,s)  = \Delta_\mu \theta_\nu(x,s) - \Delta_\nu \theta_\mu(x,s) \nn
 = ( \theta_\nu(x +\mu,s) - \theta_\nu(x,s) ) - ( \theta_\mu(x +\nu,s) - \theta_\mu(x,s) )
\ee
and
\be
E_\mu(x,s) = \Delta_s \theta_\mu(x,s) = \theta_\mu(x,s+1)  -\theta_\mu(x,s)
\ee
Therefore,
\be
S =  \frac{1}{2} \sum_{x,s}  \sum_{\mu <\nu} ( ( \theta_\nu(x +\mu,s)
- \theta_\nu(x,s) ) - ( \theta_\mu(x +\nu,s) - \theta_\mu(x,s) ))^2
+ \frac{1}{2} \sum_{x,s} \sum^{L_s/2 -1}_{s= -L_s/2} (\theta_\mu(x,s+1)  -\theta_\mu(x,s) )^2
\ee
We can go to momentum space by a unitary transformation :
\be
\theta_\mu(x,s) =  \frac{1}{(2 \pi)^2}\int^\pi_{-\pi} d^2k   e^{i x k}\widetilde\theta_\mu(k,s) \quad
\mbox{and} \quad  \widetilde \theta_\mu(k,s) = \frac{1}{L}\sum_{x \in Z}  e^{-i x k} \theta_\mu(x,s)
\ee
and
\be
\Delta_\mu \theta_\nu(x,s)
= \frac{1}{L}\sum_k(e^{ik_\mu} - 1)  e^{i x k} \widetilde
\theta_\nu(k,s)
\ee

or defining $ (e^{ik_\mu} - 1) =  i \hat k_\mu $ this  gives,
\bea
S &=&  \frac{1}{2} \sum_{k,s}\sum_{\mu <\nu} [ \hat k^*_\mu  \widetilde \theta^*_\nu(k,s) -
\hat k^*_\nu   \widetilde \theta^*_\mu(k,s) ]  [ \hat k_\mu  \widetilde \theta_\nu(k,s) -
\hat k_\nu   \widetilde \theta_\mu(k,s) ]  \nn
&+& \frac{1}{2} \sum^{L_s/2 -1}_{s= -L_s/2}  \sum_{k,\mu} (\widetilde\theta^*_\mu(k,s+1) -\widetilde\theta^*_\mu(k,s))(\widetilde\theta_\mu(k,s+1)  -\widetilde\theta_\mu(k,s) )
\eea
(Note in 2D  $\mu = x$, and $\nu = y$ so there is no sum at all!)
The quadratic form is
\be
S = \frac{1}{2} \widetilde \theta^*_\mu(k,s)
M_{\mu\nu}(k)\theta_\nu(k,s)  - \frac{1}{2} [\widetilde
\theta^*_\mu(k,s) \theta_\mu(k,s+1) + \widetilde
\theta^*_\mu(k,s+1) \theta_\mu(k,s)]
\ee
Since it is of course diagonal in k, the sum over $k$ implicit.

We now integrate all {\bf but} the zero-th central slice. Formally separating
calling the thetas on the midels slide $\widetilde \theta(k,0) \equiv
\widetilde \theta_\mu(0) $ we don the
 integral for over the others  $\Theta_{\mu s}(k) = \widetilde \theta_\mu(k,s \ne 0)$'s; to get the effective action in
$k-space$:
%
\bea
&& e^{\textstyle - S_{eff}} \nn
 &=& e^{\textstyle -\frac{1}{2} \widetilde \theta^*_\mu(k,0)
M_{\mu\nu}(k) \widetilde \theta_\nu(k,0) }  \nn &\times &\int
d^2\Theta_{\mu s}(k)
e^{  \textstyle -\frac{1}{2}  \Theta^\dag_{\mu s}(k) G^{ss'}_{\mu\nu} (k)
  \Theta_{s'\nu}(k)  + \frac{1}{2}[\widetilde \theta^*_\mu(k,0) (
  \Theta_{1,\mu}(k) + \Theta_{-1,\mu}(k))  +(\Theta^\dag_{1,\mu} + \Theta^\dag_{-1,\mu} )\widetilde \theta_\mu(k,0)]}\nn
&=& e^{\textstyle -\frac{1}{2} \widetilde \theta^*_\mu(k,0)
M_{\mu\nu}(k) \widetilde \theta_\nu(k,0)  +  \frac{1}{2} \widetilde
\theta^*_\mu(k,0) ([1/G(k)]^{11}_{\mu\nu} (k)  +[1/G(k)]^{-1-1}_{\mu\nu} (k)]\widetilde \theta_\nu(k,0) }
\eea
%
\end{document}


\chapter{Rotor-dynamic analysis}

\noindent
From this point onwards, the tail-rotor behaviour has been investigated implementing a simplified FEM model and taking advantage of the Rotordynamics capabilities of Ansys. \\
From the definition of ANSYS help, \textit{Rotor-dynamics is the study of vibrational behaviour in axially symmetric rotating structures}. At high rotational speeds, such as in a helicopter's tail rotor, the inertia effects of the rotating parts must be consistently represented in order to accurately predict the rotor behaviour. An important part of the inertia effects is the \underline{gyroscopic moment} introduced by the precession motion of the rotor which is function of the spin velocity. Hence, the velocity term in the equation of motion as well as the support flexibility and damping behaviour cannot be neglected and they are important factors in enhancing the stability of the vibrating rotor.

\section*{Modal analysis of rotating structures}
\addcontentsline{toc}{section}{Modal analysis of rotating structures}
\noindent
The modal analysis allows for the calculation of natural frequencies and critical speeds (Campbell diagram) of the rotor. \\
From dynamical point of view, the equations of motion of a generic rotating structure is:
\medskip

\begin{equation*}
\left[ M \right] \left\lbrace \ddot{u} \right\rbrace + \left( \left[ C \right] + \left[ G \right] \right) \left\lbrace \dot{u} \right\rbrace + \left( \left[ K \right] - \left[ K_c \right] \right) \left\lbrace u \right\rbrace = \left\lbrace 0 \right\rbrace
\end{equation*}

\medskip
\noindent
where [G] is the gyroscopic matrix that depends on the rotational velocity and is the major contributor to tailboom's rotor, while [$K_c$], the spin softnening matrix, also depends upon the rotational velocity and it modifies the apparent stiffness of the structure. \\
This equation holds when motion is described in a stationary reference frame.

\clearpage
\noindent
\underline{STEPS FOR MODAL ANALYSIS IN ANSYS}: \\
\begin{itemize}
	\item 1) Model implementation;

	\item 2) Boundary conditions;

	\item 3) Solution including rotational effects (centrifugal and Coriolis);

	\item 4) Postprocessing. \\
\end{itemize}

\subsection*{Tail rotor simplified model}
\addcontentsline{toc}{subsection}{Tail rotor simplified model}
\noindent
A simplified model of the tail rotor has been defined, and built up in apart macro. It consists in the following parts:
\begin{itemize}
	\item \textbf{SHAFT}: elastically supported shaft modelled with BEAM 188 elements;
	\item \textbf{ROTOR'S HUB}: modelled with a lumped mass and inertia concentrated in the center of the rotor attached to a master node;
	\item \textbf{ROTOR}: modelled with a circular ring of SHELL 181 elements with radius passing through to the center of mass of the blades. CERIG elements have been introduced in order to connect the master node (hub) to the slave nodes of the ring.
\end{itemize}

\medskip
\begin{figure}[h]
	\centering
	\subfloat[][\emph{real tail rotor assembly}.]
	{\includegraphics[width=.45\textwidth]{PICTURES/2_Lama_truss/PNG/model2/hqdefault2}} \quad
	\subfloat[][\emph{analysis model}.]
	{\includegraphics[width=.45\textwidth]{PICTURES/5_Rotordynamics/scheme.png}}\\
	\caption{Schemes of tail rotor assembly's model}
\end{figure}
%\vspace{0.5cm}

\subsection*{Model assumptions}
\addcontentsline{toc}{subsection}{Model assumptions}
\begin{itemize}
	\item Axial-symmetric structure (rotor-dynamics requirement in Ansys);
	\item Linear elastic material properties;
	\item Rotor elastically supported (2 bearings);
	\item Aerodynamic loads not considered;
	\item Rigid rotor and connections (no hinges or flexible joints).
\end{itemize}

\subsection*{Applied boundary conditions}
\addcontentsline{toc}{subsection}{Applied boundary conditions}
\noindent
\begin{itemize}
	\item Only fixed constraints are allowed for rotor-dynamic analysis;
	\item Support elasticity modelled using COMBIN14 elements to represent bearings;
\end{itemize}

\noindent
\textbf{COMBIN14} \\
Bearings have been modelled with COMBIN14 element whose properties simulate the effect of a longitudinal spring-damper as a axial tension-compression element.
The element is created between two nodes which, in our case, are overlapped. One of the nodes is rigidly attached to the shaft while the other one is constrained on the ground. These elements allows the elastic movement of the shaft in the Y and Z directions. The bearing is composed by 7 balls that ensure an overall stiffness equal to $378e+7$ N/m.\\

\medskip
\begin{figure}[h]
	\begin{center}
		\centering
		\includegraphics[width=0.55\linewidth]{PICTURES/5_Rotordynamics/2.png}
	\end{center}
	\caption {Tail rotor simplified model}
\end{figure}
%\vspace{0.5cm}

\clearpage
\subsection*{Solution including rotational effects (centrifugal and Coriolis)}
\addcontentsline{toc}{subsection}{Solution including rotational effects (centrifugal and Coriolis)}
\noindent
The modal analysis must be solved using an algorithm for damped modal analysis (complex Eigenvalues and Eigenvectors). We have chosen the \textbf{QRDAMP} solver including the rotational effects (\textbf{CORIOLIS, ON, , , ON}), as reported in listing (\ref{list:SolutionChunk}). The rotation speed (2000 RPM) has been divided in several load-steps. 10 modes have been extracted from each step.

\lstinputlisting[firstline=133, lastline=146, language=apdl-modified,label={list:SolutionChunk}, caption=solution including rotational effects]{./COMMAND_LISTS/SOLO_ROTOR.txt}

\subsection*{Postprocessing}
\addcontentsline{toc}{subsection}{Postprocessing}
\noindent
Rotor's natural frequencies have been calculated for each value of the rotation speed (hence for each load step) and assigned to a substep. Resulting natural frequencies vary with the rotor speed as it is displayed in the campbell diagram below. \\
A \textbf{critical speed} appears when the natural frequency is equal to the excitation frequency, and excitation may come from unbalance that is synchronous with the rotational velocity. \\
Critical speeds are directly determined by solving a new eigenvalue problem or by performing a Campbell diagram analysis, where the intersection points between the frequency curves and the excitation line are calculated.

\noindent
The rotational velocity of the rotor is specified via the \textbf{CMOMEGA} command which requires to specify which is the rotating component, previously defined and selected as input for the velocity vector (magnitude and direction). \\
Then, we can set the \textbf{CAMPBELL, ON} Ansys' command. \\

\noindent
\textbf{NOTE:} \\
Bearing stiffness has an important effect on the critical speeds. When analysing a rotor, it is important to understand the effect of the bearing stiffness on the critical speeds and this can be done drawing the "Critical Speed Map" (here neglected).

\vspace{5mm}
\noindent
The rotor has been achieved with some parameters determined after performed a static analysis on ANSYS. The resulting given matrix provide us the information needed for establish some following conclusions:

\begin{equation*}
\left[ I \right] = \begin{bmatrix} 8.6440 & 0.1476 \times 10^{-3} & -0.3815 \times 10^{-4} \\ 0.1476 \times 10^{-3} & 14.487 & -0.1114 \times 10^{-3} \\ -0.3815 \times 10^{-4} & -0.1114 \times 10^{-3} & 13.895
\end{bmatrix} \quad
\end{equation*}

\vspace{3mm}
\noindent
As can be seen from the preceding matrix [\textit{I}], the diagonal guys refers to polar ($I_{p1}$ = 8.6440 $kg*m^2$) and diametrical inertia ($I_{d2}$ = 14.487 $kg*m^2$, $I_{d3}$ = 13.895 $kg*m^2$), respectively. From the literature, we expected 4 critical speeds, thanks to the fact that $I_{d}$ > $I_{p}$ for thick disc.
The results match with our expectation as we can notice from the Campbell plot below:

\medskip
\begin{figure}[h]
	\begin{center}
		\centering
		\includegraphics[width=1\linewidth]{PICTURES/5_Rotordynamics/campbell1.png}
	\end{center}
	\caption{Campbell diagram}
	\label{fig:critical speed}
\end{figure}
%\vspace{0.5cm}

\begin{table}[h!]
	\centering
	\pgfplotstableset{
		% global config, for example in the preamble
		% these columns/<colname>/.style={<options>} things define a style
		% which applies to <colname> only.
		every head row/.style={before row=\hline,after row=\hline},
		every last row/.style={after row=\hline},
		display columns/0/.style={column name =Num, int detect,column type=r},
		display columns/1/.style={column name =Critical Speed [rpm], column type=r,
			fixed,fixed zerofill,precision=5,set thousands separator={\,}},
		%other style option
	}
	\pgfplotstabletypeset[col sep=space]{VelocCritic-ModalAnalisys.txt}
	\caption{Natural frequencies for the simple model}
	\label{tab:ModalFreq-Shellmodel}
\end{table}
%
\noindent The fourth critical speed isn't shown in the figure~\ref{fig:critical speed} because is much higher than the full scale (2000 rpm), which value representing the nominal rotational speed of the rotor. We can even say that, with these results, it would be not advisable to design a rotor which is expected to operate at a velocity that stands in the middle of 2 of its critical speeds, since this range is typically UNSTABLE and inside it the onset of self-excited vibration phenomenon cannot be prevented.\\
However, this model does not represent the real tail rotor but it is just a very simplified model with the idea of explore the Ansys rotordynamics capabilities.\\
Hence, we can conclude that this value of speed appears unsafe from a dynamical point of view. In these cases, in order to be a rigid rotor, the literature suggests to change the geometrical parameters of the rotors, e.g. by adding or subtracting suitable masses or essentially \underline{by made torsionally stiffer the rotor shaft}. The idea of increase the stiffness of the shaft, from our point of view, could be a smart solution to solve this issue. Anyway, we are completely aware of the roughly results and so we can go on with the remaining analysis. One brief and further consideration is given relative to the resulting orbit motion of the rotor (whirling), that turns out by the gravitational static imbalance affecting the rotor structure and produces rotating bending of the shaft.

\medskip
\begin{figure}[h]
	\begin{center}
		\centering
		\includegraphics[width=0.45\linewidth]{PICTURES/5_Rotordynamics/ModalAnalisys004.png}
	\end{center}
	\caption{Orbital motion of the rotor shaft}
\end{figure}

\subsection*{Coupling analysis}
\addcontentsline{toc}{subsection}{Coupling analysis}
\noindent Once realized the rotor, one attempt was given on mounting that system upon the main structure, in order to verify the rotor-tailboom coupling, starting with the hints written on the chapter~\ref{ch:Rotor-fuselage dynamic coupling}. Here, one goal is to check the vibrations exerted by the gravitational static imbalance of the rotor on the tailboom main structure and spend some words regarding modal shape and natural frequencies of the overall assembly.

\medskip
\begin{figure}[h]
	\centering
	\subfloat[][Truss model]
	{\includegraphics[width=0.45\linewidth]{PICTURES/5_Rotordynamics/TrussTailLumpedRotorRun003.png}} \quad
	\subfloat[][Shell model]
	{\includegraphics[width=0.45\linewidth]{PICTURES/5_Rotordynamics/ShellmodelShaftLumped005.png}}
	\caption{Rotor - fuselage coupling}
	\label{Rotor - fuselage coupling}
\end{figure}

\noindent
Unfortunately, rotordynamics in ANSYS has no advanced tools to study asymmetric structures and, as we mentioned before, it performs and solve frameworks that are only axisymmetric respect to one principal axis. Indeed, we tried to follow this way, but ANSYS provide only information relative to the sole rotor (as we can observe from the figure~\ref{Rotor - fuselage coupling}), without take into account the coupling effect. This software lack pushed ourselves to don't proceed the analysis forward, even though could be a future matter of investigation.


\documentclass[11pt]{amsart}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{fp}  % Prevents issues with arithmetic overflow.
\usepackage{pgfplots}
\usepackage{xcolor}
\usepackage[hidelinks]{hyperref}
\usepackage[section]{placeins}  % Prevents figure placement outside of section.
\usetikzlibrary{arrows, fixedpointarithmetic}

\newcommand{\shaft}{\mathrm{shaft}}
\newcommand{\kiteshaft}{\mathrm{kite-shaft}}
\newcommand{\ground}{\mathrm{ground}}
\newcommand{\tether}{\mathrm{tether}}
\newcommand{\bus}{\mathrm{bus}}

\definecolor{matlab1}{rgb}{0, 0.4470, 0.7410}
\definecolor{matlab2}{rgb}{0.8500, 0.3250, 0.0980}
\definecolor{matlab3}{rgb}{0.9290, 0.6940, 0.1250}
\definecolor{matlab4}{rgb}{0.4940, 0.1840, 0.5560}
\definecolor{matlab5}{rgb}{0.4660, 0.6740, 0.1880}
\definecolor{matlab6}{rgb}{0.3010, 0.7450, 0.9330}
\definecolor{matlab7}{rgb}{0.6350, 0.0780, 0.1840}

\title{Motor basics}
\author{Makani Technologies LLC}

\begin{document}
\maketitle

\section{Basic three-phase motor equations}

The basic equations that describe a three-phase motor circuit are
%
\begin{eqnarray}
  \label{eqn:motor_abc}
  v_a &=& R_s i_a + \frac{d\lambda_a}{dt} \\
  v_b &=& R_s i_b + \frac{d\lambda_b}{dt} \\
  v_c &=& R_s i_c + \frac{d\lambda_c}{dt}
\end{eqnarray}
%
where $v_i$ and $i_i$ are the individual phase voltages and currents
and $R_s$ is the single phase stator resistance.  The flux linkages,
$\lambda_i$, are defined such that their derivative is the induced
voltage, and they include both the effects from the self-inductance of
the motor coils as well as the back EMF induced by the permanent
magnets.

\section{Park transformation}

The Park transformation is an \textit{amplitude invariant} coordinate
transformation\footnote{There is a power invariant form of this
  coordinate transformation, which is an orthogonal rotation in a
  vector space spanned by orthogonal $a$, $b$, and $c$ axes about the
  $[1,1,1]^T$ vector.}  to a frame that rotates with the rotor (see
Fig. \ref{fig:dq0}).  Because it is amplitude invariant, the maximum
phase voltage and current remain the same; however, the naive torques
and powers calculations must be scaled by a factor of 3/2.

\begin{figure}[!htb]
  \begin{center}
    \begin{tikzpicture}
      \begin{scope}[scale=4]
        % Draw a, b, c axes.
        \draw [dashed, arrows={-latex}] (0, 0) -- (1, 0) node[below] {$a$};
        \draw [dashed, arrows={-latex}] (0, 0) -- ({-1/2}, {sqrt(3)/2}) node[above] {$b$};
        \draw [dashed, arrows={-latex}] (0, 0) -- ({-1/2}, {-sqrt(3)/2}) node[below] {$c$};

        \begin{scope}[rotate=45]
          % Draw d, q axes.
          \draw [arrows={-latex}] (0, 0) -- (1, 0) node[below] {$d$};
          \draw [arrows={-latex}] (0, 0) -- (0, 1) node[above] {$q$};

          % Draw magnet.
          \draw (0, -0.1) rectangle (0.25, 0.1) node[pos=0.5, rotate=-45] {N};
          \draw (-0.25, -0.1) rectangle (0, 0.1) node[pos=0.5, rotate=-45] {S};
        \end{scope}

        % Draw omega * t.
        \draw [arrows={-latex}] (0.4, 0) arc (0:45:0.4);
        \path (0.4, 0) arc (0:25.5:0.4) node[right] {$\omega_e t$};
      \end{scope}
    \end{tikzpicture}
  \end{center}
  \caption{\label{fig:dq0}Representation of the Park transformation.}
\end{figure}

The transformation is defined by
%
\begin{equation}
  \label{eqn:park}
  \begin{bmatrix}
    f_d \\
    f_q \\
    f_0 \\
  \end{bmatrix}
  = \frac{2}{3}
  \begin{bmatrix}
    \cos (\theta_e)  & \cos (\theta_e - \frac{2 \pi}{3})  & \cos (\theta_e + \frac{2 \pi}{3}) \\
    -\sin (\theta_e) & -\sin (\theta_e - \frac{2 \pi}{3}) & -\sin (\theta_e + \frac{2 \pi}{3}) \\
    \frac{1}{2}    & \frac{1}{2}                        & \frac{1}{2} \\
  \end{bmatrix}
  \cdot
  \begin{bmatrix}
    f_a \\
    f_b \\
    f_c \\
  \end{bmatrix}
\end{equation}
%
where $\theta_e = \omega_e t = p \theta_m$ is the electrical angle of
rotation, and $f_i$ may either be the phase voltages or currents or
the flux linkages.

Also, note that the Park transformation matrix,
$\mathbf{K}(\theta_e) = \mathbf{K}(\omega_e t)$, is a function of
time, so derivatives of parameters transform as
%
\begin{equation}
  \label{eqn:park_deriv}
  \dot{\vec{f}}_{dq0} = \mathbf{K}(\theta_e) \dot{\vec{f}}_{abc} +
                        \omega_e \frac{d \mathbf{K}(\theta_e)}{d\theta_e} \vec{f}_{abc}
\end{equation}

\section{Circuit equations in dq0 coordinates}

Using the Park transformation (Eq. \ref{eqn:park}) and its derivative
(Eq. \ref{eqn:park_deriv}), it is possible to transform the basic
three-phase motor equations (Eq. \ref{eqn:motor_abc}) into a frame
that rotates with the rotor.  Notably, because of the derivative, the
flux linkages transform as
%
\begin{equation}
  \frac{d}{dt}
  \begin{bmatrix}
    \lambda_d \\
    \lambda_q \\
    \lambda_0 \\
  \end{bmatrix}
  +
  \omega_e
  \begin{bmatrix}
    -\lambda_q \\
    \lambda_d \\
    0 \\
  \end{bmatrix}
  = \mathbf{K}(\theta_e) \cdot \frac{d}{dt}
  \begin{bmatrix}
    \lambda_a \\
    \lambda_b \\
    \lambda_c \\
  \end{bmatrix}
\end{equation}
%
Thus, the basic three-phase motor equations may be rewritten in the
rotating frame as
%
\begin{eqnarray}
  v_d &=& R_s i_d + \frac{d\lambda_d}{dt} - \omega_e \lambda_q \\
  v_q &=& R_s i_q + \frac{d\lambda_q}{dt} + \omega_e \lambda_d
\end{eqnarray}
%
In the rotating frame, the flux linkages may be written in terms of
constant inductances along the $d$- and $q$-axes, $L_d$ and $L_q$, as
well as a component due to the permanent magnets, $\lambda_m$, along
the $d$-axis.
%
\begin{eqnarray}
  \lambda_d &=& L_d i_d + \lambda_m \\
  \lambda_q &=& L_q i_q
\end{eqnarray}
%
Finally, the motor dynamics may be written in matrix form as
%
\begin{equation}
  \frac{d}{dt}
  \begin{bmatrix}
    i_d \\
    i_q \\
  \end{bmatrix}
  =
  \begin{bmatrix}
    -R_s / L_d          & \omega_e L_q / L_d \\
    -\omega_e L_d / L_q & -R_s / L_q \\
  \end{bmatrix}
  \cdot
  \begin{bmatrix}
    i_d \\
    i_q \\
  \end{bmatrix}
  +
  \begin{bmatrix}
    1/L_d & 0 \\
    0     & 1/L_q \\
  \end{bmatrix}
  \cdot
  \begin{bmatrix}
    v_d \\
    v_q - \omega_e \lambda_m \\
  \end{bmatrix}
\end{equation}

The steady-state form of these equations,
%
\begin{equation}
  \label{eqn:voltage_limit}
  \begin{bmatrix}
    v_d \\
    v_q \\
  \end{bmatrix}
  =
  \begin{bmatrix}
    R_s          & -\omega_e L_q \\
    \omega_e L_d & R_s \\
  \end{bmatrix}
  \cdot
  \begin{bmatrix}
    i_d \\
    i_q \\
  \end{bmatrix}
  +
  \begin{bmatrix}
    0 \\
    \omega_e \lambda_m \\
  \end{bmatrix}
\end{equation}
%
define a family of ellipses in the phase current plane.  The ellipse
defined by the maximum single phase voltage,
$|v_{dq}| < m \cdot v_{\bus} / \sqrt{3}$, where $m$ is the maximum
modulation index, defines the accesible region of the phase current
plane for a given $\omega_e$.  The center of this voltage-limit
ellipse is given by the short-circuit current:
%
\begin{equation}
  \vec{i}_{sc} =
  \frac{-\omega_e \lambda_m}{R_s^2 + \omega_e^2 L_d L_q}
  \begin{bmatrix}
    \omega_e L_q \\
    R_s
  \end{bmatrix}
\end{equation}
%
Equation \ref{eqn:voltage_limit} may be grossly simplied by assuming,
$L_d = L_q$ (i.e. non-salient motor), and $\omega_e L \gg R_s$.
%
\begin{equation}
  \frac{1}{\omega_e L}
  \begin{bmatrix}
    v_d \\
    v_q \\
  \end{bmatrix}
  =
  \begin{bmatrix}
    0 & -1 \\
    1 & 0 \\
  \end{bmatrix}
  \cdot
  \begin{bmatrix}
    i_d \\
    i_q \\
  \end{bmatrix}
  +
  \begin{bmatrix}
    0 \\
    \lambda_m / L \\
  \end{bmatrix}
\end{equation}
%
In this limit, the relationship between the phase voltage and current
is clear.  The voltage limit circle is centered at
$\vec{i}_{dq} = [-\lambda_m / L, 0]^T$ and has radius
$v_{\max} / \omega_e L$.  The phase voltage axes are rotated
$-\frac{\pi}{2}$ rad from the phase current axes (see
Fig. \ref{fig:voltage_limit}).

\begin{figure}[!htb]
  \begin{center}
    \begin{tikzpicture}[fixed point arithmetic]
      \begin{scope}[scale=0.012]
        \def\Rs{0.103};
        \def\L{0.868e-3};
        \def\lambdam{0.1666};
        \def\Npp{15};
        \def\imax{225};  % [A]
        \def\axislen{300};

        \def\omegam{100};
        \def\omegae{(\omegam * \Npp)};
        \def\znorm{(\Rs * \Rs + \L * \omegae * \L * \omegae)};
        \def\idcenter{(-\omegae * \omegae * \L * \lambdam / \znorm)};
        \def\iqcenter{(-\Rs * \omegae * \lambdam / \znorm)};
        \def\vdqmax{(850.0 / sqrt(3) * 0.95 * 0.94)};
        \def\iradius{(\vdqmax / sqrt(\znorm))};
        \def\iqfw{sqrt(\iradius * \iradius - \idcenter * \idcenter)};
        \def\idsc{(-\lambdam / \L)};

        % Draw axes.
        \draw [arrows={latex-latex}] (-\axislen, 0) -- (\axislen, 0)
              node[below] {$i_d$};
        \draw [arrows={latex-latex}] (0, -\axislen) -- (0, \axislen)
              node[right] {$i_q$};

        % Draw phase current magnitude limit.
        \draw [dashed] (0, 0) circle (\imax);
        \draw [arrows={-latex}] ({1.2 * \imax * cos(45) + 50},
                                 {1.2 * \imax * sin(45)})
                                node[right] {$i_{\max}$} --
                                ({1.2 * \imax * cos(45)},
                                 {1.2 * \imax * sin(45)}) --
                                ({\imax * cos(45)},
                                 {\imax * sin(45)});

        % Draw phase voltage axes.
        \draw [arrows={-latex}] ({\idsc}, 0) -- ({\idsc + 50}, 0) node[below] {$v_q$};
        \draw [arrows={-latex}] ({\idsc}, 0) -- ({\idsc}, -50) node[right] {$v_d$};
        \draw [fill] ({\idsc}, 0) circle (5) node[above right] {$\vec{i}_{sc}$};
        % $(-\frac{\lambda_m}{L}, 0)$};

        % Draw voltage limit circles.
        \def\omegam{120};
        \draw ({\idcenter}, {\iqcenter}) circle ({\iradius});
        \draw [arrows={-latex}] ({1.2 * \iradius * cos(150) - 50 + \idcenter},
                                 {1.2 * \iradius * sin(150) + \iqcenter})
                                node[left] {$\frac{v_{\max}}{\omega_e L}$} --
                                ({1.2 * \iradius * cos(150) + \idcenter},
                                 {1.2 * \iradius * sin(150) + \iqcenter}) --
                                ({\iradius * cos(150) + \idcenter},
                                 {\iradius * sin(150) + \iqcenter});

        % Draw increasing speed path.
        \def\nudge{5};
        \draw [blue!75, densely dotted, arrows={-latex}]
              (-\nudge, 0) -- (-\nudge, {\iqfw - 3*\nudge})
              node [midway, left] {a};
        \draw [blue!75, densely dotted, arrows={-latex}]
              (-\nudge, {\iqfw - 3*\nudge})
              arc
              ({atan2(\iqfw, -\idcenter) + 5}:{atan2(\iqfw, -\idcenter) + 13}:{\iradius})
              node [below] {b}
              coordinate (end);
        \draw [blue!75, densely dotted, arrows={-latex}] (end) arc (100:125:\imax)
              node [below right] {c} arc (125:150:\imax);
        \draw [blue!75, densely dotted, arrows={latex-}] ({\idsc}, \nudge) -- ({\idsc}, 100)
              node [right] {d};

        % Draw higher speed voltage limit circles.
        \def\omegam{150};
        \draw [lightgray] ({\idcenter}, {\iqcenter}) circle ({\iradius});

        \def\omegam{187.5};
        \draw [lightgray] ({\idcenter}, {\iqcenter}) circle ({\iradius});

        %
        \draw [gray, arrows={-latex}]
              ({\idcenter + (\iradius + 90) * cos(110)}, {\iqcenter + (\iradius + 90) * sin(110)})
              node [gray, above left] {increasing $\omega_e$} --
              ({\idcenter + (\iradius - 50) * cos(110)}, {\iqcenter + (\iradius - 50) * sin(110)});

        \draw [gray, arrows={-latex}] (100, 220) -- (100, 300) node [right] {increasing torque};
      \end{scope}
    \end{tikzpicture}
  \end{center}
  \caption{\label{fig:voltage_limit}Phase voltage limits in the phase
    current plane for a non-salient motor in the limit of low stator
    resistance.  The solid circle is the phase voltage limit, whose
    radius decreases as the rotor speed increases.  The dashed circle
    describes the phase current limit, which is determined by gate
    heating.  The blue line shows a typical path for increasing rotor
    speed and power: a) the torque increases, b) flux weakening is
    used to further increase the torque, c) the phase current limit is
    hit but the motor speed is still increasing, and d) the motor is
    in the constant power regime.}
\end{figure}

The equation for torque in $dq0$-coordinates is
%
\begin{eqnarray}
  \tau_e &=& \frac{3}{2} p (\lambda_d i_q - \lambda_q i_d) \\
         &=& \frac{3}{2} p \lambda_m i_q + \frac{3}{2} p (L_d - L_q) i_q i_d \\
         &\approx& \frac{3}{2} p \lambda_m i_q
\end{eqnarray}
%
where $p$ is the number of pole pairs.  The equation for power is
%
\begin{equation}
  P_e = \frac{3}{2} (v_d i_d + v_q i_q)
\end{equation}
%
which under steady-state conditions is
%
\begin{equation}
  P_e = \frac{3}{2}
  \left(
    R_s (i_d^2 + i_q^2) + \omega_e (L_d - L_q) i_q i_d + \omega_e \lambda_m i_q
  \right)
\end{equation}

\begin{table}
  \begin{tabular}{lllll}
    \hline
    \hline
    Parameter                    & Symbol      & Value   &         & Units \\
                                 &             & Protean & Yasa    & \\
    \hline
    Number of pole pairs         & $p$         & 32      & 15      & \# \\
    d-axis inductance            & $L_d$       & 165     & 1000    & $\mu$H \\
    q-axis inductance            & $L_q$       & 165     & 1000    & $\mu$H \\
    Stator resistance            & $R_s$       & 0.041   & 0.103   & $\Omega$ \\
    Permanet magnet flux linkage & $\lambda_m$ & 0.06203 & 0.16667 & N$\cdot$m/A \\
                                 &             &         &         & or V$\cdot$s/rad \\
    \hline
    \hline
  \end{tabular}
  \caption{Motor parameters\protect\footnotemark.}
\end{table}
%
\footnotetext{See
       {\texttt{config/m600/power\_sys\_sim.py}} and \\
       {\texttt{avionics/motor/firmware/params.c}}
}

\section{Space vector pulse width modulation}

The phase voltage vector is controlled using a space vector pulse
width modulation scheme \cite{broek} (see Fig. \ref{fig:svpwm}).

\begin{figure}[ht]
  \begin{center}
    \begin{tikzpicture}
      \begin{scope}[scale=5]
        % Draw d, q axes.
        \draw [dashed, arrows={-latex}] (0, 0) -- (1, 0) node[below] {$v_d$};
        \draw [dashed, arrows={-latex}] (0, 0) -- (0, 1) node[above] {$v_q$};

        % Draw circle.
        \draw [lightgray] (0, 0) circle ({1/sqrt(3)});
        \draw [arrows={latex-}]
              ({1/sqrt(3) * cos(30)}, {1/sqrt(3) * sin(30)}) --
              ({1.2/sqrt(3) * cos(30)}, {1.2/sqrt(3) * sin(30)})
              node [right] {$v_{\bus} / \sqrt{3}$};

        % Draw hexagon.
        \draw (2/3, 0) -- (1/3, {1/sqrt(3)}) -- (-1/3, {1/sqrt(3)}) --
              (-2/3, 0) -- (-1/3, {-1/sqrt(3)}) -- (1/3, {-1/sqrt(3)}) --
              (2/3, 0);
        \draw [fill] (0, 0) -- (2/3, 0) circle (0.01)
              node [above right] {(100)};
        \draw [arrows={latex-}] (2/3, -0.03) -- (2/3, -0.15)
              node [right] {$\frac{2}{3} v_{\bus}$};
        \draw [fill] (0, 0) -- (1/3, {1/sqrt(3)}) circle (0.01)
              node [above right] {(110)};
        \draw [fill] (0, 0) -- (-1/3, {1/sqrt(3)}) circle (0.01)
              node [above left] {(010)};
        \draw [fill] (0, 0) -- (-2/3, 0) circle (0.01)
              node [above left] {(011)};
        \draw [fill] (0, 0) -- (-1/3, {-1/sqrt(3)}) circle (0.01)
              node [below left] {(001)};
        \draw [fill] (0, 0) -- (1/3, {-1/sqrt(3)}) circle (0.01)
              node [below right] {(101)};

        \draw [fill] (0, 0) circle (0.01)
              node [below left] {(000)} node [below right] {(111)};

        \path (0, 0) -- ({0.25 * cos(30)}, {0.25 * sin(30)}) node {I};
        \path (0, 0) -- ({0.25 * cos(90)}, {0.25 * sin(90)}) node {II};
        \path (0, 0) -- ({0.25 * cos(150)}, {0.25 * sin(150)}) node {III};
        \path (0, 0) -- ({0.25 * cos(210)}, {0.25 * sin(210)}) node {IV};
        \path (0, 0) -- ({0.25 * cos(270)}, {0.25 * sin(270)}) node {V};
        \path (0, 0) -- ({0.25 * cos(330)}, {0.25 * sin(330)}) node {VI};

        % Draw PWM vector.
        \draw [arrows={-latex}] (0, 0) -- ({0.5 * cos(20)}, {0.5 * sin(20)});
        \draw [dashed] ({0.5 * sin(20) * tan(30)}, {0.5 * sin(20)}) --
                       ({0.5 * cos(20)}, {0.5 * sin(20)}) node [midway, above] {$t_1$};
        \draw [dashed] ({0.5 * (cos(20) - sin(20) * tan(30))}, 0) --
                       ({0.5 * cos(20)}, {0.5 * sin(20)}) node [midway, right] {$t_2$};

      \end{scope}
    \end{tikzpicture}
  \end{center}
  \caption{\label{fig:svpwm}Space vector pulse width modulation.  The
    six corners of the hexagon represent six of the on-off states of
    the three switches on one side of the H-bridge.}
\end{figure}

\section{Motor limits}

\subsection{Source power limit}

The total shaft power of the motors is limited by the maximum ground
power as well as the efficiency losses from the tether, inverters, and
motors.  This constraint is expressed as:
%
\begin{equation}
  \sum_i P_{\shaft, i} < \eta_{\kiteshaft}
  \left( 1 - \frac{P_{\ground} R_{\tether}}{V_{\ground}^2} \right) P_{\ground}
\end{equation}

\begin{table}
  \begin{tabular}{llll}
    \hline
    \hline
    Parameter & Symbol & Value & Units \\
    \hline
    Kite-bus-to-shaft efficiency    & $\eta_{\kiteshaft}$ & 0.88 & \# \\
    Ground power (SatCon I)         & $P_{\ground}$       & 660  & kW \\
    Ground power (SatCon II)        & $P_{\ground}$       & 1000 & kW \\
    Tether resistance               & $R_{\tether}$       & 0.8  & $\Omega$ \\
    Ground voltage                  & $V_{\ground}$       & 3400 & V \\
    Phase current limit             & $i_{dq,\max}$       & 225  & A \\
    Flux weakening modulation limit & $m_{\mathrm{fw}}$   & 0.95 & \# \\
    Gate hold time modulation limit & $m_{\mathrm{gate}}$ & 0.94 & \# \\
    \hline
    \hline
  \end{tabular}
  \caption{Power source and motor limitation parameters\protect\footnotemark.}
\end{table}

\footnotetext{See {\texttt{config/m600/power\_sys.py}}}

\subsection{Phase current limit}

The motor controllers enforce a phase current magnitude limit to
control the maximum temperature that the SiC modules can reach\footnote{See
       {\texttt{go/makanimotorthermallimits}}}.
%
\begin{equation}
  i_d^2 + i_q^2 < i_{dq,\max}^2
\end{equation}
%
This is the dashed line shown in Fig. \ref{fig:voltage_limit}.  Before
flux weakening was implemented, this constraint was called a ``torque
limit'' as $\tau \propto i_q$.  However, with flux weakening and
consequently non-zero $i_d$ values, it is more accurately refered to
as a phase current limit.  Currently, $i_{dq,\max} = 225$
A\footnote{See
  {\texttt{config/m600/power\_sys\_sim.py}} and \\
  {\texttt{avionics/motor/firmware/foc.c}}}; though it
could potentially be raised in the future.

\subsection{Motor power limit}

The maximum power the motor can deliver occurs when the current
command is at the top of the voltage limit circle.  Here, even as the
motor speed increases and the voltage-limit circle shrinks, the power
remains constant because the radius of the circle is inversely
proportional to the motor speed.  An approximate equation for the
maximum power is
%
\begin{equation}
  P_{\max} \approx \frac{3}{2} \frac{\lambda_m}{L_q} m \frac{v_{\bus}}{\sqrt{3}}
\end{equation}
%
where $m$ is the maximum modulation index, including the gate hold
times.  Currently, $m = 0.95 \times 0.94$ where the first term is the
maximum modulation allowed by flux weakening and the second term is
the maximum duty cycle allowed by the gate hold times.

Because parameters such as $\lambda_m$ change significantly at high
torques, it is better to measure the maximum power empirically.  The
maximum motor shaft power was measured to be
$P_{\shaft,\max} \approx 108$ kW.

A more exact equation for the maximum power is
%
\begin{equation}
  P_{\max} =
  \frac{3}{2} \lambda_m \omega_e
  \left(
    \frac{m \cdot v_{\bus} / \sqrt{3}}{\sqrt{R_s^2 + \omega_e^2 L_q^2}} -
    \frac{R_s \omega_e \lambda_m}{R_s^2 + \omega_e^2 L_d L_q}
  \right)
\end{equation}

\section{Base speed}

The constant torque section of the speed-power curve ends when the
voltage limit circle intersects the $i_q$ axis at the maximum phase
current.
%
\begin{equation}
  \omega_{m, r_1} = \frac{v_{\max}}{p L \sqrt{i_{\max}^2 + i_{d,sc}^2}}
\end{equation}
%
The constant power section of the speed-power curve begins when the
top of the voltage limit circle intersects the maximum phase current
limit circle.
%
\begin{equation}
  \omega_{m, r_2} = \frac{v_{\max}}{p L \sqrt{i_{\max}^2 - i_{d,sc}^2}}
\end{equation}

\begin{thebibliography}{1}
\bibitem{broek} H. Broeck et. al. ``Analysis and realization of a
  pulsewidth modulator based on space vectors.'' IEEE
  Trans. Industry. App., \textbf{24}, 1 (1988).
\end{thebibliography}

\end{document}