
\documentclass[swedish,a4paper,11pt,onesided]{article}
\usepackage[T1]{fontenc}
\usepackage[swedish]{babel}
\usepackage{alltt,epsfig,fancyhdr,url, amsfonts, amsmath, esint}

\begin{document}

% Fix the bold (etc) tt fonts.

\newcommand{\btt}{\ttfamily\bfseries}
\renewcommand{\ttdefault}{pcr}

% Enumerating should be done 1.2.3.4

\renewcommand{\labelenumi}{\arabic{enumi}}
\renewcommand{\labelenumii}{\arabic{enumi}.\arabic{enumii}}
\renewcommand{\labelenumiii}{\arabic{enumi}.\arabic{enumii}.\arabic{enumiii}}
\renewcommand{\labelenumiv}{\arabic{enumi}.\arabic{enumii}.\arabic{enumiii}.\arabic{enumiv}}

% Algorithm description
\newcommand{\balg}{\begin{enumerate}}
\newcommand{\ealg}{\end{enumerate}}
\newcommand{\lra}{\Leftrightarrow}
\newcommand{\Ra}{\Rightarrow}
\newcommand{\ra}{\rightarrow}
\newcommand{\ointcw}{\ointctrclockwise}
\newcommand{\beq}{\begin{equation}}
\newcommand{\eeq}{\end{equation}}
\newcommand{\bal}{\begin{align*}}
\newcommand{\eal}{\end{align*}}

% Equation numbering 
\numberwithin{equation}{section} %sets equation numbers <chapter>.<section>.<index>
\numberwithin{equation}{subsection} %sets equation numbers <chapter>.<section>.<subsection>.<index>
%\numberwithin{equation}{subsubsection} %sets equation numbers <chapter>.<section>.<subsection>.<subsubsection>.<index>

% Acknowledgements looks like this. FIXME

\newcommand{\backn}{\begin{description}}
\newcommand{\eackn}{\end{description}}

