\subsection{Algebras over semirings}\label{subsec:algebras_over_semirings}

Algebras are usually defined for fields or at least commutative rings. We extend this to semirings for the purposes of polynomial semirings.

\begin{definition}\label{def:multilinear_function}\mimprovised
  Generalizing \hyperref[def:semimodule/homomorphism]{linear maps}, if \( M_1, \ldots, M_n \) and \( N \) are \( R \)-modules, we say that the function
  \begin{equation*}
    f: M_1 \times \ldots \times M_n \to N
  \end{equation*}
  is \term{multilinear} (\term{bilinear} for \( n = 2 \)) if it is linear in each component. That is, for every tuple
  \begin{equation*}
    (x_1, \ldots, x_n) \in M_1 \times \cdots \times M_n,
  \end{equation*}
  and for every index \( k = 1, \ldots, n \), the following function is linear:
  \begin{equation*}
    y \mapsto f(x_1, \ldots, x_{k-1}, y, x_{k+1}, \ldots, x_n)
  \end{equation*}
\end{definition}

\begin{definition}\label{def:algebra_over_semiring}\mimprovised
  An \term{algebra} over a \hyperref[def:semiring/commutative]{commutative semiring} \( R \) is an \( R \)-\hyperref[def:semimodule]{semimodule} \( M \) with an \hyperref[def:magma/associative]{associative} \hyperref[def:multilinear_function]{bilinear} vector multiplication operation. This makes \( M \) a nonunital ring. By default, we will also assume that \( M \) has a multiplicative unit, although nonunital algebras as just as valid as nonunital rings.

  As in the case of general rings, by \enquote{\( M \) is commutative}, we will mean that vector multiplication is commutative. Furthermore, although we assume it by default, if needed, we will distinguish between associative and non-associative algebra.

  We identify every element \( t \) of \( R \) with its canonical embedding \( t \cdot 1_M \) in \( M \), and thus we can also regard \( R \) as a sub-semiring of \( M \).

  Algebras have the following metamathematical properties:
  \begin{thmenum}
    \thmitem{def:algebra_over_semiring/theory} The \hyperref[def:first_order_theory]{first-order theory} for algebras extends the \hyperref[def:semimodule/theory]{theory of commutative semimodules}. We add a new \hyperref[rem:first_order_formula_conventions/infix]{infix} binary function symbol \( \odot \) to the language, and add to the theory all semiring axioms from \fullref{def:semiring/theory} for \( + \) and \( \odot \). We must also add axioms ensuring that \( \odot \) is bilinear. Additivity follows from distributivity, hence it remains to account for homogeneity. Using the notation of \fullref{def:semimodule/theory}, this amounts to the following axiom schemas:
    \begin{align*}
      m_r(x) \odot y &= m_r(x \odot y), \\
      x \odot m_r(y) &= m_r(x \odot y).
    \end{align*}

    \thmitem{def:algebra_over_semiring/homomorphism} A \hyperref[def:first_order_homomorphism]{first-order homomorphism} between two \( R \)-algebras \( M \) and \( N \) is a linear map that also preserves vector multiplication.

    \thmitem{def:algebra_over_semiring/submodel} The set \( A \subseteq M \) is a \hyperref[thm:substructure_is_model]{submodel} of \( M \) if it is a \hyperref[def:monoid/submodel]{submodule} of \( M \) that is closed under algebra multiplication. We say that \( A \) is a \term{subalgebra}.

    As for general submodules, \fullref{rem:span_over_different_semirings} shows how it is important to be unambiguous about over which semiring we consider the subalgebra.

    As a consequence of \fullref{thm:positive_formulas_preserved_under_homomorphism}, the image of an \( R \)-algebra homomorphism is a subalgebra of its range.

    \thmitem{def:algebra_over_semiring/trivial} The \hyperref[thm:substructures_form_complete_lattice/bottom]{trivial} semimodule is the \hyperref[def:pointed_set/trivial]{trivial pointed set} \( \set{ 0 } \).

    \thmitem{def:algebra_over_semiring/category} We denote the category of algebras over \( R \) by \( \cat{Alg}_R \) and the subcategory of commutative algebras by \( \cat{CAlg}_R \).
  \end{thmenum}
\end{definition}

\begin{proposition}\label{thm:semiring_is_algebra}
  Every \hyperref[def:semiring]{semiring} \( R \) is an \( R \)-\hyperref[def:algebra_over_semiring]{algebra} with both scalar and vector multiplication given by the multiplication in \( R \).

  This extends \fullref{thm:semiring_is_semimodule}.
\end{proposition}
\begin{proof}
  Follows from \fullref{thm:semiring_is_semimodule} by noting that bilinearity follows from left distributivity in \( R \).
\end{proof}

\begin{proposition}\label{thm:semiring_is_natural_number_algebra}
  The categories \( \hyperref[def:semiring/category]{\cat{SRing}} \) of semirings and \( \hyperref[def:algebra_over_semiring/category]{\cat{Alg}_\BbbN} \) of natural number algebras are \hyperref[rem:category_similarity/isomorphism]{isomorphic}.

  Compare this result to \fullref{thm:commutative_monoid_is_semimodule} and \fullref{thm:ring_is_integer_algebra}.
\end{proposition}
\begin{proof}
  Follows from \fullref{thm:commutative_monoid_is_semimodule} by noting that, as in the proof of \fullref{thm:semiring_is_algebra}, distributivity implies bilinearity.
\end{proof}

\begin{proposition}\label{thm:functions_over_algebra}
  For a set \( A \) and an \( R \)-\hyperref[def:algebra_over_semiring]{algebra} \( N \), the set of all functions from \( A \) to \( N \) is itself an \( R \)-algebra with the following operations:
  \begin{thmenum}
    \thmitem{thm:functions_over_algebra/addition} Pointwise addition
    \begin{equation*}
      [f + g](x) \coloneqq f(x) + g(x)
    \end{equation*}

    \thmitem{thm:functions_over_algebra/scalar_multiplication} Pointwise scalar multiplication
    \begin{equation*}
      [t \cdot f](x) \coloneqq t \cdot f(x)
    \end{equation*}

    \thmitem{thm:functions_over_algebra/vector_multiplication} Pointwise vector multiplication
    \begin{equation*}
      [f \odot g](x) \coloneqq f(x) \cdot g(x)
    \end{equation*}

    In practice, we use juxtaposition \( fg \) or \( f \cdot g \) instead of \( f \odot g \).
  \end{thmenum}

  If \( A \) is also an \( R \)-algebra, we denote the set of all \( R \)-\hyperref[def:algebra_over_semiring/homomorphism]{algebra homomorphisms} by \( \hom(A, N) \).

  This result extends \fullref{thm:functions_over_semimodule}.
\end{proposition}
\begin{proof}
  By \fullref{thm:functions_over_model_form_model}, \( N \) is both an \( R \)-semiring and an \( R \)-semimodule. Compatibility comes from left distributivity in \( N \).
\end{proof}

\begin{definition}\label{def:multi_index}\mimprovised
  A \term{multi-index} over the \hyperref[def:set]{plain set} \( \mscrK \) is a member of the \hyperref[def:free_semimodule]{free \( \BbbN \)-semimodule} \( \mscrK^{\oplus \BbbN} \) over \( \mscrK \). We endow \( \mscrK^{\oplus \BbbN} \) with the \hyperref[def:norm]{norm}
  \begin{equation*}
    \norm{ \alpha } \coloneqq \sum_{k \in \mscrK} \alpha_k
  \end{equation*}
  and the \hyperref[def:partially_ordered_set]{partial order}
  \begin{equation*}
    \alpha \leq \beta \T{if and only if} \qforall {k \in \mscrK} \alpha_k \leq \beta_k.
  \end{equation*}

  Multi-indices are \hyperref[def:weighted_set/multiset]{multisets} with extra structure.
\end{definition}

\begin{definition}\label{def:polynomial_algebra}
  Fix a \hyperref[def:semiring/commutative]{commutative semiring} \( R \) and a set \( \mscrX \) of \hyperref[def:formal_language/symbol]{symbols}, which we will call \term{indeterminates}.

  Let \( \mscrM \) be the \hyperref[def:free_semimodule]{free \( R \)-semimodule} over \( \mscrX \), written \hyperref[rem:additive_magma]{multiplicatively}. We will call the members of \( \mscrM \) \term{monomials}. Using a \hyperref[def:multi_index]{multi-index} \( \gamma \) over \( \mscrX \), every monomial can be written as
  \begin{equation*}
    \prod_{X \in \mscrX} X^{\gamma_X},
  \end{equation*}
  where \( \gamma_X \) are the coefficients in \( R^{\oplus \mscrX} \) of the monomial.

  The \term{polynomial algebra} or \term{polynomial semiring} \( R[\mscrX] \) for the given indeterminates is the \hyperref[def:free_semimodule]{free \( R \)-semimodule} over \( \mscrM \). That is, a polynomial \( p \in R[\mscrX] \) is an \( R \)-linear combination of monomials, and we denote polynomials by
  \begin{equation}\label{eq:def:polynomial_algebra/p}
    p(\mscrX) = \sum_\gamma a_\gamma \prod_{X \in \mscrX} X^{\gamma_X}.
  \end{equation}

  We call \( a_\gamma \) the \term{coefficients} of the polynomial. We use the components of the multi-index as powers in the monomials, but we use \( \gamma \) itself as an index for the coefficient \( a_\gamma \). Unfortunately, multi-indices are sometimes confusing, but often their brevity outweighs the possible confusion.

  We do not ignore the structure of \( \mscrM \). We conflate exponentiation in \( \mscrM \) in the sense of \fullref{def:monoid/exponentiation} with exponentiation in \( R[\mscrX] \) in the sense of \fullref{def:semiring/exponentiation}. Multiplication in \( \mscrM \) motivates us to define multiplication in \( R[\mscrX] \) via a convolution of the coefficients. We define the product of \( p(X) \) from \eqref{eq:def:polynomial_algebra/p} with
  \begin{equation}\label{eq:def:polynomial_algebra/q}
    q(\mscrX) = \sum_\gamma b_\gamma \prod_{X \in \mscrX} X^{\gamma_X}
  \end{equation}
  as
  \begin{equation}\label{eq:def:polynomial_algebra/pq}
    [pq](\mscrX) \coloneqq \sum_\gamma \parens*{ \sum_{\delta + \eta = \gamma} a_\delta b_\eta } \prod_{X \in \mscrX} X^{\gamma_X}.
  \end{equation}

  We simultaneously use multi-indices as vectors with pointwise summation (i.e. \( \delta + \eta = \gamma \)) and as indices of coefficients (i.e. \( a_\delta \) and \( b_\eta \)).

  We avoid writing the embedding \( \iota: \mscrX \to R[\mscrX] \), but it is sometimes beneficial to denote it explicitly, for example in \fullref{thm:polynomial_algebra_universal_property}.
\end{definition}

\begin{theorem}[Polynomial algebra universal property]\label{thm:polynomial_algebra_universal_property}
  Fix a \hyperref[def:semiring/commutative]{commutative semiring} \( R \) and a set \( \mscrX \) of indeterminates. The \hyperref[def:polynomial_algebra]{polynomial algebra} \( R[\mscrX] \) is the unique up to a unique isomorphism commutative \hyperref[def:algebra_over_semiring]{algebra} that satisfies the following \hyperref[rem:universal_mapping_property]{universal mapping property}:
  \begin{displayquote}
    For every commutative \( R \)-algebra \( M \) and every function \( e: \mscrX \to M \), there exists a unique \( R \)-algebra homomorphism \( \Phi_e: R[\mscrX] \to M \) such that the following diagram commutes:
    \begin{equation}\label{eq:thm:polynomial_algebra_universal_property/diagram}
      \begin{aligned}
        \includegraphics[page=1]{output/thm__polynomial_semiring_universal_property.pdf}
      \end{aligned}
    \end{equation}
  \end{displayquote}

  The function \( e \) evaluates each indeterminate in \( M \), while \( \Phi_e \) substitutes this value in every polynomial. We call \( \Phi_e \) the \term{substitution homomorphism} corresponding to the \term{variable assignment} \( e \). We can parameterize this by the evaluation functions to obtain the functional evaluation homomorphism
  \begin{equation*}
    \begin{aligned}
      &\Phi: R[\mscrX] \to \fun(M^\mscrX, M) \\
      &\Phi(p) \coloneqq (e \mapsto \Phi_e(p))
    \end{aligned}
  \end{equation*}

  We call the values of \( \Phi \) \term{polynomial functions}. Given elements \( x_1, \ldots, x_n \) of \( M \), we write
  \begin{equation*}
    p(x_1, \ldots, x_n)
  \end{equation*}
  rather than
  \begin{equation*}
    \Phi(p)(x_1, \ldots, x_n).
  \end{equation*}

  Via \fullref{rem:universal_mapping_property}, \( R[\anon*] \) becomes \hyperref[def:category_adjunction]{left adjoint} to the \hyperref[def:concrete_category]{forgetful functor}
  \begin{equation*}
    U: \cat{CAlg}_R \to \cat{Set}.
  \end{equation*}

  The action of \( R[\anon*] \) on morphisms is given by \( \Phi \).
