\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{epstopdf}
\usepackage{abstract}
\usepackage[toc,page]{appendix}
\usepackage[sort]{cite}

\usepackage{hyperref}

\newcommand{\half}[0]{\frac{1}{2}}
\newcommand{\bvec}[1]{\mathbf{#1}}
\newcommand{\bigO}[2]{\mathcal{O}\left(#1^{#2} \right)}
\newcommand{\dotprod}[2]{ \left<#1 , #2 \right> }

\newcommand{\dd}[0]{ \mathrm{d} }

\title{ Some notes on implicit Runge-Kutta methods }
\author{ Stefan Paquay }
\date{  }

\begin{document}
\maketitle

\section{The idea}
Runge-Kutta methods are multi-stage, single-step methods aimed towards solving (systems of) ODEs.
They work by constructing at each time step approximations to the derivative of the function in between the current time level $t$ and the next $t + \Delta t,$ after which a linear combination of these stages is used to advance the numerical approximation to $y$ to the next level.
That is, if we have $\dd t/ \dd t = f(t,y),$ and $y_n$ is the numerical approximation to $y$ at $t_n = n \Delta t,$ then we have
\begin{equation*}
  k_i = f\left( t + c_i \Delta t, y_n + \Delta t \sum_{j=0}^{N-1} \alpha_{i,j} k_j \right), \qquad y_{n+1} = y_n + \Delta t \sum_{i=0}^{N-1} b_i k_i.
\end{equation*}
The methods can be summarized easily in so-called Butcher tableaus, which conveniently list $b_i,~c_i$ and $\alpha_{i,j}.$ See \ref{tab:butcher} for the Butcher tableau of some methods.

\begin{table}[b]
  \centering
  \caption{Butcher tableaus for some Runge-Kutta methods: The implicit midpoint (left), the classical Runge-Kutta method (center), and the fourth order Gauss-Legendre method (right). \label{tab:butcher}}
  \begin{tabular}{c|c}
    $\half$ & $\half$ \\ \hline
    & 1
  \end{tabular} \quad
  \begin{tabular}{c|cccc}
    0 & & & & \\
    $\half$ & $\half$ & & & \\
    $\half$ & & $\half$ & & \\
    1 & & & 1 & \\ \hline
    {} & $\frac{1}{6}$ & $\frac{2}{6}$ & $\frac{2}{6}$ & $\frac{1}{6}$
  \end{tabular}
  \begin{tabular}{c|cc}
    $\half - \frac{\sqrt{3}}{6}$ & $\frac{1}{4}$ & $\frac{1}{4} - \frac{\sqrt{3}}{6}$ \\
    $\half + \frac{\sqrt{3}}{6}$ & $\frac{1}{4} + \frac{\sqrt{3}}{6}$ & $\frac{1}{4}$ \\ \hline
    {} & $\half$ & $\half$ \\
    {} & $\half + \half\sqrt{3}$ & $\half - \half\sqrt{3}$ \\
  \end{tabular}
\end{table}

\section{The implementation}
A general, implicit Runge-Kutta method (RK method) involves solving a system of non-linear equations every time step.
The exact shape of this system depends on the number of equations in the system of ODEs \emph{and} the number of stages in the method.
If the ODE has $N_{ode}$ equations and the RK method has $N_s$ stages then the total non-linear system has $N_{ode} \times N_s$ equations.
To ease implementation of a general implicit RK method we deduce the general form of the system of equations to solve for a general number of stages and ODEs.
If we denote $\bvec{f}(t,\bvec{y}) = (\bvec{f}_0, \bvec{f}_1, \hdots, \bvec{f}_{N-1})^T$ then we have
\begin{align*}
  \bvec{F} =
  \begin{pmatrix}
    \bvec{f}\left( t + c_0\Delta t, \bvec{y} + \Delta t \sum_{j=0}^{N_s-1} \alpha_{0,j} \bvec{k}_j \right) - \bvec{k}_0 \\
    \vdots \\
    \bvec{f}\left( t + c_0\Delta t, \bvec{y} + \Delta t \sum_{j=0}^{N_s-1} \alpha_{n,j} \bvec{k}_j \right) - \bvec{k}_n \\
    \vdots \\
        \bvec{f}\left( t + c_0\Delta t, \bvec{y} + \Delta t \sum_{j=0}^{N_s-1} \alpha_{N_s-1,j} \bvec{k}_j \right) - \bvec{k}_{N_s-1}
      \end{pmatrix} =
  \bvec{0}
\end{align*}
To solve such a system we employ Newton iteration.
Although Newton iteration is typically costly, we shall see we only need to evaluate $N_{s}$ evaluations of $\bvec{f}$ and its Jacobi matrix $\bvec{J}.$
To find the general expression of the Jacobi matrix, we will apply a general three-stage method to the following system of ODEs:
\begin{align*}
  \bvec{f}(t, \bvec{y}) = -\lambda \bvec{y}
\end{align*}
Then we have
\begin{align*}
  \bvec{F} = \begin{pmatrix}
    -\lambda \bvec{y} - \lambda \Delta t \left( \alpha_{0,0}\bvec{k}_0 + \alpha_{0,1}\bvec{k}_1 + \alpha_{0,2}\bvec{k}_2  \right) - \bvec{k}_0 \\
    -\lambda \bvec{y} - \lambda \Delta t \left( \alpha_{1,0}\bvec{k}_0 + \alpha_{1,1}\bvec{k}_1 + \alpha_{1,2}\bvec{k}_2  \right) - \bvec{k}_1 \\
    -\lambda \bvec{y} - \lambda \Delta t \left( \alpha_{2,0}\bvec{k}_0 + \alpha_{2,1}\bvec{k}_1 + \alpha_{2,2}\bvec{k}_2  \right) - \bvec{k}_2
\end{pmatrix}\end{align*}
The Jacobi matrix of $\bvec{f}$ is given by $-\lambda \bvec{I},$ so the Jacobi matrix of $F$ with respect to $\bvec{k}_i$ becomes
\begin{equation*}
  \bvec{J}_\bvec{k} = \begin{pmatrix}
    -\lambda \Delta t \alpha_{0,0}\bvec{I} - \bvec{I} & -\lambda \Delta t \alpha_{0,1}\bvec{I} & -\lambda \Delta t \alpha_{0,2}\bvec{I} \\
    -\lambda \Delta t \alpha_{1,0}\bvec{I}  & -\lambda \Delta t \alpha_{1,1}\bvec{I}  - \bvec{I} & -\lambda \Delta t \alpha_{1,2}\bvec{I} \\
    -\lambda \Delta t \alpha_{2,0}\bvec{I} & -\lambda \Delta t \alpha_{2,1}\bvec{I} & -\lambda \Delta t \alpha_{2,2}\bvec{I} - \bvec{I} \\
  \end{pmatrix}
\end{equation*}
Now note that $-\lambda \bvec{I}$ is really the Jacobi matrix of $\bvec{J}$ evaluated at specific points. If we write $\bvec{J}\left( t + c_i \Delta t, \bvec{y}_n + \Delta t \sum_{j=0}^{N_s-1} \alpha_{i,j} \bvec{k}_j \right) := \bvec{J}_{i}$ then we have in general that
\begin{align*}
  \bvec{J}_\bvec{k} =& \Delta t\begin{pmatrix}
      \alpha_{0,0} \bvec{J}_0 &  \alpha_{0,1} \bvec{J}_0 &  \alpha_{0,2} \bvec{J}_0 \\
     \alpha_{1,0} \bvec{J}_1  &  \alpha_{1,1} \bvec{J}_1   &  \alpha_{1,2} \bvec{J}_2 \\
     \alpha_{2,0} \bvec{J}_2 &  \alpha_{2,1} \bvec{J}_2 &  \alpha_{2,2} \bvec{J}_2  \\
   \end{pmatrix} - \bvec{I}
\end{align*}
Note that we only need $N_s$ evaluations of the Jacobi matrix of $f$ and not $N_s^2.$
If we have a more general system of ODEs
\begin{equation*}
  \bvec{f}(t,\bvec{y}) = \begin{pmatrix} f_1( t, \bvec{y} ) \\
    f_2( t, \bvec{y} )
  \end{pmatrix}, \qquad \left(\frac{\partial f_k}{\partial z}\right)_i := \frac{ \partial f}{\partial z}\left(t + c_i \Delta t, \bvec{y} + \Delta t \sum_{j=0}^{N_s-1}\alpha_{i,j} \bvec{k}_j\right)
\end{equation*}
with $z = x,y$ and $k = 1,2$ then the total Jacobi matrix system becomes
\begin{align*}
  \bvec{J}_\bvec{k} =& \Delta t\begin{pmatrix}
    \alpha_{0,0} \left( \frac{\partial f_1}{\partial x} \right)_0 & \alpha_{0,0} \left( \frac{\partial f_1}{\partial y} \right)_0 &  \alpha_{0,1} \left( \frac{\partial f_1}{\partial x} \right)_0 & \alpha_{0,1} \left( \frac{\partial f_1}{\partial y} \right)_0 &  \alpha_{0,2} \left( \frac{\partial f_1}{\partial x} \right)_0 & \alpha_{0,2} \left( \frac{\partial f_1}{\partial y} \right)_0 \\
    \alpha_{0,0} \left( \frac{\partial f_2}{\partial x} \right)_0 & \alpha_{0,0} \left( \frac{\partial f_2}{\partial y} \right)_0 &  \alpha_{0,1} \left( \frac{\partial f_2}{\partial x} \right)_0 & \alpha_{0,1} \left( \frac{\partial f_2}{\partial y} \right)_0 &  \alpha_{0,2} \left( \frac{\partial f_2}{\partial x} \right)_0 & \alpha_{0,2} \left( \frac{\partial f_2}{\partial y} \right)_0 \\
    \alpha_{1,0} \left( \frac{\partial f_1}{\partial x} \right)_1 & \alpha_{1,0} \left( \frac{\partial f_1}{\partial y} \right)_1 &  \alpha_{1,1} \left( \frac{\partial f_1}{\partial x} \right)_1 & \alpha_{1,1} \left( \frac{\partial f_1}{\partial y} \right)_1 &  \alpha_{1,2} \left( \frac{\partial f_1}{\partial x} \right)_1 & \alpha_{1,2} \left( \frac{\partial f_1}{\partial y} \right)_1 \\
    \alpha_{1,0} \left( \frac{\partial f_2}{\partial x} \right)_1 & \alpha_{1,0} \left( \frac{\partial f_2}{\partial y} \right)_1 &  \alpha_{1,1} \left( \frac{\partial f_2}{\partial x} \right)_1 & \alpha_{1,1} \left( \frac{\partial f_2}{\partial y} \right)_1 &  \alpha_{1,2} \left( \frac{\partial f_2}{\partial x} \right)_1 & \alpha_{1,2} \left( \frac{\partial f_2}{\partial y} \right)_1 \\
    \alpha_{2,0} \left( \frac{\partial f_1}{\partial x} \right)_2 & \alpha_{2,0} \left( \frac{\partial f_1}{\partial y} \right)_2 &  \alpha_{2,1} \left( \frac{\partial f_1}{\partial x} \right)_2 & \alpha_{2,1} \left( \frac{\partial f_1}{\partial y} \right)_2 &  \alpha_{2,2} \left( \frac{\partial f_1}{\partial x} \right)_2 & \alpha_{2,2} \left( \frac{\partial f_1}{\partial y} \right)_2 \\
    \alpha_{2,0} \left( \frac{\partial f_2}{\partial x} \right)_2 & \alpha_{2,0} \left( \frac{\partial f_2}{\partial y} \right)_2 &  \alpha_{2,1} \left( \frac{\partial f_2}{\partial x} \right)_2 & \alpha_{2,1} \left( \frac{\partial f_2}{\partial y} \right)_2 &  \alpha_{2,2} \left( \frac{\partial f_2}{\partial x} \right)_2 & \alpha_{2,2} \left( \frac{\partial f_2}{\partial y} \right)_2 \\
   \end{pmatrix} - \bvec{I}
\end{align*}
which, in ``prettier'' notation, is
\begin{equation*}
  \bvec{J}_\bvec{k} = \Delta t \begin{pmatrix}
    \alpha_{0,0}\left( \frac{\partial \bvec{f}}{\partial \bvec{y}} \right)_0 & \alpha_{0,1}\left( \frac{\partial \bvec{f}}{\partial \bvec{y}} \right)_0 & \alpha_{0,2}\left( \frac{\partial \bvec{f}}{\partial \bvec{y}} \right)_0 \\
    \alpha_{1,0}\left( \frac{\partial \bvec{f}}{\partial \bvec{y}} \right)_1 & \alpha_{1,1}\left( \frac{\partial \bvec{f}}{\partial \bvec{y}} \right)_1 & \alpha_{1,2}\left( \frac{\partial \bvec{f}}{\partial \bvec{y}} \right)_1 \\
    \alpha_{2,0}\left( \frac{\partial \bvec{f}}{\partial \bvec{y}} \right)_2 & \alpha_{2,1}\left( \frac{\partial \bvec{f}}{\partial \bvec{y}} \right)_2 & \alpha_{2,2}\left( \frac{\partial \bvec{f}}{\partial \bvec{y}} \right)_2
  \end{pmatrix} - \bvec{I}
\end{equation*}

Since we have now deduced a general form for the non-linear function that defines all the stages as well as its Jacobi matrix, we can use Newton iteration to solve the system of equations.
For explicit methods, we will of course not resort to Newton iteration because the stages can be computed in a single step.

\section{A test case}
A simple way to test the integrators is to apply them to an ODE whose solution is known.
We consider
\begin{equation*}
  \frac{\dd}{\dd t} \bvec{y} = \begin{pmatrix}
    -\alpha & -\omega \\
    \omega & -\alpha \end{pmatrix} \bvec{y}
\end{equation*}
The solution can be composed in terms of the eigenvalues $\lambda_\pm$ of the matrix as
\begin{equation*}
  \bvec{y} = \bvec{C}_1e^{\lambda_+t} + \bvec{C}_2e^{\lambda_-t}, \qquad \lambda_\pm = \left[-\alpha \pm i \omega\right].
\end{equation*}
In other words, we have $\bvec{y} = \left[ \bvec{A}_1\cos(\omega t) + \bvec{A}_2 \sin(\omega t) \right]e^{-\alpha t}.$
To find $\bvec{A}_1$ and $\bvec{A}_2$, we apply the initial value $(x,y) = (1,0)^T$ and find
\begin{equation*}
  \bvec{y} = \left[ \begin{pmatrix} 1 \\ 0 \end{pmatrix} \cos(\omega t) +
  \begin{pmatrix} 0 \\ 1 \end{pmatrix} \sin( \omega t ) \right]e^{-\alpha t}
\end{equation*}

\section{Adaptive time step control and embedding methods}

Given a Runge-Kutta method, it is sometimes possible to find a second set of weights $\hat{\bvec{b}}$ in such a way that the approximation $\hat{\bvec{y}} = $

\section{Stability regions}

For a given Runge-Kutta method, the stability region can be determined by applying the method to a
simple test problem $y' = \lambda y.$
For a general Runge-Kutta method, the stages are implicitly defined as
\begin{align*}
  \begin{pmatrix} \bvec{k}_0 \\
    \bvec{k}_1 \\
    \vdots \\
    \bvec{k}_{N-1}
  \end{pmatrix}
  =& \lambda \begin{pmatrix} \bvec{y}_0 \\
    \bvec{y}_0 \\
    \vdots \\
    \bvec{y}_0
  \end{pmatrix}
  + \lambda \Delta t \sum_{j=0}^{N-1} \begin{pmatrix}
    \alpha_{0j}\bvec{k}_j \\
    \alpha_{1j}\bvec{k}_j \\
    \vdots \\
    \alpha_{(N-1)j}\bvec{k}_j
  \end{pmatrix} \\
  =& \lambda \begin{pmatrix} \bvec{y}_0 \\
    \bvec{y}_0 \\
    \vdots \\
    \bvec{y}_0
  \end{pmatrix} + \lambda \Delta t \begin{pmatrix}
    \alpha_{00}\bvec{I} &\alpha_{01}\bvec{I} &\hdots &\alpha_{0(N-1)}\bvec{I} \\
    \alpha_{00}\bvec{I} &\alpha_{1j}\bvec{I} &\hdots &\alpha_{1(N-1)}\bvec{I} \\
    \vdots & \vdots & \ddots & \vdots \\
    \alpha_{00}\bvec{I} &\alpha_{(N-1)j}\bvec{I} &\hdots &\alpha_{(N-1)(N-1)}\bvec{I} \\
  \end{pmatrix} \begin{pmatrix}
    \bvec{k}_0 \\
    \bvec{k}_1 \\
    \vdots \\
    \bvec{k}_{N-1}
  \end{pmatrix}
\end{align*}
and hence are given by
\begin{align*}
  \left[ \bvec{I}_{MN\times MN} - \lambda \Delta t \begin{pmatrix}
    \alpha_{00}\bvec{I} &\alpha_{01}\bvec{I} &\hdots &\alpha_{0(N-1)}\bvec{I} \\
    \alpha_{00}\bvec{I} &\alpha_{1j}\bvec{I} &\hdots &\alpha_{1(N-1)}\bvec{I} \\
    \vdots & \vdots & \ddots & \vdots \\
    \alpha_{00}\bvec{I} &\alpha_{(N-1)j}\bvec{I} &\hdots &\alpha_{(N-1)(N-1)}\bvec{I} \\
  \end{pmatrix}\right] \begin{pmatrix} \bvec{k}_0 \\
    \bvec{k}_1 \\
    \vdots \\
    \bvec{k}_{N-1}
  \end{pmatrix} = \lambda
  \begin{pmatrix}
    \bvec{y}_0 \\
    \bvec{y}_0 \\
    \vdots \\
    \bvec{y}_0 \\
  \end{pmatrix},
\end{align*}
where $\bvec{I}_{MN\times MN}$ is a $MN$ ny $MN$ identity matrix, as opposed to $\bvec{I}$, which is an $N\times N$ identity matrix, with $M$ the number of equations (1 for our problem) and $N$ the number of stages.
From the solution of this system the new value of $\bvec{y}$ is computed as
\begin{equation*}
  \bvec{y}_1 = \bvec{y}_0 + \Delta t
  \begin{pmatrix}
    \vert & \vert & \hdots & \vert \\
    \bvec{k}_0 & \bvec{k}_1 & \hdots & \bvec{k}_{N-1}\\
        \vert & \vert & \hdots & \vert \\
  \end{pmatrix}\begin{pmatrix}
    b_0 \\
    b_1 \\
    \vdots \\
    b_{N-1}
  \end{pmatrix}
\end{equation*}.
Since we only have one equation, the solution for $\bvec{K} := \left(k_0, k_1, \hdots, k_{N-1}\right)^T$ becomes
\begin{equation*}
  \bvec{K} = \lambda \left[ \bvec{I} - \lambda \Delta t \bvec{A}  \right]^{-1}
  \begin{pmatrix}
    y_0 \\
    y_0 \\
    \vdots \\
    y_0
  \end{pmatrix}
\end{equation*}
and so
\begin{equation*}
  y_1/y_0 = 1 + \lambda \Delta t \left(b_0, b_1, \hdots, b_{N-1}\right)\left[\bvec{I} - \lambda \Delta t \bvec{A}\right]^{-1}\left(1, 1, \hdots, 1\right)^T
\end{equation*}

\section{General linear methods}
A generalization to Runge-Kutta methods is the family of so-called general linear methods (GLM).
They can be thought of as a combination of RK and multistep methods.
A GLM consists of $s$ stages and $r$ history points. For $r=1$ we recover normal Runge-Kutta methods.
The $s$ stage points $Y_i,~0<i<s-1$ and stage derivatives $F_i$ are defined as follows:
\begin{align*}
  Y_i = \sum_{j=0}^{s-1} a_{ij}\Delta t F_j + \sum_{j=0}^{r-1}u_{ij} y_{n-j}, \qquad F_j := f(t+\Delta t c_i, Y_j).
\end{align*}
As one can see, we now have two matrices $\bvec{A} = (a_{ij})$ and $\bvec{U} = (u_{ij})$ that define the stages.
For the update to the numerical solution we similarly have two vectors $\bvec{b}$ and $\bvec{v}$:
\begin{align*}
  y_{n+1} = \sum_{i=0}^{s-1} b_i\Delta t F_i + \sum_{i=0}^{r-1}v_i y_{n-i}.
\end{align*}
Applying a GLM to the test problem $f(t,y) = \lambda y$ leads to the following:
\begin{align*}
  Y_i =& \sum_{j=0}^{s-1} z a_{ij} Y_j + \sum_{j=0}^{r-1} u_{ij}y_{n-j} \\
  y_{n+1} =& \sum_{i=0}^{s-1} z b_i Y_i + \sum_{i=0}^{r-1} v_iy_{n-i},
\end{align*}
where $z:=\lambda \Delta t.$
Applying some linear algebra we can write the stage values the following way:
\begin{align*}
  \bvec{Y} := \begin{pmatrix}
    Y_0 \\
    Y_1 \\
    Y_2 \\
    \vdots \\
    Y_{s-1}
  \end{pmatrix} = z \bvec{A} \bvec{Y} + \bvec{U}\begin{pmatrix}
    y_n \\
    y_{n-1} \\
    y_{n-2} \\
    \vdots \\
    y_{n-r+1}
  \end{pmatrix},
\end{align*}
which can be compacted into $(\bvec{I} - z \bvec{A})\bvec{Y} = \bvec{U} \bvec{y},$ with $\bvec{y} = (y_n, y_{n-1}, y_{n-2}, \hdots, y_{n-r+1})^T.$
Thus, for the stage points we simply find
\begin{equation*}
  \bvec{Y} = (\bvec{I} - z\bvec{A})^{-1}\bvec{Uy}
\end{equation*}
and hence the update becomes
\begin{equation*}
  \bvec{y}(t_{n+1}) = z \bvec{b} \cdot (\bvec{I} - z\bvec{A})^{-1}\bvec{Uy} + \bvec{v}\cdot\bvec{y}.
\end{equation*}
As a check, consider $\bvec{U}=1$ and $\bvec{v} = 1$ and $r=1,$ in which case we should recover the result for RK methods.
We see that in this case
\begin{equation*}
  \bvec{y}(t_{n+1}) = z \bvec{b} \cdot (\bvec{I} - z\bvec{A})^{-1}\bvec{y}(t_n) + \bvec{y}(t_n).
\end{equation*}
If we have $y(t_n) = 1$ and divide by it, we recover the stability function:
\begin{equation*}
  y(t_{n+1})/y(t_n) = z \bvec{b} \cdot (\bvec{I} - z\bvec{A})^{-1}(1,1,1,\hdots,1)^T + 1,
\end{equation*}
which indeed is the stability function for RK methods.

\end{document}


%!TEX root = ../main.tex
\documentclass[../main.tex]{subfiles}
\begin{document}
\subsection{Identifying Changes in Skill Prices} \label{app:ident_changes_in_skill_prices}
This appendix provides a more detailed derivation of the identification of skill prices that is presented in section \ref{sec:identifying-changes-in-skill-prices}. Just like in that section, I start from the total derivative of the utility function with respect to time.
\begin{align}
	\frac{d }{dt} u_{i}(\lambda_i^*(\tilde{w}_i(t)), \tilde{w}_i(t)) &=	\underbrace{\frac{\partial u_i(\lambda_i^*(\tilde{w}_i(t)), \tilde{w}_i(t))}{\partial \lambda_i^*(\tilde{w}_i(t))} \frac{\partial \lambda_i^*(\tilde{w}_i(t))}{\partial \tilde{w}_i(t)}  \frac{d \tilde{w}_i(t)}{dt}}_{(1)} \nonumber \\
	{} &+ \underbrace{\frac{\partial u_i(\lambda_i^*(\tilde{w}_i(t)), \tilde{w}_i(t))} {\partial \tilde{w}_i(t)} \frac{d \tilde{w}_i(t)}{dt}}_{(2)} \tag{\ref{eq:util_total_derivative}} \\
	\intertext{By the first order condition, part (1) of above equation equals zero for utility maximizing task choices $\lambda_{i,t}^*$. This can be thought of as an application of the envelope theorem, where $u_{i,t}$ is the objective function of a parameterized optimization problem and $\lambda_{i,t}^*(\tilde{w}_{i,t})$ is the parameterized optimizer. As parameter $\tilde{w}_{i,t}$ changes, changes in the optimizer $\lambda_{i,t}^*(\tilde{w}_{i,t})$ of the objective function do not contribute to the change in the objective function.\linebreak
	The partial derivation of $u_{i,t}$ w.r.t. $\tilde{w}_{i,t}$ in part (2) is easily calculated for the particular realized wage function that results for $J=2$ tasks:}
	\frac{\partial u_{i}(\lambda_{i}^*(\tilde{w}_{i}(t)), \tilde{w}_{i}(t)}{\tilde{w}_{i}(t)} \frac{d\tilde{w}_i(t)}{dt}   =& \frac{\partial}{\partial \tilde{w}_{i}(t)} \left( \lambda_{i}^* \tilde{w}_{i}(t) - 2 \theta |b_i - \lambda_{i}^*|^\phi\right) \frac{d\tilde{w}_i(t)}{dt} \nonumber\\
	{} &= \lambda_{i}^* \frac{d\tilde{w}_i(t)}{dt} \nonumber \\
	\intertext{By substituting this transformation of part (2) into the total differential (equation (\ref{eq:util_total_derivative})), equation (\ref{eq:util_derivative_env_th}) is obtained.}
	\frac{d }{dt} u_{i}(\lambda_i^*(\tilde{w}_i(t)), \tilde{w}_i(t)) &= \lambda^*(\tilde{w}_i(t)) \frac{d \tilde{w}_i (t)}{d t} \tag{\ref{eq:util_derivative_env_th}}
\end{align}
\begin{align}
	\intertext{Integrating equation (\ref{eq:util_derivative_env_th}) from $t-1$ to $t$:}
	\int^{t}_{t-1} \left( \frac{\partial u_{i}(\lambda_{i}^*(\tilde{w}_{i}(\tau)), \tilde{w}_{i}(\tau)}{\tilde{w}_{i}(\tau)} \frac{d\tilde{w}_i(\tau)}{dt}\right) d\tau &= \int^{t}_{t-1} \left(\lambda^*(\tilde{w}_i(t)) \frac{d \tilde{w}_i (t)}{d t} \right) d\tau \nonumber
\end{align}
I define $\Delta$ to denote the discrete change in a variable between two periods. Specifically, in case of the utility $\Delta u_{i,t}$ denotes the discrete change in utility from period $t-1$ to $t$, i.e.:
\begin{align} \label{appen:definition_delta}
	\Delta u_{i,t} &\equiv \int^{t}_{t-1} \left( \frac{\partial u_{i}(\lambda_{i}^*(\tilde{w}_{i}(\tau)), \tilde{w}_{i}(\tau)}{\tilde{w}_{i}(\tau)} \frac{d\tilde{w}_i(\tau)}{dt}\right) d\tau \nonumber \\
	{} &= \int^{t}_{t-1} \left(\lambda^*(\tilde{w}_i(t)) \frac{d \tilde{w}_i (t)}{d t} \right) d\tau \nonumber
\end{align}
Rewriting this by application of integration by substitution with $\tilde{w}_i(t) = \tilde{w}_{i,t}$ and relying on the abridged notation with $t$ as subscript again:
\begin{align}
\Delta u_{i,t} &= \int^{\tilde{w}_{i,t}}_{\tilde{w}_{i,t-1}} \lambda_{i,t}^* d\tilde{w}_{i,\tau} \tag{\ref{eq:util_integration}}
\end{align}
I now apply the following linear approximation of the task choice parameter:
\begin{align}
	\frac{\lambda_{i,\tau}^* - \lambda_{i,t-1}^*}{\tilde{w}_{i,\tau} - \tilde{w}_{i,t-1}} &\approx \frac{\lambda_{i,t}^* - \lambda_{i,t-1}^*}{\tilde{w}_{i,t} - \tilde{w}_{i,t-1}} \nonumber \\
	\Leftrightarrow \lambda_{i,\tau}^* &\approx \lambda_{i,t-1}^* + \frac{\lambda_{i,t}^* - \lambda_{i,t-1}^*}{\tilde{w}_{i,t} - \tilde{w}_{i,t-1}} (\tilde{w}_{i,\tau} - \tilde{w}_{i,t-1}) \tag{\ref{eq:lambda_approximation}}
\end{align}
Now substituting equation (\ref{eq:lambda_approximation}) into equation (\ref{eq:util_integration}):
\begin{align}
	\Delta u_{i,t} &= \int^{w_{i,t}}_{w_{i,t-1}} \left(  \lambda_{i,t-1}^* + \frac{\lambda_{i,t}^* - \lambda_{i,t-1}^*}{\tilde{w}_{i,t} - \tilde{w}_{i,t-1}} (\tilde{w}_{i,\tau} - \tilde{w}_{i,t-1})\right) dw_{i,\tau} \nonumber \\
	\intertext{Calculate the closed interval by evaluating the antiderivative:}
	{} &= \left[ \lambda_{i,t-1}^* \tilde{w}_{i,\tau} \frac{\lambda_{i,t}^* - \lambda_{i,t-1}^*}{\tilde{w}_{i,t} - \tilde{w}_{i,t-1}}(\frac{1}{2} \tilde{w}_{i, \tau}^2 - \tilde{w}_{i,t-1} \tilde{w}_{i,\tau}) \right ]^{\tilde{w}_{i,t}}_{\tilde{w}_{i,t-1}} \nonumber \\
	\intertext{Evaluating the closed interval:}
	{} &= \lambda_{i,t-1}^* \Delta \tilde{w}_{i,t} + \frac{\lambda_{i,t}^* - \lambda_{i,t-1}^*}{\tilde{w}_{i,t}-\tilde{w}_{i,t-1}} ( \frac{1}{2} \tilde{w}_{i,t}^2 - \tilde{w}_{i,t-1} \tilde{w}_{i,t} + \frac{1}{2} \tilde{w}_{i,t-1}^2 ) \nonumber \\
	\intertext{Rearranging and making use of the binomial expansion:}
	{} =& \lambda_{i,t-1}^* \Delta \tilde{w}_{i,t} + \frac{1}{2} \frac{\lambda_{i,t}^* - \lambda_{i,t-1}^*}{\tilde{w}_{i,t}-\tilde{w}_{i,t-1}} (\tilde{w}_{i,t} - \tilde{w}_{i,t-1})^2 \nonumber \\
	\intertext{Which can be rearranged to:}
	{} &= \bar{\lambda}_{i,t}^* \Delta \tilde{w}_{i,t} \tag{\ref{eq:utility_change}}
\end{align}
\end{document}


\chapter{Regularization}\label{Chp:ref:regularization}

The general cost function $J^{total}$ to be minimized has some of the cost
function $J^f$ measuring the defect of the result from the
forward model with the data, and the cost function $J^{reg}$ introducing the
regularization into the problem and makes sure that a unique answer exists.
The regularization term is a function of, possibly vector-valued, level set
function $m$ which represents the physical properties to be represented and is,
from a mathematical point of view, the unknown of the inversion problem.
It is the intention that the values of $m$ are between zero and one and that
actual physical values are created from a mapping before being fed into a
forward model. In general the cost function $J^{reg}$ is defined as
\begin{equation}\label{EQU:REG:1}
J^{reg}(m) = \frac{1}{2} \int_{\Omega} \left(
 \sum_{k} \mu_k \cdot ( \omega^{(0)}_k \cdot m_k^2 + \omega^{(1)}_{ki}m_{k,i}^2 )
+  \sum_{l<k} \mu^{(c)}_{lk} \cdot \omega^{(c)}_{lk}  \cdot  \chi(m_l,m_k) \right) \; dx
\end{equation}
where summation over $i$ is performed. The additional trade--off factors
$\mu_k$ and $\mu^{(c)}_{lk}$ ($l<k$) are between zero and one and constant across the
domain. They are potentially modified during the inversion in order to improve the balance between the different terms
in the cost function.

$\chi$ is a given symmetric, non-negative cross-gradient function\index{cross-gradient
}. We use
\begin{equation}\label{EQU:REG:4}
 \chi(a,b) =  ( a_{,i} a_{,i}) \cdot ( b_{,j} b_{,j}) -   ( a_{,i} b_{,i})^2
\end{equation}
where summations over $i$ and $j$  are performed, see~\cite{GALLARDO2005a}. Notice that cross-gradient function
is measuring the angle between the surface normals of contours of level set functions. So
minimizing the cost function will align the surface normals of the contours.

The coefficients $\omega^{(0)}_k$, $\omega^{(1)}_{ki}$ and $\omega^{(c)}_{lk}$ define weighting factors which
may depend on their location within the domain. We assume that for given level set function $k$ the
weighting factors $\omega^{(0)}_k$, $\omega^{(1)}_{ki}$ are scaled such that
\begin{equation}\label{ref:EQU:REG:5}
\int_{\Omega} ( \omega^{(0)}_k  + \frac{\omega^{(1)}_{ki}}{L_i^2} ) \; dx = \alpha_k
\end{equation}
where $\alpha_k$ defines the scale which is typically set to one. $L_i$ is the width of the domain in $x_i$ direction.
Similarly we set for $l<k$ we set
\begin{equation}\label{ref:EQU:REG:6}
\int_{\Omega} \frac{\omega^{(c)}_{lk}}{L^4} \; dx = \alpha^{(c)}_{lk}
\end{equation}
where $\alpha^{(c)}_{lk}$ defines the scale which is typically set to one and
\begin{equation}\label{ref:EQU:REG:6b}
\frac{1}{L^2} = \sum_i \frac{1}{L_i^2} \;.
\end{equation}

In some cases values for the level set functions are known to be zero at certain regions in the domain. Typically this is the region
above the surface of the Earths. This expressed using a
a characteristic function $q$ which varies with its location within the domain. The function $q$ is set to zero except for those
locations $x$ within the domain where the values of the level set functions is known to be zero. For these locations $x$
$q$ takes a positive value. for a single level set function one has
\begin{equation}\label{ref:EQU:REG:7}
q(x) = \left\{
\begin{array}{rl}
  1 & \mbox{ if } m \mbox{ is set to zero at location } x \\
  0 & \mbox{ otherwise }
\end{array}
\right.
\end{equation}
For multi-valued level set function the  characteristic function is set componentwise:
\begin{equation}\label{ref:EQU:REG:7b}
q_k(x) = \left\{
\begin{array}{rl}
  1 & \mbox{ if component } m_k \mbox{ is set to zero at location } x \\
  0 & \mbox{ otherwise }
\end{array}
\right.
\end{equation}

\section{Usage}

\begin{classdesc}{Regularization}{domain
        \optional{, w0=\None}
        \optional{, w1=\None}
        \optional{, wc=\None}
        \optional{, location_of_set_m=Data()}
        \optional{, numLevelSets=1}
        \optional{, useDiagonalHessianApproximation=\False}
        \optional{, tol=1e-8}
        \optional{, scale=\None}
        \optional{, scale_c=\None}
        }

initializes a regularization component of the cost function for inversion.
\member{domain} defines the domain of the inversion. \member{numLevelSets}
sets the number of level set functions to be found during the inversion.
\member{w0}, \member{w1} and  \member{wc} define the weighting factors
$\omega^{(0)}$,
$\omega^{(1)}$ and
$\omega^{(c)}$, respectively. A value for \member{w0} or \member{w1} or both must be given.
If more than one level set function is involved  \member{wc} must be given.
\member{location_of_set_m} sets the characteristic function $q$
to define locations where the level set function is set to zero, see equation~(\ref{ref:EQU:REG:7}).
\member{scale} and
\member{scale_c} set the scales $\alpha_k$ in equation~(\ref{ref:EQU:REG:5}) and
$\alpha^{(c)}_{lk}$ in equation~(\ref{ref:EQU:REG:6}), respectively. By default, their values are set to one.
Notice that weighting factors are rescaled to meet the scaling conditions. \member{tol} sets the
tolerance for the calculation of the Hessian approximation. \member{useDiagonalHessianApproximation}
indicates to ignore coupling in the Hessian approximation produced by the
cross-gradient term. This can speed-up an individual iteration step in the inversion but typically leads to more
inversion steps.
\end{classdesc}

\section{Gradient Calculation}

The cost function kernel\index{cost function!kernel} is given as
\begin{equation}\label{ref:EQU:REG:100}
K^{reg}(m) = \frac{1}{2}
\sum_{k} \mu_k \cdot ( \omega^{(0)}_k \cdot m_k^2 + \omega^{(1)}_{ki}m_{k,i}^2 )
+  \sum_{l<k} \mu^{(c)}_{lk} \cdot \omega^{(c)}_{lk}  \cdot  \chi(m_l,m_k)
\end{equation}
We need to provide the gradient of the cost function $J^{reg}$  with respect to the level set functions $m$.
The gradient is represented by two functions $Y$ and $X$ which define the
derivative of the cost function kernel with respect to $m$ and to the gradient $m_{,i}$, respectively:
\begin{equation}\label{ref:EQU:REG:101}
\begin{array}{rcl}
  Y_k & = & \displaystyle{\frac{\partial K^{reg}}{\partial m_k}} \\
   X_{ki} & = & \displaystyle{\frac{\partial K^{reg}}{\partial m_{k,i}}}
\end{array}
\end{equation}
For the case of a single valued level set function $m$ we get
\begin{equation}\label{ref:EQU:REG:202}
Y = \mu \cdot \omega^{(0)} \cdot m
\end{equation}
and
\begin{equation}\label{ref:EQU:REG:203}
 X_{i} = \mu \cdot \omega^{(1)}_{i} \cdot m_{,i}
\end{equation}
For a two-valued level set function $(m_0,m_1)$ we have
\begin{equation}\label{ref:EQU:REG:302}
Y_k = \mu_k \cdot \omega^{(0)}_k \cdot m_k \mbox{ for } k=0,1
\end{equation}
and for $X$
\begin{equation}\label{ref:EQU:REG:303}
\begin{array}{rcl}
 X_{0i} &  = & \mu_0 \cdot \omega^{(1)}_{0i} \cdot m_{0,i} + \mu^{(c)}_{01} \cdot \omega^{(c)}_{01} \cdot
\left( (m_{1,j}m_{1,j} ) \cdot m_{0,i} - (m_{1,j}m_{0,j} ) \cdot m_{1,i} \right) \\
 X_{1i} &  = & \mu_1 \cdot \omega^{(1)}_{1i} \cdot m_{1,i} + \mu^{(c)}_{01} \cdot \omega^{(c)}_{01} \cdot
\left( (m_{0,j}m_{0,j} ) \cdot m_{1,i} - (m_{1,j}m_{0,j} ) \cdot m_{0,i} \right)
\\
\end{array}
\end{equation}
We also need to provide an approximation of the inverse of the Hessian operator as discussed in section~\ref{chapter:ref:inversion cost function:gradient}.
For the case of a single valued level set function $m$ we get
\begin{equation}\label{ref:EQU:REG:601}
\begin{array}{rcl}
 A_{ij} & =&  \mu \cdot \omega^{(1)}_i \cdot \delta_{ij}  \\
D & = &  \mu \cdot \omega^{(0)}
\end{array}
\end{equation}
For a two-valued level set function $(m_0,m_1)$ we have
\begin{equation}\label{ref:EQU:REG:602}
D_{kl}  =   \mu_k \cdot \omega^{(0)}_k \cdot \delta_{kl}
\end{equation}
and
\begin{equation}\label{ref:EQU:REG:603}
\begin{array}{rcl}
A_{0i0j} & = & \mu_0 \cdot \omega^{(1)}_{0i} \cdot \delta_{ij} + \mu^{(c)}_{01} \cdot \omega^{(c)}_{01} \cdot
\left( (m_{1,j'}m_{1,j'} )\cdot \delta_{ij}  -  m_{1,i} \cdot m_{1,j} \right)    \\
A_{0i1j} & = & \mu^{(c)}_{01} \cdot \omega^{(c)}_{01} \cdot \left( 2 \cdot m_{0,i} \cdot  m_{1,j}
- m_{1,i} \cdot  m_{0,j} - ( m_{1,j'} m_{0,j'} ) \cdot  \delta_{ij}
\right)  \\
A_{1i0j} & = & \mu^{(c)}_{01} \cdot \omega^{(c)}_{01} \cdot \left( 2 \cdot m_{1,i} \cdot  m_{0,j}
- m_{0,i} \cdot  m_{1,j} - ( m_{1,j'} m_{0,j'} ) \cdot  \delta_{ij} \right)  \\
A_{1i1j} & = &  \mu_1 \cdot \omega^{(1)}_{1i} \cdot \delta_{ij} + \mu^{(c)}_{01} \cdot \omega^{(c)}_{01} \cdot
\left( (m_{0,j'}m_{0,j'} ) \cdot \delta_{ij}  -  m_{0,i} \cdot m_{0,j}  ) \right)
\end{array}
\end{equation}


\documentclass[a4paper,10pt]{article}
\usepackage{a4wide}

\newcommand{\rvir}{r_{\mathrm{vir}}}
\newcommand{\rs}{r_{\mathrm{s}}}
\newcommand{\rcut}{r_{\mathrm{cutoff}}}
\newcommand{\rdec}{r_{\mathrm{decay}}}
\newcommand{\Mtot}{M_{\mathrm{tot}}}
\newcommand{\Ntot}{N_{\mathrm{tot}}}

\author{Marcel Zemp}
\title{HALOGEN4MUSE}
\date{30. August 2007}

\begin{document}

\maketitle

\abstract{HALOGEN4MUSE allows to generate spherical structures from the $\alpha\beta\gamma$-model family with an isotropic velocity tensor. Particles are sampled self-consistently form the distribution function of the models.}

\section{General characteristics of the models}

We restrict ourself to models of the form given by equation (\ref{eq:rhocut}) with $\gamma < 3$ for the mass not to diverge in the centre. Similarly, for $\beta \leq 3$ the total mass would diverge and we need to introduce a cut-off radius. We chose the form
\begin{equation} \label{eq:rhocut}
\rho(r) = \left\{
\begin{array}{ll}
\frac{\rho_0}{\left(\frac{r}{\rs}\right)^\gamma \left(1+\left(\frac{r}{\rs}\right)^\alpha
\right)^{\left(\frac{\beta-\gamma}{\alpha}\right)}}	&
r \leq \rcut \\
\rho(\rcut) \left(\frac{r}{\rcut}\right)^\delta \mathrm{e}^{-\frac{r-\rcut}{\rdec}} &
r > \rcut
\end{array}
\right.
\end{equation}
where $\delta$ and $\rdec$ are free parameters. By requiring the logarithmic slope to be continuous at $\rcut$, we get
\begin{equation}
\delta = \frac{\rcut}{\rdec} - \frac{\gamma +
\beta \left(\frac{\rcut}{\rs}\right)^{\alpha}}{1 + \left(\frac{\rcut}{\rs}\right)^{\alpha}}~.
\end{equation}
We set the truncation scale $\rdec = 0.3~\rcut$ in order not to make the truncation too sharp. A too sharp truncation can lead to an instability of the model around $\rcut$. For $\beta > 3$ we simply set $\rcut = \infty$ (i.e. no cut-off) while for $\beta \leq 3$ one has to specify a cut-off scale $\rcut$ (e.g. $\rcut = 10~\rs$). By further specifying $\rs$ and the total mass $\Mtot$, the normalisation $\rho_{0}$ is given by
\begin{equation}
\rho_0 = \frac{\Mtot}{4 \pi \rs^3 (I_{\mathrm{M}} + I_{\mathrm{M,cutoff}})}
\end{equation}
where
\begin{eqnarray}
I_{\mathrm{M}} &\equiv& \int_0^{q} \frac{x^{2-\gamma}}{\left(1+x^\alpha\right)^{\left(\frac{\beta-\gamma}{\alpha}\right)}} \mathrm{d}x
\stackrel{q=\infty}{=} \frac{\Gamma\left(\frac{\beta-3}{\alpha}\right) \Gamma\left(\frac{3-\gamma}{\alpha}\right)}{\alpha \Gamma\left(\frac{\beta-\gamma}{\alpha}\right)} \\
I_{\mathrm{M,cutoff}} &\equiv&
\frac{1}
{\rs^3 q^\gamma \left(1+q^\alpha\right)^{\left(\frac{\beta-\gamma}{\alpha}\right)}}
\int_{\rcut}^{\infty} r^2 \left(\frac{r}{\rcut}\right)^{\delta} \mathrm{e}^{-\frac{r-\rcut}{\rdec}} \stackrel{q=\infty}{=} 0
\end{eqnarray}
with $q = \rcut / \rs$ and $\Gamma$ is the standard gamma function.

\section{Distribution function as probability density function}

For spherical systems with an isotropic velocity distribution one can calculate the distribution function, which in that case only depends on energy, by the Eddington inversion. Hence, by restricting to models with an isotropic velocity distribution, we can calculate the distribution function for our spherical structure models described by equation (\ref{eq:rhocut}) - at least numerically. Since the state of a system at a given time is completely described by the distribution function $f(\vec{r},\vec{v})$, we use it as a probability density function in order to sample the phase space with particles,
\begin{equation}
p_{\mathrm{6D}}(\vec{r},\vec{v}) \mathrm{d}\vec{r} \mathrm{d}\vec{v} = \frac{f(\vec{r},\vec{v})}{\Mtot} \mathrm{d}\vec{r} \mathrm{d}\vec{v}
\end{equation}
is the probability that a particle is in the volume $\mathrm{d}\vec{r} \mathrm{d}\vec{v}$ around the phase space point $(\vec{r},\vec{v})$. By integrating out the velocities and using spherical symmetry, we get for the probability density $p(r)$ in coordinate space
\begin{equation}
p(r) \mathrm{d}r = \frac{4 \pi r^2 \rho(r)}{\Mtot} \mathrm{d}r
\end{equation}
and the positions can now be sampled by using the quantile function, which is the inverse of the cumulative probability distribution function $M(r)/\Mtot$ for the above probability density function $p(r)$. For a particle at location $\vec{r}_i$ we get now the following probability density for the velocities
\begin{equation}
p(r_i,v) \mathrm{d}v = \frac{16 \pi^2}{\Mtot} f(r_i,v) r_i^2 v^2 \mathrm{d}v
\end{equation}
where $r_i = |\vec{r}_i|$. In general, the distribution function can only be calculated numerically for the large family of models described by the density profile (\ref{eq:rhocut}). Hence, numerical integration and inversion for $p(r_i,v)$ in order to calculate the quantile function is difficult and one generally uses the acceptance-rejection technique for the Monte Carlo sampling of the velocities.

This Monte Carlo sampling procedure directly from the distribution function $f(\vec{r},\vec{v})$ leads to perfectly stable equilibrium models. These models do not show the flattening in the central part of the structure during evolution as it is obtained in the case of the assumption of a local Maxwellian velocity distribution with the velocity dispersion given by the Jeans equation.

\section{Options and Usage}

HALOGEN4MUSE accepts command line options in the following way: for example the command \\
\\
\texttt{halogen4muse -a 2 -b 5 -c 0 -M 1 -rs 1 -N 100000 -name plummer} \\
\\
generates a Plummer sphere with $\alpha = 2$, $\beta = 5$, $\gamma = 0$, $\Mtot = 1$, $\rs = 1$, $\Ntot = 100000$ and writes the output into a file \texttt{plummer.IC.ascii}. The built-in default values for the total mass and the scale radius are unity, i.e. $\Mtot = 1$ and $\rs = 1$. So, we could have left these options in the command line above.

For models that need a cut-off (i.e. $\beta \leq 3$), one has to additionally specify a cut-off radius $\rcut$. For example the command \\
\\
\texttt{halogen4muse -a 1 -b 3 -c 1 -M 1 -rs 1 -rcutoff 10 -N 100000 -name nfw}\\
\\
generates an NFW profile with $\alpha = 1$, $\beta = 3$, $\gamma = 1$, $\Mtot = 1$, $\rs = 1$, $\rcut = 10$ and $\Ntot = 100000$.

If one specifies the additional options \texttt{-ogr} or \texttt{-ogdf}, the grid in radius respectively the grid for the distribution function is written out into separate files.

Normally, the Monte Carlo sampling is initiated by a random seed. The option \texttt{-randomseed} allows one to set this artificially.

The virial radius is defined by
\begin{equation}
\rvir \equiv {\Mtot^2}/{\sum_{i, j \neq i} \frac{m_i m_j}{|\vec{r}_i - \vec{r}_j|}} = - \frac{1}{2} \frac{G \Mtot^2}{E_{\mathrm{pot}}}
\end{equation}
where the second equality is only valid in the $N \rightarrow \infty$ limit. The standard way to calculate $\rvir$ is via the potential energy since it is much faster than the exact way via the $N^2$ sum over all particles. If one sets the option \texttt{-dorvirexact}, one has the possibility to calculate the virial radius exactly via the $N^2$ sum.

HALOGEN4MUSE also supports the inclusion of a central black hole. With \texttt{-MBH} one specifies the mass of the black hole which is then set with zero velocity at the geometric centre of the surrounding structure. The presence of the central point mass modifies the distribution function of the surrounding structure compared to the same model without central point mass. The self-consistent distribution function of the spherical structure with a central point mass is again calculated numerically so that the velocities are sampled correctly. Warning: choices of $\gamma < 1/2$ lead to unphysical models, i.e. the distribution function has negative values. But HALOGEN4MUSE warns you if you accidentally do so.

Of course, you can always ask HALOGEN4MUSE for help with the \texttt{-h} or \texttt{-help} options.

\end{document}


\section{Time Integration}
A maneuver is a general unsteady flight condition which includes both trimmed flight and accelerating motions. While it is computationally more expensive to simulate compared to trim, the nature of the present formulation renders it extremely straightforward to implement. Recall that the dynamics formulation resembles
\[ \textbf{f}(\vector{y}\textrm{ , }\dot{\vector{y}}\textrm{ , }\vector{u}\textrm{ , }t)
\quad = \quad \grkvec{\epsilon} \quad = \quad \vector{0} \]

Using an ODE solver (Ref. \cite{Petzold}), the values of $\vector{y}$ and $\dot{\vector{y}}$ are adjusted automatically at each time step by the solver (internally using polynomial interpolation up to order 5) until the relative and absolute errors fall below a user-specified threshold $\delta_\textrm{ODE}$. Reducing this threshold, i.e. enforcing more precision increases the computational effort, but does not significantly affect the accuracy of the solutions beyond a certain numerical value of $\delta_\textrm{ODE}$. For cases investigated so far, the point of diminishing returns is typically $\delta_\textrm{ODE}$ =  10$^{-6}$. The version of DASSL used requires a function argument without derived types, since it is written in Fortran 77. An adapter routine \textbf{ODE\_Bridge} converts the state vectors provided by DASSL into the derived types needed for \textbf{ODEResiduals}.

Integration can be performed either with user-prescribed controls (subroutine \textbf{read\_con}), fixed controls or a pseudo-flight control system to maintain trim for propulsive trim. Integration is performed for a specified number of rotor revolutions until convergence is achieved. The advantage of this technique is that it makes no assumptions on the time resolution of rotor motions, especially blade accelerations.
\begin{Figure}
 \centering
 \includegraphics[width=1.3\textwidth, angle=90]{images/time_march_callgraph.png}
 \vspace{-0.5cm}
 \captionof{figure}{Call graph to perform time marching}
 \label{fig:cg}
\end{Figure}


\section{Functional programming}

\begin{frame}[fragile]
  \frametitle{List Comprehensions}
  \begin{itemize}
  \item A different style of Programming
  \item Functions `emulate' mathematical functions
  \item Output depends only on input arguments
  \item There is no `state' associated with the program
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{List Comprehensions}
  \begin{block}{}
    Given a list of weights of people, and we need to
    calculate the corresponding weights on the moon. Return a new
    list, with each of the values divided by 6.0.
  \end{block}

  \begin{itemize}
  \item Solution using \texttt{for} loop is shown
  \end{itemize}
  \begin{lstlisting}
    weights = [30, 45.5, 78, 81, 55.5, 62.5]
    weights_moon = []
    for w in weights:
        weights_moon.append(w/6.0)
    print weights_moon
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{List Comprehensions \ldots}
  \begin{itemize}
  \item List comprehensions are compact and readable
  \end{itemize}
  \begin{lstlisting}
    [ w/6.0 for w in weights ]
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{List Comprehensions \ldots}
  \begin{itemize}
  \item Return the weight on moon, only if weight on earth > 50
  \end{itemize}
  \begin{lstlisting}
    weights = [30, 45.5, 78, 81, 55.5, 62.5]
    weights_moon = []
    for w in weights:
        if w > 50:
            weights_moon.append(w/6.0)

    print weights_moon
  \end{lstlisting}
  \begin{itemize}
  \item List comprehension that checks for a condition
  \end{itemize}
  \begin{lstlisting}
    [ w/6.0 for w in weights if w>50 ]
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{List Comprehensions \ldots}
  \begin{itemize}
  \item if weight > 50, return weight/6.0
  \item else, return weight*3.0
  \end{itemize}
  \begin{lstlisting}
    weights = [30, 45.5, 78, 81, 55.5, 62.5]
    weights_migrate = []
    for w in weights:
        if w > 50:
            weights_migrate.append(w/6.0)
        else:
            weights_migrate.append(w * 3.0)

    print weights_migrate
  \end{lstlisting}
  \begin{itemize}
  \item This problem \alert{CANNOT} be solved using list
    comprehensions
  \item Try \texttt{map}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{\texttt{map}}
  \begin{itemize}
  \item Takes a function and a sequence as arguments
  \item Calls function with each element of sequence as argument
  \item Returns a sequence with all the results as elements
  \item We solve the easier problem first, using \texttt{map}
  \end{itemize}

  \begin{lstlisting}
    def moonize(weight):
        return weight/6.0

    map(moonize, weights)
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{\texttt{map} \ldots}
  \begin{lstlisting}
    def migrate(weight):
        if weight < 50:
            return weight*3.0
        else:
            return weight/6.0
  \end{lstlisting}

  \begin{itemize}
  \item \texttt{migrate} compares weight with 50 and returns the
    required value.
  \item We can now use \texttt{map}
  \end{itemize}
  \begin{lstlisting}
    map(migrate, weights)
  \end{lstlisting}
  \begin{itemize}
  \item Now, we wish to get away with the function definition
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{\texttt{lambda}}
  \begin{itemize}
  \item Allows function definition, anonymously
  \end{itemize}
  \begin{lstlisting}
    map(lambda x: x/6.0, weights)
  \end{lstlisting}
  \begin{itemize}
  \item \texttt{lambda} actually returns a function which we could in
    fact assign to a name and use later.
  \end{itemize}
  \begin{lstlisting}
    l_moonize = lambda x: x/6.0
    map(l_moonize, weights)
  \end{lstlisting}
  \begin{lstlisting}
    l_migrate = lambda x: x*3.0 if x < 50 else x/6.0
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{\texttt{filter}}

  \begin{itemize}
  \item We avoided discussion of problem of returning new weights only
    when actual weight is more than 50.
  \item \texttt{filter} can be used to filter out ``bad'' weights
  \item Later, we could use \texttt{map}
  \end{itemize}
  \begin{lstlisting}
    filter(lambda x: x > 50, weights)
  \end{lstlisting}
  \begin{itemize}
  \item Takes a function and a sequence
  \item Returns a sequence, containing only those elements of the
    original sequence, for which the function returned \texttt{True}
  \end{itemize}
  \begin{lstlisting}
    map(lambda x: x/6.0, filter(lambda x: x > 50, weights))
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{\texttt{reduce}}
  \begin{itemize}
  \item ``reduces'' a sequence
  \end{itemize}
  \begin{lstlisting}
    reduce(lambda x,y: x*y, [1, 2, 3, 4])
  \end{lstlisting}
  \begin{itemize}
  \item Takes function and sequence as arguments; the function should
    take two arguments
  \item Passes first two elements of sequence, and continues to move
    over the sequence, passing the output in the previous step and the
    current element as the arguments
  \item The function above essentially calculates $((1*2)*3)*4$
  \end{itemize}
\end{frame}


\title{Experimental Laboratory 1\\Calibration of a load cell}
\author{
        Sergio M. Vanegas A.\\
        Francesco de Pas\\
                Department of Mathematics\\
        Polimi---Politecnico di Milano\\
        Milano, Italia
}
\date{\today}

\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{siunitx}

\begin{document}
\maketitle

\begin{abstract}
        The second test case consists on the experimental calibration of a Load Cell (instrument for measuring Force). The load cell produces an electrical output (Voltage) which is approximately proportional to the applied force. In order to estimate the value a force with a load cell, it is necessary to determine the transfer function from voltage to newton \( V [volt] \rightarrow F [Newton] \). \cite{FL:02}
\end{abstract}

\section{Introduction}

        The transfer function is obtained by using mass samples whose weight force \( F^* \) is accurately known (see Figure~\ref{fig:sketch}); this operation is what's usually called calibration.

        \begin{figure}[!ht]
                \includegraphics[width=0.75\textwidth]{Sketch.png}
                \centering
                \caption{Sketch of the Case}
                \label{fig:sketch}
        \end{figure}

        Each mass sample is applied to the load cell for a few seconds and the correspondent electrical output is recorded (the sampling frequency is \( f_s=100 \: H\!z \)). This produces a noisy signal like the one shown in Figure~.

        \begin{figure}[!ht]
                \includegraphics[width=0.75\textwidth]{Noise.png}
                \centering
                \caption{Noisy Load Cell signal example}
                \label{fig:noise}
        \end{figure}

        The voltage output histories and the corresponding values of the reference force, \( F^* \), are listed in the Table and provided in the form of MATLAB workspaces. Each workspace contains the vector of the readings in Volt (remember that the sampling frequency is \( f_s = 100 H\!z \)). In Table~\ref{tab:data}, the sign of F* is as presented in the sketch, namely, \( F^* > 0 \) when the force is directed upwards, and \( F^* < 0 \) when it is directed downwards.

        \begin{table}[!ht]
                \label{tab:data}
                \begin{tabular}{|cc|cc|}
                        \hline
                        \textbf{TestID} & \( \pmb{F^* \: \left[ N \right]} \) & \textbf{TestID} & \( \pmb{F^* \: \left[ N \right]} \) \\ \hline
                        calibr01        & 0.000               & calibr09        & -5.598              \\
                        calibr02        & -0.314              & calibr10        & -10.501             \\
                        calibr03        & -0.411              & calibr11        & 0.126               \\
                        calibr04        & -0.695              & calibr12        & 1.097               \\
                        calibr05        & -0.891              & calibr13        & 2.078               \\
                        calibr06        & -1.186              & calibr14        & 4.039               \\
                        calibr07        & -1.676              & calibr15        & 8.942               \\
                        calibr08        & -2.656              & calibr16        & 13.845              \\ \hline
                \end{tabular}
                \centering
                \caption{Reference Forces}
        \end{table}

        The main objective of the laboratory is to determine the transfer function for the load cell, and estimate the uncertainty of the instrument. For that purpose, the remainder of the report is organized as follows: Section~\ref{sec:stabilized_signal} graphically stimates the acquisition time required in order to get a stabilized measure out of an accumulating averaged voltage signal, and then provides the stabilized voltage value for each test case; Section~\ref{sec:lin_regression} performs a linear regression over the averaged voltage measure in function of the reference force; Section~\ref{sec:uncertainty} provides an approximation of the uncertainty we can expect from using the coefficients obtained in Section~\ref{sec:lin_regression} to estimate the value of the measured force in function of the averaged voltage signal; finally, Section~\ref{sec:resolution} provides an approximation of the resolution of the load cell based on the measured data from the test cases.

\section{Stabilized voltage measurement} \label{sec:stabilized_signal}

        \begin{figure}[!ht]
                \includegraphics[width=0.75\textwidth]{Averaged_Data.png}
                \centering
                \caption{Accumulated averaged data over time (normalized)}
                \label{fig:averaged}
        \end{figure}

        Following the guidelines, we proceeded to calculate the accumulated average of the sensor signal over time. Additionally, we normalized the difference between each partial average and the final one by this last value and plotted the square of this quantity, which is the signal we can observe on Figure~\ref{fig:averaged}. We can observe how the power of the error decreases by 3 orders of magnitude (at the very least) by the 10 second mark, turning it into a convenient criterion to start considerating the averaged measure as valid.

        Nevertheless, as a countermeasure to overfitting (and considered the data had already been gathered anyway), we decided to extract the median of each accumulated average after the 10 second mark and use it as the final measure.

\section{Linear regression} \label{sec:lin_regression}

        \begin{figure}[!ht]
                \includegraphics[width=0.75\textwidth]{Fitting.png}
                \centering
                \caption{Data linear regression}
                \label{fig:regression}
        \end{figure}

        After extracting the averaged measure, we proceeded to perform a linear regression on the data as a function of the reference applied force. The resulting formula corresponds to the one in Equation~\ref{eq:regression}.

        \begin{equation} \label{eq:regression}
                \mu_{V_0} = m F^{*} + b = (\SI{-4.077e-4}{\volt\per\newton}) F^{*} + (\SI{-2.901e-5}{\volt})
        \end{equation}

\section{Uncertainty estimation} \label{sec:uncertainty}

        Considering the random nature of the measurement process, we had to calculate the uncertainty \( U \) associated to the estimated force \( \widetilde{F} = \frac{V_0 - b}{m} \). For that purpose, we followed 2 different approaches:

        \begin{enumerate}
                \item Set \( U \) as the maximum absolute difference between \( \widetilde{F} \) and \( F^{*} \) for all the 16 calibration cases.
                \item Set \( U \) as a function of the confidence intervals for a perfect Gaussian PDF (\( 95\% \) confidence).
        \end{enumerate}

        The resulting uncertainties can be seen in Equation~\ref{eq:uncertainty_1} and Equation~\ref{eq:uncertainty_2} respectively.

        \begin{equation} \label{eq:uncertainty_1}
                U_1 = \sup_{i \in \{1,...,16\}} \left| \frac{V_i - b}{m} - F^* \right| = \SI{1.381e-2}{\newton}
        \end{equation}

        \begin{equation} \label{eq:uncertainty_2}
                U_2 = 2 \bar{S}_F = 2 \sqrt{\frac{1}{N-2} \sum_{i = 1}^{16}{\left(\frac{V_i - b}{m} - F\right)^2}} = \SI{1.179e-2}{\newton}
        \end{equation}

\section{Resolution estimation} \label{sec:resolution}

        Finally, we estimated the sensor's voltage resolution by ordering each measuring sequence and extraction the global minimum variation, which ended up being \( \SI{6.593e-6}{\volt} \). Then, making use of the linear regression coefficients calculated beforehand, we obtained the expression in Equation~\ref{eq:resolution} for the sensor's force resolution.

        \begin{equation} \label{eq:resolution}
                \Delta F = \inf_{\left\{ V_1, V_2 \right\}} \left| \frac{V_1 - b}{m} - \frac{V_2 - b}{m} \right| = \left| \frac{\Delta V}{m} \right| = \SI{1.617e-2}{\newton}
        \end{equation}

\bibliographystyle{abbrv}
\bibliography{main}

\end{document}


\section{Question 2}
System:
$$
G_{1_{(s)}} = \dfrac{1}{(17s+1)(5s+1)}\exp(-30s) = \dfrac{1}{102s^2+23s+1}
$$
We use Optimal PID to design controller with ITAE, ISE and IAE cost function. In program we use 100 second for optimization but use 1000 second for simulation beacuse optimization takes too long time and in 100 second beacuse of long delay time we can't see system behavior.
\newpage
 \begin{itemize}
     \item ITAE
     $$
     K_p = 0.6867, \quad K_i = 0.0347, \quad K_d = 15.0543
     $$
     \begin{figure}[H]
        \caption{Step responde with PID controller and ITAE cost function}
        \centering
        \includegraphics[width=11cm]{../Figure/Q2/ITAE.png}
    \end{figure}
    \item ISE
    $$
    K_p =0.5399, \quad K_i = 0.0446, \quad  K_d =20.8391
    $$
    \begin{figure}[H]
       \caption{Step responde with PID controller and ISE cost function}
       \centering
       \includegraphics[width=11cm]{../Figure/Q2/ISE.png}
   \end{figure}
   \item IAE
   $$
   K_p = 0.6522, \quad K_i = 0.0393, \quad K_d = 17.5028
   $$
   \begin{figure}[H]
      \caption{Step responde with PID controller and IAE cost function}
      \centering
      \includegraphics[width=11cm]{../Figure/Q2/IAE.png}
  \end{figure}
 \end{itemize}
 PID designed with ITAE and IAE cost function work better system is fast with lower overshoot but in ITAE cost function system has better undershoot.