\documentclass[letterpaper, twoside, 12pt]{book}
\usepackage{packet}

\begin{document}

\setcounter{chapter}{3}

\chapter{Packet 4.1: Sections 16.1-16.4}

\setcounter{chapter}{16}
\setcounter{section}{0}

\section{Vector Fields} %16.1

\begin{definition}
  A \textbf{vector field} assigns a vector to each point in 2D or 3D space.
    \[
      \vect{F}=
      \vect{F}(\vect{r})=
      \vect{F}(x,y)=
      \<P(x,y),Q(x,y)\>=
      \<P(\vect{r}),Q(\vect{r})\>=
      \<P,Q\>
    \]
    \[
      \vect{F}=
      \vect{F}(\vect{r})=
      \vect{F}(x,y,z)=
      \<P(x,y,z),Q(x,y,z),R(x,y,z)\>=
      \<P(\vect{r}),Q(\vect{r}),R(\vect{r})\>=
      \<P,Q,R\>
    \]
\end{definition}

          \begin{problem}
            Sketch the vector field $\vect{F}=\<x+y,2y\>$ for
            all $x\in\{0,1,2\}$ and $y\in\{0,1,2\}$.
          \end{problem}

          \begin{solution}
            See
            \url{http://kevinmehall.net/p/equationexplorer/vectorfield.html#(x+y)i+2yj%7C%5B-1,4,-1,4%5D}
          \end{solution}

\begin{remark}
  The gradient vector function
    \[
      \nabla f (x,y)
        =
      \<f_x(x,y),f_y(x,y)\>
    \]
    \[
      \nabla f (x,y,z)
        =
      \<f_x(x,y,z),f_y(x,y,z),f_z(x,y,z)\>
    \]
  is a vector field which yields normal vectors
  to the level surfaces of the function $f$.
\end{remark}

          \begin{problem}
            Compute $\nabla f$ for the function
            $f(x,y)=x^2-2xy+y$, and then
            sketch the vector field $\nabla f$
            all $x\in\{0,1,2\}$ and $y\in\{0,1,2\}$.
          \end{problem}

          \begin{solution}
            \(
              \nabla f
                =
              \<
                \frac{\partial}{\partial x}[x^2-2xy+y],
                \frac{\partial}{\partial y}[x^2-2xy+y]
              \>
                =
              \<
                2x-2y,
                -2x+1
              \>
            \)

            See
            \url{http://kevinmehall.net/p/equationexplorer/vectorfield.html#(x+y)i+2yj%7C%5B-1,4,-1,4%5D}
          \end{solution}

\section{Line Integrals} %16.2

\begin{theorem}
  Some vector functions which parameterize curves follow.
  \begin{itemize}
    \item
    A line segment beginning at $P_0$ and ending at $P_1$:
      \[
        \vect{r}(t) = \vect{P_0} + t\vect{P_0P_1}, 0\leq t\leq 1
      \]
    \item
    A circle centered at the origin with radius $a$:
      \[
        \vect{r}(t) = \<a\cos t,a\sin t\>, 0\leq t\leq 2\pi
        \text{ (full counter-clockwise rotation)}
      \]
      \[
        \vect{r}(t) = \<a\sin t,a\cos t\>, 0\leq t\leq 2\pi
        \text{ (full clockwise rotation)}
      \]
    \item
    A planar curve given by $y=f(x)$ from $(x_0,y_0)$ to $(x_1,y_1)$
      \[
        \vect{r}(t) = \<t,f(t)\>, x_0\leq t\leq x_1
        \text{ (left-to-right)}
      \]
      \[
        \vect{r}(t) = \<-t,f(-t)\>, -x_0\leq t\leq -x_1
        \text{ (right-to-left)}
      \]
    \end{itemize}
\end{theorem}

          \begin{problem}
            Give a vector function which parameterizes the line segment
            from the point $(0,3,-2)$ to the point $(4,-1,0)$.
          \end{problem}

          \begin{solution}
            Using the above theorem:
            \[
              \vect{r}(t) = \vect{P_0} + t\vect{P_0P_1}, 0\leq t\leq 1
            \]
            \[
              \vect{r}(t) = \<0,3,-2\> + t\<4-0,-1-3,0-(-2)\>, 0\leq t\leq 1
            \]
            \[
              \vect{r}(t) = \<4t,3-4t,-2+2t\>, 0\leq t\leq 1
            \]
          \end{solution}

          \begin{problem}
            Give a vector function which parameterizes the curve
            $y=x^3-2x$ from the point $(1,-1)$ to the point $(-1,1)$.
          \end{problem}

          \begin{solution}
            Using the above theorem (noting that \((1,-1)\) is to
            the right of \((-1,1)\)):
              \[
                \vect{r}(t) = \<-t,f(-t)\>, -x_0\leq t\leq -x_1
              \]
              \[
                \vect{r}(t) = \<-t,(-t)^3-2(-t)\>, -1\leq t\leq 1
              \]
              \[
                \vect{r}(t) = \<-t,-t^3+2t\>, -1\leq t\leq 1
              \]
          \end{solution}

          \begin{problem}
            Give a vector function which parameterizes the curve
            $x^2+y^2=9$ from the point $(3,0)$ clockwise to the point $(0,-3)$.
          \end{problem}

          \begin{solution}
            Using the above theorem, we may get a full clockwise rotation
            by using:
              \[
                \vect{r}(t) = \<a\sin t,a\cos t\>, 0\leq t\leq 2\pi
              \]
              \[
                \vect{r}(t) = \<3\sin t,3\cos t\>, 0\leq t\leq 2\pi
              \]

            To obtain the portion from \((3,0)\) to \((0,-3)\), we note
            that we may plug in \(\pi/2\) and \(\pi\) respectively to
            get those points.
            Therefore the vector function is:
              \[
                \vect{r}(t) = \<3\sin t,3\cos t\>, \frac{\pi}{2}\leq t\leq \pi
              \]
          \end{solution}

\begin{definition}
The \textbf{line integral with respect to arclength} of a function of many
variables $f(\vect{r})$ along a curve $C$ is given by
  \[
    \int_C f(\vect{r})\dvar{s} =
    \lim_{n\to\infty}\sum_{i=1}^n f(\vect{r}_{n,i})\Delta s_{n,i}
  \]
where for each positive integer $n$ we've defined a way to partition $C$
into $n$ pieces
  \[
    \Delta C_{n,1},\Delta C_{n,2},\dots,\Delta C_{n,n}
  \]
where $\Delta C_{n,i}$ has length $\Delta s_{n,i}$, contains the position
vector $\vect{r}_{n,i}$, and
  \[
    \lim_{n\to\infty} \max(\Delta s_{n,i}) = 0
  \]
\end{definition}

\begin{theorem}
If $\vect{r}(t)$ is a parametrization of $C$ for $a \leq t \leq b$, then
  \[
    \int_C f(\vect{r})\dvar{s}
    =\int_{t=a}^{t=b} f(\vect{r}(t))\frac{ds}{dt}\dvar{t}
  \]
\end{theorem}

          \begin{problem}
            Evaluate $\int_C z + 2xy\dvar{s}$ where $C$ is the line segment
            from $(0,-1,3)$ to $(2,2,-3)$.
          \end{problem}

          \begin{solution}
            We'll use the parametrization
              \[
                \vect{r}(t)
                  =
                \<2t,-1+3t,3-6t\>,
                0\leq t\leq 1
              \]
            for which
              \[
                \frac{d\vect{r}}{dt}
                  =
                \<2,3,-6\>
              \]
              \[
                \frac{ds}{dt}
                  =
                \left|\frac{d\vect{r}}{dt}\right|
                  =
                \sqrt{2^2+3^2+(-6)^2}
                  =
                \sqrt{4+9+36}
                  =
                7
              \]

            Therefore
              \[
                \int_C z + 2xy\dvar{s}
                  =
                \int_0^1 (z + 2xy)\frac{ds}{dt}\dvar{t}
                  =
                \int_0^1 [(3-6t) + 2(2t)(-1+3t)](7)\dvar{t}
              \]
              \[
                  =
                \int_0^1 [3-10t+12t^2](7)\dvar{t}
                  =
                (7)[3t-5t^2+4t^3]_0^1
                  =
                (7)(2)
                  =
                14
              \]
          \end{solution}

          \begin{problem}
            Prove that $\int_C xy\dvar{s}=\int_0^1 t^3\sqrt{1+4t^2}\dvar{t}$
            where $C$ is the parabolic arc
            on $y=x^2$ from $(0,0)$ to $(1,1)$.
          \end{problem}

          \begin{solution}
            We'll use the parametrization
              \[
                \vect{r}(t)
                  =
                \<t,t^2\>,
                0\leq t\leq 1
              \]
            for which
              \[
                \frac{d\vect{r}}{dt}
                  =
                \<1,2t\>
              \]
              \[
                \frac{ds}{dt}
                  =
                \left|\frac{d\vect{r}}{dt}\right|
                  =
                \sqrt{1^2+(2t)^2}
                  =
                \sqrt{1+4t^2}
              \]

            Therefore
              \[
                \int_C xy\dvar{s}
                  =
                \int_0^1 xy\frac{ds}{dt}\dvar{t}
                  =
                \int_0^1 (t)(t^2)\sqrt{1+4t^2}\dvar{t}
                  =
                \int_0^1 t^3\sqrt{1+4t^2}\dvar{t}
              \]
          \end{solution}

\begin{definition}
The \textbf{line integral of a vector field} $\vect F$
over the curve $C$ is given by
  \[
    \int_C \vect F\cdot\dvar{\vect r} =
    \lim_{n\to\infty}\sum_{i=1}^n
    \vect F(\vect{r}_{n,i})\cdot\Delta\vect{C}_{n,i}
  \]
where for each positive integer $n$ we've defined a way to approximate $C$
with $n$ vectors
  \[
    \Delta \vect{C}_{n,1},\Delta \vect{C}_{n,2},\dots,\Delta \vect{C}_{n,n}
  \]
where $\vect{r}_{n,i}+\Delta \vect{C}_{n,i}=\vect{r}_{n,i+1}$
and
  \[
    \lim_{n\to\infty} \max(|\Delta \vect{C}_{n,i}|) = 0
  \]
\end{definition}

\begin{definition}
The line integral of a vector field $\vect F$ over the curve $C$
may be computed by
    \[
      \int_C \vect{F}\cdot\dvar{\vect r}
        =
      \int_C \vect{F}\cdot\vect{T}\dvar{s}
    \]
where $\vect T$ yields the unit tangent vectors to the curve $C$.
\end{definition}

\begin{definition}
If $\vect{r}(t)$ is a parametrization of $C$ for $a \leq t \leq b$, then
    \[
      \int_C \vect{F}\cdot\dvar{\vect r}
        =
      \int_{t=a}^{t=b} \vect{F}\cdot\frac{d\vect{r}}{dt}\dvar{t}
    \]
\end{definition}

          \begin{problem}
            Prove that
            $\int_C \<2x,y-x\>\cdot\dvar{\vect{r}}
              =
            \int_0^1 23t-7 \dvar{t}$
            where $C$ is the line segment given by the vector equation
            $\vect{r}(t)=\<1-2t,3t\>$ for $0\leq t\leq 1$.
          \end{problem}

          \begin{solution}
            We'll use the parametrization
              \[
                \vect{r}(t)
                  =
                \<1-2t,3t\>,
                0\leq t\leq 1
              \]
            for which
              \[
                \frac{d\vect{r}}{dt}
                  =
                \<-2,3\>
              \]

            Therefore
              \[
                \int_C \<2x,y-x\>\cdot\dvar{\vect{r}}
                  =
                \int_0^1 \<2x,y-x\>\cdot\frac{d\vect{r}}{dt}\dvar{t}
                  =
                \int_0^1 \<2(1-2t),3t-(1-2t)\>\cdot\<-2,3\>\dvar{t}
              \]
              \[
                  =
                \int_0^1 \<2-4t,5t-1\>\cdot\<-2,3\>\dvar{t}
                  =
                \int_0^1 (-4+8t)+(15t-3)\dvar{t}
                  =
                \int_0^1 23t-7\dvar{t}
              \]
          \end{solution}

\begin{remark}
  The work done by a force vector field $\vect{F}$ over the curve $C$
  is given by $\int_C\vect{F}\cdot\dvar{\vect{r}}$.
\end{remark}

          \begin{problem}
            Find the work done by the force vector field
            $\<-3y,3x\>$ moving a particle one rotation counter-clockwise
            around the unit circle $x^2+y^2=1$.
          \end{problem}

          \begin{solution}
            We'll use the parametrization
              \[
                \vect{r}(t)
                  =
                \<\cos t,\sin t\>,
                0\leq t\leq 2\pi
              \]
            for which
              \[
                \frac{d\vect{r}}{dt}
                  =
                \<-\sin t,\cos t\>
              \]

            Therefore
              \[
                W
                  =
                \int_C \<-3y,3x\>\cdot\dvar{\vect{r}}
                  =
                \int_0^{2\pi} \<-3y,3x\>\cdot\frac{d\vect{r}}{dt}\dvar{t}
                  =
                \int_0^{2\pi}
                  \<-3\sin t,3\cos t\>\cdot\<-\sin t,\cos t\>
                \dvar{t}
              \]
              \[
                  =
                \int_0^{2\pi}
                  3\sin^2 t + 3\cos^2 t
                \dvar{t}
                  =
                \int_0^{2\pi}
                  3
                \dvar{t}
                  =
                6\pi
              \]
          \end{solution}

\begin{theorem}
  If $C$ may be split into two curves $C_1$ and $C_2$, then
  \[
    \int_C f\dvar{s}
      =
    \int_{C_1} f\dvar{s}
      +
    \int_{C_2} f\dvar{s}
  \]
  and
  \[
    \int_C \vect F\cdot\dvar{\vect r}
      =
    \int_{C_1} \vect F\cdot\dvar{\vect r}
      +
    \int_{C_2} \vect F\cdot\dvar{\vect r}
  \]
\end{theorem}

\begin{theorem}
  If $-C$ is the curve $C$ oriented in the opposite direction, then
  \[
    \int_C f\dvar{s}
      =
    \int_{-C} f\dvar{s}
  \]
  and
  \[
    \int_C \vect F\cdot\dvar{\vect r}
      =
    - \int_{-C} \vect F\cdot\dvar{\vect r}
  \]
\end{theorem}

          \begin{problem}
            Write a paragraph explaining why a negative appears in the
            previous theorem for the
            line integral of a vector field but not for an arclength
            line integral.
          \end{problem}

          \begin{solution}
            \(\int_C f\dvar{s}\) measures the area of a ribbon
            of height \(f\) at each point above \(C\), so the orientation
            of \(C\) is irrelevant.

            But \(\int_C \vect F\cdot\dvar{\vect r}\) measures the work
            done by \(\vect F\) moving through the curve \(C\), so
            if the orientation of \(C\) is reversed, then the motion is
            in the opposite direction as before, so work is negated.
          \end{solution}

\section{The Fundamental Theorem for Line Integrals} %16.3

\begin{definition}
  If $\nabla f=\vect{F}$, then $f$ is a \textbf{potential function}
  for the \textbf{conservative field} $\vect{F}$.
\end{definition}

          \begin{problem} %x^2-3yz
            Prove that $\<2x,-3z,-3y\>$ is a conservative field by
            finding a potential function $f$ for it. Hint: such an $f$
            must satisfy that $f=x^2+\Phi_1(y,z)$, $f=-3yz+\Phi_2(x,z)$,
            and $f=-3yz+\Phi_3(x,y)$ for some functions $\Phi_i$. (Why?)
          \end{problem}

          \begin{solution}
            We want to solve the system
              \[
                f_x = 2x
              \]
              \[
                f_y = -3z
              \]
              \[
                f_z = -3y
              \]
            for a potential function \(f\). Note \(f\) must satisfy
            each of the anti-partial-derivatives:
              \[
                f = x^2+\Phi_1(y,z)
              \]
              \[
                f = -3yz+\Phi_2(x,z)
              \]
              \[
                f = -3yz+\Phi_3(x,y)
              \]

            So \(f=x^2-3yz\) satisfies all three. Therefore since
            \(\nabla f=\vect{F}\), \(\vect F\) is conservative.
          \end{solution}

\begin{theorem}
  The Fundamental Theorem for Line Integrals:
  If $C$ is any smooth curve beginning at the point $A$ and ending at the
  point $B$, then
  \[
    \int_C \nabla f\cdot \dvar{\vect{r}} = \left[f\right]_A^B = f(B)-f(A)
  \]
\end{theorem}

          \begin{problem}
            Prove that if $C$ is any smooth \textbf{closed curve}
            (beginning and ending at the same point), then
            \[
              \int_C \nabla f\cdot \dvar{\vect{r}} = 0
            \]
          \end{problem}

          \begin{solution}
            Let \(C\) begin and end at the point \(A\).
            \[
              \int_C \nabla f\cdot \dvar{\vect{r}}
                =
              [f]_A^A
                =
              f(A)-f(A)
                =
              0
            \]
          \end{solution}

          \begin{problem}
            Compute $\int_C\<4,z^2,2yz\>\cdot\dvar{\vect r}$ where
            $C$ is the curve given by
            $\vect{r}(t)=\<2^t,\sin (\pi t),4t^2\>$ for $0\leq t\leq 1$.
            Then compute $\int_{C'}\<4,z^2,2yz\>\cdot\dvar{\vect r}$ where
            $C'$ is the line segment starting at $(1,0,0)$ and ending
            at $(2,0,4)$.
          \end{problem}

          \begin{solution}
            By plugging in \(0\) and \(1\) into \(\vect{r}(t)\),
            we see that both curves begin at \((1,0,0)\) and end at
            \((2,0,4)\).

            It follows that \(f=4x+2yz^2\) satisfies
              \[
                f_x=4
              \]
              \[
                f_y=z^2
              \]
              \[
                f_z=2yz
              \]
            so for both curves,
              \[
                \int_C\<4,z^2,2yz\>\cdot\dvar{\vect r}
                  =
                \int_{C'}\<4,z^2,2yz\>\cdot\dvar{\vect r}
                  =
                [4x+2yz^2]_{(1,0,0)}^{(2,0,4)}
                  =
                [8+0]-[4+0]
                  =
                4
              \]
          \end{solution}

          \begin{problem}
            Prove that if $f$ is a potential function for the vector field
            $\<P,Q,R\>$, then
            $P_y=Q_x$, $P_z=R_x$, and $Q_z=R_y$. (Hint: use the mixed derivative
            theorem.)
          \end{problem}

          \begin{solution}
            Note that \(P=f_x\), \(Q=f_y\), and \(R=f_z\). So it follows
            by the mixed derivative theorem that:
              \[
                P_y=f_{xy}=f_{yx}=Q_x
              \]
              \[
                P_z=f_{xz}=f_{zx}=R_x
              \]
              \[
                Q_z=f_{yz}=f_{zy}=R_y
              \]
          \end{solution}

\begin{theorem}
  $\vect{F}=\<P,Q,R\>$ is a conservative vector field if and only if
  $P_y=Q_x$, $P_z=R_x$, and $Q_z=R_y$.
\end{theorem}

          \begin{problem}
            Prove that
            $\int_C\<ye^{xy+z},xe^{xy+z},e^{xy+z}\>\cdot\dvar{\vect r}=0$
            where $C$ is the curve given by
            $\vect{r}(t)=\<\frac{1}{1+t^2},\cos t,e^{1-t^2}\>$
            for $-1 \leq t \leq 1$.
          \end{problem}

          \begin{solution}
            By the previous theorem, we can show that
            \(\<ye^{xy+z},xe^{xy+z},e^{xy+z}\>\) is conservative by:
              \[
                P_y=e^{xy+z}+xye^{xy+z}=Q_x
              \]
              \[
                P_z=ye^{xy+z}=R_x
              \]
              \[
                Q_z=xe^{xy+z}=R_y
              \]

            Since \(C\) starts at \(\vect{r}(-1)=\<\frac{1}{2},\cos 1,1\>\)
            and ends at the same point
            \(\vect{r}(1)=\<\frac{1}{2},\cos 1,1\>\), the result of
            Problem 25 shows that
              \[
                \int_C\<ye^{xy+z},xe^{xy+z},e^{xy+z}\>\cdot\dvar{\vect r}=0
              \]
          \end{solution}

\section{Green's Theorem} %16.4

\begin{theorem}
  Let $C$ be the boundary of the region $R$ in the $xy$ plane oriented
  counter-clockwise, and let $\vect{F}$ be a two-dimensional vector field. Then
  \[
    \int_C \vect{F}\cdot\dvar{\vect{r}}
      =
    \iint_R \left(\frac{\p Q}{\p x}-\frac{\p P}{\p y}\right)\dvar{A}
  \]
\end{theorem}

          \begin{problem}
            Evaluate $\int_C\<x^2+y,x+y\>\cdot\dvar{\vect r}$ where
            $C$ is the boundary of the unit square oriented counter-clockwise.
          \end{problem}

          \begin{solution}
            Green's Theorem tells us that
            \[
              \int_C \<x^2+y,x+y\>\cdot\dvar{\vect{r}}
                =
              \iint_R \left(1-1\right)\dvar{A}
                =
              \iint_R 0\dvar{A}
                =
              0
            \]

            (This also works because \(C\) is a closed curve and
            \(\<x^2+y,x+y\>\) is conservative.)
          \end{solution}

          \begin{problem}
            Find the work done by a force vector field $\<y,2x\>$ moving an
            object around the
            boundary of the triangle with vertices $(1,2)$, $(-1,-2)$, and
            $(3,-2)$ oriented clockwise.
          \end{problem}

          \begin{solution}
            Green's Theorem tells us that
            \[
              \int_C \<y,2x\>\cdot\dvar{\vect{r}}
                =
              \iint_R \left(2-1\right)\dvar{A}
                =
              \iint_R 1\dvar{A}
            \]

            Since \(R\) is a triangle with base \(4\) and height \(4\),
            we conclude that
            \[
              \iint_R 1\dvar{A}
                =
              \frac{1}{2}(4)(4)
                =
              8
            \]
            (or we could set it up like a Type II double integral
            if we like doing extra work).
          \end{solution}

\end{document}


\documentclass[11pt, oneside]{article}   	% use "amsart" instead of "article" for AMSLaTeX format
\usepackage{geometry}                		% See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}                   		% ... or a4paper or a5paper or ...
%\geometry{landscape}                		% Activate for for rotated page geometry
%\usepackage[parfill]{parskip}    		% Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}				% Use pdf, png, jpg, or eps§ with pdflatex; use eps in DVI mode
								% TeX will automatically convert eps --> pdf in pdflatex
\usepackage{amssymb}
\usepackage{amsmath}

\usepackage{url}
\usepackage{hyperref}

\title{Notes on continuous- and discrete-time chemical master equation kinetics}
\author{Vincent Voelz}
\date{May 4, 2018}							% Activate to display a given date or no date

\begin{document}
\maketitle

\section*{The Chemical Master Equation}

The \textbf{chemical master equation} describes the time evolution of the populations of multiple chemical species, based on their rates of interconversion:
\[
\frac{d}{dt} \mathbf{p} = \mathbf{K} \mathbf{p}
\]
Here, $\mathbf{p} = (p_1, p_2, ..., p_n)$ is a column vector of state populations (i.e. chemical species), and $\mathbf{K}$ is a \textit{rate matrix}.  The solution of this equation gives the time evolution of the populations $\mathbf{p}(t)$  in terms of the initial populations $\mathbf{p}(0)$, and some properties of the rate matrix $\mathbf{K}$.

\subsection*{A two-state master equation}

As a simple example, let's first consider the case when $n=2$. Here, there are two chemical species, $A$ and $B$, and a reaction $A \rightarrow B$ with forward rate $k_f$ (s$^{-1}$) and backward rate $k_b$ (s$^{-1}$).

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=0.3\textwidth]{two-state}
%\caption{\label{fig:your-figure}Caption goes here.}
\end{center}
\end{figure}

The population flux of the species is described by the differential equations
\[
\frac{d}{dt} p_A = -k_f p_A + k_b p_B
\]
\[
\frac{d}{dt} p_B = +k_f p_A - k_b p_B
\]
Or, more consisely,
\[
\frac{d}{dt} \begin{pmatrix}
	p_A \\
	p_B \end{pmatrix} = \begin{pmatrix}
	-k_f & k_b \\
	k_f  & -k_b \end{pmatrix} \begin{pmatrix}
	p_A \\
	p_B \end{pmatrix}
\]
This is now in the form of a master equation with $\mathbf{p} = (p_A, p_B)$ and $\mathbf{K} = \begin{pmatrix}
	-k_f & k_b \\
	k_f  & -k_b \end{pmatrix} $:
\[
\frac{d}{dt} \mathbf{p} = \mathbf{K} \mathbf{p}
\]

Note that the rate matrix $\mathbf{K}$ is \textit{singular} (i.e. \textit{degenerate}), meaning it's not invertible because one row of this matrix is a linear combination of the other.  This means that the determinant of this matrix will be zero, and there must be at least one eigenvalue of $K$ that is zero.

This fact has physical significance.  Consider the eigenvector $\psi$ whose corresponding eigenvalue is zero.  For this eigenvector,
\[
\frac{d}{dt} \mathbf{p} = \mathbf{K} \psi = 0.
\]
Thus, when the populations are $(p_A, p_B) = \psi$, the dynamics have reached a \textit{steady-state} condition; in other words: $\psi$ are the steady-state populations.

Under the stronger condition where \textit{detailed balance} is obeyed (i.e. $p_A k_f = p_B k_b$, as must be the case for all processes at \textit{equilibrium}), then $\psi = \mathbf{p}_{eq}$ must be the \textit{equilibrium} populations.

\paragraph{Exercise 1.} Use the equilibrium constant $K_{eq} = k_f/k_b$ along with the constraint $p_A + p_B = 1$ to solve for the equilibrium populations $\mathbf{p}_{eq}$.

\paragraph{Exercise 2.} Use the master equation to show that $\mathbf{p}_{eq}$ is the dynamical steady-state. Show that $\mathbf{p}_{eq}$ obeys detailed balance, and therefore are also the equilibrium populations.

\paragraph{Exercise 3.} Find the equilibrium populations $\mathbf{p}_{eq}$ a different way, this time by finding the eigenvalues and eigenvectors of $\mathbf{K}$ ($\mathbf{p}_{eq}$ will be the eigenvector whose corresponding eigenvector is zero).

\subsection*{A many-state master equation}

Now consider the case where we have $n$ states, whose populations change according to simple first-order kinetic rates.  We define $k_{ji}$ to be the rate at which species $i$ converts to species $j$.\footnote{NOTE: This nomenclature ($k_{ji}$ used for $k_{i \rightarrow j}$) can be confusing; the reason for this is so  the elements of the rate matrix $K_{ij}$ correspond to row $i$ and column $j$.   Be aware that other authors define the rate matrix using the opposite convention, and then define the master equation in terms of $d\mathbf{p}/dt = \mathbf{pK}$ where $\mathbf{p}$ is a \textit{row} vector.}

The differential equations describing the population flux are:

$$\frac{dp_1}{dt} = -(k_{21}+k_{31}+ ... +k_{n1})p_1 + k_{12} p_2 +k_{13} p_3 + ... +  k_{1n} p_n $$
$$\frac{dp_2}{dt} = k_{21} p_1 + -(k_{12}+k_{32}+ ... +k_{n2})p_2 + k_{23} p_3 + ... +  k_{2n} p_n $$
$$ ... $$
$$\frac{dp_n}{dt} = k_{n1} p_1 + k_{n2} p_2 + ...   -(k_{1n}+k_{2n}+ ... +k_{(n-1)n})p_n$$

This can be expressed succinctly as the master equation  $\frac{d}{dt} \mathbf{p} = \mathbf{K} \mathbf{p}$ where $\mathbf{K}$ is the rate matrix
$$\mathbf{K} =
 \begin{pmatrix}
  -\sum_{i \neq 1} k_{i1} & k_{12} & \cdots & k_{1n} \\
  k_{21} & -\sum_{i \neq 2}k_{i2} & \cdots & k_{2n} \\
  \vdots  & \vdots  & \ddots & \vdots  \\
  k_{n1} & k_{n2} & \cdots & -\sum_{i \neq n}k_{in}
 \end{pmatrix}
$$

\subsection*{Solving the chemical master equation}

Recall that that for a one-dimensional differential equation $dp/dt = \lambda p$, the solution is:
\[
p(t) = p_0 \exp(\lambda t)
\]
where $p_0$ is the initial value of $p$ at time $t=0$.   The strategy for solving the multi-state master equation will be to transform to a new coordinate system in which the rate matrix is diagonal.  In this new (``primed'') coordinate system, the master equation will read
\[
\frac{d}{dt} \mathbf{p}' =  \begin{pmatrix}
  \lambda_1 & 0 & \cdots & 0 \\
 0 & \lambda_2 & \cdots & 0 \\
  \vdots  & \vdots  & \ddots & \vdots  \\
  0 & 0 & \cdots & \lambda_n
 \end{pmatrix} \mathbf{p}'
\]
This equation describes $n$ independent first-order equations whose solutions are
$$p_i'(t) = p_i'(0) \exp (\lambda_i t)$$
where $i = 1, ... n$.

To perform this coordinate transformation, we must find the eigenbasis of $\mathbf{K}$.  Let $\mathbf{V}$ be the matrix whose columns are (right) eigenvectors of $\mathbf{K}$. Then
\[
\mathbf{K} \mathbf{V} = \mathbf{V} \mathbf{\Lambda}
\]
where $\mathbf{\Lambda}$ is the diagonal matrix containing the eigenvalues of $\mathbf{K}$.  Let $\mathbf{p}'(t)$ be the populations in the new coordinate system, such that
$$\mathbf{V}\mathbf{p}' = \mathbf{p}$$
so that the master equation reads:

$$\frac{d}{dt} \mathbf{V}\mathbf{p}' = \mathbf{K} \mathbf{V}\mathbf{p}'$$

Applying $\mathbf{V}^{-1}$ yields
$$\mathbf{V}^{-1} \frac{d}{dt} \mathbf{V}\mathbf{p}' = \mathbf{V}^{-1} \mathbf{K} \mathbf{V}\mathbf{p}'$$
$$\frac{d}{dt} \mathbf{p}'=  \mathbf{\Lambda} \mathbf{p}'$$

We now have $n$ independent first-order equations whose solutions are
$$p_i'(t) = p_i'(0) \exp (\lambda_i t)$$

Transforming back to the original coordinate system (see Appendix for how this works), we get:
$$\mathbf{p}(t) = \sum_{i=1}^n [\psi^L_i \cdot  \mathbf{p}(0)] \psi^R_i \exp (\lambda_i t)$$

where $\psi_i^R$ and $\psi_i^L$ are the right and left eigenvectors (respectively) of $\mathbf{K}$ .

\subsection*{The time-evolution of state populations is a superposition of relaxation eigenmodes}

In the previous section (and Appendix), we find that the complete solution is the sum
\[
\boxed{
\mathbf{p}(t) = \sum_{i=1}^n [\psi^L_i \cdot  \mathbf{p}(0)] \psi^R_i \exp (\lambda_i t)
}
\]
The interpretation of this solution is a \textbf{superposition of relaxation eigenmodes}. The right eigenvectors of $\mathbf{K}$ give the shape of each eigenmode; the projection of the initial populations onto each \textit{left} eigenvector, $[\psi^L_i \cdot  \mathbf{p}(0)]$, give the \textbf{amplitude} of each relaxation; and the eigenvalues $\lambda_i$ give the \textbf{rate} of each relaxation.

We have already seen in two-state example that one of the eigenvalues of $\mathbf{K}$ is $\lambda_1 = 0$.  It can be proven (not here) that the remaining eigenvalues of  $\mathbf{K}$ are all \textit{less} that zero, and can be ordered
\[
\lambda_1 > \lambda_2 > ... > \lambda_n.
\]
This means that in the limit of $t \rightarrow \infty$, all the relaxation eigenmodes decay to zero, except for the first eigenmode:
\[
\lim_{t \rightarrow \infty} \mathbf{p}(t) =  [\psi^L_1 \cdot  \mathbf{p}(0)] \psi^R_1
\]
The first eigenmode $\psi_1^R$ thus proportional to the equilibrium populations, $\mathbf{p}_{eq}$.  In fact, it is typical to define $\psi^R_1 = \mathbf{p}_{eq}$, in which case we also define $\psi^L_1 = (1,1,...,1)$ to maintain the orthogonality of $\psi^L_1$ and $\psi^R_1$.

What do the other eigenmodes look like?  Let's consider a specific three-state example:

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=0.6\textwidth]{three-state}
\caption{\label{fig:three-state}A simple three-state kinetic model}
\end{center}
\end{figure}

This model is represented by the following non-zero rates: $k_{21}$ = 100 s$^{-1}$, $k_{12}$ = 200 s$^{-1}$, $k_{32}$ = 10 s$^{-1}$, $k_{23}$ = 1 s$^{-1}$, and a resulting rate matrix of
\[
\mathbf{K} =
 \begin{pmatrix}
  -\sum_{i \neq 1} k_{i1} & k_{12} & k_{13} \\
  k_{21} & -\sum_{i \neq 2}k_{i2} & k_{23} \\
  k_{31} & k_{32} & -\sum_{i \neq 3}k_{3i}
 \end{pmatrix} =    \begin{pmatrix}
  -100 & 200 & 0 \\
 100 & -210 & 1 \\
  0 & 10 & -1
 \end{pmatrix}\]

Using a numerical eigensolver, we find that the eigenvalues and eigenvectors of this model are
\[
\lambda_1 = 0 \text{ (s}^{-1}),  \lambda_2 = -4.238 \text{ (s}^{-1}),  \lambda_3 = -306.8 \text{ (s}^{-1}),
\]
There is one \textit{stationary} eigenmode (the equilibirum state), and two \textit{relaxation} eigenmodes that relax on timescales of $\tau_2 = -1/\lambda_2$ = 0.236 s and $\tau_3 = -1/\lambda_3$ = 3.26 ms.

The eigenmodes can be characterized their shape, $\psi_i^R$ and their amplitude $[\psi^L_i \cdot  \mathbf{p}(0)]$.   The eigensolver gives the right eigenvectors as:

\[
\psi_1^R  = \begin{pmatrix} 0.19518  \\ 0.09759 \\ 0.97590 \end{pmatrix},
\psi_2^R  = \begin{pmatrix} -0.54104  \\ -0.25906 \\ 0.80010 \end{pmatrix},
\psi_3^R  = \begin{pmatrix} -0.69506  \\ 0.71856 \\ -0.02350 \end{pmatrix},
\]
But we have some choice in normalizing the left and right eigenvalues so that $(\psi^L_i)^T\psi_j^R = \delta_{ij}$; in particular it's useful normalize the elements of $\psi_1^R$ so it's equal to the equilibrium populations $\mathbf{p}_{eq}$ (making $\psi_1^L = (1,1,1)$. We also have some choice to flip the \textit{sign} of each pair of right and left eigenvalues, so that the amplitude $[\psi^L_i \cdot  \mathbf{p}(0)]$ will always be \textit{positive}.   That way, we can think of each eigenmode $\psi^R_i$ as having an initial positive amplitude, and decaying away over time.

Let's assume the initial populations at time $t=0$ is given by $\mathbf{p}(0) = (1,0,0)$, i.e. all the population is in state 1. In this case, we inspect the left eigenvectors given by the eigensolver, and flip the sign of both $\psi_i^L$ and $\psi_i^R$ if any of the amplitudes are negative.  We additionally scale the $\psi_i^L$ so that  $(\psi^L_i)^T\psi_j = \delta_{ij}$.

After performing these modifications, we get a new set of eigenmodes and amplitudes:

\[
\psi_1^R  = \begin{pmatrix} 0.1538  \\ 0.0769  \\ 0.7692  \end{pmatrix},
\psi_2^R  = \begin{pmatrix} 0.54104  \\ 0.25906 \\ -0.80010 \end{pmatrix},
\psi_3^R  = \begin{pmatrix} 0.69506  \\ -0.71856 \\ 0.02350 \end{pmatrix},
\]

\[
[\psi^L_1 \cdot  \mathbf{p}(0)] = 1,
[\psi^L_2 \cdot  \mathbf{p}(0)] = 0.9749,
[\psi^L_3 \cdot  \mathbf{p}(0)] = 0.4585,
\]

This information is visually represented in the plot below:

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=0.8\textwidth]{relaxation-eigenmodes}
\caption{\label{fig:relaxation-eigenmodes}Relaxation eigenmodes of the three-state kinetic model.}
\end{center}
\end{figure}

With this choice of normalization for the left and right eigenvalues, the eigenmodes are more physically meaningful.  The stationary eigenmode, $\psi_1^R  = (0.1538, 0.0769, 0.7692)$, gives the equilibrium populations.  At equilibrium, about 77\% of the population is in state 3.   For the non-stationary modes, the \textit{sign structure} of the eigenmode now shows the \textit{direction} that population is flowing over time, and over what timescale.  For example, from an inspection of the kinetic model in Figure \ref{fig:three-state}, one would predict that if all the population was in state 1 at $t=0$, , there would be a fast \textit{pre-equilibration} between states 1 and 2.  Indeed, the fastest relaxation eigenmode ($\tau_3 \sim 3.26$ ms) is dominated by components 1 and 2. The positive components (blue) show states for which population is leaving on this timescale, and the negative components show states where the population is entering.

\section*{Reconciling experimental and theoretical kinetic models}

\paragraph{Separation of timescales.} A feature evident from Figure \ref{fig:relaxation-eigenmodes} is the clear separation in timescales corresponding to each relaxation eigenmode.  An experiment with limited time-resolution may be able to detect the relaxation on timescale $\tau_2 \sim 0.236$ s, but not the fast relaxation at $\tau_3 \sim 3.26$ ms.   In that case, the experimentalist would conclude a two-state kinetic mechanism, when in reality the situation is more complicated.    This reasoning is often invoked when comparing protein folding experiments to molecular simulations of folding dynamics.  Whereas simulations may identify many microscopic intermediates, experiments may only detect two-state folding behavior.  Figure \ref{fig:ww-eigenmodes} shows the eigenmodes and relaxation time scales for a 200-state kinetic model of WW domain protein folding dynamics.

\begin{figure}[htbp]
\includegraphics[width=0.8\textwidth]{ww-eigenmodes}
\caption{\label{fig:ww-eigenmodes}A 200-state kinetic model of WW domain protein folding dynamics, constructed from 200 $\mu$s of trajectory data generated on the Anton supercomputer. In order to visualize the relaxation eigenmodes, states are projected to an RMSD-to-native reaction coordinate.  Folding kinetics experiments have shown that  WW domain a two-state folding mechanism, while the simulation-based kinetic model predicts additional fast relaxations ($\sim$ 500 ns) due to the solvent exposure/burial of a tryptophan residue. Figure taken from:  Lane, T. J., et al. (2011). \textit{Journal of the American Chemical Society}, 133(45), 18413--18419. \url{http://doi.org/10.1021/ja207470h} }
\end{figure}

\paragraph{Experimental detection of eigenmode relaxations.} Depending on the situation, there may be problems in experimentally measuring molecular kinetics that give rise the each eigenmode relaxation.  Even if the amplitude $[\psi^L_i \cdot  \mathbf{p}(0)]$ of eigenmode $\psi_i^R$ is significant, it may be that a given spectroscopic observable is very insensitive to population changes captured by $\psi_i^R$.  Consider a vector $\chi$ of experimental observables (e.g. solvent-exposure of a fluorophore) whose elements capture the value of the observable for each of the $n$ kinetic states.  In order to measure relaxation of the $i^{th}$ eigenmode, $\chi \cdot \psi_i^R$ must be sufficiently large to be detected.

Another case of experimentally ``invisible'' relaxation eigenmodes occurs when the amplitude $[\psi^L_i \cdot  \mathbf{p}(0)]$ of the relaxation is small.   The amplitude depends on the initial (out-of-equilibrium) populations, which can be very different to control exactly.   Protein folding kinetic experiments often use a temperature-jump to perturb the equilibrium populations, so that the relaxation back to equilibrium can be observed.  The initial $\mathbf{0}$ in this case is very similar to $\mathbf{p}_{eq}$, which may result in low amplitudes for some relaxations.  Figure \label{fig:beauchamp} shows a calculation of these amplitudes for eigenmodes of a number of small ``two-state'' proteins.  While many eigenmodes may be present, only a few of them have significant amplitude to be detected experimentally.

\begin{figure}[htbp]
\includegraphics[width=0.8\textwidth]{beauchamp}
\caption{\label{fig:beauchamp} Amplitudes of eigenmodes for a kinetic models of a number of small mini-proteins, constructed from  trajectory data generated on the Anton supercomputer by D. E. Shaw Research.  Figure taken from:  Beauchamp, K. A. et al. (2012). \textit{Proceedings of the National Academy of Sciences of the United States of America}, 109(44), 17807--17813. \url{http://doi.org/10.1073/pnas.1201810109}   }
\end{figure}

\newpage

\subsection*{Discrete-time Markov models}

A Markov model describes population changes in discrete time intervals, using a \textit{transition matrix} $\mathbf{T}^{(\tau)}$, whose elements $T_{ji}$ are probability of transitioning from state $i$ to state $j$ in some time $\tau$.  The time evolution of $\mathbf{p}(t)$ can thus be obtained by iterated multiplication:

$$\mathbf{p}(t+\tau) = \mathbf{T}^{(\tau)}\mathbf{p}(t)$$

From a practical standpoint, transition probabilities $T_{ji}$ are much more easily estimated from molecular simulations than rates. This is because with enough simulation trajectories that start from some state $i$, one can simply \textit{count} how many trajectories have reached state $j$ after some time $\tau$.

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=0.4\textwidth]{markov}
\caption{\label{fig:markov}An example of a discrete-time Markov model}
\end{center}
\end{figure}

\subsection*{What is the relationship between $\mathbf{T}^{(\tau)}$ and $\mathbf{K}$?}

Consider the time-evolution of populations according to the master equation $\frac{d}{dt}\mathbf{p}(t) = \mathbf{K} \mathbf{p}$. After some small time interval $\delta t << \tau$ has elapsed, the populations have changed by
$$\delta \mathbf{p} = \mathbf{K}\mathbf{p}(t) \delta t$$
so that
$$\mathbf{p}(t+\delta t) = \mathbf{p}(t) + \delta \mathbf{p} = (\mathbf{1} + \mathbf{K} \delta t)\mathbf{p}(t)$$

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=0.6\textwidth]{tau-chopped}
\caption{\label{fig:tau-chopped}}
\end{center}
\end{figure}

If the time window $\tau$ is divided up into $N$  small intervals (such that $N \delta t = \tau$), iteration of the above  yields

$$\mathbf{p}(t+N\delta t) = (\mathbf{1} + \mathbf{K} \delta t)^N\mathbf{p}(t)$$

Taking the limit where $N \rightarrow \infty$ and $\delta t \rightarrow 0$, we get

$$\mathbf{p}(t+\tau) = \lim_{N \rightarrow \infty} (\mathbf{1} + \mathbf{K} \frac{\tau}{N})^N\mathbf{p}(t) = \exp(\mathbf{K}\tau)\mathbf{p}(t)$$

Therefore,
$$\mathbf{T}^{(\tau)} = \exp(\mathbf{K}\tau)$$

\subsection*{$\mathbf{T}^{(\tau)}$ and $\mathbf{K}$ share eigenvectors }

If we write the matrix exponential as an Euler series, we can easily see that $\mathbf{T}^{(\tau)}$ and $\mathbf{K}$ share eigenvectors.

$$\mathbf{T}^{(\tau)}\psi_i^R = \exp(\mathbf{K}\tau) \psi_i^R$$
$$= [1 + \mathbf{K}\tau + \frac{1}{2!}\mathbf{K}^2\tau^2 + \frac{1}{3!}\mathbf{K}^3\tau^3 + ...] \psi_i^R$$
$$= [1 + \lambda_n\tau + \frac{1}{2!}\lambda_i^2\tau^2 + \frac{1}{3!}\lambda_i^3\tau^3 + ...]  \psi_i^R$$
$$ \mathbf{T}^{(\tau)} \psi_i^R = \exp(\lambda_i \tau) \psi_i^R$$
Apparently, the eigenvalues of $ \mathbf{T}^{(\tau)}$, $\mu_i$, are $\exp(\lambda_i \tau)$.  \textbf{This means we can get information about the timescales of the continuous-time dynamics (i.e. $\lambda_i$) directly through the discrete-time transition matrix}:
$$ \lambda_i = \log(\mu_i)/\tau$$

Useful quantities to define are the so-called \textbf{implied timescales} of each eigenmode relaxation, $\tau_i = -1/\lambda_i$.  We can compute this directly from the discrete-time transition matrix, using

\begin{center}
$\boxed{ \tau_i = \frac{\tau}{-\log(\mu_i)}  }$
\end{center}

\section*{Appendix: Left and Right Eigenvectors}

The eigenvectors $\psi$ referred to in linear algebra are usually \textit{right} eigenvectors:
\[
\mathbf{K} \psi^R = \lambda \psi^R
\]
The \textit{left} eigenvectors are defined by
\[
\psi_i^L \mathbf{K} = \lambda_i \psi_i^L
\]
In the above expression they multiply the matrix as row vectors.   The left and the right eigenvectors are related through the transpose of $\mathbf{K}$, as you can verify in the exercise below:

\paragraph{Exercise.}  Show that the left eigenvectors are the right eigenvectors of $\mathbf{K}^T$ and vice versa.  \textit{Hint:} Take the transpose of both sides of the equation $\mathbf{K} \psi^R = \lambda \psi^R$.  Recall that by the definition of matrix multiplication,  $(\mathbf{ABC})^T = \mathbf{C}^T\mathbf{B}^T\mathbf{A}^T.$

\vskip 0.6 cm

Now, consider $\mathbf{V}$ as the matrix whose columns are right eigenvectors $\psi^R_i$ of $\mathbf{K}$.  Since $\mathbf{V}^{-1}\mathbf{KV} = \mathbf{\Lambda}$ is a diagonal matrix, it must be equal to its own transpose:
\[
\mathbf{V}^{-1}\mathbf{KV} = (\mathbf{V}^{-1}\mathbf{KV})^T = \mathbf{V}^T\mathbf{K}^T (\mathbf{V}^{-1})^T
\]
The last term implies that $(\mathbf{V}^{-1})^T$  has the \textit{left} eigenvectors $\psi_i^L$ as its column vectors.  In other words, $\mathbf{V}^{-1}$ is a matrix whose \textit{rows} are the left eigenvectors $\psi_i^L$.

Since $\mathbf{V}^{-1} \mathbf{V} = \mathbf{1}$, this also means that left and right eigenvectors must obey $\psi_i^L \cdot \psi_j^R = \delta_{ij}$).  Each set of $\psi_i^L$ or $\psi_i^R$ are not in general orthonormal, but through normalization, the products of left and right eigenvector can always be made to equal 1 (when $i=j$) or 0 ($i \neq j$). In the case that the matrix $\mathbf{K}$ is symmetric (i.e. $\mathbf{K}^T = \mathbf{K}$), the left and right are equal to each other, and there is a single set of orthonormal eigenvectors.

\subsection*{Using left and right eigenvectors in the master equation solution}

Recall that in the eigenbasis coordinate system, the solution to the master equation is a column vector $\mathbf{p}'(t)$ whose elements are
\[
p_i'(t) = p_i'(0) \exp (\lambda_i t)
\]
Using $\mathbf{Vp}' = \mathbf{p}$, we can transform back to the original coordinate system:
\[
\mathbf{p}(t) = \mathbf{Vp}'(t) = \sum_i \psi_i^R p_i'(0) \exp (\lambda_i t)
\]
Next, using $\mathbf{p}' = \mathbf{V}^{-1}\mathbf{p}$, we can get $p_i'(0)$ in terms of the original coordinate system.   Since $\mathbf{V}^{-1}$ is a matrix whose rows are the left eigenvectors, $\psi^L_i$, the $i^{th}$ element of $\mathbf{V}^{-1}\mathbf{p}(0)$ is
\[
p_i'(0) = (\psi^L_i)^T\mathbf{p}(0) =  \psi^L_i \cdot \mathbf{p}(0)
\]
Thus, the complete solution of the master equation is
\[
\mathbf{p}(t) = \sum_i [ \psi^L_i \cdot \mathbf{p}(0)  ] \psi_i^R \exp (\lambda_i t)
\]

\end{document}


\section{Exercise 02}
\subsection{}

\begin{frame}
\frametitleTC{Problem}
\framesubtitleTC{This one we solve together}
\myPause
 Take the same system as in exercise 01, i.e.,
 \begin{displaymath}
  A = \begin{bmatrix} 0.4 & 0.4 \\ 0.05 & 0.3 \end{bmatrix}, \quad
  b = \begin{bmatrix} 1 \\ 0.5 \end{bmatrix}, \quad
  c = \begin{bmatrix} 2 & -1 \end{bmatrix}, \quad
  d = 0,
 \end{displaymath}
 and turn it into a block diagram.\\ \myPause
 \vspace{5mm}Important hint: since $z$ is the one-step advance operator, $z^{-1}$ is the one-step\\
 \emph{delay} operator, i.e.,
 \begin{displaymath}
  z^{-1} v(k) = v(k-1).
 \end{displaymath}
\end{frame}

\begin{frame}
\frametitleTC{Solution}
\framesubtitleTC{}
\myPause
 \only<2 >{One delay element per state variable:\\
           \begin{center}
            \includegraphics[width=0.85\columnwidth]{./Unit-03/img/PS01-ex02-fig01.pdf}
           \end{center}}
 \only<3 >{If the inputs are the $x_i(k+1)$, then the outputs are the $x_i(k)$:\\
           \begin{center}
            \includegraphics[width=0.85\columnwidth]{./Unit-03/img/PS01-ex02-fig02.pdf}
           \end{center}}
 \only<4 >{Now place the elements of \textcolor{magenta}{$A$} and \textcolor{blue}{$b$}:\\
           \begin{center}
            \includegraphics[width=0.85\columnwidth]{./Unit-03/img/PS01-ex02-fig03.pdf}
           \end{center}}
 \only<5 >{Read the state equation and wire how $x(k+1)$ depends on $x(k)$:\\
           \begin{center}
            \includegraphics[width=0.85\columnwidth]{./Unit-03/img/PS01-ex02-fig04.pdf}
           \end{center}}
 \only<6 >{Continue reading and wire how $x(k+1)$ depends on $u(k)$:\\
           \begin{center}
            \includegraphics[width=0.85\columnwidth]{./Unit-03/img/PS01-ex02-fig05.pdf}
           \end{center}}
 \only<7 >{Now place the elements of \textcolor{green!80!black}{$c$}:\\
           \begin{center}
            \includegraphics[width=0.85\columnwidth]{./Unit-03/img/PS01-ex02-fig06.pdf}
           \end{center}}
 \only<8->{Read the output equation and complete the block diagram:\\
           \begin{center}
            \includegraphics[width=0.85\columnwidth]{./Unit-03/img/PS01-ex02-fig07.pdf}
           \end{center}}
\end{frame}

\begin{frame}
\frametitleTC{Proposed exercise 02}
\framesubtitleTC{Try this at home, ask questions next time if needed}
\myPause
 Take the same system as in the previous proposed exercise, i.e.,
 \begin{displaymath}
  A = \begin{bmatrix} 0.5 & 0 \\ 2 & -0.3 \end{bmatrix}, \quad
  b = \begin{bmatrix} 1 \\ 0 \end{bmatrix}, \quad
  c = \begin{bmatrix} 0 & 4 \end{bmatrix}, \quad
  d = 1,
 \end{displaymath}
 and turn it into a block diagram.\\ \myPause
 \vspace{5mm}Remark: in this case you will see a \TC{direct feedthrough} from $u$ to $y$,\\
 because $d \neq 0$.
\end{frame}

\begin{frame}
\frametitleTC{Takeaways}
\framesubtitleTC{from exercise 02 (and the proposed one)}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Generalise the situation with compact matrix notation (thick lines are vectors):
       \begin{center}
        \includegraphics[width=0.65\columnwidth]{./Unit-03/img/PS01-ex02-takeaway-FBscheme.pdf}
       \end{center}
 \item Do you see the \textcolor{blue!80!black}{feedback loop} inherently inside the system?
 \item Feedback is not ``an invention for control''; it is an integral part\\
       of how nature works!
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Takeaways}
\framesubtitleTC{from exercise 02 (and the proposed one)}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Get acquainted with the $(k,k-1)$ and the $(k+1,k)$ ways of writing a DT system,\\
       and always pay attention to which symbol is what.
 \item In particular, the $u$ in the state equation is NOT at the same instant as that\\
       in the output equation, no matter how the system is written.
 \item \vspace{4mm}As feedback is a powerful weapon, ``slight'' approximations as taking\\
       the previous input of a block and not the current, or \emph{vice versa},\\
       can in fact be devastating...
 \item ...and without a system-theoretical analysis, this is impossible\\
       to figure out before the system is run --- i.e., possibly, broken.
 \end{itemize}
\end{frame}


\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{graphicx}

\setlength{\parindent}{0em}
\setlength{\parskip}{0.5em}

\title{CPM Vehicle Parameter Identification and Model Predictive Control}
\author{Janis Maczijewski}
\date{\today}

\begin{document}
\maketitle

\section{Vehicle Dynamics Model}

This is an end-to-end, grey-box model for the vehicle dynamics. The model parameters are not measured directly, but optimized to best fit the vehicle behavior.

\begin{align*}
\boldsymbol{x} &= [p_x, p_y, \psi, v] \\
\boldsymbol{u} &= [f, \delta, V] \\
\end{align*}

\begin{center}
\begin{tabular}{ r | l }
 $p_x$ & IPS x-position  \\
 $p_y$ & IPS y-position   \\
 $\psi$ & IPS yaw angle  \\
 $v$ & Odometer Speed  \\
 $f$ & Dimensionless motor command  \\
 $\delta$ & Dimensionless steering command  \\
 $V$ & Battery voltage  \\
\end{tabular}
\end{center}

\begin{align}
\dot{p}_x &= p_1 \cdot v \cdot (1+p_2 \cdot (\delta + p_{9})^2) \cdot \cos(\psi + p_3 \cdot (\delta + p_{9}) + p_{10}) \\
\dot{p}_y &= p_1 \cdot v \cdot (1+p_2 \cdot (\delta + p_{9})^2) \cdot \sin(\psi + p_3 \cdot (\delta + p_{9}) + p_{10}) \\
\dot{\psi} &= p_4 \cdot v \cdot (\delta + p_{9}) \\
\dot{v} &= p_5 \cdot v + (p_6 + p_7 \cdot V) \cdot \text{sign}(f) \cdot |f|^{p_8}
\end{align}

This is a kinematic bicycle model with some added terms to account for various errors.

\begin{itemize}
\item $p_1$: Compensate calibration error between IPS speed and odometer speed.
\item $(1+p_2 \cdot (\delta + p_{9})^2)$: Compensate for speed differences due to different reference points between the IPS and odometer. The formulation is simplified with a second-order Taylor approximation.
\item $p_3$: Side slip angle (Schwimmwinkel) due to steering.
\item $p_{10}$: IPS Yaw calibration error.
\item $p_{4}$: Unit conversion for the steering state.
\item $p_{5}$: Speed low pass (PT1).
\item $p_{6}, p_{7}$: Motor strength depends on the battery voltage.
\item $p_{8}$: Compensate non-linear steady-state speed.
\item $p_{9}$: Steering misalignment correction.
\end{itemize}

\section{Model Discretization}

The model is discretized with the explicit Euler method, as follows:

\begin{align}
\boldsymbol{x}_{k+1} = \boldsymbol{x}_k + \Delta t \cdot f(\boldsymbol{x}_k,  \boldsymbol{u}_k, \boldsymbol{p})
\end{align}

This discretization is chosen for its simplicity and computational efficiency.

TODO justification inaccuracy: small timestep, and discretization is included in identification.

\section{Parameter Identification}

Optimal parameter estimation problem for the vehicle dynamics. The optimization tries to find a set of model parameters, that best explain/reproduce the experiment data.

\begin{align}
\underset{\boldsymbol{x}_k^j, \boldsymbol{p}}{\text{minimize}} && \sum_{j=1}^{n_{experiments}} \sum_{k=1}^{n_{timesteps}} E(\boldsymbol{x}_k^j - \hat{\boldsymbol{x}}_k^j) \\
\text{subject to} &&  \boldsymbol{x}_{k+1}^j = \boldsymbol{x}_k^j + \Delta t \cdot f(\boldsymbol{x}_k^j,  \hat{\boldsymbol{u}}_k^j, \boldsymbol{p}) \\
&& \quad k=1..(n_{timesteps}-1) \\
&& \quad j=1..n_{experiments} \\
\end{align}

\begin{center}
\begin{tabular}{ r | l }
 $\hat{\boldsymbol{x}}_k^j$ & Measured States  \\
 $\hat{\boldsymbol{u}}_k^j$ & Measured Inputs   \\
 $f$ & Vehicle dynamics model  \\
 $\boldsymbol{p}$ & Model parameters  \\
 $\Delta t$ & Constant timestep $0.02s$ \\
 $E$ & Error penalty function \\
\end{tabular}
\end{center}

\textbf{Error penalty $E$}: Weighted quadratic error with model specific extensions. The yaw error function has a period of $2\pi$, so that a full rotation does not count as an error. This is done using $\sin(\Delta\psi/2)^2$.

\textbf{Delays}: This kind of optimization problem is not well suited for identifying the delay times (Totzeiten). The delays are solved in an outer loop. The delay is guessed/assumed and the measurement data is modified by appropriately shifting it in the time index $k$. This optimization problem is solved many times for combinations of delay times. The delays that create the lowest objective value are taken as the solution.

\section{Model Predictive Control}

The MPC uses the identified model to calculate control inputs in real time. The MPC must run in an environment with narrow computational constraints. It must run on a Raspberry Pi Zero W and in less than 10ms per time step. This results in a computational budget of roughly 100000 to 500000 double precision floating point operations per time step.

\subsection{Optimization Problem and Optimizer}

The MPC is formulated as a (mostly) unconstrained minimization problem, with a box constraint:

\begin{align}
\underset{z \in \mathbb{R}^n}{\text{minimize}} \quad & J(z) \\
\text{subject to} \quad & -1 \leq z_i  \leq 1
\end{align}

The optimization problem is solved using a simple and lightweight method, the gradient descent method with momentum.
The method works by iterating the following two equations:

\begin{align*}
m^{(j)} &:= \beta  m^{(j-1)} - \nabla J(z^{(j-1)}) \\
z^{(j)} &:= \text{clip}(z^{(j-1)} + \alpha  m^{(j)})
\end{align*}

where
$z^{(j)}$ is the approximate solution, which is improved in each iteration,
$m^{(j)}$ is the momentum term,
$\alpha = 0.4$ and $\beta = 0.6$ are constants,
$\text{clip}(\cdot)$ applies the following function element-wise $\min(1, \max(-1, x_i))$ and
$\nabla J(z^{(j-1)})$ is the gradient of the objective.

A constant number of iterations $j = 1 ... N$ are executed.
This guarantees a constant computation time, but not the convergence to the solution.
The approximate solution $z^{(N)}$ is however sufficient in practice.

\subsection{Trajectory Tracking Problem}

The goal of the MPC is to make the vehicle follow a given reference trajectory.
The reference trajectory is given as a Cubic Hermite spline function $r(t) = [p_{x,ref}, p_{y,ref}]$.
This function is evaluated on an appropriate time grid $t_k$ to give the discrete reference trajectory $[p_{x,ref,k}, p_{y,ref,k}]$ where $k = 1 ... H_p$.
The MPC objective is defined as follows:

\begin{align*}
J &= \sum_{k=1}^{H_p} \left[ (p_{x,k} - p_{x,ref,k})^2 + (p_{y,k} - p_{y,ref,k})^2 \right] \\
  &+ 0.5 \sum_{k=1}^{H_u}  (f_k - f_{k-1})^2 \\
  &+ 0.01 \sum_{m=1}^{H_u}  (\delta_{m} - \delta_{m-1})^2
\end{align*}

The predicted states are calculated explicitly and recursively. This is known as a single shooting method.

\begin{align*}
\begin{bmatrix}
p_{x,k+1}  \\
p_{y,k+1}  \\
\psi_{k+1}  \\
v_{k+1}
\end{bmatrix}
=
\begin{bmatrix}
p_{x,k}  \\
p_{y,k}  \\
\psi_{k}  \\
v_{k}
\end{bmatrix}
+
\Delta t_{MPC} \cdot f\left(
\begin{bmatrix}
p_{x,k}  \\
p_{y,k}  \\
\psi_{k}  \\
v_{k}
\end{bmatrix}
,
\begin{bmatrix}
f_{\boldsymbol{m}_k}  \\
\delta_{\boldsymbol{m}_k}  \\
V_{\boldsymbol{m}_k}
\end{bmatrix}
, \boldsymbol{p}\right), \quad k = 0 ... (H_p-1)
\end{align*}

where $f$ is the identified model from section (ref).

$\boldsymbol{m}$ is an index vector that allows reuse of input vectors. Current implementation $\boldsymbol{m} = [1, 1, 2, 2, 3, 3]$.

$H_p = 6$, $H_u = 3$

The MPC uses a longer discretization time step $\Delta t_{MPC} = 0.05s$.

The inputs for the trajectory tracking problem are:

\begin{itemize}
\item $[p_{x,0}, p_{y,0}, \psi_{0}, v_{0}]$: The measured state with delay compensation
\item $[f_{0}, \delta_{0}]$: The previous command
\item $[p_{x,ref,k}, p_{y,ref,k}]$: The discrete reference trajectory
\item $V_m$: The measured battery voltage
\item $\boldsymbol{p}$: The model parameters
\end{itemize}

The variables for the trajectory tracking problem are: $z = [f_1, ..., f_{H_u}, \delta_1, ..., \delta_{H_u}]$.

\subsection{Delay Compensation}

The processes of measuring the state, running the MPC controller and applying the new command introduces a delay in the control loop. This delay is compensated by performing a short simulation after measuring the state and before running the MPC. Thus, the MPC will optimize the inputs for a future state, which matches the time by which the new commands take effect.
This simulation requires some command inputs. These are the MPC commands from the previous timesteps.
The simulation runs for 3 samples, or 60ms.

\subsection{CasADi and Code Generation}

The trajectory tracking problem and the optimizer are implemented symbolically with Matlab and CasADi.
CasADi's code generator for C is used to obtain an efficient implementation for the Raspberry Pi.

\subsection{Finetuning}

The controller implementation collects statistical data about the trajectory tracking errors in real time.

TODO play with all the MPC hyperparameters to minimize the tracking error.

\end{document}


\subsection{ Preparation of \py{Dex_Mw} kDa Dextran-FITC stock solution}

\begin{enumerate}
\item The final concentration should be \py{final_concentration_DexFITC} \py{final_concentration_units_DexFITC}
\item We have weighted \py{masse_weighted_DexFITC} \py{masse_weighted_DexFITC_units}
\item We add \py{final_volume_DexFITC} \py{final_volume_DexFITC_units} of medium
\item Wrap the 15 mL falcon tube with aluminum foil
\end{enumerate}

\subsection{Working solution preparation}

\begin{pycode}

Final_volume_ = int(nb_puits * volume_per_well)
Final_volume_lost = int(Final_volume_ * 1.1)
Vi_ = int(working_concentration * Final_volume_lost / final_concentration_DexFITC)
delta_Volume = int(Final_volume_lost - Vi_)
\end{pycode}

We have \py{nb_puits} wells to test, in each we will add \py{volume_per_well} $\mu L$ per well. We need \py{Final_volume_} $\mu L$. We will prepare \py{Final_volume_lost} $\mu L$ of solution at \py{working_concentration} \py{working_concentration_units} of \py{Dex_Mw} kDa Dextran-FITC in medium.

\begin{enumerate}
\item We take \py{delta_Volume} $\mu L$ of medium
\item Then we add \py{Vi_} $\mu L$ of the \py{Dex_Mw} kDa Dextran-FITC at \py{final_concentration_DexFITC} \py{final_concentration_units_DexFITC}
\item Protect the falcon from light abd keep it in fridge
\end{enumerate}


\section{\textbf{ArraySumTest}}

\subsection{Particular Case (problem)}
The problem we want to solve is that of summing up all the elements of
an array; but of course, we want to do it in parallel.

\subsection{Solution}
The solution uses the divide and conquer approach, creating a tree of
tasks (\C{Future} from java concurrency library); which represent
the processing we do while splitting the arrays in two halves and summing up
each half recursively. Base case are arrays of a single element. Below
the heart of the solution, the recursive function which create new tasks
for subarrays: \\

\begin{lstlisting}[style=numbers]
    public Integer call() throws InterruptedException, ExecutionException {
      if (size == 1) {
        return a[start];
      } else {
        int lhsSize = size / 2;
        int rhsStart = lhsSize + 1;
        int rhsSize = size - lhsSize;
        Future<Integer> lhs = exec.submit(new SumTask(a, start, lhsSize));
        Future<Integer> rhs = exec.submit(new SumTask(a, rhsStart, rhsSize));
        return rhs.get() + lhs.get();
      }
    }
\end{lstlisting}
\hfill

\subsection{Experiment Description}
The test merely consists in trying the algorithm with a couple of
arrays of consecutive positive integers, of respective sizes 3 and
4. The test validates that the expected sum is 6 and 10 respectively.

\subsection{Observations and Interpretations}
The test presents failures like the one below, where the array of 3
elements gave as sum 7 instead of 6: \\

\begin{verbatim}
[oraadm@gdlaa008 orig]$ junit steal.ArraySumTest
.F
Time: 0.009
There was 1 failure:
1) testRun(steal.ArraySumTest)junit.framework.AssertionFailedError:
                expected:<6> but was:<7>