\newcommand{\ackn}[2]{\item{#1} #2}

\newcommand{\bitem} {\begin{itemize}}
\newcommand{\eitem} {\end{itemize}}

% Who, what, where etc:
\def\author			{Camilla Westrin}
\def\email			{ayrin@acc.umu.se}
\def\course			{Numerical Methods in Physics}
%d\def\delivery			{Första Inlämningen}
%\def\trivialname		{Samtliga labbar}
% Headers and footers
\headrulewidth 0pt
\pagestyle{fancy}
\lhead{\course\\\author}
\footrulewidth 0.5pt
\rfoot{\today}
%\lfoot{{\bf \delivery}}

% Titlepage
\begin{titlepage}
\begin{center}
%	\Huge \trivialname \\
        \vspace{10mm}
        \Large \course \\
        \vspace{20mm}
        %\huge \delivery \\
	\vspace{10mm}
        \large {\em \author}\\
	(\email) \\
        \vspace{10mm}
        \large \today \\
        \vspace{20mm}
	%\large $\sim${\tt tfy02cvn/edu/vr2/newtry}\\
	\vfill
\end{center}
\end{titlepage}
\section{Excercise 1}
\subsection{Variables calculated for known object}
Calculating moment of inertia around an axis through the tip of the top, perpendicular to the symmetry axis $I_1$, the moment of inertia around the symmetry axis $I_3$, the mass $M$ and center of mass $l$ for a homogenous cone with height $h = 5$ cm, top radius $r = 5$ cm and density $\rho = 1 g/cm^3$.

\[
M = \mathrm{Volume} * \mathrm{Density} = \frac{\pi}{3}* 5^3*1 = \frac{125\pi}{3} \approx 130.8997
\]

\[
l = \frac{3}{4}h = \frac{15}{4} = 3.75
\]

%\begin{eqnarray}
%I_1 & = & \int\rho(z)(z^2 + r^2 \sin^2\theta) r\: \mathrm{d}r\: \mathrm{d}\theta\: \mathrm{d}z \Ra \left[ \rho(z) = 1 \right] \Ra \int(z^2 + r^2 \sin^2\theta) r\: \mathrm{d}r\: \mathrm{d}\theta\: \mathrm{d}z \nonumber \\
%& = & \int^5_0 \int^{5}_0 \int^{2\pi}_0 (r z^2 + r^3 \sin^2\theta)\: \mathrm{d}\theta\: \mathrm{d}r\: \mathrm{d}z = \left. \int^5_0 \int^{5}_0 (r z^2 \theta - r^3 \sin\theta\cos\theta) \right|^{2\pi}_0\mathrm{d}r\: \mathrm{d}z  \nonumber \\
%& = & \left. \int^{5}_0 2\pi\frac{r^2}{2} z^2 \right|^{5}_0 \mathrm{d}z = \left. 2\pi\frac{25}{2} \frac{z^3}{3} \right|^{5}_0 = 2\pi\frac{25}{2} \frac{125}{3} = 2\pi\frac{3125}{6} \approx 3272.4923 \nonumber
%\end{eqnarray}

\[
I_1 = \frac{3M}{5}\left(\frac{r^2}{4} + h^2 \right) = \frac{3*125\pi}{15}\left(\frac{5^2}{4} + 5^2 \right) \approx 2454.3693
\]

\[
I_3 = \frac{3}{10}M r^2 = \frac{3}{10}\frac{125\pi}{3}5^2 = \frac{9375\pi}{30} \approx 981.7477 
\]

\subsection{Results from test runs}
\begin{table}[ht]
%\caption{Nonlinear Model Results}
% title of Table
\centering
% used for centering table
\begin{tabular}{c c c c c}
% centered columns (4 columns)
\hline\hline
%inserts double horizontal lines
% inserts table heading
Object & $I_1$ & $I_3$ & $M$ & $l$ \\ [0.5ex]
% inserts single horizontal line
\hline
% inserting body of the table
Analytic & 2454.3693 & 981.7477 & 130.8997 & 3.75 \\
cone.o & 2454.369 & 981.7477 & 130.8997 & 3.75 \\
top1.o & 1215.005 & 482.5677 & 100.9551 & 3.0063 \\
top2.o & - & - & - & - \\
top3.o & 202.5644 & 12.30290 & 21.30722 & 2.9241 \\ 
top4.o & 177.4822 & 13.96901 & 24.83551 & 2.5006 \\ [1ex] 
% [1ex] adds vertical space
\hline
%inserts single line
\end{tabular}
\label{table:nonlin}
% is used to refer this table in the text
\end{table}

\section{Excercise 2}
\subsection{First order equations used}
The six equations used are preceded by $f[number]$. The other three are substitutions used.
\[
\phi = y_0
\]
\[
f[0] = \dot{\phi} = y_1;
\]
\[
\psi = y_2
\]
\[
f[2] = \dot{\psi} = y_3;
\]
\[
\theta = y_4
\]
\[
f[4] = \dot{\theta} = y_5;
\]
\[
f[1] = \ddot{\phi} = \frac{I_3}{I_1}\left(\frac{y_5 y_1 \cos(y_4)}{\sin(y_4)}\right) + \frac{y_3 y_5}{\sin(y_4)} - 2 y_5 y_1 \cot(y_4)
\]
\[
f[3] = \ddot{\psi} = y_1 y_5\sin(y_4) + 2 y_5y_1\frac{\cos^2(y_4)}{\sin(y_4)} - \frac{I_3}{I_1}\left(\frac{y_1y_5\cos^2(y_4)}{\sin(y_4)} + y_3y_5\cot(y_4) \right)
\]
\[
f[5] = \ddot{\theta} = \frac{M g l}{I_1}\sin(y_4) + y_1^2\sin(y_4)\cos(y_4) - \frac{I_3}{I_1}(y_3 + y_1\cos(y_4))y_1\sin(y_4)
\]
\section{Excercise 3}
\subsection{Derivations for excercise 3}

\[ T(\theta_k) = \frac{I_3}{2} \left[ \begin{array}{ccc}
s & 0 & 0 \\
0 & s\sin^2{\theta_k} +\cos^2{\theta_k} & \cos{\theta_k} \\
0 & \cos{\theta_k} & 1 \end{array} \right]\]

\[ \Delta k = \left[ \begin{array}{c}
(\theta_{k+1} - \theta_{k})/h\\ 
(\phi_{k+1} - \phi_{k})/h\ \\
(\psi_{k+1} - \psi_{k})/h \\ \end{array} \right]\]

\[ T_v = \left[ \begin{array}{c}
\Delta^T_k \frac{\partial T}{\partial \theta} |_{\theta_k} \Delta_k + M g l \sin{\theta_k} \\ 
0 \\
0 \\ \end{array} \right] = \]

\[\left[ \begin{array}{c}
\frac{I_3}{2}(k_\phi^2(2s-2)\cos\theta_k\sin\theta_k - k_\phi k_\psi \sin\theta_k - k_\psi k_\phi\sin\theta_k + M g l \sin{\theta_k)} \\ 
0 \\
0 \\ \end{array} \right]
\]

\[ T^{-1}(\theta_k) = \frac{2}{I_3} \left[ \begin{array}{ccc}
1/s & 0 & 0 \\
0 & \frac{1}{s\sin^2{\theta_k}} & -\frac{\cos{\theta_k}}{s\sin^2{\theta_k}} \\
0 & -\frac{\cos{\theta_k}}{s\sin^2{\theta_k}} & -\frac{s\sin^2{\theta_k} + \cos^2{\theta_k}}{s\sin^2{\theta_k}} \end{array} \right]\]

\beq \label{eq:1}
T(\theta_k)\Delta_k = T(\theta_{k-1})\Delta_{k-1} + \frac{h T_v}{2}
\eeq
As given in the intructions, we should multiply equation \ref{eq:1} with $T^{-1}$ to produce the stepper, \ref{eq:2}.

\beq \label{eq:2}
T^{-1}(\theta_k) T(\theta_k)\Delta_k = T^{-1}(\theta_k) T(\theta_{k-1})\Delta_{k-1} + T^{-1}(\theta_k) \frac{h T_v}{2} 
\eeq

Written in matrix-form this gives:

\begin{eqnarray} \left[ \begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \end{array} \right] 
\left[ \begin{array}{c}
(\theta_{k+1} - \theta_{k})/h\\ 
(\phi_{k+1} - \phi_{k})/h\ \\
(\psi_{k+1} - \psi_{k})/h \\ \end{array} \right] = \\
\left[ \begin{array}{ccc}
1 & 0 & 0 \\
0 & \frac{s\sin^2{\theta_{k-1}} +\cos^2{\theta_{k-1}}}{s\sin^2{\theta_{k}}} - \frac{\cos{\theta_{k}}\cos{\theta_{k-1}}}{s\sin^2{\theta_{k}}} & \frac{\cos{\theta_{k-1}-\cos{\theta_{k}}}}{s\sin^2{\theta_{k}}} \\
0 & \frac{(s\sin^2{\theta_{k}} + \cos^2{\theta_k})\cos{\theta_{k-1}}-(s\sin^2{\theta_{k-1}} + \cos^2{\theta_{k-1}})\cos{\theta_{k}}}{s\sin^2{\theta_{k}}} & \frac{s\sin^2{\theta_{k}} +\cos^2{\theta_{k}}-\cos{\theta_{k}\cos{\theta_{k-1}}}}{s\sin^2{\theta_{k}}} \end{array} \right]
\left[ \begin{array}{c}
(\theta_{k} - \theta_{k-1})/h\\ 
(\phi_{k} - \phi_{k-1})/h\ \\
(\psi_{k} - \psi_{k-1})/h \\ \end{array} \right] + 
\end{eqnarray}
\[ 
\frac{h}{2s} 
\left[ \begin{array}{c}
(k_\phi^2(2s-2)\cos\theta_k\sin\theta_k - k_\phi k_\psi \sin\theta_k - k_\psi k_\phi\sin\theta_k + M g l \sin{\theta_k)} \\ 
0 \\
0 \\ \end{array} \right]
\]
%\end{eqnarray}

Which can be further simplified as
\[
\begin{split}
\left[ \begin{array}{c}
\frac{\theta_{k+1} - \theta_{k}}{h}\\ 
\frac{\phi_{k+1} - \phi_{k}}{h} \\
\frac{\psi_{k+1} - \psi_{k}}{h} \\ \end{array} \right] = \\ 
\left[ \begin{array}{c}
\frac{\theta_{k} - \theta_{k-1}}{h} \\
\frac{\frac{\phi_{k} - \phi_{k-1}}{h}(s \sin^2\theta_{k-1} + \cos^2\theta_{k-1})+\frac{\psi_{k} - \psi_{k-1}}{h}\cos\theta_{k-1}-\cos\theta_k(\frac{\phi_{k} - \phi_{k-1}}{h}\cos\theta_{k-1}+\frac{\psi_{k} - \psi_{k-1}}{h})}{s\sin^2{\theta_{k}}} \\
\frac{(s \sin^2\theta_{k} + \cos^2\theta_{k})(\frac{\phi_{k} - \phi_{k-1}}{h}\cos\theta_{k-1} + \frac{\psi_{k} - \psi_{k-1}}{h}) - \cos\theta_{k}(\frac{\phi_{k} - \phi_{k-1}}{h}(s\sin^2\theta_{k-1} + \cos^2\theta_{k-1}) + \frac{\psi_{k} - \psi_{k-1}}{h}\cos\theta_{k-1})}{s\sin^2{\theta_{k}}} \\ \end{array} \right] + \\
\frac{h}{2s} 
\left[ \begin{array}{c}
k_\phi^2(2s-2)\cos\theta_k\sin\theta_k - k_\phi k_\psi \sin\theta_k - k_\psi k_\phi\sin\theta_k + M g l \sin{\theta_k} \\ 
0 \\
0 \\ \end{array} \right]
\end{split}
\]

We split this by row to get the following three equations
\[
\begin{split}
\frac{\theta_{k+1} - \theta_{k}}{h} = \frac{\theta_{k} - \theta_{k-1}}{h} + \frac{h}{2s} (k_\phi^2(2s-2)\cos\theta_k\sin\theta_k - k_\phi k_\psi \sin\theta_k - k_\psi k_\phi\sin\theta_k + M g l \sin{\theta_k}) = \\
 \theta_k + \frac{h}{2s} ((\frac{\phi_{k+1} - \phi_{k}}{h})^2(2s-2)\cos\theta_k\sin\theta_k -
2 \frac{\phi_{k+1} - \phi_{k}}{h} \frac{\psi_{k+1} - \psi_{k}}{h} \sin\theta_k + M g l \sin{\theta_k}) \Ra 
\end{split}
\]

\[
\begin{split}
\theta_{k+1} = 2 \theta_k-\theta_{k-1} + \frac{1}{2s}( (\phi_{k+1} - \phi_{k})^2(2s-2)\cos\theta_k\sin\theta_k - 2 (\phi_{k+1} - \phi_{k}) (\psi_{k+1} - \psi_{k}) \sin\theta_k + \\
 M g l \sin{\theta_k} h^2)
\end{split}
\]

\[
\begin{split}
\phi_{k+1} = \frac{\frac{\phi_{k} - \phi_{k-1}}{h}(s \sin^2\theta_{k-1} + \cos^2\theta_{k-1})+\frac{\psi_{k} - \psi_{k-1}}{h}\cos\theta_{k-1}}{s\sin^2{\theta_{k}}} h -\\
\frac{\cos\theta_k(\frac{\phi_{k} - \phi_{k-1}}{h}\cos\theta_{k-1}+\frac{\psi_{k} - \psi_{k-1}}{h})}{s\sin^2{\theta_{k}}} h + \phi_{k} = \\
\frac{(\phi_{k} - \phi_{k-1})(s \sin^2\theta_{k-1} + \cos^2\theta_{k-1})+(\psi_{k} - \psi_{k-1})\cos\theta_{k-1}}{s\sin^2{\theta_{k}}}-\\
\frac{\cos\theta_k((\phi_{k} - \phi_{k-1})\cos\theta_{k-1}+(\psi_{k} - \psi_{k-1}))}{s\sin^2{\theta_{k}}} + \phi_{k}
\end{split}
\]

\[
\begin{split}
\psi_{k+1} = \frac{(s \sin^2\theta_{k} + \cos^2\theta_{k})(\frac{\phi_{k} - \phi_{k-1}}{h}\cos\theta_{k-1} +\frac{\psi_{k} - \psi_{k-1}}{h})}{s\sin^2{\theta_{k}}} h -\\
\frac{\cos\theta_{k}(\frac{\phi_{k} - \phi_{k-1}}{h}(s\sin^2\theta_{k-1} + \cos^2\theta_{k-1}) + \frac{\psi_{k} - \psi_{k-1}}{h}\cos\theta_{k-1})}{s\sin^2{\theta_{k}}} h + \psi_{k} = \\
 \frac{(s \sin^2\theta_{k} + \cos^2\theta_{k})((\phi_{k} - \phi_{k-1})\cos\theta_{k-1} +(\psi_{k} - \psi_{k-1}))}{s\sin^2{\theta_{k}}} -\\
\frac{\cos\theta_{k}((\phi_{k} - \phi_{k-1})(s\sin^2\theta_{k-1} + \cos^2\theta_{k-1}) + (\psi_{k} - \psi_{k-1})\cos\theta_{k-1})}{s\sin^2{\theta_{k}}} + \psi_{k}
\end{split}
\]
\end{document}