\end{theorem}
\begin{proof}
  For every indeterminate \( X \), we want
  \begin{equation*}
    \Phi_e(\iota(X)) = f(X).
  \end{equation*}

  This suggests defining \( \Phi_e \) for the polynomial
  \begin{equation*}
    p(\mscrX) = \sum_\gamma a_\gamma \prod_{X \in \mscrX} \iota(X)^{\gamma_X}
  \end{equation*}
  as the evaluation
  \begin{equation*}
    \Phi_e(p) \coloneqq \sum_\gamma a_\gamma \prod_{X \in \mscrX} f(X)^{\gamma_X}.
  \end{equation*}

  We discuss well-definedness of infinitary operations in direct sums in \fullref{rem:binary_operation_syntax_trees/infinite/direct_sum}.
\end{proof}

\begin{remark}\label{rem:polynomials_over_infinitely_many_indeterminates}
  As we saw in \fullref{def:polynomial_algebra} and \fullref{thm:polynomial_algebra_universal_property}, there is no formal problem in defining polynomial algebras over infinitely many indeterminates.

  There is a problem, however. Polynomials in one indeterminate, which we will call univariate in accordance to \fullref{def:multi_valued_function/arguments}, have a \hyperref[def:well_ordered_set]{well-ordering} on their monomials, induced by the degree of their monomials. This is defined and discussed in \fullref{subsec:univariate_polynomials}.

  Polynomials in more than one variable do not have a well-ordering by default. If the indeterminates themselves are well-ordered, as is the case for finitely many indeterminates, we may introduce, for example, a \hyperref[def:lexicographic_order]{reverse lexicographic order} on the monomials. Furthermore, for finitely many variables, \fullref{thm:def:polynomial_algebra/iterated} allows us to use \hyperref[rem:induction/peano_arithmetic]{natural number induction} on the number of variables in order to prove statements about multivariate polynomial rings.

  For infinitely many, especially uncountably many variables, however, the theory is seriously crippled by the lack of the tools described above. For this reason, only polynomials in finitely many variables are often considered.
\end{remark}

\begin{proposition}\label{thm:def:polynomial_algebra}
  The following are basic properties of \hyperref[def:polynomial_algebra]{polynomial semirings}:
  \begin{thmenum}
    \thmitem{thm:def:polynomial_algebra/empty} If \( \mscrX \) is empty, \( R[\mscrX] \cong R \).

    \thmitem{thm:def:polynomial_algebra/iterated} The polynomial algebras \( R[\mscrX] \) and \( R[\mscrX \setminus \set{ X_0 }][X_0] \) are isomorphic for any \( X_0 \in \mscrX \) (in case \( \mscrX \) has more than one member).

    In particular,
    \begin{equation*}
      R[X_1, \ldots, X_{n-1}][X_n] \cong R[X_1, \ldots, X_n].
    \end{equation*}

    \thmitem{thm:def:polynomial_algebra/entire} The univariate \hyperref[def:polynomial_algebra]{polynomial semiring} \( R[X] \) is \hyperref[def:divisibility/zero]{entire} if and only if \( R \) is entire.

    \thmitem{thm:def:polynomial_algebra/units} If \( R \) is entire, the \hyperref[def:divisibility/unit]{units} in \( R[X_1, \ldots, X_n] \) are precisely the (embeddings of) the units of \( R \).
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:polynomial_algebra/empty} Trivial.

  \SubProofOf{thm:def:polynomial_algebra/iterated} Polynomials in \( R[\mscrX] \) have the form
  \begin{equation*}
    p(\mscrX) = \sum_{k=0}^\infty \sum_\gamma \parens*{ a_{(k,\gamma)} \prod_{\mathclap{X \in \mscrX \setminus \set{ X_0 }}} X^{\gamma_X} } X_0^k,
  \end{equation*}
  where \( \gamma \) is a \hyperref[def:multi_index]{multi-index} on \( \mscrX \).

  Due to associativity, commutativity and distributivity, this can be rewritten as
  \begin{equation*}
    p(\mscrX) = \sum_{k=0}^\infty \parens*{ \sum_\gamma a_{(k,\gamma)} \prod_{\mathclap{X \in \mscrX \setminus \set{ X_0 }}} X^{\gamma_X} } X_0^k.
  \end{equation*}

  This shows how \( R[\mscrX] \) can be embedded into \( R[\mscrX \setminus \set{ X_0 }][X_0] \). This embedding is surjective because the coefficients \( a_\gamma \) range through \( R \). Therefore, the embedding is an isomorphism.

  \SubProofOf{thm:def:polynomial_algebra/entire}

  \SufficiencySubProof Since \( R \) is an \( R \)-subalgebra of \( R[X] \), if the latter is entire, so is the former.

  \NecessitySubProof Suppose that \( R \) is entire and that \( R[X] \) isn't. Then there exist nonzero polynomials \( p(X) \) and \( q(X) \) such that \( p(X) q(X) = 0 \). If \( a_n \) is the leading coefficient of \( p(X) \) and \( b_m \) --- of \( q(X) \), the leading coefficient of \( p(X) q(X) \) is \( a_n b_m \). Since \( p(X) q(X) \) is the zero polynomial, \( a_n b_m = 0 \), which contradicts the assumption that \( R \) is entire.

  Therefore, \( R[X] \) is entire.

  \SubProofOf{thm:def:polynomial_algebra/units} As in \fullref{thm:def:polynomial_algebra/entire}, it is sufficient to prove the statement for one indeterminate.

  Clearly every constant is invertible as a constant polynomial.

  Now suppose that \( p(X) q(X) = 1 \). By definition of multiplication, the product has only one nonzero coefficient. Since \( R \) is entire, it follows that both \( p(X) \) and \( q(X) \) have only one nonzero coefficient, and are hence constants.
\end{proof}

\begin{example}\label{ex:def:polynomial_algebra}
  We list several examples of \hyperref[def:polynomial_algebra]{polynomials} over semirings.
  \begin{thmenum}
    \thmitem{ex:def:polynomial_algebra/natural_numbers} Consider the polynomial \( p(X) \coloneqq aX^2 + bX + c \) in \( \BbbN[X] \). A function from the set \( \set{ X } \) to \( \BbbN \) corresponds to an element of \( \BbbN \), and hence evaluating the polynomial is done by simply replacing \( X \) symbolically in \( p \) and then evaluating the obtained \hyperref[rem:binary_operation_syntax_trees]{syntax tree}.

    We seek the roots of \( p(X) \). We will only formally define roots in \fullref{def:polynomial_root}; for the purposes of the example, a root is a natural number \( n \) such that \( \Phi_n(p) = 0_R \).

    By \fullref{thm:fundamental_theorem_of_algebra} and \fullref{def:algebraically_closed_field/exactly_n_roots}, \( p \) has two roots in the \hyperref[def:set_of_complex_numbers]{complex plane}. That is, we regard \( \BbbC \) as an algebra over \( \BbbN \) and use \fullref{thm:polynomial_algebra_universal_property} to obtain a polynomial function on \( \BbbC \). Furthermore, over the complex numbers the roots can be explicitly found using
    \begin{equation*}
      \frac {-b \pm \sqrt{b^2 - 4ac}} {2a}.
    \end{equation*}

    Finding a root of \( p \) over the natural numbers cannot be done in general, however. If \( p(n) = 0 \), by the ordering of the natural numbers we have
    \begin{equation*}
      p(n) = an^2 + bn + c \geq c,
    \end{equation*}
    and hence \( c \) must necessarily be \( 0 \). If \( c = 0 \), then zero is a root of the polynomial \( p(X) = aX^2 + bX \).

    Now let \( n \) be any root of \( p \). We have
    \begin{equation*}
      an^2 + bn \geq bn,
    \end{equation*}
    and hence \( bn \) must also be \( 0 \). Thus, either \( b = 0 \) or \( n = 0 \). If we want a root other than \( n \), both \( a \) and \( b \) must be \( 0 \).

    Therefore, the only natural number solution to the quadratic equation is \( 0 \), and it is only a solution if \( c = 0 \).

    \thmitem{ex:def:polynomial_algebra/tropical} Consider again the polynomial \( p(X) \coloneqq aX^2 + bX + c \) over \( \BbbN \), but this time evaluated over the \hyperref[def:tropical_semiring]{\( \min \)-plus semiring} \( (\BbbN \cup \set{ \infty }, \min, +) \).

    Expressed via the standard natural number operations, this polynomial becomes
    \begin{equation*}
      \min\set{ 2X + a, X + b, c }.
    \end{equation*}

    This allows us to express certain optimization problems via polynomials.

    This polynomial has a root if and only if \( a = b = c \). Roots in the tropical semiring are not very interesting, however.
  \end{thmenum}
\end{example}

\begin{proposition}\label{thm:generators_via_polynomials}
  For a set \( A \) in an \( R \)-\hyperref[def:algebra_over_semiring]{algebra} \( M \), the \hyperref[def:algebra_over_semiring/submodel]{generated subalgebra} of \( A \), defined as the \( R \)-subalgebra generated by \( A \) in the sense of \fullref{def:first_order_generated_substructure}, equals the set
  \begin{equation*}
    \bigcup \set[\Big]{ R[a_1, \ldots, a_n] \given* a_1, \ldots, a_n \in A }
  \end{equation*}
  obtained by evaluating all multivariate polynomials over \( R \) with elements of \( A \).

  The \( \BbbN \)-subalgebras of \( M \) correspond to \hyperref[def:semiring/submodel]{sub-semirings} and the \( M \)-subalgebras correspond to \hyperref[def:semiring_ideal/generated]{ideals}.

  Compare this result to \fullref{thm:span_via_linear_combinations} for modules.
\end{proposition}
\begin{proof}
  Similar to \fullref{thm:span_via_linear_combinations}.
\end{proof}

\begin{proposition}\label{thm:adjoining_elements_to_semiring}
  Let \( R \subseteq S \) be \hyperref[def:semiring/commutative]{commutative semirings} and let \( A \subseteq S \) be an arbitrary subset.

  Fix a set \( \mscrX \) of indeterminates and a bijective function \( e: \mscrX \to A \) and consider the \hyperref[thm:polynomial_algebra_universal_property]{evaluation homomorphism}
  \begin{equation*}
    \Phi_e: R[\mscrX] \to S.
  \end{equation*}

  The image \( R[A] \) of \( \Phi_e \) is the smallest super-semiring of \( R \) that contains \( A \).

  We say that \( R[A] \) is obtained by \term{adjoining} the elements of \( A \) to \( R \).
\end{proposition}
\begin{proof}
  Follows from \fullref{thm:generators_via_polynomials}.
\end{proof}

\begin{example}\label{ex:adjoining_root}
  Continuing \fullref{ex:def:polynomial_algebra/natural_numbers}, consider the polynomial equation
  \begin{equation*}
    X + 1 = 0.
  \end{equation*}

  It has no natural number root as a consequence of \eqref{eq:def:peano_arithmetic/PA2}.

  It does have an integer root, however, \( -1 \). We can \hyperref[thm:adjoining_elements_to_semiring]{adjoin} \( -1 \) to the semiring \( \BbbN \) to obtain the semiring \( \BbbN[-1] \). But this latter semiring is (isomorphic to) \( \BbbZ \).

  Therefore, \( \BbbZ \) is the smallest extension of \( \BbbN \) that contains a root to the polynomial \( X + 1 \).

  This example extends to the theory of \hyperref[def:transcendetal_element]{transcendental and algebraic} elements of fields.
\end{example}

\begin{definition}\label{def:formal_power_series}\mimprovised
  If we extend the concept of \hyperref[def:polynomial_algebra]{polynomials} to allow countably many nonzero terms, we obtain a set \( R\Bracks{\mscrX} \) which we call the \term{formal power series} over \( R \) with indeterminates from the set \( \mscrX \).

  The evaluation homomorphism defined in \fullref{thm:polynomial_algebra_universal_property} is problematic, however, since algebraic operations are finitary by nature. This is discussed in \fullref{rem:binary_operation_syntax_trees/infinite}, along with how sometimes we can make sense of infinitary algebraic operations.
\end{definition}


\chapter{Vectors}
\label{chp:vector}
\label{sec:vector}

Pick two points \(A\) and \(B\).

The vector \(AB\) tells us how to go from \(A\) to \(B\).

The vector \(AB\) is the subtraction \( B - A \).

We draw the vector \(AB\) as a straight arrow from \(A\) to \(B\).

If we know \(A\) and \(B\), then we can find \(AB\).

If we are at \(A\), then following \(AB\) leads us to \(B\).
We notate this fact as \( A + AB = B \).
We write the point on the left and the vector on the right:
write \( A + AB \), and don't write \( AB + A \).

The zero vector \(0\) tells us how to get to the same point:
\(A + 0 = A\).

The length of a vector \(v\) is written \(\norm{v}\).

A unit vector is a vector whose length is one.

\section*{Adding two vectors}

