\section{Dudley Integrals}

Given random variables $X_t,\ t\in \Omega$. A most typical example is Guassian process, a time continuous stochastic process $X_t,\ t\in \Omega$ is called Gaussian if and only if every finite set of indices $t_1$, $t_2$, $\cdots , t_k$ in the index set $\Omega$
$$
X_{t_1,\cdots, t_k}=(X_{t_1}, \cdots, X_{t_k})
$$
is a multivariate Gaussian random variables. If $k=1$, for any given $t$, $X_t$ is a random variable  with a Gaussian distribution, and $\mathbb{E}X_t$ is the expectation of this random variable $X_t$.

Dudley's Entropy Integral gives a bound on
$$
\mathbb{E}\left[\sup _{t, s \in \Omega} (X_{t} - X_s)\right].
$$

\begin{lemma}\label{lm:dudley0}
If $\mathbb{E} e^{\lambda Z} \leq e^{\frac{\lambda^{2} \nu}{2}}$, then
\begin{equation}
\mathbb{E} \max_{1\le i \le N} Z_{i} \leq \sqrt{2 \nu \log N}.
\end{equation}
\end{lemma}
\begin{proof}
Since $e^{\lambda z}$ is a convex function of $z$, by Jensan's inequality,
\begin{equation}
\displaystyle
e^{\lambda \mathbb{E}\max_{1\le i \le N} Z_{i}} \leq \mathbb{E} \max_{1\le i \le N}  e^{\lambda Z_{i}} \leq  \sum_{i=1}^{N} \mathbb{E} e^{\lambda Z_{i}} \leqslant N e^{\frac{\lambda^{2} \nu}{2}},\qquad \forall \lambda.
\end{equation}
It follows that
\begin{equation}
\mathbb{E} \max _{1 \leqslant i \leq N} Z_{i} \leq  {\log N\over \lambda}+\frac{\lambda \nu}{2}
\end{equation}
Choose $\lambda=\sqrt{2\log N\over \nu}$,
\begin{equation}
\mathbb{E} \max _{1 \leqslant i \leq N} Z_{i} \leq  \sqrt{2\nu \log N }.
\end{equation}
\end{proof}

\begin{definition} (Covering Number) A $ \delta$ -cover of a set $\mathcal{T}$ w.r.t metric $d$ is a set $\left\{x_{1}, x_{2} \ldots x_{N}\right\} \subseteq \mathcal{T}$ such that for each $x \in \mathcal{T},$ there exists some $i \in\{1,2, \ldots N\}$ such that $d\left(x, x_{i}\right) \leq \delta .$ The $\delta$ -covering number $N(\delta, \mathcal{T})$ is the cardinality of the smallest $\delta$-cover.
\end{definition}