at steal.ArraySumTest.testRun(ArraySumTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

FAILURES!!!
Tests run: 1,  Failures: 1,  Errors: 0
\end{verbatim}
\hfill

The problem lied on the program \C{ArraySum.java}, on the assignment
of variable \C{rhsStart}, which represents the starting point of the
right subarray. While the original program had $lhsSize + 1$
as assignment, the proper expression was $lhsSize + start$ (so we take
into account both the relative start of the initial subarray we got,
as well as the length of the left sub-subarray). After this fix, the test
worked fine on both 2 and 24 cores machines.


\section{Correctness proof of Algorithms~\ref{alg:findisointersection}, \ref{alg:getautomorphisms} and \ref{alg:getsingleisomorphism}}

\todo[inline]{rewrite \& check if all references to algorithm contents are still correct}

Here, we prove that \findisomorphismsetintersection~is correct, for which we need the following remark and lemma.

\begin{remark}
    \label{remark:correctness-isomorphism-intersection}
    If $I$ is an \isomorphismset, then denote by $\pi_I$ the single isomorphism and by $G_I$ the \automorphismgenerators~object it contains.
    Furthermore, denote by $\langle G\rangle$ the automorphism group generated by $G$.
    Upon input $I_A, I_B$ and output $I$, the correctness criterion of the function \findisomorphismsetintersection~is equivalent to
    \[
        \pi_{I} \cdot \langle G \rangle = \left( \pi_{I_A} \cdot \langle G_{I_A} \rangle \right) \cap \left( \pi_{I_B} \cdot \langle G_{I_B} \rangle \right)
    \]
    where we have denoted $\pi \cdot G = \{\pi \cdot g \mid g \in G\}$.
\end{remark}

\begin{lemma}
    \label{thm:coset-intersection-nonempty}
    Let $G$ be a group with subgroups $G_A$ and $G_B$, and let $\pi_A, \pi_B \in G$.
    The intersection $(\pi_A \cdot G_A)\cap (\pi_B \cdot G_B)$ is nonempty if and only if $\pi_A^{-1} \pi_B$ can be written as $\pi_A^{-1}\pi_B=g_A \cdot g_B$, for some $g_A \in G_A$ and $g_B \in G_B$.
\end{lemma}
\begin{proof}
    \todo[inline]{fill (with proof that Lieuwe wrote before)}
\end{proof}

Now we prove that the algorithm \findisomorphismsetintersection~is correct.

\begin{lemma}
    The function \findisomorphismsetintersection~satisfies its correctness criterion given in
    \cref{remark:correctness-isomorphism-intersection}.
\end{lemma}
\begin{proof}
    First, we start by using the well-known fact that the intersection of two cosets $\pi_{I_A} \cdot \langle G_{I_A} \rangle$ and $\pi_{I_B} \cdot \langle G_{I_B} \rangle$ is either empty, or it is a coset of $\langle G_{I_A} \rangle \cap \langle G_{I_B} \rangle$.
    Hence, the algorithm is correct if
    \begin{enumerate}[(a)]
        \item the \automorphismgenerators~object it outputs is $\langle G_{I_A} \rangle \cap \langle G_{I_B} \rangle$;
        \item the single isormorphism it outputs is an element of $\left( \pi_{I_A} \cdot \langle G_{I_A} \rangle \right) \cap \left( \pi_{I_B} \cdot \langle G_{I_B} \rangle \right)$, or \none~if this intersection is empty.
    \end{enumerate}

    We first prove (a).
    The central insight is that the removal of elements containing $X$ or $Y$ from $H_B^{\textnormal{echelon}}$ in step 7 does not alter the intersection we aim for. That is:
    \begin{equation}
        \label{eq:noXY}
    \langle H_A \rangle \cap \langle H_B^{\textnormal{echelon}} \rangle =
    \langle H_A \rangle \cap \langle H^{\textnormal{noXY}}_B \rangle
        .
    \end{equation}
    The reason why this holds is that elements of $H_A$ do not contain $X$ or $Y$, while for each $k \in \{1, 2, \dots n\}$, there is at most a single operator in $H_B^{\textnormal{echelon}}$ which contains a pivot at position $k$ which is either $X$ or $Y$; therefore, no element in $\langle H_A\rangle \cap \langle H_B^{\textnormal{echelon}}\rangle$ will have a decomposition in $H_B^{\textnormal{echelon}}$ containing operators containing $X$ or $Y$.
    \todo[inline]{argument is simpler than I manage to put into words; add picture to clarify the argument?}

    Let us now prove the (a).
    We use the notation $U^{\dagger} A U = \{U^{\dagger} a U | a \in A\}$ where $A$ is an arbitrary set.
    Following the notation from the algorithm,
    \begin{eqnarray*}
        \langle G\rangle
        &=&
        U^{\dagger} \left(
        \langle H_A \rangle \cap \langle H_B^{\textnormal{noXY}}\rangle
        \right) U
        \\
        &\stackrel{\textnormal{eq.~\eqref{eq:noXY}}}{=}&
        U^{\dagger} \left(
        \langle H_A \rangle \cap \langle H_B^{\textnormal{echelon}}\rangle
        \right) U
        \\
        &=&
        U^{\dagger} \left(
        \langle H_A \rangle \cap \langle H_B\rangle
        \right) U
        \\
        &=&
        U^{\dagger} \left(
        \langle U G_A U^{\dagger} \rangle \cap \langle U G_B U^{\dagger}\rangle
        \right) U
        \\
        &\stackrel{*}{=}&
        U^{\dagger} U \left(
        \langle G_A \rangle \cap \langle G_B\rangle
        \right) U^{\dagger} U
        \\
        &=&
        \langle G_A \rangle \cap \langle G_B\rangle
    \end{eqnarray*}
    where $\stackrel{*}{=}$ holds because the map $x \mapsto Ux U^{\dagger}$ is a group isomorphism.

    We prove (b) by showing the following two statements: (b1) if the single isomorphism that is outputted by the isomorphism is not \none, then it is an element of the intersection of $\pi_{I_A} \cdot \langle G_{I_A} \rangle$ and $\pi_{I_B} \cdot \langle G_{I_B} \rangle$, and (b2) if the outputted isomorphism is $\none$, then this intersection is empty.

    Proving (b1) is straightforward: from step 8 of the algorithm we see that
    \begin{equation}
        \label{eq:tau-prime-1}
   \tau' = \prod_{j} h_j^A \cdot \prod_{k} h_k^B
    \end{equation}
    where $h_j^A \in H_A, h_k^B\in H_{B}^{\textnormal{noXY}}$
    while from steps 1--5 we observe that $\tau'$ can also be written as
    \begin{equation}
        \label{eq:tau-prime-2}
        \tau' = U\pi_A^{-1} U^{\dagger} U \pi_B U^{\dagger} \cdot \prod_{\ell} h_{\ell}^{\textnormal{echelon}}
    \end{equation}
 for $h_{\ell}^{\textnormal{echelon}} \in \langle H_B^{\textnormal{echelon}} \rangle = \langle H_B \rangle$.
    Combining eqs.~\eqref{eq:tau-prime-1} and~\eqref{eq:tau-prime-2} and reshuffling yields
    \begin{equation}
        \label{eq:tau-prime-3}
        U \pi_B U^{\dagger} \cdot \prod_{\ell} h_{\ell}^{\textnormal{echelon}}  \cdot \prod_{k} \left(h_k^B\right)^{-1}
        =
        U\pi_A U^{\dagger} \cdot \prod_{j} h_j^A
    \end{equation}
    Applying the group isomorphism $x \mapsto U^{\dagger} x U$ to both sides of eq.~\eqref{eq:tau-prime-3}, we find
    \[
        \pi_B \cdot \prod_{\ell} U^{\dagger} h_{\ell}^{\textnormal{echelon}} U  \cdot \prod_{k} U^{\dagger} \left(h_k^B\right)^{-1} U
        =
        \pi_A \cdot \prod_{j} U^{\dagger} h_j^A U
    \]
    Since $H_A = \{UgU^{\dagger} | g \in G_A\}$ and similary for $H_B$ we infer that
    \[
        \pi_A \cdot \prod_{j} U^{\dagger} h_j^A U \in
    \]
    is an element of
    both
    $\pi_{I_A} \cdot \langle G_{I_A} \rangle$
    and
    $\pi_{I_B} \cdot \langle G_{I_B} \rangle$
    , which proves (b1).

    For proving (b2), we note that the algorithm outputs \none~precisely if $\tau'$ cannot be written as $h_A \cdot h_B$ where $h_{\Box} \in \langle H_{\Box} \rangle$.
    To see this, consider the two steps in the algorithm where \none~is returned:
    \begin{itemize}
        \item in step 6; $\tau'$ contains at least one $X$ or $Y$ which has not been eliminated in step 5, while $H_A$ only contains $Z$ and $\unit_2$, so $\tau'\notin \langle H_A \rangle \cdot \langle H_B \rangle$.\todo[inline]{maybe needs more rigorous proof}
        \item in step 8, where the Zassenheim algorithm finds a decomposition of $\tau'$ of the form $h_A \cdot h_B$ only if it exists.
    \end{itemize}
It follows from the fact that $\tau'$ is not an element of $\langle H_A \rangle \cdot \langle H_B \rangle$ that $\tau$ is neither, and using the fact that $x\mapsto UxU^{\dagger}$ is a group isomorphism, we find by definition of $\tau$ that $\pi_A^{-1} \pi_B \notin \langle G_A \rangle \cdot \langle G_B\rangle$.
    It now follows from Lemma~\ref{thm:coset-intersection-nonempty} that the intersection between $\pi_A\cdot \langle G_A \rangle$ and $\pi_B\cdot \langle G_B \rangle$ is empty, which concludes the proof for (b2).
\end{proof}

\begin{lemma}[Conditional correctness of function \getautomorphisms]
    \label{lemma:condition-correctness-1}
    The function \getautomorphisms~satisfies its correctness criterions for single-qubit states.
    Moreover, it also does so for $n$-qubit states for $n>1$, conditioned on the correctness of the function \getsingleisomorphism~for quantum states on strictly fewer than $n$ qubits.
\end{lemma}
\begin{proof}
    In case two states are given as input, then the correctness of the algorithm is an immediate consequence of the correctness of the single-state-input case and the correctness of the algorithm \findautomorphismsetintersection.
    So we only need to prove the case where a single state is given as input.
    We use induction on the number of qubits $n$.
    The case $n=1$ is a brute-force search over the (six) single-qubit stabilizer states and is thus correct.
    For the case $n>1$, we show the following two statements:

    (i) each operator that is outputted by \getautomorphisms~is an automorphism of $\ket{\phi}$;

    (ii) each automorphism of $\ket{\phi}$ can be written as product of operators outputted by \getautomorphisms.

    Showing (i) is straightforward, assuming that \getsingleisomorphism~and \getautomorphisms~are both correct for $(n-1)$-qubit states.
    For example, for the operators of the form $\unit_2\otimes g$ with $g$ an automorphism to both $\ket{\phi_0}$ and $\ket{\phi_1}$, we write
    \begin{eqnarray*}
        \unit_2 \otimes g \ket{\phi}
        =
        \alpha_0 \ket{0} \otimes g\ket{\phi_0} + \alpha_1 \ket{1} \otimes g\ket{\phi_1}
        =
        \alpha_0 \ket{0} \otimes \ket{\phi_0} + \alpha_1 \ket{1} \otimes \ket{\phi_1}
        = \ket{\phi}
        .
    \end{eqnarray*}
    The other three cases are similar.

    Regarding (ii), we first note that if $\unit_2 \otimes g$ is an automorphism of $\ket{\phi}$, then $
    \alpha_0 \ket{\phi_0} =
    \left(\bra{0}\otimes \unit_{2^{n-1}}\right) \left(\unit_2 \otimes g \ket{\phi}\right) =
    g (\alpha_0 \ket{\phi_0})
    $
    and hence $g$ must be an automorphism to $\ket{\phi_0}$, while following a similar argument $g$ is also an automorphism to $\ket{\phi_1}$.
    This shows that all automorphisms of the form $\unit_2 \otimes g$ are in the output of \getautomorphisms.
    Regarding automorphisms of the form $X\otimes a$ with $a$ an $(n-1)$ qubit Pauli operator, it is not hard to see that $a$ is an isomorphism mapping $\alpha_0 \ket{\phi_0}$ to $\alpha_1 \ket{\phi_1}$ and vice versa.
    Now note that $X\otimes a = (X\otimes s)(\unit_2 \otimes s a)$ where $s$ is as in the algorithm.
    Since $s$ maps $\alpha_0 \ket{\phi_0}$ to $\alpha_1 \ket{\phi_1}$, we see that $sa$ is an automorphism to both $\ket{\phi_0}$ and $\ket{\phi_1}$, so that $X\otimes a$ can be written as product of elements outputted by \getautomorphisms.
    The argument for the remaining two cases, i.e. automorphisms of the form $Z\otimes a$ and $Y\otimes a$, are similar.
    This finishes the proof for (ii).
\end{proof}

\begin{lemma}[Conditional correctness of function \getsingleisomorphism]
    \label{lemma:condition-correctness-2}
    The function \getsingleisomorphism~satisfies its correctness criterions for $0$-qubit states.
    Moreover, it also does so for $n$-qubit states for $n>0$, conditioned on the correctness of the function \getautomorphisms~for quantum states on strictly fewer than $n$ qubits.
    \todo[inline]{Readers might $0$-qubit states find confusing...Reason we chose it is because starting the recursion at 0 qubits gives a concise formulation than at 1 qubit}
\end{lemma}
\begin{proof}
    If the input are $0$-qubit states, then correctness is immediate, both for single- and two-tuple input.
    For the case of $n$-qubit input states with $n>0$, correctness of the two-tuple case follows immediately from the correctness of the functions \getautomorphisms~(on $n$ qubits) and \findisomorphismsetintersection.
    The case $n>0$ for the single-tuple input case is also correct since the algorithm is a brute-force search over the four single-qubit Pauli operators on the most-significant qubit.
\end{proof}

By induction on the number of qubits, the correctness of the algorithm \getsingleisomorphism~now follows straighforwardly from Lemma~\ref{lemma:condition-correctness-1} and \ref{lemma:condition-correctness-2}.

\begin{corollary}[Unconditional correctness of function \getsingleisomorphism]
    The function \getsingleisomorphism~satisfies its correctness criterions.
\end{corollary}

\section{Correctness proof of \textsc{GetCanonicalLabels}}
\label{sec:proof-getcanonicallabels}

\todo[inline]{TODO}

\begin{lemma}
    \label{lemma:automorphism-phase}
    If $\lambda P$ is an element of a Pauli automorphism group, then $\lambda \in \{\pm 1\}$.
\end{lemma}
\begin{proof}
    Let $\lambda P$ be an element of the automorphism group of a state $\ket{\phi}$.
    Then $\lambda^2 \ket{\phi} = (\lambda P)^2 \ket{\phi} = \lambda P (\lambda P \ket{\phi}) = \lambda P \ket{\phi} = \ket{\phi}$, so $\lambda^2 = 1$, so $\lambda \in \{\pm 1\}$.
\end{proof}

\begin{lemma}
    Let $G_0, G_1$ be Pauli automorphism groups and $G = \langle G_0 \cup G_1 \rangle$.
    If $g\in G$, then $\pm i g \notin G$.
\end{lemma}
\begin{proof}
    To reach a contradiction, assume there exists a $g\in G$ for which $\pm i g \in G$ also.
    Since Pauli LIMs commute or anticommute, we can decompose both as $g = (-1)^x g_0 g_1$ and $\pm i g = (-1)^y h_0 h_1$ for some $x, y \in \{0, 1\}$ and $g_0, h_0\in G_0$ and $g_1, h_1 \in G_1$.
    Combining yields $\pm i (-1)^x g_0 g_1 = (-1)^y h_0 h_1$, which we rewrite as $\pm i (-1)^{x+y} \underbrace{g_1 h_1^{-1}}_{\in G_1} = \underbrace{g_0^{-1} h_0}_{\in G_0}$.
    Squaring both sides yields the contradiction $-1 \cdot \id[2] = \id[2]$ where we used that automorphisms square to $\id[2]$ (corollary of \autoref{lemma:automorphism-phase}).
\end{proof}

\begin{corollary}
    Let $G_0, G_1$ be Pauli automorphism groups and $G = \langle G_0 \cup G_1 \rangle$.
    If $\lambda P \in G$ and $\mu P \in G$, then $\lambda = \pm \mu$.
\end{corollary}


\paragraph{}
This section details the proposed error indicators for adaptive scaled boundary finite element analysis.
In comparison with the works in \citep{NME:NME439}, no stress recovery is required.
This reduces the computational effort and leads to an efficient adaptive analysis.
The error estimation is directly invoked from the scaled boundary finite element solutions.

%   ----    %
\subsection{Mesh Size}
\paragraph{}
The area of a subdomain will apparently influences the accuracy of the result.
Generally speaking, a larger subdomain tends to lead to a higher error.
The area of any polygon in Fig.~\ref{adap_fig:ei_polygon} can be calculated as
%
\begin{equation}
    S = \frac{1}{2}
        \sum_{k=1}
        \left(
            x_k y_{k+1} - x_{k+1} y_k
        \right)
\end{equation}
%
\begin{figure}[h!]
    \centering
    \scalebox{1}{
        \includegraphics{adaptivity/images/adap_ei_polygon.eps}
    }
    \caption{A polygon with $n$ vertexes}
    \label{adap_fig:ei_polygon}
\end{figure}
%
%   ----    %
\subsection{Mesh Quality}
\paragraph{}
Mesh quality is another important factor that influences the accuracy.
In SBFEM, the mesh quality is highly related to the minimal angle formed by the intersecting lines connected to the scaling center and the adjacent polygon vertexes.
An extremely smaller angel as shown in Fig.~\ref{adap_fig:ei_mesh_quality} may raises numerical stability issue and hence decreases the accuracy of the result.

\begin{figure}[h!]
    \begin{subfigure}[b]{0.5\linewidth}
        \centering
        \scalebox{1}{
            \includegraphics{adaptivity/images/adap_ei_mesh_quality_good.eps}
        }
        \caption{Acceptable}
    \end{subfigure}
    \begin{subfigure}[b]{0.5\linewidth}
        \centering
        \scalebox{1}{
            \includegraphics{adaptivity/images/adap_ei_mesh_quality_poor.eps}
        }
        \caption{Unacceptable}
    \end{subfigure}
    \caption[Mesh quality in SBFEM]{Scaling center (
        \tikz[baseline=-0.5ex]\draw[black,fill=black] (0,0) circle (0.7ex);
    ), minimal angle $\alpha$}
    \label{adap_fig:ei_mesh_quality}
\end{figure}
%
%   ----    %
%
\subsection{Eigenvalue in SBFEM}
\paragraph{}
From Eq.~\ref{lr_eq:sbfem_displacement_field} ,the displacement SBFEM can be calculated as
\begin{equation}
    u(\xi) = c_1 \xi ^{-\lambda_1} \phi_u^1
            +c_2 \xi ^{-\lambda_2} \phi_u^2
            +\dots
\label{adap_eq:ei_terms}
\end{equation}
where $\phi_u^i$ stands for the eigenvector corresponding to the $i$th eigenvalue in the eigenvalue matrix $\Lambda^{n}$ in Eq.~\ref{lr_eq:sbfem_eigen_decomp}.
The contribution of each mode is represented by every individual term in Eq.~\ref{adap_eq:ei_terms} \citep{Deeks2002}.
On the other hand, after the displacement solution is calculated on the boundary nodes by Eq.~\ref{lr_eq:sbfem_general_sol_disp} with $\xi=1$, displacement solution in circumferential direction then is interpolated by the help of the $p$-th order shape function $R(\eta)$ in Eq.~\ref{lr_eq:sbfem_displacement_field}.
As a result, terms corresponding to the eigenvalue $\lambda_i \leq p$ can be exactly interpolated by the shape function.
These terms can be regarded as exact.
However, therms corresponding to the eigenvalue $\lambda_i > p$ indicates the shape function is not capable to interpolate the displacement exactly and thus shall be taken as error terms.
Consequently, displacement on the boundary $u(\xi=1)$ can be expressed with exact terms and approximation terms as followed
\begin{equation}
    u_b = u_e (\xi=1) + u_a(\xi=1)
\end{equation}
where the exact terms for the displacement on the boundary can be expressed as
\begin{equation}
    u_e (\xi=1) = \sum c_i \phi_u^i \text{   for all  } \lambda_i \leq p
\end{equation}
and similarly, the approximation terms can be written as
\begin{equation}
    u_a (\xi=1) = \sum c_i \phi_u^i \text{   for all  } \lambda_i > p
\end{equation}
Follow the same logic, nodal forces on the boundary can be expressed As
\begin{equation}
    \begin{aligned}
        q_b &= q_e(\xi=1) + q_a(\xi=1) \\
        q_e(\xi=1) &= \sum c_i \phi_q^i \text{   for all  } \lambda_i \leq p \\
        q_a(\xi=1) &= \sum c_j \phi_q^j \text{   for all  } \lambda_j > p \\
    \end{aligned}
\end{equation}
% energy
The energy can be calculated by the
\begin{equation}
    \begin{aligned}
        U &= \frac{1}{2} u_b^T q_b = U_e + U_a \\
        U_e &= u_e q_e\\
        U_a &= u_e q_a + u_a q_e + u_e q_e
    \end{aligned}
\end{equation}
The relative error for energy can be calculated as $\frac{U_a}{U}$ and the same logic for displacement and stress.
\paragraph{}
In order to obtain satisfactory solutions within certain accuracy, the contribution of $U_a$ towards $U$ should be minimized and $U_a$ should also be distributed evenly to all cells. This is, in fact, similar to the relative energy norm error used in the literature \citep{NME:NME1620280411}.