If \(AB\) tells us how to go from \(A\) to \(B\)
and \(BC\) tells us how to go from \(B\) to \(C\),
then \(AC = AB + BC\)
tells us how to go from \(A\) to \(C\).

We can see that \( a + b = b + a \) by drawing a parallelogram.
We say that vector addition is commutative
because commuting (swapping) the arguments doesn't change the result.

\section*{Negating a vector}

The negation of a vector \(v\) is \(-v\).
It is the vector such that the sum \(v + (-v)\) is the zero vector \(0\).

If we know the vector \(AB\), then we know \(BA\).
We write \(BA = -AB\).
If \(AB\) tells us how to go from \(A\) to \(B\),
then \(BA\) tells us how to go from \(B\) to \(A\).
\(BA\) is the reverse of \(AB\).

\(BA\) is also called the negation of \(AB\) because \(AB + BA = 0\) is the zero vector.
\(A + 0 = A\).

Negating a vector preserves its length:
\(\norm{-AB} = \norm{BA} = \norm{AB}\).

\section{Vector spaces}

A vector space is a set of vectors.

An example vector space is the set of all two-dimensional Euclidean vectors.
We can imagine this vector space as the set of all arrows that we can draw on an unbounded flat sheet of paper.
We don't care about where the arrow is.
We only care about its length and direction.
If two arrows have the same length and direction,
then they represent the same vector.

An example vector space is the space of all two-dimensional Euclidean vectors.

\section{Collinearity and linear combination}

Two vectors \(a\) and \(b\) are collinear
iff they have the same direction or the opposite direction.
If we place them so that their origins coincide, they form a straight line segment.

The zero vector is collinear with every vector.
% This simplifies the definition of basis later.
% Now we can write \enquote{non-collinear} instead of \enquote{non-zero non-collinear}.