\iffalse
\begin{theorem}\label{th:dudley}
(Dudley's theorem for bounded  $\Omega$)Suppose $\Omega$ is a bounded set. Let $\left\{X_x: x \in \Omega\right\}$ be a collection of random variables such that
$$
\mathbb{E}\left[ e^{\lambda (X_x - X_{x'})}\right]\le e^{\nu\lambda^2 d^2(x, x')\over 2},
$$
with metric d on a finite set $\Omega$.
Then, for any $x_0\in \Omega$,
\begin{equation}\label{eq:dudley}
\mathbb{E}\left[\sup _{x \in \mathcal{T}} X_x-X_{x_0}\right] \leq 12\sqrt{\nu}\int_0^{\delta\over 2} \sqrt{\log N(\mu, \Omega)} d \mu
\end{equation}
is the $\displaystyle \delta=\sup_{x\in \Omega} d(x, x_0)$.
\end{theorem}
\begin{proof}
Given $x_0\in \Omega$, let $\displaystyle \delta=\sup_{x\in \Omega} d(x, x_0)$.
Consider a sequence of triangulation $\mathcal{T}_j$ such that
$$
\sup_{x, x'\in \mathcal{T}_j}d(x, x')\le \delta_j=2^{-j}\delta.
$$
Denote the number of elements in $\mathcal{T}_j$ by $N_j$.
Define a mapping $\Pi_j: \mathcal{T}\rightarrow \mathcal{T}_j$ with
$$
d(x, \Pi_j x)\le \delta_j\quad \forall x\in \Omega.
$$
Since $\Omega$ is finite, there exists a positive integer $J$ such that for all $x\in \Omega$,
$$
\Pi_{J+1}(x)=x.
$$
Thus,
$$
X_x=X_{\Pi_0 x} + \sum_{j=0}^J (X_{\Pi_{j+1}(x)} - X_{\Pi_{j}(x)}).
$$
By definition, $N_0=1$.
For any $x$, let $\Pi_0 (x)=x_0$, and
$$
\mathbb{E}\left [ \sup_{x\in\Omega } X_{\Pi_0 (x)} - X_{x_0}\right]\leq \sum_{j=1}^J \mathbb{E}\left [ \sup_{x\in \Omega} (X_{\Pi_{j+1} (x)} - X_{\Pi_j (x)})\right]
$$
By the triangle inequality, for any $x\in \Omega$,
$$
d(\Pi_j(x), \Pi_{j+1}(x))\le \delta_{j+1} + \delta_{j}\le 3\delta_{j+1}.
$$
Note that for every integer $j$,
$$
\# \{(\Pi_j(x), \Pi_{j+1}(x)): x\in \Omega\} \le N_{j+1}^2.
$$
Since $\left\{X_x: x \in \Omega\right\}$ be a zero-mean sub-Gaussian process, by Lemma \ref{lm:dudley0},
\begin{equation}
\begin{split}
\mathbb{E}\left [ \sup_{x\in \Omega} (X_{\Pi_{j+1} (x)} - X_{\Pi_j (x)})\right]
&\le \sqrt{2 \nu \log N}d(\Pi_j(x), \Pi_{j+1}(x))
\\
&\le 6\delta_{j+1}\sqrt{\nu \log N_{j+1}}.
\end{split}
\end{equation}
Hence, summing over $j$,
\begin{equation}
\begin{split}
\mathbb{E}\left [ \sup_{x\in \Omega} X_x - X_{x_0}\right]
&\le \sum_{j=1}^J \mathbb{E}\left [ \sup_{x\in \Omega} (X_{\Pi_{j+1} (x)} - X_{\Pi_j (x)})\right]
\\
&\le \sum_{j=1}^J 6\delta_{j+1}\sqrt{\nu \log N_{j+1}}
\\
& = 6\sqrt{\nu}\sum_{j=1}^J \delta_{j+1}\sqrt{\log N_{j+1}}.
\end{split}
\end{equation}
Note that
\begin{equation}
N_{j+1}=N(\delta_{j+1}, \Omega)\le N(\delta, \Omega) \le N(\delta_{j+2}, \Omega)=N_{j+2},\quad \delta_{j+2}\le \delta \le \delta_{j+1}.
\end{equation}
Thus,
\begin{equation}
\delta_{j+1}\sqrt{\log N_{j+1}}\le 2\int^{\delta_{j+1}}_{\delta_{j+2}} \sqrt{ \log N(\delta, \Omega) }d\delta.
\end{equation}
Consequently,
\begin{equation}
\begin{split}
\mathbb{E}\left [ \sup_{x\in \Omega} X_x - X_{x_0}\right]
&\le 12\sqrt{\nu} \int^{\delta\over 2}_{\delta_{J+1}}\sqrt{\log N(\mu, \Omega) }d\mu
\\
&\le 12\sqrt{\nu} \int^{\delta\over 2}_{0}\sqrt{\log N(\mu, \Omega) }d\mu.
\end{split}
\end{equation}
\end{proof}
\begin{remark}
Dudley's Entropy Integral \eqref{eq:dudley} still holds for an infinite set $\mathcal{T}$.
\end{remark}

\fi
\begin{theorem}\label{th:dudley}
(Dudley's theorem) Suppose $\Omega$ is a bounded set. Let $\left\{X_x: x \in \Omega\right\}$ be a collection of random variables such that
$$
\mathbb{E}\left[ e^{\lambda (X_x - X_{x'})}\right]\le e^{\nu\lambda^2 d^2(x, x')\over 2},
$$
with metric $d$ on$\Omega$.
Then, for any $x_0\in \Omega$,
\begin{equation}\label{eq:dudley}
\mathbb{E}\left[\sup _{x \in \Omega} X_x-X_{x_0}\right] \leq 12\sqrt{\nu}\int_0^{\delta\over 2} \sqrt{\log N(\mu, \Omega)} d \mu
\end{equation}
is the $\displaystyle \delta=\sup_{x\in \Omega} d(x, x_0)$.
\end{theorem}
\begin{proof}
Let $\hat{\Omega}=\{\hat x_1, \cdots, \hat x_N\}$ be a minimal $\epsilon$-covering of $\Omega$ and N be the covering number. There exist $\hat x, \hat x_0\in \hat{\Omega}$ such that
$$
d(x, \hat x)\le \epsilon,\quad d(\hat x, \hat x_0)\le \epsilon.
$$
Thus,
\begin{equation}
X_x - X_{x_0}\le 2\sup_{d(y,y')\le \epsilon} (X_y - X_{y'}) + \sup_{\hat x\in \hat{\Omega}} (X_{\hat x} - X_{\hat x_0}).
\end{equation}
Let $\displaystyle \hat \delta=\sup_{\hat x\in \hat \Omega} d(\hat x, \hat x_0)$.
Consider a sequence of triangulation $\hat{\mathcal{T}}_j$ such that
$$
\sup_{\hat x, \hat x'\in\hat{\mathcal{T}}_j}d(\hat x, \hat x')\le \hat \delta_j=2^{-j}\hat \delta.
$$
Denote the number of elements in $\hat{\mathcal{T}}_j$ by $N_j$.
Define a mapping $\Pi_j: \hat{\mathcal{T}}\rightarrow \hat{\mathcal{T}}_j$ with
$$
d(\hat x, \Pi_j \hat x)\le \hat \delta_j\quad \forall \hat x\in \hat \Omega.
$$
Since $\hat \Omega$ is finite, there exists a positive integer $J$ such that for all $\hat x\in \hat \Omega$,
$$
\Pi_{J+1}(\hat x)=\hat x.
$$
Thus,
$$
X_{\hat{x}}=X_{\Pi_0 \hat{x}} + \sum_{j=0}^J (X_{\Pi_{j+1}(\hat{x})} - X_{\Pi_{j}(\hat{x})}).
$$
By definition, $N_0=1$.
For any $\hat x$, let $\Pi_0 (\hat{x})=\hat{x}_0$, and
$$
\mathbb{E}\left [ \sup_{\hat{x}\in \hat  \Omega } X_{\Pi_0 (\hat{x})} - X_{\hat{x}_0}\right]\leq \sum_{j=1}^J \mathbb{E}\left [ \sup_{\hat{x}\in \hat \Omega} (X_{\Pi_{j+1} (\hat x)} - X_{\Pi_j (\hat x)})\right]
$$
By the triangle inequality, for any $\hat x\in \hat \Omega$,
$$
d(\Pi_j(\hat x), \Pi_{j+1}(\hat x))\le \hat \delta_{j+1} + \hat \delta_{j}\le 3\hat \delta_{j+1}.
$$
Note that for every integer $j$,
$$
\# \{(\Pi_j(\hat x), \Pi_{j+1}(\hat x)): \hat x\in \hat \Omega\} \le N_{j+1}^2.
$$
Since $\left\{X_{\hat x}: \hat x \in \hat \Omega\right\}$ be a zero-mean sub-Gaussian process, by Lemma \ref{lm:dudley0},
\begin{equation}
\begin{split}
\mathbb{E}\left [ \sup_{\hat x\in \hat \Omega} (X_{\Pi_{j+1} (\hat x)} - X_{\Pi_j (\hat x)})\right]
&\le \sqrt{2 \nu \log N}d(\Pi_j(\hat x), \Pi_{j+1}(\hat x))
\\
&\le 6\delta_{j+1}\sqrt{\nu \log N_{j+1}}.
\end{split}
\end{equation}
Hence, summing over $j$,
\begin{equation}
\begin{split}
\mathbb{E}\left [ \sup_{\hat x\in \hat \Omega} X_{\hat x} - X_{\hat x_0}\right]
&\le \sum_{j=1}^J \mathbb{E}\left [ \sup_{\hat x\in\hat  \Omega} (X_{\Pi_{j+1} (\hat x)} - X_{\Pi_j (\hat x)})\right]
\\
&\le \sum_{j=1}^J 6\hat \delta_{j+1}\sqrt{\nu \log N_{j+1}}
\\
& = 6\sqrt{\nu}\sum_{j=1}^J \hat \delta_{j+1}\sqrt{\log N_{j+1}}.
\end{split}
\end{equation}
Note that
\begin{equation}
N_{j+1}=N(\hat \delta_{j+1}, \hat \Omega)\le N(\hat \delta, \hat \Omega) \le N(\hat{\delta}_{j+2}, \hat \Omega)=N_{j+2},\quad \hat \delta_{j+2}\le \hat \delta \le \delta_{j+1}.
\end{equation}
Thus,
\begin{equation}
\hat \delta_{j+1}\sqrt{\log N_{j+1}}\le 2\int^{\hat \delta_{j+1}}_{\hat \delta_{j+2}} \sqrt{ \log N(\hat \delta, \hat \Omega) }d\delta.
\end{equation}
Consequently,
\begin{equation}
\begin{split}
\mathbb{E}\left [ \sup_{\hat x\in \hat \Omega} X_{\hat x} - X_{\hat x_0}\right]
&\le 12\sqrt{\nu} \int^{\hat \delta\over 2}_{\hat \delta_{J+1}}\sqrt{\log N(\mu, \hat \Omega) }d\mu
\\
&\le 12\sqrt{\nu} \int^{\hat \delta\over 2}_{0}\sqrt{\log N(\mu, \hat \Omega) }d\mu
\\
&\leq 12\sqrt{\nu}\int_{0}^{\delta\over 2} \sqrt{\log N(\mu, \Omega)} d \mu
\end{split}
\end{equation}
with   $\displaystyle \delta=\sup_{x\in  \Omega} d(x, x_0)$.
Thus,
\begin{equation}
\mathbb{E}\sup_{x\in \Omega} (X_{x} - X_{x_0})
\leq 2\mathbb{E}\sup_{d(y,y')\le \epsilon} (X_y - X_{y'}) + 12\sqrt{\nu}\int_{0}^{\delta\over 2} \sqrt{\log N(\mu,\Omega)} d \mu
\end{equation}
Let $\epsilon\rightarrow 0$,
\begin{equation}
\mathbb{E}\sup_{x\in\Omega} (X_{x} - X_{x_0})
\leq 12\sqrt{\nu}\int_{0}^{\delta\over 2} \sqrt{\log N(\mu,\Omega)} d \mu
\end{equation}
\end{proof}

\begin{lemma}\label{logintegral}
\begin{align}
\int_0^t\sqrt{\log {1\over u}} du\le  \sqrt{t(1-\log t)}.
\end{align}
\end{lemma}
\begin{proof}
Since $\sqrt{x}$ is a concave function,   by Jansen's inequality,
\begin{align}
\int_0^t\sqrt{\log {1\over u}} du&\le \sqrt{-\int_0^t \log u du} = \sqrt{-t\log t + t}=\sqrt{t(1-\log t)}.
\end{align}
\end{proof}

Define Rademacher variable $\sigma$ by
\begin{equation}
P(\sigma)=\left\{\begin{array}{cc}
1 / 2 & \text { if } \sigma=-1 \\
1 / 2 & \text { if } \sigma=+1
\end{array}\right.
\end{equation}

\begin{lemma}\label{th:rademacherprocess}
Let $\sigma_{i}$ be a sequence of independent identically distributed Rademacher variables.
\[
\begin{aligned}
\mathbb{E}_n^{X}\left[\sup _{x \in \Omega} \big | \frac{1}{n} \sum_{i=1}^{n} f\left(X_{i}, x\right)-\mathbb{E}[f]\big | \right]
& \leq \mathbb{E}_n^{X, Y, \sigma}\left[\sup _{x \in \Omega} \mid \frac{1}{n} \sum_{i=1}^{n} \sigma_{i}\left[f\left(X_{i}, x\right)-f\left(Y_{i}, x\right)\right]\big |\right].
\end{aligned}
\]
\end{lemma}
\begin{proof}
\begin{equation}
\begin{aligned}
\mathbb{E}_n^{X}\left[\sup _{x \in \Omega} \big | \frac{1}{n} \sum_{i=1}^{n} f\left(X_{i}, x\right)-\mathbb{E}[f]\big | \right]
&=\mathbb{E}_n^{X}\left[\sup _{x \in \Omega} \big |\frac{1}{n} \sum_{i=1}^{n} f\left(X_{i}, x\right)-\mathbb{E}_{Y_{i}} f\left(Y_{i}, x\right)\big |\right]
\\
&=\mathbb{E}_n^{X}\left[\sup _{x \in \Omega} \big | \mathbb{E}_n^{Y} \frac{1}{n} \sum_{i=1}^{n}\left[f\left(X_{i}, x\right)-f\left(Y_{i}, x\right)\right]\big | \right]
\\
& \leq \mathbb{E}_n^{X}\left[\sup _{x \in \Omega} \mathbb{E}_n^{Y}\big |\frac{1}{n} \sum_{i=1}^{n}\left[f\left(X_{i}, x\right)-f\left(Y_{i}, x\right)\right]\big |\right]
\\
& \leq \mathbb{E}_n^{X, Y}\left[\sup _{x \in \Omega} \big | \frac{1}{n} \sum_{i=1}^{n}\left[f\left(X_{i}, x\right)-f\left(Y_{i}, x\right)\right]\big |\right]
\end{aligned}
\end{equation}
Consider the distribution of $f(X_{i}, x)-f(Y_{i}, x)$ and $\sigma_{i}(f(X_{i}, x)-f(Y_{i}, x))$. Define
$$
U_k=\{(X, Y): f(X,x)-f(Y,x)=k\}.
$$
For $f(X_{i}, x)-f(Y_{i}, x)$,
\begin{equation}
P(f(X_{i}, x)-f(Y_{i}, x)=k)=\sum_{(a, b)\in U_k} p(X_i=a)p(Y_i=b).
\end{equation}
For $\sigma_{i}(f(X_{i}, x)-f(Y_{i}, x))$,
\begin{align}
P(\sigma_{i}(f(X_{i}, x)-f(Y_{i}, x))=k)=&\sum_{(a, b)\in U_k} p(\sigma_i=1)p(X_i=a)p(Y_i=b) \\
&+ \sum_{(a, b)\in U_k} p(\sigma_i=-1)p(X_i=b)p(Y_i=a)\\
=&\sum_{(a, b)\in U_k} p(X_i=a)p(Y_i=b).
\end{align}
This implies that the distribution of the difference $f\left(X_{i}, x\right)-f\left(Y_{i}, x\right)$ is the same as the distribution of$\sigma_{i}(f(X_{i}, x)-f(Y_{i}, x))$ so we obtain
\[
\begin{aligned}
\mathbb{E}_n^{X}\left[\sup _{x \in \Omega} \big | \frac{1}{n} \sum_{i=1}^{n} f\left(X_{i}, x\right)-\mathbb{E}[f]\big |\right]
& \leq \mathbb{E}_n^{X, Y, \sigma}\left[\sup _{x \in \Omega} \big | \frac{1}{n} \sum_{i=1}^{n} \sigma_{i}\left[f\left(X_{i}, x\right)-f\left(Y_{i}, x\right)\right]\big |\right].
\end{aligned}
\]

\end{proof}

\begin{lemma}\label{lm:rademacher}
Suppose $\theta$ is a random variable and $\{\theta_i\}_{i=1}^n$ are $n$ samples of $\theta$. Let $\sigma_{i}$ be a sequence of independent identically distributed Rademacher variables. For $h(\theta, x)$, define
$$
X_x = \sum_{i=1}^n \sigma_ih(\theta_i, x),
$$
it holds that
$$
\mathbb{E}_\sigma\left[ e^{\lambda (X_x - X_{x'})}\right]\le e^{\lambda^2 d^2(x, x')\over 2},
$$
with $\displaystyle d^2(x, x') = \sum_{i=1}^n(h(\theta_i, x) - h(\theta_i, x'))^2$.
\end{lemma}
\begin{proof}
For any $z$,
$$
{e^{z} + e^{-z}\over 2}=\sum_{i\ge 0} {z^{2i}\over (2i)!} \le \sum_{i\ge 0} {z^{2i}\over 2^i i!} = e^{z^2\over 2}.
$$
Let $z_i= \lambda  (h(\theta_i, x) - h(\theta_i, x'))$.
\begin{align}
\displaystyle
\mathbb{E}_\sigma\left[ e^{\lambda (X_x - X_{x'})}\right]&=\mathbb{E}_\sigma\left[ e^{\lambda \sum_{i=1}^n \sigma_i(h(\theta_i, x) - h(\theta_i, x'))}\right]
\\
&=\prod_{i=1}^n \int e^{\lambda \sigma_i(h(\theta_i, x) - h(\theta_i, x'))} P(\sigma_i) d\sigma_i
\\
&=\prod_{i=1}^n  {e^{z_i} + e^{-z_i}\over 2}  \le \prod_{i=1}^n  e^{z_i^2\over 2}
\\
&= e^{{1\over 2}\lambda^2  \sum_{i=1}^n(h(\theta_i, x) - h(\theta_i, x'))^2}.
\end{align}
\end{proof}
\newpage
\begin{remark}\label{remark:supest}
It holds that
\begin{align}
\mathbb{E}_n^{X}\left[\sup _{x \in \Omega} \mid \frac{1}{n} \sum_{i=1}^{n} f\left(X_{i}, x\right)-\mathbb{E}[f]\right]  \leq {12\over n}\int_0^{\delta\over 2} \sqrt{\log N(\mu, \Omega)} d \mu
\end{align}
with $\displaystyle \delta^2=\sup_{x\in \Omega} d^2(x, x_0) = \sup_{x\in \Omega}\sum_{i=1}^n(f(X_i, x) - f(Y_i, x))^2$.
 \end{remark}
\begin{enumerate}
\item By Lemma \ref{th:rademacherprocess},
\[
\begin{aligned}
\mathbb{E}_n^{X}\left[\sup _{x \in \Omega} \mid \frac{1}{n} \sum_{i=1}^{n} f\left(X_{i}, x\right)-\mathbb{E}[f]\right]
& \leq \mathbb{E}_n^{X, Y, \sigma}\left[\sup _{x \in \Omega} \mid \frac{1}{n} \sum_{i=1}^{n} \sigma_{i}\left[f\left(X_{i}, x\right)-f\left(Y_{i}, x\right)\right]\right].
\end{aligned}
\]
with a sequence of independent identically distributed Rademacher variables $\sigma_{i}$.
\item Let $$
X_x = \sum_{i=1}^n \sigma_i\left[f\left(X_{i}, x\right)-f\left(Y_{i}, x\right)\right],
$$
By Lemma \ref{lm:rademacher}, it holds that
$$
\mathbb{E}_\sigma\left[ e^{\lambda (X_x - X_{x'})}\right]\le e^{\lambda^2 d^2(x, x')\over 2},
$$
with $\displaystyle d^2(x, x') = \sum_{i=1}^n(h(X_i, Y_i, x) - h(X_i, Y_i, x'))^2$ and $h(X_i, Y_i, x) = f\left(X_{i}, x\right)-f\left(Y_{i}, x\right)$, which satisfies the assumption in Theorem \ref{th:dudley}.
\item Let $\displaystyle \delta=\sup_{x\in \Omega} d(x, x_0)$. By Theorem \ref{th:dudley},  for any $x_0\in \mathcal{T}$,
\begin{equation}
\mathbb{E}\left[\sup _{x \in \Omega} X_x-X_{x_0}\right] \leq {12\over n}\int_0^{\delta\over 2} \sqrt{\log N(\mu, \Omega)} d \mu,
\end{equation}
 furthermore,
\begin{equation}
\mathbb{E}_n^{X}\left[\sup _{x \in \Omega} \mid \frac{1}{n} \sum_{i=1}^{n} f\left(X_{i}, x\right)-\mathbb{E}[f]\right]  \leq {12\over n}\int_0^{\delta\over 2} \sqrt{\log N(\mu, \Omega)} d \mu,
\end{equation}
\end{enumerate}

\section{Maximum norm error estimates ReLU DNN}
\begin{lemma}\label{lm:Cstratifyrange1}
Suppose $\Omega$ is bounded, say $\|x\|_{\ell_p}\le T$, and $f\in \mathcal B^{m+1, q}(\Omega)$.
%$$
% \int_{\mathbb{R}^{d}} |\hat{f}(\theta)|\|\theta\|_{\ell_1}^{m+1} d\theta<\infty.
%$$
There exist $\theta_j=(z_j, t_j, \omega_j)$ with $\|\bar \omega_j\|_{\ell_1}=1$, $t\in [0,T]$,  $\beta_j\in [-1,1]$ such that
$$
f_n(x)= \sum_{|\alpha|\le m}{1\over \alpha!}D^\alpha f(0) x^\alpha + {2\nu\over m!n}\sum_{j=1}^{n}\beta_j (\bar \theta_j\cdot x - t_j)_+^m
$$
with $\nu=\int_{\{-1,1\}\times [0,T]\times \mathbb{R}^{d}} \rho(\theta)d\theta$ and $\rho(\theta)$  defined in \eqref{eq:straglam}
satisfies the following estimate
\begin{equation}
\|f - f_n \|_{C(\Omega)} \leq C\sqrt{d} n^{-{1\over 2}-{1\over d}}\|f\|_{\mathcal B^{m+1, q}(\Omega)}.
\end{equation}
\end{lemma}
\begin{proof}
According to Lemma \ref{lem:stratifiedapprox}, there exists a partition $G=G_1\cup \cdots \cup G_M$ such that
$$
|t -t'| + \|\bar\omega - \bar\omega'\|_{\ell_1}\leq \epsilon \sim M^{-{1\over d}},\quad \forall 1\le i\le M.
$$
with $\theta=(z, t, \omega)\in G$. By \eqref{eq:straglam},
$$
g(\theta, x)= (z\bar \omega\cdot x - t)_+^m {\rm sgn} s(zt,\omega),\quad s(zt,\omega)=
\begin{cases}
(-1)^{m+1\over 2}\cos(z\|\omega\|_{\ell_1}t + b(\omega)) & m \text{ is odd}
\\
(-1)^{m+2\over 2}\sin(z\|\omega\|_{\ell_1}t + b(\omega)) & m \text{ is even}.
\end{cases}
$$
Thus,
$$
|g(\theta,x) - g(\theta',x)|\le c_0\epsilon,\quad \theta, \theta'\in G_i.
$$
Let $n_i=\lceil \lambda(G_i) n\rceil$, $\displaystyle g_{n_i}^i={1\over n_i}\sum_{j=1}^{n_i}g(\theta_{i, j}, x)$ and
$$
g_n(x) = \sum_{i=1}^M \lambda(G_i)g_{n_i}^i
$$
It follows that
\begin{align}
|\mathbb{E}_G g -g_n(x)|
&= |\sum_{i=1}^M \lambda(G_i)(\mathbb{E}_{G_i} g - g_{n_i}^i)|
\\
&\le {1\over n}\sum_{i=1}^M \sum_{j=1}^{n_i}|\mathbb{E}_{G_i} g - g(\theta_{i, j},x)|.
\end{align}
Let $\sigma_{i,j}$ be a sequence of independent identically distributed Rademacher variables
\begin{equation}
P(\sigma_{i, j}=k)=\left\{\begin{array}{cc}
1 / 2 & \text { if } k=-1 \\
1 / 2 & \text { if } k=+1 \\
0 & \text { otherwise }
\end{array}.
\right.
\end{equation}
By Lemma \ref{th:rademacherprocess},
\begin{align}
\mathbb{E}_n\sup_x |\mathbb{E}_{G} g(x) - g_{n}(x)|\le &
{1\over n}\mathbb{E}_n\sup_x  |\sum_{i=1}^M\sum_{j=1}^{n_i} (\mathbb{E}_{G_i} g  - g(\theta_{ij}, x))|
\end{align}
According to Remark \ref{remark:supest},
\begin{align}
\mathbb{E}_n\sup_x |\mathbb{E}_{G} g(x) - g_{n}(x)|
  \leq {24\over n }\int_0^{\delta\over 2} \sqrt{\log N(\mu, \mathcal{T})} d \mu
\end{align}
with
$$
\displaystyle \delta^2= \sup_{x\in \mathcal{T}}\sum_{i=1}^M\sum_{j=1}^{n_i}(g(\theta_{ij}, x) - g(\theta_{ij}, x))^2 \le c^2\epsilon^2 \sum_{i=1}^M n_i
$$
Let $h_{i, j}(x)= g(\theta_{ij}, x) - g(\theta_{ij}', x)$. Note that
\begin{align}
d^2(x, x') = \sum_{i=1}^M\sum_{j=1}^{n_i}(h_{i, j}(x) - h_{i, j}(x'))^2
 \le c^2\epsilon^2  \|x -x'\|_\infty^2\sum_{i=1}^M n_i.
\end{align}
it follows that
\begin{align} % requires amsmath; align* for no eq. number
N(\mu, \Omega)\le \left ({2\over \|x -x'\|_\infty}\right)^d
\le \left ({2c\epsilon\sqrt{\sum_{i=1}^M n_i}\over \mu}\right)^d.
\end{align}
By Lemma \ref{logintegral},
\begin{align} % requires amsmath; align* for no eq. number
\int_0^{\delta\over 2} \sqrt{\log N(\mu, \Omega)} d \mu
&\le \sqrt{d}\int_0^{c\epsilon\sqrt{\sum_{i=1}^M n_i}\over 2} \sqrt{\log {2c\epsilon\sqrt{\sum_{i=1}^M n_i}\over \mu}} d \mu
\\
& \le C\epsilon\sqrt{d\sum_{i=1}^M n_i} .
\end{align}
Thus,
\begin{align}
\mathbb{E}_n\sup_x |\mathbb{E}_{G} g(x) - g_{n}(x)|
\le C\epsilon\sqrt{d\sum_{i=1}^M n_i\over n^2}
\end{align}
Since $\epsilon=n^{-1/d}$ and $\displaystyle M\le 4({5\over \epsilon})^d$,
\begin{align}
\mathbb{E}_n\sup_x |\mathbb{E}_{G} g(x) - g_{n}(x)|
\le C\sqrt{d} n^{-{1\over 2}- {1\over d}}.
\end{align}
Consequently, there exists $g_n(x)$ such that
$$
\sup_x |\mathbb{E}_{G} g(x) - g_{n}(x)|\le C\sqrt{d} n^{-{1\over 2}- {1\over d}}.
$$
Namely, there exist $\beta_j\in [-1, 1]$, $\|\bar \omega_j\|_{\ell_1}=1$, $t_i\in [0,T]$ such that
$$
f_n(x)= \sum_{|\alpha|\le m}{1\over \alpha!}D^\alpha f(0) x^\alpha + {2\nu\over m!n}\sum_{j=1}^{n}\beta_j (\bar \omega_j\cdot x - t_j)_+^m
$$
satisfies the following estimate
\begin{equation}
\sup_x |f - f_n| \leq C\sqrt{d} n^{-{1\over 2}-{1\over d}}\|f\|_{\mathcal B^{m+1, q}(\Omega)},\qquad k\le m.
\end{equation}
%
%
%
%------
%
%\begin{align}
%\mathbb{E}_n\sup_x |\mathbb{E}_{G} g(x) - g_{n}(x)|\le &
%{1\over n}\mathbb{E}_n\sup_x  |\sum_{i=1}^M\sum_{j=1}^{n_i} (\mathbb{E}_{G_i} g  - g(\theta_{ij}, x))|
%\\
%\le &{2\over n}\mathbb{E}_n^{\theta_{ij}, \bar{\theta}_{ij}, \sigma_{ij}}\sup_x  |\sum_{i=1}^M\sum_{j=1}^{n_i} \sigma_{i,j} (g(\theta_{ij}, x)  - g(\bar\theta_{ij}, x))|
%\\
%\le &{2\over n}\mathbb{E}_n^{\theta_{ij}, \bar{\theta}_{ij}, \sigma_{ij}}\sup_x  |\sum_{i=1}^M\sum_{j=1}^{n_i} \sigma_{i,j} h_{i, j}(x)|
%\end{align}
%with $h_{i, j}(x) =  g(\theta_{i, j},x) - g(\bar\theta_{i, j}, x)$.
%Let $\displaystyle X_x = \sum_{i=1}^M\sum_{j=1}^{n_i} \sigma_{i,j} h_{i, j}(x)$ and
%\begin{align}
%d^2(x, x') = \sum_{i=1}^M\sum_{j=1}^{n_i}(h_{i, j}(x) - h_{i, j}(x'))^2
% \le c^2\epsilon^2 (M+n)\|x -x'\|_\infty^2.
%\end{align}
%It follows from Lemma \ref{lm:rademacher} that
%\begin{equation}
%\mathbb{E}_n^{\sigma_{ij}}\left [ e^{\lambda (X_x - X_{x'})}\right]\le e^{\lambda^2d^2(x,x')\over 2},
%\end{equation}
%which satisfies the assumption in Theorem \ref{th:dudley}.
%\begin{equation}
%\mathbb{E}_n^{\sigma_{ij}}\sup_x  |\sum_{i=1}^M\sum_{j=1}^{n_i} \sigma_{i,j} h_{i, j}(x)|
%=\mathbb{E}_n^{\sigma_{ij}}\sup_x X_x
%\le 12 \int_0^{\delta\over 2} \sqrt{\log N(\mu, \mathcal{T})} d \mu
%\end{equation}
%with  $
%\displaystyle \delta^2=\sup_{x\in \mathcal{T}} \sum_{i=1}^M\sum_{j=1}^{n_i}h_{i, j}^2(x) \le c^2\epsilon^2 (M+n).
%$
%%for $\theta_{i, j}, \theta_{i, j}'\in G_i$,
%%$$
%%|s_{i, j}(x)|\le m\epsilon, \quad \forall x.
%%$$
%%Since $|g(\theta, x) - g(\theta, x')\le L|x-x'|$ for any $\theta$,
%%$$
%%|h_{i, j}(x) - h_{i, j}(x')|\le \sup_x |\partial_xh_{i,j}||x-x'|\le c\epsilon \|x-x'\|_\infty.
%%$$
%%By , $\{h_{i,j}(x)\}$ satisfy the assumptions in Theorem \ref{th:dudley}, which leads to
%%\begin{align}
%%\mathbb{E}_n\sup_x |\mathbb{E}_{G} g(x) - g_{n}(x)|\le &
%%{1\over n}\mathbb{E}_n\sup_x  |\sum_{i=1}^M\sum_{j=1}^{n_i} (\mathbb{E}_{G_i} g  - g(\theta_{ij}, x))|
%%\\
%%\le &{2\over n}\mathbb{E}_n\sup_x  |\sum_{i=1}^M\sum_{j=1}^{n_i} \sigma_{i,j} (g(\theta_{ij}, x)  - g(\bar\theta_{ij}, x))|
%%\\
%%\le &{2\over n}\mathbb{E}_n\sup_x  |\sum_{i=1}^M\sum_{j=1}^{n_i}   h_{i,j}(x)|.
%%\\
%%\leq & {12\over n} \int_0^{\delta\over 2} \sqrt{\log N(\mu, \mathcal{T})} d \mu
%%\end{align}
%%and
%%\begin{align}
%%d^2(x, x') = \sum_{i=1}^M\sum_{j=1}^{n_i}(h_{i, j}(x) - h_{i, j}(x'))^2
%% \le c^2\epsilon^2 (M+n)\|x -x'\|_\infty^2.
%%\end{align}
%Thus,
%\begin{align} % requires amsmath; align* for no eq. number
%N(\mu, \mathcal{T})\le \left ({2\over \|x -x'\|_\infty}\right)^d
%\le \left ({2c\epsilon\sqrt{M+n}\over \mu}\right)^d.
%\end{align}
%By Lemma \ref{logintegral},
%\begin{align} % requires amsmath; align* for no eq. number
%\int_0^{\delta\over 2} \sqrt{\log N(\mu, \mathcal{T})} d \mu
%&\le \sqrt{d}\int_0^{c\epsilon\sqrt{M+n}\over 2} \sqrt{\log {2c\epsilon\sqrt{M+n}\over \mu}} d \mu
%\\
%& \le C\epsilon\sqrt{(M+n)d} .
%\end{align}
%Thus,
%\begin{align}
%\mathbb{E}_n\sup_x |\mathbb{E}_{G} g(x) - g_{n}(x)|
%\le C\epsilon\sqrt{d(M+n)\over n^2}
%\end{align}
%Since $\epsilon=n^{-1/d}$ and $M\le 4({5\over \epsilon})^d$,
%\begin{align}
%\mathbb{E}_n\sup_x |\mathbb{E}_{G} g(x) - g_{n}(x)|
%\le C\sqrt{d} n^{-{1\over 2}- {1\over d}}.
%\end{align}
%Consequently, there exists $g_n(x)$ such that
%$$
%\sup_x |\mathbb{E}_{G} g(x) - g_{n}(x)|\le C\sqrt{d} n^{-{1\over 2}- {1\over d}}.
%$$
%Namely, there exist $\beta_j\in [-2^d,2^d]$, $\|\bar \theta_j\|_{\ell_1}=1$, $t_i\in [0,T]$ such that
%$$
%f_n(x)= \sum_{|\alpha|\le m}{1\over \alpha!}D^\alpha f(0) x^\alpha + {1\over m!n}\sum_{j=1}^{n}\beta_j (\bar \theta_j\cdot x - t_j)_+^m
%$$
%satisfies the following estimate
%\begin{equation}
%\sup_x |f - f_n| \leq C\sqrt{d} n^{-{1\over 2}-{1\over d}},\qquad k\le m.
%\end{equation}
\end{proof}

\begin{lemma}\label{lm:Cstratifypm1}
Suppose $\Omega$ is bounded, say $\|x\|_{\ell_p}\le T$, and $f\in \mathcal B^{m+1, q}(\Omega)$.
There exist $\theta_j=(z_j, t_j, \omega_j)$ with $\|\bar \omega_j\|_{\ell_1}=1$, $t\in [0,T]$,  $\beta_j\in \{-1,1\}$ such that
$$
f_n(x)= \sum_{|\alpha|\le m}{1\over \alpha!}D^\alpha f(0) x^\alpha + {\nu\over m!n}\sum_{j=1}^{n}\beta_j (\bar \theta_j\cdot x - t_j)_+^m
$$
with $\nu=\int_{\{-1,1\}\times [0,T]\times \mathbb{R}^{d}} \rho(\theta)d\theta$ and $\rho(\theta)$  defined in \eqref{eq:straglam}
satisfies the following estimate
\begin{equation}
\|f - f_n \|_{C(\Omega)} \leq C{\sqrt{d} \over m!}N^{-{1\over 2}-{1\over d+2}}\|f\|_{\mathcal B^{m+1, q}(\Omega)}.
\end{equation}
\end{lemma}
\begin{proof}
According to Lemma \ref{lem:stratifiedapprox}, there exists a partition $G=G_1\cup \cdots \cup G_M$ such that
$$
|t -t'| + \|\bar\omega - \bar\omega'\|_{\ell_1}\leq \epsilon \sim  M^{-{1\over d}},\quad \forall 1\le i\le M.
$$
with $\theta=(z, t, \omega)\in G$. By \eqref{eq:straglam},
$$
g(\theta, x)= (z\bar \omega\cdot x - t)_+^m {\rm sgn} s(zt,\omega),\quad s(zt,\omega)=
\begin{cases}
(-1)^{m+1\over 2}\cos(z\|\omega\|_{\ell_1}t + b(\omega)) & m \text{ is odd}
\\
(-1)^{m+2\over 2}\sin(z\|\omega\|_{\ell_1}t + b(\omega)) & m \text{ is even}.
\end{cases}
$$
Thus,
$$
|g(\theta,x) - g(\theta',x)|\le c_0\epsilon,\quad \theta, \theta'\in G_i.
$$
Let  $\theta_{i,j} \in G_i(1\leq j\leq n_i)$,  $n_i$ equal $\lceil \lambda(G_i)n\rceil$ and $\lfloor \lambda(G_i)n\rfloor$ with probabilities chosen to make its mean equal to $\lambda(G_i)n$ and $m_i=n_i + \mathbb{I}(n_i=0)$. Then
\begin{align}
\sum_{i=1}^M m_i&=\sum_{i=1}^M n_i\mathbb{I}(n_i>0) + \sum_{i=1}^M n_i\mathbb{I}(n_i=0)
\\
&\le \sum_{i=1}^M (n\lambda(G_i) + 1)\mathbb{I}(n_i>0) + \sum_{i=1}^M \mathbb{I}(n_i=0)
\\
&\le  n\sum_{i=1}^M \lambda(G_i)\mathbb{I}(n_i>0) + M
\le n+M.
\end{align}
Define $\displaystyle N=\sum_{i=1}^Mn_i$  and
$$
r_{m,n}(x)= {1\over N}\sum_{i=1}^M {n_i\over m_i} \sum_{j=1}^{m_i} g(x,\theta_{i,j}).
$$
By the definition of $m_i$, $\displaystyle {n_i\over m_i}=0$ or $1$.  This means that $f_n(x) $ is in the form of  $\displaystyle {1\over N}\sum_{i=1}^N\beta_i g(x,\theta_i)$ with $\beta_i\in \{-1, 1\}$. Define
$$
\bar{r}_{m,n}(x)= \sum_{i=1}^M {n_i\over N}\mathbb{E}_{G_i}g.
$$
Since
$
\displaystyle r_m(x)=\mathbb{E}_G g= \sum_{i=1}^M \lambda(G_i) \mathbb{E}_{G_i}g,
$
\begin{align}
r_{m,n} - r_m  &= r_{m,n} - \bar{r}_{m,n} + \bar{r}_{m,n} - f_m
\\
&= {1\over N}\sum_{i=1}^M {n_i\over m_i} \sum_{j=1}^{m_i} \big (g(x,\theta_{i,j}) - \mathbb{E}_{G_i}g \big ) + {1\over N}\sum_{i=1}^M (n_i - \lambda(G_i)N)  \mathbb{E}_{G_i}g.\label{f1}
\end{align}
Consider the first term in \eqref{f1}. By Remark \eqref{remark:supest},
\begin{align}
\mathbb{E}_n \left[ \sup_{x\in \Omega}{1\over N}\sum_{i=1}^M {n_i\over m_i} \sum_{j=1}^{m_i} \big (g(x,\theta_{i,j}) - \mathbb{E}_{G_i}g \big )\right] \le {12\over N}\int_0^{\delta_1} \sqrt{\log N_1(\mu, \Omega)}d\mu
\end{align}
with $\displaystyle \delta_1^2=\sup_{x\in \Omega} \sum_{i=1}^M {n_i\over m_i} \sum_{j=1}^{m_i} \big (g(x,\theta_{i,j}) - g(x,\theta_{i,j}')\big )^2\le Nc_0^2\epsilon^2$. Let $h_{i, j}(x)= g(\theta_{ij}, x) - g(\theta_{ij}', x)$. Note that
\begin{align}
d^2(x, x') = \sum_{i=1}^M {n_i\over m_i} \sum_{j=1}^{m_i}(h_{i, j}(x) - h_{i, j}(x'))^2
 \le c^2N\epsilon^2  \|x -x'\|_\infty^2.
\end{align}
it follows that
\begin{align} % requires amsmath; align* for no eq. number
N_1(\mu, \Omega) \le \left ( {2\over \|x -x'\|_\infty} \right)^d
\le \left ( {2c\epsilon\sqrt{N} \over \mu}\right)^d.
\end{align}
By Lemma \ref{logintegral},
\begin{align}
\mathbb{E}_n \left[ \sup_{x\in \Omega}{1\over N}\sum_{i=1}^M {n_i\over m_i} \sum_{j=1}^{m_i} \big (g(x,\theta_{i,j}) - \mathbb{E}_{G_i}g \big )\right] \le {12\over N}\int_0^{\delta_1} \sqrt{\log N_1(\mu, \Omega)}d\mu
\le {24c\epsilon \sqrt{d}\over \sqrt{N}}.\label{eq:dud1}
\end{align}
Consider the second term in \eqref{f1}.
$$
\mathbb{E}_n\sup_{x\in \Omega}\left |\sum_{i=1}^M (n_i - \lambda(G_i)N)  \mathbb{E}_{G_i}g \right |\le \mathbb{E}_n\sup_{x\in \Omega}\left |\sum_{i=1}^M \sigma_i(n_i - \lambda(G_i)N)  \mathbb{E}_{G_i}g \right |
$$
Let $\tilde h_i(x) =(n_i - \lambda(G_i)N)  \mathbb{E}_{G_i}g$ and
$$
\delta_2^2 = \sum_{i=1}^M (\tilde h_i(x) - \tilde h_i(x'))^2\le M\|x-x'\|_\infty^2
$$
with $|n_i - \lambda(G_i)N|\le 1$ and $|\mathbb{E}_{G_i}g(x) - \mathbb{E}_{G_i}g(x')|\le \|x-x'\|_\infty$. It follows from Remark \eqref{remark:supest} that
$$
\mathbb{E}_n\left [\sup_{x\in \Omega}{1\over N}\sum_{i=1}^M (n_i - \lambda(G_i)N)  \mathbb{E}_{G_i}g\right ]\le  {12\over N}\int_0^{\delta_2} \sqrt{\log N_2(\mu, \Omega)}d\mu,
$$
where $\delta_2\le \sqrt{M}$ and $N_2(\mu, \Omega)\le ({2\sqrt{M}\over \mu})^d$. Thus,
\begin{align}
\mathbb{E}_n\left [\sup_{x\in \Omega}{1\over N}\sum_{i=1}^M (n_i - \lambda(G_i)N)  \mathbb{E}_{G_i}g\right ]\le  {24\sqrt{dM}\over N}.\label{eq:dud2}
\end{align}
A combination of \eqref{eq:dud1} and \eqref{eq:dud2} gives
\begin{align}
\mathbb{E}_n\sup_{x\in \Omega} |r_{m,n} - r_m |\le {24c\epsilon \sqrt{d}\over \sqrt{N}} + {24\sqrt{dM}\over N}.
\end{align}
Choose
$
\displaystyle M= c^2\epsilon^2 N,
$
$\displaystyle \mathbb{E}_n\sup_{x\in \Omega} |f_n - f |$ is at most
$
\displaystyle {48c\epsilon \sqrt{d}\over \sqrt{N}}.
$
Since $M \sim \epsilon^{-d}$,  we have
$
N\sim \epsilon^{-(d+2)}
$
and
$
\epsilon\sim N^{-{1\over d+2}}
$
Thus,
\begin{align}
\mathbb{E}_n\sup_{x\in \Omega} |f_n - f |\le {\nu\over m!}\mathbb{E}_n\sup_{x\in \Omega} |r_{m,n} - r_m| \lesssim {\sqrt{d} \over m!}N^{-{1\over 2}-{1\over d+2}}\|f\|_{\mathcal B^{m+1, q}(\Omega)},
\end{align}
which completes the proof.
\end{proof}
\begin{remark}
Similar to Theorem \ref{est:stratify}, the estimates in Lemma \ref{lm:Cstratifyrange1} and \ref{lm:Cstratifypm1} also holds for the $C^k$-norm of the errors.

\end{remark}


\section{Radiation} \label{sec:rad}
Radiation is energy waves, some waves are visible like light, others are invisible like radio signals. As is the basis for physics, energy cannot be created nor destroyed, only changed from one
form to another.

\subsection{The First Law of Thermodynamics and the Stefan-Boltzmann Equation} \label{sec:first thermolaw}
If energy goes into an object it must equal the outflowing energy plus the change of internal energy. Which is exactly what happens with the atmosphere. Radiation from the sun comes in, and
radiation from the atmosphere goes out. And along the way we heat the atmosphere and the planet which causes less radiation to be emitted than received. At least, that is the idea for Earth which
may not apply to all planets. Let one thing be clear, more radiation cannot be emitted than is inserted, unless the planet and atmosphere are cooling. Anyway, we assume that the planet is a black
body, i.e. it absorbs all radiation on all wavelengths. This is captured in Stefan-Boltzmann's law (\autoref{eq:stefan-boltzmann}) \cite{stefan-boltzmann}.

In \autoref{eq:stefan-boltzmann} the symbols are:

\begin{itemize}
    \item $S$: The energy that reaches the top of the atmosphere, coming from the sun or a similar star, per second per meters squared (\si{Wm^{-2}}). This is also called the insolation.
    \item $\sigma$: The Stefan-Boltzmann constant, $5.670373 \cdot 10^-8$ (\si{Wm^{-2}K^{-4}}) \cite{stefan-boltzmann}.
    \item $T$: The temperature of the planet (\si{K}).
\end{itemize}

The energy difference between the energy that reaches the atmosphere and the temperature of the planet must be equal to the change in temperature of the planet, which is written in
\autoref{eq:sb rewritten}. The symbols on the right hand side are:

\begin{itemize}
    \item $\Delta U$: The change of internal energy (\si{J}) \cite{thermo1}.
    \item $C$: The specific heat capacity of the object, i.e. how much energy is required to heat the object by one degree Kelvin (\si{JK^{-1}}).
    \item $\Delta T$: The change in temperature (\si{K}).
\end{itemize}

We want to know the change of temperature $\Delta T$, so we rewrite the equation into \autoref{eq:sb rewritten2}. Here we added the $\delta t$ term to account for the time difference (or time
step). This is needed as we need an interval to calculate the difference in temperature over. Also we need to get the energy that we get (\si{J}) and not the energy per second (\si{W}), and by
adding this time step the units all match up perfectly.

\begin{subequations}
    \label{eq:basis}
    \begin{equation}
        \label{eq:stefan-boltzmann}
        S = SB = \sigma T^4
    \end{equation}
    \begin{equation}
        \label{eq:sb rewritten}
        S - \sigma T^4 = \Delta U = C \Delta T
    \end{equation}
    \begin{equation}
        \label{eq:sb rewritten2}
        \Delta T = \frac{\delta t(S - \sigma T^4)}{C}
    \end{equation}
\end{subequations}

The set of equations in \autoref{eq:basis} form the basis of the temperature exchange of the planet. However two crucial aspects are missing. Only half of the planet will be receiving light from
the sun at once, and the planet is a sphere. So we need to account for both in our equation. We do that in \autoref{eq:basis sphere correction}. We view the energy reacing the atmosphere as a
circular area of energy, with the equation for the are of a circle being \autoref{eq:basis circle} \cite{areaCircle}. The area of a sphere is in \autoref{eq:basis sphere} \cite{areaSphere}. In
both equations, $r$ is the radius of the circle/sphere. By using \autoref{eq:basis circle} and \autoref{eq:basis sphere} in \autoref{eq:sb rewritten2} we get \autoref{eq:basis sphere2} where
$r$ is replaced by $R$. It is common in physics literature to use capitals for large objects like planets. However we are not done yet since we can divide some stuff out. We end up with
\autoref{eq:basis sphere final} as the final equation we are going to use.

\begin{subequations}
    \label{eq:basis sphere correction}
    \begin{equation}
        \label{eq:basis circle}
        \pi r^2
    \end{equation}
    \begin{equation}
        \label{eq:basis sphere}
        4 \pi r^2
    \end{equation}
    \begin{equation}
        \label{eq:basis sphere2}
        \Delta T = \frac{\delta t (\pi R^2S - 4\pi R^2\sigma T^4)}{4\pi CR^2}
    \end{equation}
    \begin{equation}
        \label{eq:basis sphere final}
        \Delta T = \frac{\delta t (S - 4\sigma T^4)}{4C}
    \end{equation}
\end{subequations}

\subsection{Insolation}

With the current equation we calculate the global average surface temperature of the planet itself. However, this planet does not have an atmosphere just yet. Basically we modelled the
temperature of a rock floating in space, let's change that with \autoref{eq:atmos}. Here we assume that the area of the atmosphere is equal to the area of the planet surface. Obviously
this assumption is false, as the atmosphere is a sphere that is larger in radius than the planet, however the difference is not significant enough to account for it. We also define the
atmosphere as a single layer. This is due to the accessibility of the model, we want to make it accessible, not university simulation grade. One thing to take into account for the
atmosphere is that it only partially absorbs energy. The sun (or a similar star) is relatively hot and sends out energy waves (radiation) with relatively low wavelengths. The planet is
relatively cold and sends out energy at long wavelengths. As a side note, all objects radiate energy. You can verify this by leaving something in the sun on a hot day for a while and
almost touch it later. You can feel the heat radiating from the object. The planet is no exception and radiates heat as well, though at a different wavelength than the sun. The
atmosphere absorbs longer wavelengths better than short wavelengths. For simplicity's sake we say that all of the sun's energy does not get absorbed by the atmosphere. The planet's
radiation will be absorbed partially by the atmosphere. Some of the energy that the atmosphere absorbs is radiated into space and some of that energy is radiated back onto the planet's
surface. We need to adjust \autoref{eq:basis sphere final} to account for the energy being radiated from the atmosphere back at the planet surface.

So let us denote the temperature of the planet surface as $T_p$ and the temperature of the atmosphere as $T_a$. Let us also write the specific heat capacity of the planet surface as $C_p$
instead of $C$. We add the term in \autoref{eq:atmos on surface improved} to \autoref{eq:basis sphere final} in \autoref{eq:surface change}. In \autoref{eq:atmos on surface}, $\epsilon$ is the
absorbtivity of the atmosphere, the fraction of energy that the atmosphere absorbs. We divided \autoref{eq:atmos on surface} by $\pi R^2$ as we did that with \autoref{eq:basis sphere final} as
well, so we needed to make it match that division.

\begin{subequations}
    \label{eq:atmos}
    \begin{equation}
        \label{eq:atmos on surface}
        4\pi R^2 \epsilon \sigma T_a^4
    \end{equation}
    \begin{equation}
        \label{eq:atmos on surface improved}
        4\epsilon \sigma T_a^4
    \end{equation}
    \begin{equation}
        \label{eq:surface change}
        \Delta T_p = \frac{\delta t (S + 4\epsilon \sigma T_a^4 - 4\sigma T_p^4)}{4C_p}
    \end{equation}
\end{subequations}

As you probably expected, the atmosphere can change in temperature as well. This is modelled by \autoref{eq:atmos change}, which is very similar to \autoref{eq:surface change}. There are
some key differences though. Instead of subtracting the radiated heat of the atmosphere once we do it twice. This is because the atmosphere radiates heat into space and towards the
surface of the planet, which are two outgoing streams of energy instead of one for the planet (as the planet obviously cannot radiate energy anywhere else than into the atmosphere).
$C_a$ is the specific heat capacity of the atmosphere.

\begin{equation}
    \label{eq:atmos change}
    \Delta T_a = \frac{\delta t (\sigma T_p^4 - 2\epsilon\sigma T_a^4)}{C_a}
\end{equation}

\subsection{The Latitude Longitude Grid}
With the current model, we only calculate the global average temperature. To calculate the temperature change along the surface and atmosphere at different points, we are going to use a grid.
Fortunately the world has already defined such a grid for us, the latitude longitude grid \cite{latlong}. The latitude is the coordinate running from the south pole to the north pole, with -90
being the south pole and 90 being the north pole. The longitude runs parallel to the equator and runs from 0 to 360 which is the amount of degrees that an angle can take when calculating the
angle of a circle. So 0 degrees longitude is the same place as 360 degrees longitude. To do this however we need to move on from mathematical formulae to code (or in this case pseudocode).

Pseudocode is a representation of real code. It is meant to be an abstraction of code such that it does not matter how you present it, but every coder should be able to read it and implement
it in their language of preference. This is usually easier to read than normal code, but more difficult to read than mathematical formulae. If you are unfamiliar with code or coding, look up
a tutorial online as there are numerous great ones.

The pseudocode in \autoref{alg:stream1v1} defines the main function of the radiation part of the model. All values are initialised beforehand, based on either estimations, trial and error or
because they are what they are (like the Stefan-Boltzmann constant $\sigma$), which is all done in \autoref{sec:cp}. The total time $t$ starts at 0 and increases by $\delta t$ after every
update of the temperature. This is to account for the total time that the model has simulated (and it is also used later). What you may notice is the $T_p[lan, lon]$ notation. This is to indicate
that $T_p$ saves a value for each $lan$ and $lon$ combination. It is initialised as all zeroes for each index pair, and the values is changed based on the calculations. You can view $T_p$ like
the whole latitude longitude grid, where $T_p[lat, lon]$ is an individual cell of that grid indexed by a specific latitude longitude combination. Do note that from here on most, if not all
functions need to be called \footnote{In case you are unfamiliar with calls, defining a function is defining how it works and calling a function is actually using it.} from another file which I
will call the master. The master file decides what parts of the model to use, what information it uses for plots and the like. We do it this way because we want to be able to switch out
calculations. Say that I find a more efficient way, or more detailed way, to calculate the temperature change. If everything was in one file, then I need to edit the source code of the project.
With the master file structure, I can just swap out the reference to the project's implementation with a reference to my own implementation. This makes the life of the user (in this case the
programmer who has another implementation) easier and makes changing calculations in the future easier as well. Also note that what we pass on as parameters \footnote{Parameters are variables
that a function can use but are defined elsewhere. The real values of the variables are passed on to the funciton in the call.} in \autoref{alg:stream1v1} are the
things that change during the execution of the model or that are calculated beforehand and not constants. $S$ for instance is not constant (well at this point it is but in \autoref{sec:daynight}
we change that) amd the current time is obviously not constant. All constants can be found in \autoref{sec:cp}.

\begin{algorithm}[hbt]
    \caption{The main function for the temperature calculations}
    \label{alg:stream1v1}
    \SetAlgoLined
    \SetKwInput{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{time $t$, amount of energy that hits the planet $S$}
    \Output{Temperature of the planet $T_p$, temperature of the atmosphere $T_a$}
    \For{$lat \leftarrow -90$ \KwTo $90$}{
        \For{$lon \leftarrow 0$ \KwTo $360$}{
            $T_p[lat, lon] \leftarrow T_p[lat, lon] + \frac{\delta t (S + 4\epsilon \sigma (T_a[lat, lon])^4 - 4\sigma (T_p[lat, lon])^4)}{C_p}$ \;
            $T_a[lat, lon] \leftarrow T_a[lat, lon] + \frac{\delta t (\sigma (T_p[lat, lon])^4 - 2\epsilon\sigma (T_a[lat, lon])^4)}{C_a}$ \;
        }
    }
\end{algorithm}

\subsection{Day/Night Cycle} \label{sec:daynight}
As you can see, the amount of energy that reaches the atmopsphere is constant. However this varies based on the position of the sun relative to the planet. To fix this, we have to assign a
function to $S$ that gives the correct amount of energy that lands on that part of the planet surface. This is done in \autoref{alg:solar}. In this algorithm the term insolation is mentioned,
which is $S$ used in the previous formulae if you recall. We use the $\cos$ function here to map the strength of the sun to a number between $0$ and $1$. The strength is dependent on the latitude,
but since that is in degrees and we need it in radians we transform it to radians by multiplying it by $\frac{\pi}{180}$. This function assumes the sun is at the equinox (center of the sun is
directly above the equator) \cite{equinox} at at all times. The second $\cos$ is needed to simulate the longitude that the sun has moved over the longitude of the equator. For that we need the
difference between the longitude of the point we want to calculate the energy for, and the longitude of the sun. The longitude of the sun is of course linked to the current time (as the sun is
in a different position at 5:00 than at 15:00). So we need to map the current time in seconds to the interval $[0,$ seconds in a day$]$. Therefore we need the mod function. The mod function
works like this: $x$ mod $y$ means subtract all multiples of $y$ from $x$ such that $0 \leq x < y$. So to map the current time to a time within one day, we do $-t$ mod $d$ where $-t$ is the
current time and $d$ is the amount of seconds in a day. We need $-t$ as this ensures that the sun moves in the right direction, with $t$ the sun would move in the opposite direction in our model
than how it would move in real life. When we did the calculation specified in \autoref{alg:solar} we return the final value (which means that the function call is "replaced"
\footnote{Replaced is not necessarily the right word, it is more like a mathematical function $f(x)$ where $y = f(x)$. You give it an $x$ and the value that correpsonds to that $x$ is saved in
$y$. So you can view the function call in pseudocode as a value that is calculated by a different function which is then used like a regular number.} by the value that the function calculates).
If the final value is less than 0, we need to return 0 as the sun cannot suck energy out of the planet (that it does not radiate itself, which would happen if a negative value is returned).

\begin{algorithm}[hbt]
    \caption{Calculating the energy from the sun (or similar star) that reaches a part of the planet surface at a given latitude and time}
    \label{alg:solar}
    \SetAlgoLined
    \SetKwInput{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{insolation $ins$, latitude $lat$, longitude $lon$, time $t$, time in a day $d$}
    \Output{Amount of energy $S$ that hits the planet surface at the given latitude-time combination.}
    $longitude \leftarrow 360 \cdot \frac{(-t \text{ mod } d)}{d}$ \;
    $S \leftarrow ins \cdot \cos(lat \frac{\pi}{180}) \cos((lon - longitude) \cdot \frac{\pi}{180})$ \;
    \eIf{$S < 0$}{
        \Return{$0$}
    }{
        \Return{$S$}
    }
\end{algorithm}

By implementing \autoref{alg:solar}, \autoref{alg:stream1v1} must be changed as well, as $S$ is no longer constant for the whole planet surface. So let us do that in \autoref{alg:stream1v2}. Note
that $S$ is defined as the call to \autoref{alg:solar}.
\begin{algorithm}[hbt]
    \caption{The main function for the temperature calculations}
    \label{alg:stream1v2}
    \SetAlgoLined
    \SetKwInput{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{amount of energy that hits the planet $S$}
    \Output{Temperature of the planet $T_p$, temperature of the atmosphere $T_a$}

    \For{$lat \leftarrow -nlat$ \KwTo $nlat$}{
        \For{$lon \leftarrow 0$ \KwTo $nlot$}{
            $T_p[lat, lon] \leftarrow T_p[lat, lon] + \frac{\delta t (S + 4\epsilon \sigma (T_a[lat, lon])^4 - 4\sigma (T_p[lat, lon])^4)}{C_p}$ \;
            $T_a[lat, lon] \leftarrow T_a[lat, lon] + \frac{\delta t (\sigma (T_p[lat, lon])^4 - 2\epsilon\sigma (T_a[lat, lon])^4)}{C_a}$ \;
        }
    }
\end{algorithm}

\subsection{Albedo}
Albedo is basically the reflectiveness of a material (in our case the planet's surface) \cite{albedo}. The average albedo of the Earth is about 0.2. Do note that we change $C_p$ from a constant
to an array. We do this to allow adding in oceans or other terrain in the future. Same thing for the albedo, different terrain has different reflectiveness.

\begin{algorithm}[hbt]
    \caption{Defining albedo}
    \label{alg:albedo}
    $a \leftarrow 0.2$ \;

    $C_p \leftarrow 10^6$ \;
\end{algorithm}

Now that we have that defined, we need to adjust the main loop of the program (\autoref{alg:stream1v2}). We need to add albedo into the
equation and change $C_p$ from a constant to an array. The algorithm after these changes can be found in \autoref{alg:stream2v3}. We multiply by $1 - a$ since albedo represents how much energy is
reflected instead of absorbed, where we need the amount that is absorbed which is exactly equal to $1$ minus the amount that is reflected.

\begin{algorithm}[hbt]
    \caption{The main function for the temperature calculations}
    \label{alg:stream2v3}
    \SetAlgoLined
    \SetKwInput{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{amount of energy that hits the planet $S$}
    \Output{Temperature of the planet $T_p$, temperature of the atmosphere $T_a$}
    \For{$lat \leftarrow -nlat$ \KwTo $nlat$}{
        \For{$lon \leftarrow 0$ \KwTo $nlot$}{
            $T_p[lat, lon] \leftarrow T_p[lat, lon] + \frac{\delta t ((1 - a[lat, lon])S + 4\epsilon \sigma (T_a[lat, lon])^4 - 4\sigma (T_p[lat, lon])^4)}{C_p[lat, lon]}$ \;
            $T_a[lat, lon] \leftarrow T_a[lat, lon] + \frac{\delta t (\sigma (T_p[lat, lon])^4 - 2\epsilon\sigma (T_a[lat, lon])^4)}{C_a}$ \;
        }
    }
\end{algorithm}

\subsection{Temperature with Varying Density}
The air density is not at all points exactly the same. This may be due to the winds blowing, or due to height changes in the terrain. We need to account for that, which is done in
\autoref{alg:temperature with density}.

\begin{algorithm}[hbt]
    \caption{The main function for the temperature calculations}
    \label{alg:temperature with density}
    \SetAlgoLined
    \SetKwInput{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{amount of energy that hits the planet $S$}
    \Output{Temperature of the planet $T_p$, temperature of the atmosphere $T_a$}
    \For{$lat \leftarrow -nlat$ \KwTo $nlat$}{
        \For{$lon \leftarrow 0$ \KwTo $nlot$}{
            $T_p[lat, lon] \leftarrow T_p[lat, lon] + \frac{\delta t ((1 - a[lat, lon])S + 4\epsilon \sigma (T_a[lat, lon])^4 - 4\sigma (T_p[lat, lon])^4)}{\rho[lat, lon]C_p[lat, lon]}$ \;
            $T_a[lat, lon] \leftarrow T_a[lat, lon] + \frac{\delta t (\sigma (T_p[lat, lon])^4 - 2\epsilon\sigma (T_a[lat, lon])^4)}{\rho[lat, lon]C_a}$ \;
        }
    }
\end{algorithm}

\subsection{Adding Layers} \label{sec:rad layers}
To add layers, we need a vertical coordinate. You would think that height is the logical and obvious choice right? You are right, but also quite wrong. We instead are going to use pressure. The
reason for this is quite simple: vertical advection (see \autoref{sec:adv}) when using height as vertical coordinate will not work, whereas using pressure allows vertical advection to work.
Therefore we will use pressure as the vertical coordinate. This makes sense too, as there is less pressure at the top of the atmosphere than at the bottom of the atmosphere.

Using pressure as your vertical coordinate does have some effect on the temperature. Instead of using the temperature, we will need to use potential temperature (as explained in
\autoref{sec:thermal pot}). Therefore you will need to convert from and back to temperature when reading in and outputting thermal data. A benefit is though that we do not need to keep track
of the air density anymore, as that is incorporated into the potential temperature. To avoid confusion with the previously defined temperature of the planet $T_p$ we will from now on refer to
the potential temperature as $T_{pot}$. The initial temperature of the atmosphere (which is read in in \autoref{sec:usatmosp}) is henceforth referred to as $T_i$.

\subsection{Grey Radiation Scheme}
Inspired by the Isca project \cite{isca} and a paper describing the grey radiation scheme\cite{greyRad}.

A radiation scheme is a model for how energy is redistributed using light in a system. Such a model is a Grey radiation scheme if you split it into two parts, short and long wavelength radiation.
So you have two redistribution systems, one for short wavelength light and one for long wavelength light. Another assumption we make when using the Grey radiation scheme, is that the atmosphere
is transparent to short wavelength radiation, meaning it lets through light with short wavelengths. Additionally we use a two stream approximation, which means that we have a stream of radiation
going up, and another stream of radiation going down. Note that these two streams are both long wavelength radiation, because we said earlier we assume the atmosphere completely ignores short
wavelength radiation.

The two long wavelength radiation streams are described in \autoref{eq:upward radiation} and \autoref{eq:downward radiation} \cite{greyRad}. In those equations, the symbols are:

\begin{itemize}
    \item $U$: Upward flux.
    \item $D$: Downward flux.
    \item $B$: The Stefan-Boltzmann equation (see \autoref{eq:stefan-boltzmann}).
    \item $\tau$: Optical depth.
\end{itemize}

\begin{subequations}
    \begin{equation}
        \label{eq:upward radiation}
        \frac{dU}{d\tau} = U - B
    \end{equation}
    \begin{equation}
        \label{eq:downward radiation}
        \frac{dD}{d\tau} = B - D
    \end{equation}
\end{subequations}

With \autoref{eq:upward radiation} and \autoref{eq:downward radiation} written down, we can discuss how they work. These equations need a boundary condition to work, a starting point if you like.
For those equations the boundary conditions are that $U$ is at the surface equal to $B$ and that $D$ at the top of the atmosphere is equal to $0$. Meaning that in the beginning the top of the
atmosphere has no downward flux as there is no heat there, and that the bottom of the atmosphere has a lot of upward flux as most if not all of the heat is located there. Then after the spin up
time this should stabilise. We are interested in the change of the fluxes, so $dU$ and $dD$, to get those we need to multiply the right hand side by $d\tau$. Then we have the flow of radiation
that we need. However we cannot solely use these two equations to calculate the heat of a given layer. For that we need a few more components. These are described in \autoref{eq:heat layer}.
Here $Q_R$ is the amount of heat in a layer, $c_p$ is the specific heat capacity of dry air (our atmosphere), $\rho$ is the density of the air in that layer and $\delta z$ is the change in height.
$\delta U - D$ are the change in net radiation, meaning the amount of radiation that is left over after you transferred the upward and downward flux. See it as incoming and outgoing energy for a
given layer, the net change (either cooling down or heating up) is what remains after you have subtracted the incoming energy from the outgoing energy. While this explanation is not entirely true
(as flux is not entirely equivalent to energy), it explains the concept the best.

\begin{equation}
    \label{eq:heat layer}
    Q_R = \frac{1}{c_p\rho}\frac{\delta(U - D)}{\delta z}
\end{equation}

Now only one question remains: what is optical depth? Optical depth is the amount of work a photon has had to do to get to a certain point. This might sound really vague, but bear with me.
Optical depth describes how much stuff a certain photon has had to go through to get to a point. As you'd expect this is $0$ at the top of the atmosphere as space is a big vacuum so no stuff to
move through, so no work. Then the further the photon moves into the atmosphere, the more work the photon has had to do to get there. This is because it now needs to move through gases, like air,
water vapour and other gases. Hence the closer the photon gets to the surface of the planet, the larger the optical depth is because the photon has had to work more to get there. This phenomenon
is described in \autoref{eq:optical depth}. The symbols in the equation mean:

\begin{itemize}
    \item $\tau_0$: Optical depth at the surface.
    \item $p$: Atmospheric pressure (\si{Pa}).
    \item $p_s$: Atmospheric pressure at the surface (\si{Pa}).
    \item $f_l$: The linear optical depth parameter, with a value of 0.1.
\end{itemize}

\begin{equation}
    \label{eq:optical depth}
    \tau = \tau_0[f_l(\frac{p}{p_s}) + (1 - f_l)(\frac{p}{p_s})^4]
\end{equation}

As one can see, \autoref{eq:optical depth} has two parts, a linear part and a quatric part (to the power $4$). The quatric term approximates the structure of water vapour in the atmosphere, which
roughly scales with $\frac{1}{4}$ with respect to the height. The linear term is present to fix numerical behaviour because this is an approximation which will not be completely correct (that's
why it is an approximation) so we add this term to make it roughly right. The same thing holds for $f_l$ which can be manually tuned to fix weird numerical behaviour.

With these equations in our mind, let's get coding. First we add the pressure profile, the pressure of all atmospheric layers at a lat lon point. We need this to accurately represent the optical
depth per atmospheric layer. Then we need to use the pressure profile with regards to \autoref{eq:optical depth}. The resulting code can be found in \autoref{alg:optical depth}. This algorithm
replaces the temperature calculations we have done in \autoref{alg:temperature layer}, as this is basically a better version of the calculations done in that algorithm. $f_l$ has a value of $0.1$
and is located near all the other constants in the code, henceforth we will refer to this section in the code as the control panel, since most if not all of the constants can be tweaked here.
$\tau_0$ is a function that gives the surface optical depth for a given latitude. The corresponding equation can be found in \autoref{eq:optical depth surface} \cite{simon}. Translating this
into code is left as an exercise to the reader. $U[0]$ is the boundary condition discussed before (being the same as \autoref{eq:stefan-boltzmann}), just as $D[nlevels]$ is the boundary condition.
$S_z$ represents the call to \autoref{alg:gradient z}. \texttt{solar} represents the call to \autoref{alg:solar}. $T_{trans}$ represents the call to \autoref{alg:temp to pot}.

\begin{algorithm}
    \caption{Main function for calculating the temperature using radiation}
    \label{alg:optical depth}
    \SetKwInput{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{amount of energy that hits the planet $S$}
    \Output{Potential temperature $T_{pot}$, temperature of the planet surface $T_p$}
    $T_a \leftarrow T_{trans}(T_{pot}, p_z, \texttt{False})$ \;
    \For{$lat \leftarrow -nlat$ \KwTo $nlat$}{
        $\tau = \tau_0(lat)f_l\frac{p_z}{p_z[0]} + (1 - f_l)(\frac{p_z}{p_z[0]})^4)$ \;
        \For{$lon \leftarrow 0$ \KwTo $nlot$}{
            $U[0] \leftarrow \sigma T_p[lat, lon]^4$ \;

            \For{$level \leftarrow 1$ \KwTo $nlevels$}{
                $U[level] \leftarrow U[level - 1] - \frac{(\tau[level] - \tau[level - 1])(\sigma \cdot (mean(T_a[:, :, level]))^4)}{1 + (\tau[level - 1] - \tau[level])}$ \;
            }

            $D[nlevels - 1] \leftarrow 0$ \;

            \For{$level \leftarrow nleves - 1$ \KwTo $0$}{
                $D[level] \leftarrow D[level + 1] - \frac{(\tau[level + 1] - \tau[level])(\sigma \cdot (mean(T_a[:, :, level]))^4)}{1 + (\tau[level] - \tau[level + 1])}$ \;
            }

            \For{$level \leftarrow 0$ \KwTo $nlevels$}{
                $Q[level] \leftarrow -287T_a[lat, lon, level] \frac{S_z(U - D, p_z, level)}{10^3 \cdot p_z[level]}$ \;
            }

            $T_a[lat, lon, :] \leftarrow T_a[lat, lon, :] + Q$ \;

            $S \leftarrow \texttt{solar}(I, lat, lon, t)$ \;

            $T_p[lat, lon] \leftarrow T_p[lat, lon] \frac{\delta t((1 - a[lat, lon]) S + (D[0] - U[0]))}{C_p[lat ,lon]}$ \;
        }
    }
    $T_{pot} \leftarrow T_{trans}(T_a, p_z, \texttt{True})$ \;
    \Return{$T_p, T_{pot}$}
\end{algorithm}

\begin{equation}
    \label{eq:optical depth surface}
    \tau_0 = 3.75 + \cos(lat \frac{\pi}{90})\frac{4.5}{2}
\end{equation}

\subsection{Adding In Some Ozone (Or Something Else That Approximates It)}
Adding in ozone in the stratosphere is hella complicated, so we leave that as an exercise to the reader as in true academic fashion. Just joking, if you want you can work on implementing ozone
however we opt not to because it is quite complicated. Instead we approximate it, which is decent enough for our purpose. We need to do it in \autoref{alg:optical depth} as we need to adjust the
$Q$. We add in a check to see if we are currently calculating the radiation in the stratosphere. If so we add some radiation extra to replicate the effect of ozone. As can be seen in
\autoref{alg:ozone}, where we only focus on the $Q$ part of \autoref{alg:optical depth}, we add in some extra radiation based on how high the current layer calculation is, which scales with the
height.

\begin{algorithm}
    \caption{Replicating the effect of ozone}
    \label{alg:ozone}
    $inv \leftarrow \frac{1}{24*60*60}$ \;
    \For{$level \leftarrow 0$ \KwTo $nlevels$}{
        $Q[level] \leftarrow -287T_a\frac{S_z(U - D, 0, level)}{10^3 \cdot p_z[level]}$ \;
        \uIf{$p_z[level] < 40000$}{
            $Q[level] \leftarrow Q[level] + \texttt{solar}(5, lat, lon, t) inv(\frac{100}{p_z[level]})$ \;
        }
    }
\end{algorithm}

\subsection{Tilting the Planet}
In order to model a planet that has seasons, like Earth, we need to tilt the planet. This has as effect that the sun is not always directly above the equator but is above a certain band around
the equator as the year moves on. This means that some hemispheres receive more/less sun based on what part of the year it is. Which corresponds to the various seasons we have on Earth. But in
order to do that, we have to change the \texttt{solar} function. The new version as shown in \autoref{alg:solar tilt} will replace \autoref{alg:solar}. Here $\alpha$ is the tilt in degrees.

\begin{algorithm}
    \caption{Calculating the energy from the sun (or similar star) that reaches a part of the planet surface at a given latitude and time}
    \label{alg:solar tilt}
    \SetKwInput{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{insolation $ins$, latitude $lat$, longitude $lon$, time $t$, time in a day $d$}
    \Output{Amount of energy $S$ that hits the planet surface at the given latitude, longitude and time combination.}
    $sun\_lon \leftarrow -t \text{ mod } d$ \;
    $sun\_lon \leftarrow sun\_lon \cdot \frac{360}{d}$ \;
    $sun\_lat \leftarrow \alpha\cos(\frac{2t\pi}{year})$ \;
    $S \leftarrow insolation\cos(\frac{\pi(lat - sun\_lat)}{180})$ \;

    \uIf{$S < 0$}{
        \Return{$0$} \;
    } \uElse {
        $lon\_diff \leftarrow lon - sun\_lon$ \;
        $S \leftarrow S\cos(\frac{lon\_diff\pi}{180})$ \;

        \uIf{$S < 0$}{
            \uIf{$lat + sun\_lat > 90$ or $lat + sun\_lat < -90$}{
                \Return{$insolation\cos(\frac{\pi(lat + sun\_lat)}{180})\cos(\frac{lon\_diff\pi}{180})$} \;
            } \uElse {
                \Return{$0$} \;
            }
        } \uElse {
            \Return{$S$} \;
        }
    }
\end{algorithm}

What the code in \autoref{alg:solar tilt} does boils down to calculating the latitude and longitude of the sun and checking whether the planet receives any energy. If not return $0$ immediately.
If so we check if the difference between the sun's longitude and the planet's longitude and calculate how much energy would hit the planet given that the sun is not straight above the equator.
We do this by multiplying the energy it would receive from the sun if it were above the equator $S$ by the cosine of the difference in longitudes, which represents the tilt. Then we check again
if the planet is receiving energy, if not we check if it happens around the poles. We do this because due to the tilt it can be the case that at certain points in the year the pole is in constant
sunlight, i.e. the sun does not go down. This creates a sort of overshoot which needs to be accounted for. If it does this then we add the latitudes of the sun and the planet together and use
that to calculate the energy that would hit that spot. If it is not the case that we are around the poles and we do not receive energy, then we return $0$. If it happens to be that we do receive
energy (so no negative values) then we return $S$.


% declare document class and geometry
\documentclass[12pt]{article} % use larger type; default would be 10pt
\usepackage[margin=1in]{geometry} % handle page geometry

% import packages and commands
\input{../header.tex}

\title{Phys 220A -- Classical Mechanics -- HW02}
\author{UCLA, Fall 2014}
\date{\formatdate{16}{10}{2014}} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed

\begin{document}
\maketitle

\section*{Problem 1: Lenz vector (20 pts)}
\textit{
In this problem we use the fact that the Kepler problem has an additional conserved quantity to solve it without doing an integral.
\newline
Consider the motion of a particle with mass $\mu$ in a potential $V = -\alpha / r$ and define the Lenz vector
\begin{equation}
\v{\Lambda} = \frac{\mu}{\alpha} \left( \frac{d}{dt} \v{r} \right) \times (\v{r} \times \left( \frac{d}{dt} \v{r} \right) ) - \frac{\v{r}}{r}.
\end{equation}
}

\begin{enumerate}[label=\textbf{(\alph*)}]

% part A
\item \textit{
Show that $\v{\Lambda}$ is conserved.
}

% part B
\item \textit{
Show that $\abs{\v{\Lambda}}$ is equal to the eccentricity $\epsilon$ of the orbit.
\newline \textbf{Note:} Remember that for the Kepler problem
\begin{equation}
\frac{L^2}{\mu \alpha} \frac{1}{r} = 1 + \epsilon \cos\phi.
\label{eq:orbit}
\end{equation}
}

% part C
\item \textit{
Show that $\v{\Lambda} \cdot \v{r}$ can be evaluated in two ways
\begin{align}
\v{\Lambda} \cdot \v{r} &= \epsilon r \cos\phi, \text{and} \\
\v{\Lambda} \cdot \v{r} &= \frac{L^2}{\mu \alpha} - r,
\end{align}
and use these results to derive the orbit equation \eqref{eq:orbit}.
}

% part D
\item \textit{
Can $\v{\Lambda}$ be conserved for other central potentials?
}

\end{enumerate}

\section*{Problem 2: Good vibrations (20 pts)}
\textit{
Consider a particle of mass $\mu$ moving in a central harmonic oscillator potential
\begin{equation}
V(r) = \alpha r^2, \quad \alpha > 0.
\end{equation}
}

\begin{enumerate}[label=\textbf{(\alph*)}]

% part A
\item \textit{
In terms of the angular momentum and the other parameters of the problem, find the minimum of the effective potential. If the energy is equal to this minimal value what is the orbit?
}

% part B
\item \textit{
For an energy which is larger than the minimal value, find the minimal and maximal radius $r_\text{min}, r_\text{max}$.
}

% part C
\item \textit{
Find the orbit equation $r = r(\phi)$, what do these orbits look like?
}

% part D
\item \textit{
How does the problem look like if you formulate it in Cartesian coordinates in the $(x,y)$ plane (assuming that the motion is in this plane)?
}

\end{enumerate}

\section*{Problem 3 (15 pts)}
\textit{
Consider a soap-film spanned between two rings of radius $R$ located at $x = -x_0$ and $x = +x_0$. Assume that the film is rotationally symmetric around the $x$-axis.
}

\begin{enumerate}[label=\textbf{(\alph*)}]

% part A
\item \textit{
Show that the area functional can be expressed in terms of the location $y(x)$ of the soap-film at the cross section $z=0$, $y>0$ and is given by
\begin{equation}
A[y] = 2\pi \int_{-x_0}^{x_0} dx y(x) \sqrt{1+y'(x)^2}.
\end{equation}
}

% part B
\item \textit{
Find the Euler-Lagrange equations.
}

% part C
\item \textit{
Find the solution which minimizes the area of the soap-film.
}

\end{enumerate}

\section*{Problem 4: I walk the line (10 pts)}
\textit{
Consider a system with $N$ degrees of freedom and coordinates $q^i$, $i = 1, \dots, N$. The Lagrangian for motion without a potential is given by
\begin{equation}
L = \frac{1}{2} \sum_{ij} g_{ij} (q^k) \dot{q}^i \dot{q}^j
\end{equation}
where the function (``metric'') is assumed to be symmetric, $g_{ij} = g_{ji}$, depends on the coordinates $q^k$ and has an inverse (i.e. $\det g \neq 0$).
}

\begin{enumerate}[label=\textbf{(\alph*)}]

% part A
\item \textit{
Show that the Euler-Lagrange equations are given by
\begin{equation}
\ddot{q}^k + \sum_{ij} \Gamma\indices{^k_{ij}} \dot{q}^i \dot{q}^j = 0
\end{equation}
where
\begin{equation}
\Gamma\indices{^k_{ij}} = \frac{1}{2} \sum_l g^{kl} \left( \frac{\pd g_{il}}{\pd q^j} + \frac{\pd g_{jl}}{\pd q^i} - \frac{\pd g_{ij}}{\pd q^l} \right)
\end{equation}
where $g^{ij}$ is the inverse of $g_{ij}$, i.e.
\begin{equation}
\sum_k g^{ik} g_{kj} = \delta^i_j
\end{equation}
}

% part B
\item \textit{
What does this equation reduce to if $g_{ij}$ is the identity matrix?
}

\end{enumerate}

\end{document}