See also Wikipedia\footnote{\url{https://en.wikipedia.org/wiki/Collinearity}}.

\paragraph{Linear combination of two vectors}

Let \(p\) and \(q\) be vectors.
A linear combination of \(p\) and \(q\) is a vector \( ap+bq \)
where \(a\) and \(b\) are real numbers.
In other words, we say that \(r\) is a linear combination of \(p\) and \(q\)
iff there exists \(a,b\in \Real\) such that \(r = ap+bq\).

\section{External resources}

See Wikipedia%
\footnote{\url{https://en.wikipedia.org/wiki/Euclidean_vector}}%
\footnote{\url{https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics)}}%
.

\section{What?}

\paragraph{Finding the angle between two vectors}

For every pair of vectors,
we can always find a plane such that both of them lie on that plane.
The plane defines the angle.

\paragraph{Deciding orthogonality by dot product}

Let \(a\) be a vector.

Let \(b\) be vector.

Let the angle from \(a\) to \(b\) is \(\theta\). Positive means counterclockwise.

The dot product is \(a \cdot b = \norm{a} \cdot \norm{b} \cdot \cos \theta\).

Note that the dot symbol \(\cdot\) is overloaded.
It may mean real multiplication,
vector scaling,
vector dot product,
or something else.
Its meaning depends on the things around it.

If \(\norm{a} \cdot \norm{b} \neq 0\) and \(a \cdot b = 0\),
then \(a\) and \(b\) are orthogonal.
Two orthogonal vectors form a right angle.


\section{Backpropagation} \label{sec:backpropagation}
Last section showed how learning in a neural network is a minimization problem on the loss function and that it is solved by repeatedly updating the network's parameters using the gradient of the loss. But how exactly is the gradient calculated? The loss function is a surface in very high dimensions, calculated by averaging some distance function between the network's output and the true output for all inputs on the dataset; and the output of the network is a mapping calculated by passing the input through possible thousands, millions, or more units, where each one can apply a nonlinearity to its output. In summary, the loss surface is extremely complex and the same should be expected for its gradient.

To make it simpler to understand how the gradient is calculated, the procedure will be shown only for the weights and biases parameters, since those are present in practically all neural networks (although sometimes the bias is omitted in some units). This section will suppose a neural network with $L+1$ layers, where layer $0$ is the input, layer $L$ is the output and values in between are hidden layers.

Essentially, calculating the gradients consists of a smart application of the chain rule of calculus. Recall that the chain rule is a way of calculating the derivative of composite functions, this means that for a function $y$ that depends on $t$, and $t$ that depends on $x$, then the chain rule allows for calculating the derivative of $y$ with respect to $x$ by compounding how $x$ changes $t$ and how $t$ changes $y$. For the case of single variable functions the chain rule is given by \autoref{eq:chain_rule} \cite[p. 406]{calculusIII2016}.
\begin{equation} \label{eq:chain_rule}
    \frac{dy}{dx} = \frac{dy}{dt} \frac{dt}{dx}
\end{equation}

But for the case of neural networks, the loss function is dependent on all the activations of the output layer, and those are dependent on their weights, biases, and possibly other parameters, besides being dependent on activations of the previous layer. So it is necessary to use the multi-variable generalization of the chain rule shown in \autoref{eq:chain_rule_general} \cite[p. 412]{calculusIII2016}, here it is necessary to compound the effect of $x$ for all the variables $(t_0, t_1, \dots t_n)$ that $y$ is dependent on.
\begin{equation} \label{eq:chain_rule_general}
    \frac{\partial y}{\partial x} = \sum_{i}^{n}{\frac{\partial y}{\partial t_i} \frac{\partial t_i}{\partial x}}
\end{equation}

Having the chain rule in mind, it is also useful to define an additional term $\delta$ that represents the partial derivative of the loss with respect to the weighted input of a unit. For unit $i$ on layer $l$ this term is given by \autoref{eq:neuron_delta}.
\begin{equation} \label{eq:neuron_delta}
    \delta_{i}^{(l)} = \frac{\partial J}{\partial z_i^{(l)}}
\end{equation}

And lastly, note that by differentiating Equations \ref{eq:activation_again} and \ref{eq:dense_weighted_input}, the following relations are obtained.
\begin{equation*}
    \frac{\partial z_i^{(l+1)}}{\partial a_i^{(l)}} = w_{ij}^{(l+1)}
    \qquad
    \qquad
    \frac{\partial z_i^{(l)}}{\partial b_i^{(l)}} = 1
    \qquad \qquad
    \frac{\partial z_i^{(l)}}{\partial w_{ij}^{(l)}} = a_j^{(l-1)}
    \\[2pt]
\end{equation*}

The main idea of this algorithm is to derive $\delta$ for all layers, then use these values to calculate the gradient terms for all parameters, the first step is to calculate $\delta$ in the last layer. For the following derivation, consider $\hat{\bm{y}}$ as the network output, and notice that it is the same as the activations of the output layer $\bm{a}^{(L)}$. By using this knowledge, \autoref{eq:delta_last_layer} is derived as follows.
\begin{align}
    \frac{\partial J}{\partial z_i^{(L)}} = \delta_{i}^{(L)} &=
    \frac{\partial J}{\partial \hat{y_i}} \frac{\partial \hat{y_i}}{\partial z_i^{(L)}} \nonumber \\[10pt]
    %
    &= \frac{\partial J}{\partial \hat{y_i}} \frac{\partial a_i^{(L)}}{\partial z_i^{(L)}} \nonumber \\[10pt]
    %
    &= \frac{\partial J}{\partial \hat{y_i}} \frac{\partial}{\partial z_i^{(L)}} {f\left(z_i^{(L)}\right)} \nonumber \\[10pt]
    %
    \delta_{i}^{(L)} &= \frac{\partial J}{\partial \hat{y_i}} f'\left(z_i^{(L)}\right) \label{eq:delta_last_layer}
\end{align}

Recall that the loss function $J$ and activation function $f$ should both be continuous and differentiable, and since they are chosen when building the network their derivatives are known. The values $\hat{y_i}$ and $z_i^{(L)}$ are also known since they are calculated by the network and can be easily stored during training. This means that \autoref{eq:delta_last_layer} can be used to calculate all $\delta$ values in the last layer.

By using \autoref{eq:delta_last_layer} it is also possible to find an expression to calculate all the other $\delta$ values, the following derivation shows how this can be done by writing $\delta^{(l)}$ in terms of $\delta^{(l+1)}$ as shown in \autoref{eq:delta_hidden_layer}.
\begin{align}
    \frac{\partial J}{\partial z_j^{(l)}} = \delta_j^{(l)} &= \sum_{i}{
        \frac{\partial J}{\partial z_i^{(l+1)}}
        \frac{\partial z_i^{(l+1)}}{\partial a_j^{(l)}}
        \frac{\partial a_j^{(l)}}{\partial z_j^{(l)}}
    } \nonumber \\[10pt]
    %
    &= \sum_{i}{
        \delta_i^{(l+1)}
        \frac{\partial z_i^{(l+1)}}{\partial a_j^{(l)}}
        \frac{\partial a_j^{(l)}}{\partial z_j^{(l)}}
    } \nonumber \\[10pt]
    %
    &= \sum_{i}{
        \delta_i^{(l+1)}
        w_{ij}^{(l+1)}
        \frac{\partial a_j^{(l)}}{\partial z_j^{(l)}}
    } \nonumber \\[10pt]
    %
    \delta_j^{(l)} &= \sum_{i}{
        \delta_i^{(l+1)}
        w_{ij}^{(l+1)}
        f'\left( z_j^{(l)} \right)
    } \label{eq:delta_hidden_layer}
    %
\end{align}

Notice how the algorithm works, first the input is feedforwarded through the network to obtain the output $\hat{\bm{y}}$, this value is used to calculate $\delta^{(L)}$, that is then \textit{backpropagated} through the network in order to calculate $\delta^{(l)}$ for all previous layers. This process gives the name \textit{Backpropagation} to the algorithm.

Now for calculating the gradients using $\delta$. Notice that for this case, where only the weights and biases are being considered, the gradient depends on the change $\partial J$ with respect to $\partial b_i^{(l)}$ and $\partial w_{ij}^{(l)}$ for all units and layers.

The following derivation applies the chain rule to obtain the relation in \autoref{eq:gradient_bias} for the partial derivatives of the loss with respect to all the biases parameters.
\begin{align}
    \frac{\partial J}{\partial b_i^{(l)}} &= \sum_{k}{
        \frac{\partial J}{\partial z_k^{(l)}}
        \frac{\partial z_k^{(l)}}{\partial b_i^{(l)}}
    } \nonumber \\
    %
    &= \frac{\partial J}{\partial z_i^{(l)}} \frac{\partial z_i^{(l)}}{\partial b_i^{(l)}} +
    \sum_{k \neq i}{
        \frac{\partial J}{\partial z_k^{(l)}}
        \cancelto{0}{\frac{\partial z_k^{(l)}}{\partial b_i^{(l)}}}
    } \nonumber \\[10pt]
    %
    &= \frac{\partial J}{\partial z_i^{(l)}} \nonumber \\[10pt]
    %
    \frac{\partial J}{\partial b_i^{(l)}} &= \delta_{i}^{(l)} \label{eq:gradient_bias}
\end{align}

The derivation for \autoref{eq:gradient_bias} first breaks the partial derivative of the cost in terms of the weighted inputs $\bm{z}^{(l)}$ in the layer where the bias is present. This is enough since the bias can not influence any previous layers and all influences in the next layers are already captured in the change $\partial J$ with respect to the weighted inputs $\partial z_k^{(l)}$. Since it is also known that the bias does not influence any other unit in the layer, the derivation could have been made directly without breaking the derivative into a sum of all the terms in the layer, but the whole process was shown here for completion sake.

A similar rationale can be used for the weight parameters, obtaining the relation seen in \autoref{eq:gradient_weight}.
\begin{align}
    \frac{\partial J}{\partial w_{ij}^{(l)}} &= \sum_{k}{
        \frac{\partial J}{\partial z_k^{(l)}}
        \frac{\partial z_k^{(l)}}{\partial w_{ij}^{(l)}}
    } \nonumber \\
    %
    &= \frac{\partial J}{\partial z_i^{(l)}} \frac{\partial z_i^{(l)}}{\partial w_{ij}^{(l)}} +
    \sum_{k \neq i}{
        \frac{\partial J}{\partial z_k^{(l)}}
        \cancelto{0}{\frac{\partial z_k^{(l)}}{\partial w_{ij}^{(l)}}}
    } \nonumber \\[10pt]
    %
    &= \frac{\partial J}{\partial z_i^{(l)}} a_{j}^{(l-1)} \nonumber \\[10pt]
    %
    \frac{\partial J}{\partial w_{ij}^{(l)}} &= \delta_{i}^{(l)} a_{j}^{(l-1)} \label{eq:gradient_weight}
\end{align}

\autoref{eq:gradient_weight} was the last piece of the puzzle, together with Equations \ref{eq:delta_last_layer}, \ref{eq:delta_hidden_layer}, and \ref{eq:gradient_bias}, it can be applied to calculate the gradient for all parameters in the network, and this allows for gradient descent to update the parameters and minimize the loss function. From data to model, a complete procedure for a machine to learn by itself.

There are still some more concepts that will be briefly explored in the next section. One further detail to mention about backpropagation is the fact that the derivations in this section were only made for the weights and biases parameters, what about possible others? There are many different types of additional parameters, but the idea with backpropagation is that the $\delta$ values are already calculated for all the units in the network, any new parameter $\theta$ must simply have a correlation with some of these values in order to obtain $\partial J$ in terms of $\partial\theta$ and apply gradient descent for updates. Modern libraries and frameworks already abstract most of these calculations for the programmer via underlying procedures of automatic differentiation, for example, Tensorflow \cite{tensorflow2015} provides a \texttt{GradientTape} object to automatically watch and calculate the gradients for any desired parameter.


\subsection{Homology theory}

Let's remind ourselves of the First Isomorphism Theorem
\begin{thm}
(First Isomorphism Theorem) Let $G$, $H$ be groups, and let $\phi:G\to H$
be a homomorphism, then\end{thm}
\begin{enumerate}
\item $\text{Img}(\phi)\cong G/\ker(\phi)$,
\item $\ker(\psi)$ is a normal subgroup of $G$,
\item $\text{Img}(\psi)$ is a subgroup of $H$,
\end{enumerate}
In particular if $\phi$ is onto (surjective) then $H\cong G/\ker(\psi)$.
\begin{defn}
The standard $p$-simplex is

\begin{align*}
\Delta^{p} & =\{(t_{0},\dots,t_{p})\in\mathbb{R}^{p+1}\mid t_{i}\geqslant0,\sum_{i}t_{i}=1\}.
\end{align*}

\end{defn}

\begin{defn}
Let $X$ be a topological space. A singular $p$-simplex is a continuous
map $\sigma:\Delta^{p}\to X$.
\end{defn}

\begin{defn}
A $p$-chain is denoted $C_{p}(X)=\{\text{singular \ensuremath{p}-simplesx in \ensuremath{X}}\}=\text{free Abelian group generated by all singular \ensuremath{p}-simplexes in \ensuremath{X}.}$
So a $p$-chain is a formal sum

\begin{align*}
C_{p}(X)=\sum_{i}n_{i}\sigma_{i}
\end{align*}

where each $\sigma_{i}$ is a signular $p$-simplex in $X$ (and all
but finitely-many $n_{i}$ are $0$.)
\end{defn}

\begin{defn}
(Boundaries)

The $i$th face map for the standard $p$-simplex is the affine map

\begin{align*}
[e_{0},\dots,\hat{e}_{i},\dots,e_{p}] & :\Delta^{p-1}\to\Delta^{p}
\end{align*}

(where the hat means we \emph{omit} this vertex.) Let $\sigma^{(i)}:\Delta^{p-1}\to X$
be given by

\begin{align*}
\sigma^{(i)} & =\sigma\circ[e_{0},\dots,\hat{e}_{i},\dots,e_{p}]
\end{align*}

then defined the boundary of $\sigma$, $\partial\sigma$ as

\begin{align*}
\partial\sigma & =\sum_{i=0}^{p}(-1)^{i}\sigma^{(i)}
\end{align*}

and we have $\partial\sigma\in C_{p-1}(X)$. So extend by linearity
to get a homeomorphism $\partial=\partial_{p}:C_{p}(X)\to C_{p-1}(X)$.
Say $C_{p}(X)=0$ for $p<0$, and that $\partial\sigma=0$ for an
singular $0$-chain.\end{defn}
\begin{lem}
$\partial\circ\partial=0$. (Proof by linearity, doing the sums, separating
and noting that they cancel.)\end{lem}
\begin{defn}
Cycles and boundaries\end{defn}
\begin{itemize}
\item $\alpha\in C_{p}(X)$ is a $p$-cycle if $\partial\alpha=0$.
\item $\alpha$ is a $p$-boundary if $\alpha=\partial\beta$ for $\beta\in C_{p+1}(X)$
\item Let $Z_{p}=\{\text{\ensuremath{p}-cycles in \ensuremath{X}}\}=\ker(\partial_{p})$
\item $B_{p}(X)=\{\text{\ensuremath{p}-boundaries in \ensuremath{X}}\}=\text{Img}(\partial_{p+1})$.
\end{itemize}
Then $B_{p}(X)\leq Z_{p}(X)$ (subgroup) since $\partial\circ\partial=0$.
\begin{defn}
The $p$th singular homology group of $X$ is

\begin{align*}
H_{P}(X) & =\frac{Z_{p}(X)}{B_{p}(X)}\\
 & =\frac{\ker(\partial_{p})}{\text{Img}(\partial_{p+1})}.
\end{align*}

Geometrically, the elements of $H_{p}(X)$ are equivalence classes
of $p$-cycles where $\alpha\sim\alpha'$ if $\alpha'=\alpha+\partial\beta$,
$\beta\in C_{p+1}(X)$. We write $[\alpha]$ for the homology class
of a $p$-cycle $\alpha\in C_{p}(X)$.\end{defn}
\begin{lem}
If $\{X_{\alpha}\}$ are the path connected components of $X$, then
$C_{p}(X)=\bigoplus_{\alpha}C_{p}(X_{\alpha})$ and $\partial:C_{p}(X_{\alpha})\to C_{p-1}(X_{\alpha})$,
hence $H_{p}(X)=\bigoplus_{\alpha}H_{p}(X_{\alpha})$.\end{lem}
\begin{defn}
The singular chain complex of $X$

\begin{align*}
C_{p}\to C_{p-1}\to\dots\to C_{0}\to0
\end{align*}

is a sequence of Abelian groups and homomorphisms such that $\partial_{p-1}\circ\partial_{p}=0$.\end{defn}
\begin{thm}
(Interpretation of $H_{0}$)

If $X$ is non-empty and path connected, then $H_{0}(X)\cong\mathbb{Z}$.\end{thm}
\begin{cor}
If $X$ has $k$ path components, then $H_{0}(X)\cong\mathbb{Z}^{k}$.\end{cor}
\begin{thm}
(Interpretation of $H_{1}$) If $X$ is path connected, $x_{0}\in H$,
then $H_{1}(X)$ is the Abelianisation of $\pi_{1}(X,x_{0})$.
\end{thm}

\subsubsection{Effects of continuous maps}

If $f:X\to Y$ is continuous and $\sigma:\Delta^{p}\to X$ is a singular
$p$-simplex in $X$ then $f\circ\sigma:\Delta^{p}\to Y$ is a singular
$p$-simplex in $Y$. Then there exists an induced homomorphism $f_{\#}:C_{p}(X)\to C_{p}(Y)$
with $f_{\#}\left(\sum n_{i}\sigma_{i}\right)=\sum n_{i}(f\circ\sigma_{i})$.
\begin{lem}
$\partial\circ f_{\#}=f_{\#}\circ\partial$.\end{lem}
\begin{thm}
There are induced homomorphisms $f_{*}=H_{p}(f):H_{p}(X)\to H_{p}(Y)$,
where $f_{*}([c])=[f_{\#}c]$.\end{thm}
\begin{cor}
If $f:X\to Y$ is a homeomorphism, then $f_{*}:H_{p}(X)\to H_{p}(Y)$
is an isomorphism. Therefore $H_{p}$ are topological invariants.
\end{cor}

\subsubsection{Homotopy invariance of homology}
\begin{thm}
If $f,g$ are two homotopic maps $f,g:X\to Y$, then $f_{*}=g_{*}:H_{p}(X)\to H_{p}(Y),$
$p\geqslant0$.\end{thm}
\begin{cor}
If $f:X\to Y$ is a homotopy equivalence, then $f_{*}:H_{p}(X)\to H_{p}(Y)$
is an isomorphism.\end{cor}
\begin{defn}
(Simplicial homology)

A $\Delta$-complex is a topological space $X$ obtained from the
disjoint uninion of simplies, with ordered vertices glued together
by affine homeomorphisms.
\end{defn}
Define $C_{n}=C_{n}^{\Delta}(X)=\left\{ \sum n_{i}\sigma_{i}\mid n_{i}\in\mathbb{Z},\,\sigma_{i}\,\text{geometric \ensuremath{n}-simplices in \ensuremath{X}}\right\} $.
Then define $\partial:C_{n}\to C_{n-1}$ as before. Then we define
simplicial homology

\begin{align*}
H_{n}^{\Delta}(X) & =\frac{\ker(\partial_{n})}{\text{Img}(\partial_{n+1})}
\end{align*}

\begin{thm}
If $X$ is a $\Delta$-complex then $H_{n}^{\Delta}(X)\cong H_{n}(X)$.
(I.e. ``simplicial homology'' = ``singular homology'').\end{thm}
\begin{defn}
(Reduced homology) If $X=\emptyset$ then we can define an augmented
singular chain complex

\begin{align*}
\cdots\to C_{n}\to^{\partial_{n}}\partial_{n-1}\to^{\partial_{n-1}}\cdots\partial_{1}\to^{\partial_{1}}C_{0}\to^{\epsilon}\mathbb{Z}\to0
\end{align*}

where we let $\epsilon\left(\sum n_{x}x\right)=\sum n_{x}$. Now $\tilde{H}_{n}(X)=0$
if $X$ is contractible.
\end{defn}

\subsubsection{Exact sequences}
\begin{defn}
A sequence (finite or infinite) of Abelian groups and homomorphisms

\begin{align*}
\xymatrix{\cdots\ar[r] & A_{n}\ar[r]^{\alpha_{n}} & A_{n-1}\ar[r]^{\alpha_{n-1}} & \cdots}
\end{align*}

is called \emph{exact} if the image of each homomorphism is the kernal
of the next. I.e. $\text{Img}(\alpha_{n})=\ker(\alpha_{n-1})$ $\forall n$.
\end{defn}

\paragraph{Examples}
\begin{enumerate}
\item $0\to A\to^{f}B$ exact $\Leftrightarrow\ker(f)=\{0\}\Leftrightarrow f$
is 1-1 (injective)
\item $A\to^{f}B\to^{0}0$ exact $\Leftrightarrow\text{Img}(f)=\ker(B)=0$
$\Leftrightarrow f$ is onto (surjective).
\item $0\to^{0}A\to^{f}B\to^{0}0$ exact $\Leftrightarrow$ $f$ is an isomorphism.
\item $0\to^{0}A\to^{f}B\to^{g}C\to^{0}0$ exact $\Leftrightarrow$ $f$
is 1-1, $g$ is onto, $C=\text{Img}(g)\cong B/\ker(g)=B/\text{Img}(f)$\end{enumerate}
\begin{defn}
A short exact sequence $0\to A\to^{f}B\to^{g}C\to0$ splits if there
exists a homomorphism such that $g\circ h=1_{C}$ ($h:C\to B$). Then
$B=f(A)\oplus h(C)\cong A\oplus B$. The sequence splits if, for example,
$C$ is a free Abelian group.
\end{defn}

\paragraph{Mayer-Vietoris sequence}

An analogue to SvK. Let $X=X_{1}\cup X_{2}$ be a topological space,
where $X_{1},X_{2},X_{0}=X_{1}\cap X_{2}$ are open sets in $X$,
then we have inclusion maps

\begin{align*}
\xymatrix{ & X_{1}\ar[dr]_{j_{1}}\\
X_{0}\ar[ur]^{i_{1}}\ar[dr]_{i_{2}} &  & X\\
 & X_{2}\ar[ur]_{j_{2}}
}
\end{align*}

then we have

\begin{align*}
\xymatrix{ & H_{p}(X_{1})\ar[dr]_{j_{1*}}\\
H_{P}(X_{0})\ar[ur]^{i_{1*}}\ar[dr]_{i_{2*}} &  & H_{p}(X)\\
 & H_{p}(X_{2})\ar[ur]_{j_{2*}}
}
\end{align*}

\begin{thm}
(Mayer-Vietoris theorem) There is a long exact sequence in Homology

\begin{align*}
\xymatrix{\cdots\ar[r] & H_{p}(X_{0})\ar[r]^{i_{1*}\oplus i_{2*}} & H_{p}(X_{1})\oplus H_{p}(X_{2})\ar[r]^{j_{1*}-j_{2*}} & H_{p}(X)\ar[r]^{\partial_{*}} & H_{p-1}(X_{0})}
\end{align*}

\end{thm}

From this we can calculate the homotopy of the $n$-sphere as

\begin{align*}
\tilde{H}_{p}(S^{n}) & =\begin{cases}
\mathbb{Z} & p=n\\
0 & \text{otherwise}
\end{cases}
\end{align*}

\begin{thm}
(Topological invariance of dimension) If $\mathbb{R}^{n}\approx\mathbb{R}^{m}$,
then $n=m$. \end{thm}
\begin{proof}
If $f:\mathbb{R}^{n}\to\mathbb{R}^{m}$ is a homeomorphism then we
also have an induced homeomorphism $\mathbb{R}^{n}-\{pt\}\to\mathbb{R}^{m}-\{pt\}$
so we have a homotopy $S^{n-1}\simeq S^{m-1}$ so $\tilde{H}_{*}(S^{n-1})\cong\tilde{H}_{*}(S^{m-1})$
so $n=m$.\end{proof}
\begin{defn}
A continous map $f:S^{n}\to S^{n}$ has degree $d$ if $f_{*}=H_{n}(f):\tilde{H}_{n}(S^{n})\to\tilde{H}_{n}(S^{n})$
is multiplication be the integer $d$.\end{defn}
\begin{rem}
$f\simeq g\implies\deg(f)=\deg(g)$. $\deg(f\circ g)=\deg(f)\times\deg(g)$.\end{rem}
\begin{thm}
(Subdivision theorem) Let $U=\{U_{j}\}$ be an open cover of $X$
(or a collection of subsets of $X$ whose inteiors cover $X$.). Then
let $C_{p}^{U}(X)$ be the subgroup of $C_{p}(X)$ consisting of singular
$p$-chains $\sum n_{i}\sigma_{i}$ such that the image $\sigma_{i}(\Delta^{p})$
of each $\sigma_{i}$ lies in one of the sets in $U$.

Then $\partial:C_{p}^{U}(X)\to C_{p-1}^{U}(X)$, so we get a chain
complex $C_{*}^{U}(X)$ with homology $H_{*}^{U}(X)$.

The inclusion $C_{*}^{U}(X)\to C_{*}(U)$ induces an isomorphism $H_{p}^{U}(X)\to H_{p}(X)$
for all $p$.\end{thm}
\begin{defn}
(Relative homotopy) Let $(X,A)$ be a pair of topological spaces with
$A\leq X$ ($A$ is a subspace.) The inclusion $A\hookrightarrow X$
induces the inclusions $C_{p}(A)\to C_{p}(X)$ for all $p$ and we
can define

\begin{align*}
C_{p}(X,A) & =\frac{C_{p}(X)}{C_{p}(A)}.
\end{align*}

\end{defn}
Then $\partial:C_{p}(X)\to C_{p-1}(X)$ takes $C_{p}(A)\to C_{p-1}(A)$
so induces a homomorphism $\bar{\partial}:C_{p}(X,A)\to C_{p-1}(X,A)$
and $\bar{\partial}\circ\bar{\partial}=0$. This gives a chain complex
with homology $H_{p}(X,A)=\frac{\ker(\bar{\partial}_{p})}{\text{Img}(\bar{\partial}_{p+1})}$
called the ``relative homology of $X$ mod $A$.''


\section{Multigrid methods for numerical PDEs}\label{sec:mg}

With the restriction $R_{\ell}^{\ell+1}$ and prolongation $P_{\ell+1}^\ell$
obtained in Lemma \ref{ris:plon},
%as defined in \eqref{mg-prolong} and restriction $R_{\ell}^{\ell+1} = (P_{\ell+1}^{\ell})^T$.
we have the following relationship to define coarse operation
\begin{equation}\label{eq:def_coarse}
A^{\ell+1}=R_{\ell}^{\ell+1} A^{\ell}P_{\ell+1}^{\ell} \quad (\ell = 1:J-1),
\end{equation}
with $A^1 = A$.
Similarly, with the smoother obtained by \eqref{eq:convS}, we can define
$S^{\ell}: \mathbb{R}^{m_\ell \times n_\ell} \mapsto \mathbb{R}^{m_\ell \times n_\ell}$.

Now using the smoother $S^\ell$, prolongation $P^{\ell}_{\ell+1}$, restriction $R_{\ell}^{\ell+1}$ and mapping
$A^\ell$ as given in \eqref{eq:def_coarse}, we can formulate the following algorithm
 as a major component of a multigrid algorithm.
\begin{breakablealgorithm}%[!htb]
	\caption{$(u^{\ell,\nu_\ell}: ~\ell = 1:J) = {\text{MG0}}(f; J,\nu_1, \cdots, \nu_J)$}
	\label{alg:L-Slash0}
	\begin{algorithmic}
		\State Set up
		$$
		f^1 = f, \quad u^{1,0}=0.
		$$
		\State Smoothing and restriction from fine to coarse level (nested)
		\For{$\ell = 1:J$}
		\State Pre-smoothing:
		\For{$i = 1:\nu_\ell$}
		\State
		\begin{equation}\label{eq:smoothing}
		u^{\ell,i} = u^{\ell,i-1} + K_S \ast (f^\ell - K_A \ast u^{\ell,i-1}).
		\end{equation}
		\EndFor
		\State Form restricted residual and set initial guess:
		$$
		u^{\ell+1,0} = 0, \quad f^{\ell+1} = K_R \ast_2 (f^\ell -  K_A \ast u^{\ell,\nu_\ell}).
		$$
		\EndFor
	\end{algorithmic}
\end{breakablealgorithm}
Here $K_S$ can be chosen as $K_{S_0}$ or $K_{S_1}$ as definition in \eqref{eq:kernel-S} and \eqref{eq:kernel-S2}.

Using the above algorithm, there are different multigrid algorithms such as: $\backslash$-cycle, V-cycle and W-cycle.
Let us now only give one special form of multigrid algorithm for solving \eqref{laplace} or \eqref{laplace-h} as follows.
\begin{breakablealgorithm}%[!htb]
	\caption{$u = {\backslash\text{-MG}}(f; J,\nu_1, \cdots, \nu_J)$}
	\label{alg:L-Slash1}
	\begin{algorithmic}
		\State Call Algorithm \ref{alg:L-Slash0},
		$$
		(u^{\ell,\nu_\ell}: ~\ell = 1:J) = {\text{MG0}}(f; J,\nu_1, \cdots, \nu_J).
		$$
		\State Prolongation and restriction from coarse to fine level
		\For{$\ell = J-1:1$}
		\State
		$$
		u^{\ell,\nu_\ell} \leftarrow u^{\ell,\nu_\ell} + K_R  \ast_2^{\top} u^{\ell+1, \nu_{\ell+1}}.
		$$
%		%		\IF{V-cycle}
%		\For{$i = 1:\nu_\ell$}
%		\State
%		$$
%		u^{\ell,i} \leftarrow u^{\ell,i-1} + [B^{\ell,i}]^T (f^\ell - A^{\ell} u^{\ell,i-1})
%		$$
%		\EndFor
%		%		\ENDIF
		\EndFor
		\State Output
		$$
		u = u^{1,\nu_\ell}.
		$$
	\end{algorithmic}
\end{breakablealgorithm}


%&LaTeX

\section{Signals in the Computer}

In this lab, you will use JDSP to explore how a physical signal can
be considered to be composed of a sum of sinusoids --- its
\emph{Fourier series}. You will then investigate how capturing this
signal for computer use --- sampling and quantization --- modifies the
signal. Finally, you will see how the choices you make in the
parameters for sampling and quantization affect the quality of the
digitized, computer signal.

\subsection{Fourier series representation of a physical signal}
	In this section, you will use the  \block{Cont Sig} block in J-DSP to simulate a analog signal
	generator. The \block{Cont
	Sig} block is capable of simulating analog signals of varying frequency content.

	% Block diagram, step 1
	\begin{figure}[b]
	  \begin{center}
	    \includegraphics[height=1.5in]{lab3/block_diagram_step1}
	  \end{center}
	\caption{ A J-DSP diagram for summing and plotting analog sine waves (left) and a single
	triangle wave (right).
	\label{fg:step1}}
	\end{figure}

\paragraph{Step 1.1} Create a J-DSP diagram that sums together four continuous time sinusoids
	and plots their sum. Use the ``Analog Blocks'' function set and the \block{Adder} block only (See
	Figure \ref{fg:step1}). Also create a separate set of blocks that plots a continuous time triangle
	wave at a \option{frequency} of 200 Hz and \option{amplitude} of 1. In the next section you will
	simulate this triangle wave using the sum of sinusoids.

\paragraph{Step 1.2} Recall that any periodic signal can be represented as a sum of harmonic
	sinusoids. The amplitude of each harmonic is known as the Fourier Series. It may at first seem
	like sums of sinusoids would be poor approximations of real periodic signals, but this is not
	the case. We
 	can illustrate this using a triangle wave. The formula for synthesis of a triangle wave with
	frequency $\omega_0$ is a sum of harmonically related sine waves (its Fourier series):
	\[
	x(t) = \sum_{k=0}^{\infty}
	\left(
	\underbrace{ \frac{8}{\pi^2} \frac{(-1)^k}{(2k+ 1)^2} }_{ \text{amplitude} }
	\underbrace{ \sin((2k+1)\omega_0 t) }_{ (2k+1)^{th}\text{ harmonic} }
	\right)
	\]
	In this case, in the analog domain, we are dealing with frequencies in
	Hz, and so $\omega_0 = 2\pi f_0$. Notice that the Fourier Series of the triangle wave only uses
	odd harmonics (i.e., the only non-zero frequencies are $(2k+1)\omega_0=\omega_0, 3\omega_0,
	5\omega_0 \cdots$). Also notice that resulting wave will be zero mean because there is no ``DC''
	term (i.e., $2k+1 \neq 0$ for any integer k).
	Use your diagram from Step 1.1 to generate and sum the
	first 7 harmonics of the Fourier Series of a triangle wave and plot the resultant signal (i.e., use
	$f_0$, $2f_0$, $3f_0$, $\cdots 7f_0$, where $f_0=$200 Hz). How does this signal compare to
	the triangle wave computed directly in \block{Cont. Sig}?

\paragraph{Step 1.3} Another way to view a signal is in the \emph{frequency domain}. For a
	signal expressed in terms of its Fourier Series, the \emph{frequency domain} representation
	is merely the coefficients of the harmonics. Use your favorite spreadsheet or plotting tool to
	compute and plot
	the spectrum of a triangle wave.  Note that you are \emph{not} being asked to plot the triangle
	wave as a function of time; you should plot the amplitudes of the Fourier Series as a
	function of the harmonics' frequencies (like the vertical lines in textbook figure~1.12).

\subsection{Sampling}

The first step in digitization is \emph{sample and hold}, in which the
continuous analog signal is converted to a \textit{discrete-time}
analog signal (an analog signal that only changes its value at
particular points in time). You will use the \block{Sample-Hold} block
in J-DSP to simulate this.

% Block diagram, step 2
\begin{figure}[h]
  \begin{center}
    \includegraphics[height=1.5in]{lab3/block_diagram_step2}
  \end{center}
  \caption{ A J-DSP diagram for investigating sampling rate. \label{fg:step2}}
\end{figure}

\paragraph{Step 2.1} Create an \block{Cont Sig} sine waveform ranging
from -5 to 5V with a frequency of 200Hz (Figure \ref{fg:step2},
leftmost block).

\paragraph{Step 2.2} Use four \block{Sample-Hold} blocks to sample
this signal at 300Hz, 500Hz, 1000Hz and 2000Hz. Plot the original and
all four sampled signals separately (Figure \ref{fg:step2}). Clearly,
the results are not the same, and none look identical to the original
sine wave. What are the two essential pieces of information about a
sine wave that need to be preserved when sampling it?  Does it appear
that all sampled versions are equally useful in achieving this? Why or
why not (in other words, your answer to this question should not be
just ``yes'' or ``no'')?

\subsection{Analog to Digital Conversion}

	The last step of digitization is called ``analog to digital conversion,'' or \emph{quantization}. In
	this step, the sampled analog signal is converted to a discrete signal, with values represented
	by $b$ bit integers. We will use the \block{Quantizer} block under the ``Statistical DSP'' function
	set to perform this conversion.

	% Block diagram, step 3
	\begin{figure}[h]
	  \begin{center}
	    \includegraphics[width=5.0in]{lab3/block_diagram_step3}
	  \end{center}
	\caption{ A J-DSP diagram for investigating quantization error and calculating SNR.
	\label{fg:step3}}
	\end{figure}

\paragraph{Step 3.1} Next we will be using the \block{SNR} block under the ``Basic Blocks''
	function set to compute signal-to-noise ratio (SNR) as a result of quantization. Place the
	\block{SNR} block and open the dialog associated with it. You will see the equations used to
	calculate the numerator and denominator. In your own words, what quantity is calculated in the
	numerator and what quantity is calculated in the denominator of this ratio (Hint: can you
	describe the values in terms of \emph{root mean square} (RMS) values)? Note that this SNR is
	different than what we did in the textbook, because we are now doing the computation for a
	\textit{specific} signal, not just figuring SNR for a possible \textit{range} of signal values.

\paragraph{Step 3.2} Use the J-DSP diagram in Figure \ref{fg:step3} to compute the SNR for a
	quantized sinusoid. Use the \block{SigGen} block with a ``amplitude'' of 5, ``frequency'' of 0.05$
	\pi$, and ``Pulsewidth'' of 256. Use 2, 4, 8, 12, and 16 bits quantization. Use your favorite
	spreadsheet or plotting software to plot SNR versus number of quantization bits (i.e., a scatter
	plot). Use the output of \block{SigGen} as reference for the \block{SNR} block.

\paragraph{Step 3.3} Plot the quantization error by using the \block{Adder} block (bottom right
	blocks of Figure \ref{fg:step3}). Set the \block{Adder} block to ``subtract'' by opening the dialog
	and switching the ``$+$'' sign to ``$-$.'' You only need to include the quantization error plot for
	``4 bits'' in your report (but be sure to view the error plots for all quantization levels).

\paragraph{Step 3.4} Repeat Step 3.2 using a triangle waveform with ``Gain'' of 5 and
	``Pulsewidth'' of 256.

\paragraph{Step 3.5} As you double the number of bits used in quantization, how does the SNR
	change? Refer to specific features of your plots from Steps~3.2--3.4 to justify your answer.

% LocalWords:  WebQ MATLAB


 \documentclass{article}
\usepackage[margin=1in]{geometry}
\setlength{\parindent}{0in}

\usepackage[utf8]{inputenc}

\usepackage{latexsym,amsfonts,amssymb,amsthm,amsmath}
\usepackage {tikz}
\usetikzlibrary {positioning}
\usetikzlibrary{quantikz}

\usepackage{braket}

\newcommand{\norm}[1]{\left\lVert#1\right\rVert}

\title{Quantum Computing - Assignment 1}
\author{Kishlaya Jaiswal}

\begin{document}

\maketitle

\subsection*{Exercise 1}
\begin{proof}
Observe that
$$\norm{ \ket{\psi} }^2 = \braket{\psi|\psi} = \big(\ket{\psi}\big)^{\dag} \big(\ket{\psi}\big)$$
$U$ is unitary that is $U^{\dag} U = I$, and hence
$$\norm{ U\ket{\psi} }^2 = \big(U\ket{\psi}\big)^{\dag} \big(U\ket{\psi}\big) = \bra{\psi} U^\dag U \ket{\psi} = \bra{\psi} I \ket{\psi} = \braket{\psi|\psi} = \norm{ \ket{\psi} }^2$$

Since $\norm{.} \geq 0 \implies \norm{ U\ket{\psi} } = \norm{ \ket{\psi} }$
\end{proof}

\subsection*{Exercise 2}
\begin{proof}
\begin{align*}
    [X,Z] \ket{0} &= (XZ-ZX) \ket{0} = X\ket{0} - Z\ket{1} = \ket{1} + \ket{1} = 2 \ket{1} \\
    [X,Z] \ket{1} &= (XZ-ZX) \ket{1} = -X\ket{1} - Z\ket{0} = -\ket{0} - \ket{0} = -2 \ket{0}
\end{align*}

Hence $[X,Z] = \begin{pmatrix} 0 & -2 \\ 2 & 0\end{pmatrix}$
\end{proof}

\subsection*{Exercise 3}
\begin{proof}
$$X = \begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix} \implies X^\dag = \begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix} = X \text{ and } X^\dag X = \begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix}\begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix} = \begin{pmatrix}1 & 0 \\ 0 & 1\end{pmatrix} = I$$
$$Y = \begin{pmatrix}0 & -i \\ i & 0\end{pmatrix} \implies Y^\dag = \begin{pmatrix}0 & -i \\ i & 0\end{pmatrix} = Y \text{ and } Y^\dag Y = \begin{pmatrix}0 & -i \\ i & 0\end{pmatrix}\begin{pmatrix}0 & -i \\ i & 0\end{pmatrix} = \begin{pmatrix}1 & 0 \\ 0 & 1\end{pmatrix} = I$$
$$Z = \begin{pmatrix}1 & 0 \\ 0 & -1\end{pmatrix} \implies Z^\dag = \begin{pmatrix}1 & 0 \\ 0 & -1\end{pmatrix} = Z \text{ and } Z^\dag Z = \begin{pmatrix}1 & 0 \\ 0 & -1\end{pmatrix}\begin{pmatrix}1 & 0 \\ 0 & -1\end{pmatrix} = \begin{pmatrix}1 & 0 \\ 0 & 1\end{pmatrix} = I$$

Thus Pauli matrices are Hermitian and Unitary. And,

% Suppose $A$ is any Hermitian matrix, then for any non-zero eigenvector $v$ with eigenvalue $\lambda$:
% $$\bar\lambda \braket{v, v} = \braket{\lambda v, v} = \braket{Av, v} = \braket{v, A^\dag v} = \braket{v, Av} = \braket{v, \lambda v} = \lambda \braket{v, v}$$
% So, eigenvalues of Hermitian matrices are real.

% Suppose $A$ is any Unitary matrix, then for any non-zero eigenvector $v$ with eigenvalue $\lambda$:
% $$\bar \lambda \braket{v, Av} = \braket{\lambda v, Av} = \braket{Av, Av} = \braket{v, A^\dag A v} = \braket{v, v}$$

% But $\bar \lambda \braket{v, Av} = \lambda\bar \lambda \braket{v, v}$ and hence $|\lambda|^2 = 1$. So eigenvalues of Unitary matrices lie on the unit circle.

% Since the only real numbers satisfying $|\lambda|^2 = 1$ are $\pm 1$, so only possible eigenvalues of Pauli matrices are $\pm 1$ and in particular

$$X\begin{pmatrix}1/\sqrt{2} \\ 1/\sqrt{2}\end{pmatrix} = \begin{pmatrix}1/\sqrt{2} \\ 1/\sqrt{2}\end{pmatrix}, X\begin{pmatrix}1/\sqrt{2} \\ -1/\sqrt{2}\end{pmatrix} = - \begin{pmatrix}1/\sqrt{2} \\ -1/\sqrt{2}\end{pmatrix}$$

$$Y\begin{pmatrix}1/\sqrt{2} \\ i/\sqrt{2}\end{pmatrix} = \begin{pmatrix}1/\sqrt{2} \\ i/\sqrt{2}\end{pmatrix}, Y\begin{pmatrix}1/\sqrt{2} \\ -i/\sqrt{2}\end{pmatrix} = -\begin{pmatrix}1/\sqrt{2} \\ -i/\sqrt{2}\end{pmatrix}$$

$$Z\begin{pmatrix}1 \\ 0\end{pmatrix} = \begin{pmatrix}1 \\ 0\end{pmatrix}, Z\begin{pmatrix}0 \\ 1\end{pmatrix} = -\begin{pmatrix}0 \\ 1\end{pmatrix}$$
\end{proof}

Thus the eigenvalues are of Pauli matrices are $\pm 1$.

\subsection*{Exercise 4}
\begin{proof}
\begin{align*}
  HXH \ket{0} &= HX \ket{+} = H \ket{+} = \ket{0}  \\
  HXH \ket{1} &= HX \ket{-} = H (-\ket{-}) = -\ket{1}
\end{align*}
And hence $HXH = Z$

\begin{align*}
  HZH \ket{0} &= HZ \ket{+} = H \ket{-} = \ket{1} \\
  HZH \ket{1} &= HZ \ket{-} = H \ket{+} = \ket{0}
\end{align*}
And hence $HZH = X$
\end{proof}

\subsection*{Exercise 5}
\begin{proof}
From the above exercise $3$, we know that $\ket{+}$ is an eigenvector of $X$ with eigenvalue $1$ and $\ket{-}$ is an eigenvector of $X$ with eigenvalue $-1$, that is $X = \ket{+}\bra{+} - \ket{-}\bra{-}$

Hence $\{\ket+ \bra+, \ket- \bra-\}$ is an eigenbasis for $X$ and so $\ket+ \bra+$ and $\ket- \bra-$ are the measurement operators corresponding to a measurement of $X$ observable.
\end{proof}

\subsection*{Exercise 6}
\begin{proof}
First we check that $\frac{1}{\sqrt{2}}\big(\ket{00} + \ket{11}\big) = \frac{1}{\sqrt{2}}\big(\ket{++} + \ket{--}\big)$ indeed.

\begin{align*}
    \ket{++} + \ket{--} &= \frac{1}{2}(\ket0 + \ket1)(\ket0 + \ket 1) + \frac{1}{2}(\ket0 - \ket1)(\ket0 - \ket 1) \\
    &= \frac12 (\ket{00} + \ket{01} + \ket{10} + \ket{11} + \ket{00} - \ket{01} - \ket{10} + \ket{11}) = \ket{00} + \ket{11}
\end{align*}

So it suffices to show that $\frac{1}{\sqrt{2}}\big( \ket{00} + \ket{11}\big)$ is an entangled state. \\

Suppose not and so it can be written as $\ket{\psi} \otimes \ket{\phi}$ where $\ket{\psi} = \alpha \ket0 + \beta \ket1$ and $\ket\phi = \gamma \ket0 + \delta \ket1$.

Then $\ket{\psi} \otimes \ket{\phi} = \alpha \gamma \ket{00} + \alpha \delta \ket{01} + \beta \gamma \ket{10} + \beta \delta \ket{11} = \frac{1}{\sqrt{2}}\big(\ket{00} + \ket{11}\big)$ \\

Since $\{\ket{00}, \ket{01}, \ket{10}, \ket{11}\}$ is a linearly independent set in $\mathbb{C}^4$, we get $\alpha \delta = \beta \gamma = 0$ and $\alpha \gamma = \beta \delta \neq 0$ whose solution doesn't exist. Hence $\frac{1}{\sqrt{2}}\big(\ket{00} + \ket{11}\big)$ is an entangled state.
\end{proof}

\subsection*{Exercise 7}
\begin{center}
\begin{quantikz}
\lstick{$\ket{0}$} & \gate{H} & \ctrl{1} & \qw \rstick[wires=2]{$\ket\phi$} \\
\lstick{$\ket{0}$} & \qw & \targ{} & \qw
\end{quantikz}
\end{center}

\begin{proof}
We start with $\ket\psi = \ket{00}$ state.\\

Applying $H \otimes I$ to $\ket\psi$, we get $\frac{1}{\sqrt2}\big(\ket0 + \ket1\big)\ket0 = \frac{1}{\sqrt2}\big(\ket{00} + \ket{10}\big)$\\

Applying controlled-NOT gate (where first qubit is the control and second qubit is target) to this, we finally get $\ket\phi = \frac{1}{\sqrt2}\big(\ket{00} + \ket{11}\big)$ as required.
\end{proof}

\end{document}


\section{Dual Averaging}
In this section, we introduce Nesterov's method of dual averaging. In order to best explain where this method comes from,
we first start with a different perspective on subgradient descent. We note that the subgradient descent iteration
(\ref{subgradient_descent_variable}) (with $x_1 = 0$) can also be written as
\begin{equation}\label{minimizing_subgradient_descent}
 x_{n+1} = \arg\min_x \left(\displaystyle\sum_{i = 1}^n \langle s_ig_i, x\rangle + \frac{1}{2}\|x\|_2^2\right)
\end{equation}

This can easily be proven by induction on $n$ and we leave this as an exercise. The above minimization problem
can be rewritten as
\begin{equation}
 x_{n+1} = \arg\min_x \left(\displaystyle\sum_{i = 1}^ns_i(f(x_i) + \langle g_i, x - x_i\rangle) + \frac{1}{2}\|x\|_2^2\right)
\end{equation}
and we note that the terms $f(x_i) + \langle g_i, x - x_i\rangle$ are lower bounds on the objective $f$. Thus, the next iterate
can be obtained by optimizing an average of these lower bounds (with weights $s_i$) plus a regularization term which is a multiple
of $\|x\|_2^2$.

In the previous section we set $s_i = \frac{1}{\sqrt{i}}$. This means that we are giving more weight to the bounds coming from
earlier iterates, which is odd. The starting point for Nesterov's dual averaging method is to ask whether we can change the
weights $s_i$. It turns out that we can, but in order to do this we must weight the regularization term differently as well.

We thus modify (\ref{minimizing_subgradient_descent}) and consider methods of the form
\begin{equation}\label{generalized_dual_averaging}
 x_{n+1} = \arg\min_x \left(\displaystyle\sum_{i = 1}^n \langle s_ig_i, x\rangle + \frac{\alpha_{n+1}}{2}\|x\|_2^2\right)
\end{equation}
where the sequences $s_i,\alpha_i > 0$ are parameters (and $g_i\in \partial f(x_i)$ as before).

We obtain the following convergence result for the dual averaging method as a consequence of the definition (\ref{generalized_dual_averaging}).
\begin{theorem}\label{dual_averaging_theorem}
 Assume that $f$ is convex and Lipschitz with constant $M$, i.e. $\|g\|_2\leq M$ for $g\in \partial f(x)$. Let
 $x^*\in \arg\min_x f(x)$.

 Then if the sequence $x_i$ is given by (\ref{generalized_dual_averaging}) (note in particular that this means that
 $x_1 = 0$) with $\alpha_i \leq \alpha_{i+1}$, we have
 \begin{equation}
  f(\bar{x}_n) - f(x^*) \leq \frac{1}{2}\left(\displaystyle\sum_{i = 1}^n s_i\right)^{-1} \left(\alpha_n\|x^*\|_2^2 + M\displaystyle\sum_{i = 1}^n \frac{s_i^2}{\alpha_i}\right)
 \end{equation}
 where $\bar{x}_n = (\sum_{i = 1}^n s_i)^{-1}\sum_{i = 1}^n s_ix_i$ is a weighted average of the iterates with weights $s_i$.
 We also have
 \begin{equation}
  \min_{i = 1,...,n} f(x_i) - f(x^*) \leq \frac{1}{2}\left(\displaystyle\sum_{i = 1}^n s_i\right)^{-1} \left(\alpha_n\|x^*\|_2^2 + M\displaystyle\sum_{i = 1}^n \frac{s_i^2}{\alpha_i}\right)
 \end{equation}

\end{theorem}
\begin{proof}
 We begin by noting that by convexity we have
 \begin{equation}
  f(\bar{x}_n) - f(x^*) \leq \left(\displaystyle\sum_{i = 1}^n s_i\right)^{-1}\displaystyle\sum_{i = 1}^n s_i(f(x_i) - f(x^*))
 \end{equation}
 and since the minimum is smaller than the average we have
 \begin{equation}
  \min_{i = 1,...,n} f(x_i) - f(x^*) \leq \left(\displaystyle\sum_{i = 1}^n s_i\right)^{-1}\displaystyle\sum_{i = 1}^n s_i(f(x_i) - f(x^*))
 \end{equation}
 Thus it suffices to prove that
 \begin{equation}
  \displaystyle\sum_{i = 1}^n s_i(f(x_i) - f(x^*)) \leq \frac{1}{2}\left(\alpha_n\|x^*\|_2^2 + M\displaystyle\sum_{i = 1}^n \frac{s_i^2}{\alpha_i}\right)
 \end{equation}
 For this we first use the subgradient property to get
 \begin{equation}\label{eq_dual_averaging_59}
  \displaystyle\sum_{i = 1}^n s_i(f(x_i) - f(x^*)) \leq \displaystyle\sum_{i = 1}^n \langle s_ig_i, x_i - x^*\rangle
 \end{equation}
 and rewrite this sum as follows
 \begin{equation}\label{eq_dual_averaging_63}
  \displaystyle\sum_{i = 1}^n \langle s_ig_i, x_i - x^*\rangle = \displaystyle\sum_{i = 1}^n \langle s_ig_i, x_n - x^*\rangle
  + \displaystyle\sum_{i = 1}^{n-1} \displaystyle\sum_{j = 1}^i\langle s_jg_j, x_i - x_{i+1}\rangle
 \end{equation}
 We proceed by bounding the term
 \begin{equation}
  \sum_{j = 1}^i\langle s_jg_j, x_i - z\rangle
 \end{equation}
 Recall from (\ref{generalized_dual_averaging}) that $x_i = \arg\min_x f_i(x)$ where
 \begin{equation}
  f_i(x) = \displaystyle\sum_{j = 1}^{i-1} \langle s_jg_j, x\rangle + \frac{\alpha_{i}}{2}\|x\|_2^2
 \end{equation}
 This means that $0\in\partial f_i(x_i)$ and thus, defining
 $$f_i^\prime(x) = f_i(x) + \langle s_ig_i, x\rangle$$
 we see that $s_ig_i\in \partial f_i^\prime(x_i)$. Moreover, $f_i^\prime$ is strongly convex with convexity parameter
 $\alpha_i$, which implies that
 $$f_i^\prime(x_i) \leq f_i^\prime(z) + \frac{1}{2\alpha_i}\|s_ig_i\|_2^2
 $$
 Using the definition of $f_i^\prime$ and rearranging, this gives
 \begin{equation}
  \sum_{j = 1}^i\langle s_jg_j, x_i - z\rangle \leq \frac{\alpha_i}{2}(\|z\|_2^2 - \|x_i\|_2^2) + \frac{s_i^2}{2\alpha_i}\|g_i\|_2^2
 \end{equation}
 Pluggin this into (\ref{eq_dual_averaging_63}) with $z = x_{i+1}$ and $z = x^*$, we obtain (recalling that $x_1 = 0$ and
 $\|g_i\|_2 \leq M$)
 \begin{equation}
  \displaystyle\sum_{i = 1}^n \langle s_ig_i, x_i - x^*\rangle \leq \frac{\alpha_n}{2}\|x^*\|_2^2
  + \frac{1}{2}\displaystyle\sum_{i = 2}^n (\alpha_{i-1} - \alpha_i)\|x_i\|_2^2 + \frac{M}{2}\displaystyle\sum_{i = 2}^n \frac{s_i^2}{\alpha_i}
 \end{equation}
 Now we use the assumption that $\alpha_i$ is an non-decreasing sequence, i.e. $$\alpha_{i-1} - \alpha_i \leq 0$$ to see that
 the middle sum above is negative. Combining this with (\ref{eq_dual_averaging_59}) we see that
 \begin{equation}
  \displaystyle\sum_{i = 1}^n s_i(f(x_i) - f(x^*)) \leq \frac{1}{2}\left(\alpha_n\|x^*\|_2^2 + M\displaystyle\sum_{i = 1}^n \frac{s_i^2}{\alpha_i}\right)
 \end{equation}
 which completes the proof.

\end{proof}

We conclude this section by noting several consequences of this theorem. First, the choice $s_i = \frac{1}{\sqrt{i}}$
and $\alpha_i = 1$ recovers subgradient descent. For this choice of $s_i$ and $\alpha_i$
$$\displaystyle\sum_{i = 1}^n s_i = O(\sqrt{n})
$$
and
$$\displaystyle\sum_{i = 1}^n \frac{s_i^2}{\alpha_i} = O(\log(n))
$$
and we obtain the bound from Theorem \ref{variable_subgradient_descent_thm}.

We can remove the logarithmic factor by
shifting the weights $s_i$ to the more recent iterates. For example, if we set $s_i = 1$ and $\alpha_i = \sqrt{i}$,
we recover the dual averaging method originally proposed by Nesterov, for which we have
$$\displaystyle\sum_{i = 1}^n s_i = n
$$
and
$$\displaystyle\sum_{i = 1}^n \frac{s_i^2}{\alpha_i} = O(\sqrt{n})
$$
which removes the logarithmic factor we had for subgradient descent.

Other choices are also reasonable, for example we could
set $s_n = n^c$ and $\alpha_n = n^{c + (1/2)}$ for any $c \geq -(1/2)$. As long as $c > -(1/2)$ we obtain the same convergence
rate up to a constant, if $c = (1/2)$, then we recover subgradient descent which introduces a logarithmic factor.

Next, we address the issue of scaling. If we scale the sequences $s_i$ and $\alpha_i$ by the same (positive) factor,
then both the bound in Theorem \ref{dual_averaging_theorem} and the method (\ref{generalized_dual_averaging}) don't change.
However, scaling one sequence and not the other has the effect of changing the `step size' of the method, and
changes the relative importance of the initial distance to the optimum $\|x^*\|_2^2$ and the Lipschitz constant $M$ in the
our bound.

Finally, we address the issue of setting the initial iterate to $0$. This is done solely out of notational convenience. In particular,
if we change the regularization term $\|x\|_2^2$ to $\|x - x_1\|_2^2$, our analysis is unchanged. In fact, we can replace the
$2$-norm regularizer by any strongly convex function, as the reader can easily verify. This is an important point which we
will use in a later section to extend these methods to the Banach space setting.


\input{SingleAssignmentSetup.tex}
\input{../WeekTitles.tex}
\begin{document}

\begin{center}
\subsection*{MNTC P01 - Week \#8 - \WeekTitleEight}
\end{center}

\newcommand{\Fext}{  F_{\mbox{ext}} }
\newcommand{\Fspring}{  F_{\mbox{spring}} }
\newcommand{\Fdamping}{  F_{\mbox{damp}} }

\begin{enumerate}

% ******************************
\item
  \begin{Question}
    Use \verb@ode45@ to generate a graph of the solution to the
    following DEs, over the specified interval, given the initial
    condition.

\begin{enumerate}
\item $\ds \frac{dy}{dt} = t^2 + y^2$, $y(0) = 0$, and $0 \le t \le 1$.
\item $\ds \frac{dy}{dt} = \sin(t) + \cos(y)$, $y(0) = 0$, and $0 \le t \le 10$.
\item $\ds \frac{dy}{dt} = (1-y^2) + 0.2 \sin(t)$, $y(0) = 0$, and $0 \le t \le 20$.
\end{enumerate}
\end{Question}

\begin{Solution}
Link to the MATLAB code: \\
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08DE01.m}{W08DE01.m}

Here are the graphs of the solutions.

\includegraphics[width = 0.3\linewidth]{graphics/Week08_DESolutions/W08DE01_a}
\includegraphics[width = 0.3\linewidth]{graphics/Week08_DESolutions/W08DE01_b}
\includegraphics[width = 0.3\linewidth]{graphics/Week08_DESolutions/W08DE01_c}

\end{Solution}

% ******************** Newton's Cooling  ********************************
\item
\begin{Question}
Newton's law of heating and cooling states that an object with
  temperature $T$ in an environment at temperature $T_{ext}$ will heat
  up or cool down according to the differential equation

 $$\frac{dT}{dt} = -k (T - T_{ext})$$

 Consider a garage used as a workshop.  Its insulation and surface
 area give $k$ a value of 0.1, if time $t$ is measured in hours and
 the temperatures, $T$ and $T_{ext}$, are in degrees Celsius.

The temperature outside changes during the day, as described by
the formula
$$T_{ext} =  10 + 7 \cos\left(\frac{\pi}{12} t\right)$$

We now imagine that the power goes out, with the garage at 23$^o$ C at
$t=0$.

\begin{enumerate}
\item Use ode45 and the DE to generate a numerical prediction of the
  garage's temperature $T$ over time.  Graph the solution over a time
  interval that shows both the initial and long-term behaviour of the
  temperature.

  In your script, try to use the functions \verb#title#,
  \verb#xlabel#, \verb#ylabel#, and \verb#legend# to annotate the
  graph to make it easier for a reader to understand.

  For the following questions, just use the graph or the numerical
  prediction of the temperature. You are {\em not} expected to solve
  the DE analytically.

\item How many days does it take for the garage to get into a
  consistent temperature cycle?  (You will need to estimate this by
  eye.)

\item How many degrees does the temperature in the building fluctuate
  by, once the temperature gets into a steady cycle?

\item Suppose the building were better insulated, so that the rate of
  heat loss were cut in half.  Should $k$ be half as large, or twice
  as large?

\item Generate a numerical prediction for the temperature over time in
  the better-insulated scenario, and produce a graph of the
  temperature vs time for both scenarios on the same axes.

\item How large are the temperature fluctuations in the building, now
  that the extra insulation has been added?  Does halving the net heat
  flow also halve the net temperature fluctuations?
\end{enumerate}

\end{Question}

\begin{Solution}
\begin{enumerate}
\item  A graph of the temperature over time is shown below:

  \includegraphics[width=3in]{graphics/Week08_Spring/W08GarageTemp_1}

The file
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08GarageTemp.m}{W08GarageTemp.m}
has the MATLAB code that generated the graph above.

\item From the graph, it takes the building roughly 2 days (48 hours)
  to get into a repeating cycle of temperature variation.

\item Careful zooming of the graph (or a look at the $y$ values in the
  ode45 output) give a highest temperature of 12.5 (high) and 7.5
  (low), for a net fluctuation of approximately 2.6 degrees per day.

\item $k$ represents the coefficient of heat flow between the building
  and the environment. The bigger $k$ is, the {\em larger} the
  headflow between the two.  Since we're adding insulation, this
  should {\em reduce} the heat flow, and so {\em lower} the value of
  $k$.

\item A graph of the heat change over time, given better insulation,
  is shown below.

  \includegraphics[width=3in]{graphics/Week08_Spring/W08GarageTemp_2}

\item Zooming in on the peaks of the {\bf red line} graph (new
  insulation model), the temperature now fluctuates between
  approximately 11.3 and 8.8 degrees Celsius, for a range of 2.5
  degrees.  This {\em is} roughly half the magnitude of the
  fluctuations we saw earlier.

\end{enumerate}
\end{Solution}

\hrulefill

\subsection*{Modelling Spring Systems}
% ******************************
\item  \label{SpringNoDampingNoFext}
  \begin{Question}
    \begin{minipage}[t]{0.6\linewidth}
\vspace{0pt}
Consider the single spring/mass system shown to the right, with no damper.

Newton's second law gives us the relationship:
\begin{align*}
ma & = \sum F = \Fspring \\
m x'' & = -k x
\end{align*}
where $k$ is the spring constant.
    \end{minipage}
    \begin{minipage}[t]{0.3\linewidth}
\vspace{0pt}
\begin{center}
\includegraphics[width=1.0in]{graphics/Week08_Spring/SpringNoDamping}
\end{center}
    \end{minipage}

\begin{enumerate}
\item By hand, write this second order DE as a system of 1st order
  DEs, using the new variables $w_1 = x$ and $w_2 = x'$

\item Write a MATLAB function file called \verb#springDE1.m# starting
  with the first line \\
  \verb#function dw_dt = springDE1(t, w, m, k)# \\
  that implements the system of differential equations from part (a).
\item Write a MATLAB script that simulates the motion of the mass
  using $m = 0.5$ kg and $k = 10$ N/m.  Choose the time interval for
  the simulation so that 4-5 cycles of oscillation are shown.
\end{enumerate}
\end{Question}

\begin{Solution}
 \begin{enumerate}
 \item  The first-order system would be:
 \begin{align*}
 \frac{d}{dt} w_1 & = x' = w_2 \\
 \frac{d}{dt} w_2 & = x'' = \frac{1}{m} \left(-kx \right) = \frac{1}{m} \left(-k w_1  \right)
 \end{align*}

\item The function file
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/springDE1.m}{springDE1.m}
  implements the differential equation system, with the $\Fext $ term
  left out.

\item The main script
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08SpringSimulation01.m}{W08SpringSimulation01.m}
has the code that will run this simulation.

In the resulting plot, we see a very nice example of simple harmonic
motion.

\begin{center}
\includegraphics[width=3in]{graphics/Week08_Spring/W08SpringSimulation01}
\end{center}

\end{enumerate}
\end{Solution}

% ******************************
\item  \label{SpringNoDampingWithFext}
  \begin{Question}
    \begin{minipage}[t]{0.6\linewidth}
\vspace{0pt}
Consider the single spring/mass system shown to the right, with no damper.

This is the same as in Question \ref{SpringNoDampingNoFext}, except
with the addition of the $\Fext$ shown as an external applied force.

Newton's second law gives us the relationship:
\begin{align*}
ma & = \sum F = \Fspring + \Fext \\
m x'' & = -k x + \Fext
\end{align*}
where $k$ is the spring constant.
    \end{minipage}
    \begin{minipage}[t]{0.3\linewidth}
\vspace{0pt}
\begin{center}
\includegraphics[width=1.0in]{graphics/Week08_Spring/SpringNoDampingWithFext}
\end{center}
    \end{minipage}

\begin{enumerate}
\item By hand, write this second order DE as a system of 1st order
  DEs, using the new variables $w_1 = x$ and $w_2 = x'$

\item We will now incorporate an external force of the form
  $F_{\mbox{ext}} = a \sin(b t)$. Write a MATLAB function file called
  \verb#springDE2.m# starting with the first line \\
  \verb#function dw_dt = springDE2(t, w, m, k, a, b)# \\
  that implements the system of differential equations from part (a).

\item Create a new MATLAB script.  In the script, set $m =0.5$ kg,
  $k = 10$ N/m, and use $a = 5$ and $b = 1$ in
  $F_{\mbox{ext}} = a \sin(bt)$.  Use \verb#ode45# to simulate the
  motion of the spring for 30 seconds (\verb#tspan = [0, 30]#), given
  an initial displacement of $x(0) = 0.2$ m, and initial velocity of
  zero: $x'(0) = 0$. \label{forced}

\item Explain why the motion looks so disorganized.

\item Repeat Question (\ref{forced}), but with an external force of
  $\Fext = \sin(4 t)$. Explain why the motion in this case has cyclic
  waves in its amplitude.

\end{enumerate}
  \end{Question}

\begin{Solution}
 \begin{enumerate}
 \item  The first-order system would be:
 \begin{align*}
 \frac{d}{dt} w_1 & = x' = w_2 \\
 \frac{d}{dt} w_2 & = x'' = \frac{1}{m} \left(-kx + \Fext\right) = \frac{1}{m} \left(-k w_1  + \Fext \right)
 \end{align*}

\item The file
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/springDE2.m}{springDE2.m}
  implements the differential equation system, with new external force
  $\Fext = a \sin(bt)$.
\item The file
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08SpringSimulation02.m}{W08SpringSimulation02.m}
has the code that will run this simulation.

In the resulting plot, see some wildly varying and irregular
oscillations.

\begin{center}
\includegraphics[width=3in]{graphics/Week08_Spring/W08SpringSimulation02}
\end{center}

\item The motion of the mass looks very disorganized because the
  natural frequency (the frequency at which the mass would oscillate
  if you just let swing on its own) is different from the frequency
  that we are pushing and pulling on it with through $\Fext$.

  Recall: the natural frequency of a spring/mass system is given by
  $\omega = \sqrt{k/m}$, which for this scenario gives $\omega = \sqrt{ \frac{25}{4}}$

\item
The file
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08SpringSimulation02.m}{W08SpringSimulation02.m}
  has the code that will run this simulation.  Here is a graph of the
  resulting mass motion.

\begin{center}
\includegraphics[width=3in]{graphics/Week08_Spring/W08SpringSimulation03}
\end{center}

In the plot, we see
  that the natural frequency and the regular stimulation by the
  outside force are close to each other: the natural frequency is
  $\omega = \sqrt{\frac{10}{0.5}} \approx 4.5$, rad/s, and the
  stimulating frequency is at $\omega = 4$ rad/s.  This close match of
  the frequencies leads to the phenonenon called {\em beats}, or {\em
    near resonance}.
\end{enumerate}
\end{Solution}

% ******************************
\item
  \begin{Question}
    \begin{minipage}[t]{0.6\linewidth}
      \vspace{0pt} We return to the same single spring/mass system
      from Question \ref{SpringNoDampingWithFext}, shown to the right,
      with no damper.

The $\Fext$ shown is an external applied force.
    \end{minipage}
    \begin{minipage}[t]{0.3\linewidth}
\vspace{0pt}
\begin{center}
\includegraphics[width=1.0in]{graphics/Week08_Spring/SpringNoDampingWithFext}
\end{center}
    \end{minipage}

\begin{enumerate}
\item For a mass of $m = 5$ kg , and a spring constant of $k = 2$ N/m,
  what is the natural frequency of the system?
\item Define an external force of the form $\Fext = \sin(b t)$ that will
produce {\bf resonance} in the system.
\item Use MATLAB to simulate the motion of the spring, with your
  selected external force, using an initial condition where the mass
  starts at its equilibrium and at rest.
\item The system will break if the oscillations become too large,
  specifically if $x(t)$ exceeds 2 m (in the positive or negative
  directions).  Does the system break, and if so, how long does it
  take for the system to break?
\end{enumerate}
\end{Question}

\begin{Solution}
  \begin{enumerate}[(a)]
  \item The natural frequency of a spring/mass system is given by
    $\omega = \sqrt{\frac{k}{m}} = \sqrt{\frac{2}{5}} \approx 0.6325$
    rad/s.
  \item If we want to produce resonance in the oscillations, our
    applied force's frequency must be exactly at the same frequency as
    the natural frequency. That way, the natural oscillations and the
    applied force are perfectly synchronized, leading to a build-up of
    the energy in the system.

    This means we should select $\Fext = sin( 0.6325 t)$.

  \item For this problem, we can recycle the differential equation
    code in
    \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/springDE2.m}{springDE2.m}
    from Question \ref{SpringNoDampingWithFext}, because the forces
    (spring and $\Fext$) are in the same form as in that problem.

    In our new main script,
    \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08Resonance1.m}{W08Resonance1.m}
    we will set up all the constants needed for the simulation:
    \begin{itemize}
    \item $m = 5$, $k = 2$,
    \item $a = 1$ and $b= 0.6325$ to define
      $\Fext = a \sin(bt) = 1 \cdot \sin(0.6325 t)$.
    \end{itemize}

    Below is a graph of the simulated motion of the mass over 30
    seconds:
\begin{center}
\includegraphics[width=0.4\linewidth]{graphics/Week08_Spring/W08SpringResonance1}
\end{center}

\item From the graph, we see that the amplitude of the oscillations
  are growing with every cycle, as expected when we induce resonance.  This means that the system {\em will } break at some point.

  The specific limit we were given was that breakage will occur if
  $x(t)$ exceeds 2 m. Zooming in, we can see that the system will
  reach $x(t) \approx 2$ m around $t = 14$ seconds.

\begin{center}
\includegraphics[width=0.4\linewidth]{graphics/Week08_Spring/W08Resonance1_BreakPoint}
\end{center}

  \end{enumerate}
\end{Solution}

% ******************************
\item
  \begin{Question}
    \begin{minipage}[t]{0.6\linewidth}
      \vspace{0pt} Consider the damped system shown at right.

      The damping force exerted by the dashpot/damper is proportional
      to the velocity of the mass.

Newton's second law gives us the relationship:
\begin{align*}
ma & = \sum F = \Fspring + \Fdamping \\
m x'' & = -k x  - c x'
\end{align*}
where $k$ is the spring constant in N/m, and $c$ is the damping
coefficient in N/(m/s).

    \end{minipage}
    \begin{minipage}[t]{0.3\linewidth}
\vspace{0pt}
\begin{center}
\includegraphics[width=0.9\linewidth]{graphics/Week08_Spring/W08DampedSpringNoFext}
\end{center}
    \end{minipage}

\begin{enumerate}[(a)]
\item By hand, write this second order DE as a system of 1st order
  DEs, using the new variables $w_1 = x$ and $w_2 = x'$

\item We define a system with a mass of $m = 10$ kg, spring constant
  $k = 2$ N/m, and a damping coefficient of $c = 0.4$ N/(m/s).  If the
  system is displaced by 0.5 m and then let go with zero initial
  velocity, use MATLAB to find out how long (in both seconds and
  cycles) it takes for the oscillations to reach approximately 10\% of
  their original amplitude.  Note: you will need to write both a
  MATLAB function for the differential equation, and a main script to
  run the simulation.
\item What damping coefficient would be needed for the oscillations to
  be reduced to 10\% of their original amplitude within 3 cycles?  You
  will need to estimate your answer based on guessing and checking
  against the graph.  Hint: add horizontal lines to the solution plot
  at $x = 0.05$ and $x = -0.05$ to see easily whether the oscillations
  are reduced to that level.
\end{enumerate}
\end{Question}

\begin{Solution}
  \begin{enumerate}[(a)]
 \item  The first-order system would be:
 \begin{align*}
 \frac{d}{dt} w_1 & = x' = w_2 \\
 \frac{d}{dt} w_2 & = x'' = \frac{1}{m} \left(-kx -c x'\right) = \frac{1}{m} \left(-k w_1  - c w_2\right)
 \end{align*}

\item To simulate the motion of the spring/mass system, we need a
  function file with the differential equation, which we called
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/springDEDamped.m}{springDEDamped.m},
  and a main script, which we called
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08DampedSpringSystem.m}{W08DampedSpringSystem.m}.

  Here is the graph of the resulting motion, showing a nice
  oscillatory pattern, but the amplitude diminishing over time due to
  the damping.

\begin{center}
\includegraphics[width=3in]{graphics/Week08_Spring/W08DampedSpring1A}
\end{center}

seconds, or between 8 and 9 cycles.

\begin{center}
\includegraphics[width=3in]{graphics/Week08_Spring/W08DampedSpring1B}
\end{center}

\item Using $c = 1.1$ N/(m/s) gives the required damping, putting the amplitude
of the oscillations below 10\% of their original magnitude after 3 cycles.

\begin{center}
\includegraphics[width=3in]{graphics/Week08_Spring/W08DampedSpring1C}
\end{center}

  \end{enumerate}
\end{Solution}

% %****************
\item
\begin{Question}
  For a spring/mass system with $m=1$ kg, $c=0.5$ N/(m/s), and $k=45$,
  approximately what frequency of external forcing would produce the
  largest amplitude steady-state vibration?  Give your answer to the
  nearest 0.5 rad/s.

  You will need to estimate the answer based on guessing and checking
  against the graph.
 \end{Question}

 \begin{Solution}
   The natural frequency is going to be near
   $\omega = \sqrt{k/m} \approx 6.7$ rad/s.

   To run the simulation in MATLAB, we know we need to build the
   differential equation based on the sum of the forces in MATLAB.  $ma = \sum F$ gives us
$$ m x'' = -kx - c x' + \sin(bt)$$
where $b$ is the frequency we can experiment with to maximize the
steady-state oscillations.

The first-order system version of this is:
 \begin{align*}
 \frac{d}{dt} w_1 & = x' = w_2 \\
 \frac{d}{dt} w_2 & = x'' =  \frac{1}{m} \left(-kx -c x'+ \sin(bt) \right) \\
& = \frac{1}{m} \left(-k w_1  - c w_2 + \sin(bt) \right)
 \end{align*}

 We built this system of differential equations into the file
 \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/springDEDampedAndForced.m}{springDEDampedAndForced.m}
and the main script
 \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08DampedSpringSystem3.m}{W08DampedSpringSystem3.m}

 If we are only interested in the steady-state behaviour, then the
 initial conditions won't matter (their influence fades in the long
 run). We make the easiest choice, which is the mass starting at
 equilibrium at rest, $x(0) = 0$ and $x'(0) = 0$.

 Below is a graph showing the response of the system to
 $b = 5, 5.5, 6, 6.5$ and 7 rad/s, and then the graph for $b = 6.5$
 rad/s only.

\begin{center}
\includegraphics[height=2.6in]{graphics/Week08_Spring/W08DampedAndForced1}
\includegraphics[height=2.6in]{graphics/Week08_Spring/W08DampedAndForced2}
\end{center}

Looking for the steady-state oscillations means looking at the
amplitudes once the graph steadies into a regular repeating pattern.
The graph corresponding to $\Fext = \sin(6.5~t)$ is the graph with the
highest amplitude once that steady-state oscillation pattern is
reached.

 \end{Solution}

\end{enumerate}

\end{document}