\section{Economical Bases: Wavelets and REACT estimators }
If one consider the ``equally spaced'' Gaussian regression:
\begin{equation}
\label{lastmodel}
y_i = f(t_i) + \varepsilon_i, i=1,\dots,n
\end{equation}
$t_i = (i-1)/n$ and the $\varepsilon_i$s IID $N(0,\sigma^2)$, many things
simplify.

We can write this in matrix notation: the response vector $\by$ is
$N_n(\f,\sigma^2{\mathbf I})$
with $\f=\{f(t_1),\dots,f(t_n)\}'$.

As usual we want to find an estimation  procedure that minimizes risk:
\[
n^{-1} \E || \hat{\f} - \f ||^2 = n^{-1} \E \left[ \sum_{i=1}^m \{
 \hat{f}(t_i) - f(t_i) \}^2\right].
\]

We have seen that the MLE is $\hat{f}_i = y_i$ which intuitively does
not seem very useful. There is actually an
important result in statistics that makes this more precise.

Stein (1956) noticed that the MLE is
inadmissible: There is an estimation
procedure producing estimates with smaller risk that the MLE for any
$\f$.

To develop a non-trivial theory MLE won't do. A popular procedure is
to specify
some fixed class $\cal F$ of functions where  $f$ lies and seek an
estimator $\hat{f}$ attaining minimax risk
\[
\inf_{\hat{f}} \sup_{f \in {\cal F}} R(\hat{f},f)
\]

By restricting $f \in \cal F$ we make assumptions on the smoothness of
$f$. For example, the $L^2$ Sobolev family makes an assumption
on the number $m$ of continuous derivatives and a limits the size of the
$m$th derivative.

\subsection{Useful transformations}
Remember  $\f \in {\mathbb R}^n$ and that there are many
orthogonal bases for this space. Any orthogonal basis can be
represented with an
orthogonal transform $\bU$ that gives us the coefficients for any $\f$
by multiplying $\bxx = \bU' \f$. This means that we can represent any
vector as $\f = \bU \bxx$.

Remember that the eigen analysis of smoothing splines we can view the
eigenvectors a such a transformation.

If we are smart, we can choose a transformation $\bU$ such that $\bxx$
has some useful interpretation. Furthermore, certain
transformation may be more ``economical'' as we will see.

For {\bf equally spaced data} a widely used transformation is the
Discrete Fourier Transform (DFT).  Fourier's theorem says that any $\f
\in {\mathbb R}^n$
can be re-written as
\[
f_i = a_0 + \sum_{k=1}^{n/2 - 1} \left\{
  a_{k} \cos \left(\frac{2\pi k}{n}  \, i \right) \,
  + b_{k} \sin \left(\frac{2 \pi k}{n} \,i \right) \right\} + a_{n/2}
  \cos (\pi i)
\]
for $i=1,\dots,n$. This defines a basis and the coefficients $\ba =
(a_0,a_1,b_1,\dots,\dots,a_{n/2})'$ can be obtained via $\ba = \bU'
\f$ with $\bU$ having columns of sines and cosines:
\begin{eqnarray*}
  U_1 &=& [n^{-1/2}: 1 \leq i \leq n] \\
  U_{2k} &=& [(2/n)^{1/2}\sin\{2\pi k i/n\} : 1 \leq i \leq
  n], k=1,\dots,n/2 \\
  U_{2k+1} &=& [(2/n)^{1/2}\cos\{2\pi k i/n\} : 1 \leq i \leq
  n], k=1,\dots,n/2-1.
\end{eqnarray*}
Note: This can easily be changed to the case where $n$ is odd by
substituting $n/2$ by $\lfloor n/2 \rfloor$ and taking out the last
term last term $a_{\lceil n/2 \rceil}$.

If a signal is close to a sine wave $f(t) = \cos(2 \pi j t / n +
\phi)$ for some integer $1\leq j \leq n$, only
two of the coefficients in $\ba$ will be big, namely the ones associated with
the columns $2j-1$ and $2j$, the
rest will be close to 0.

This makes the basis associated with the DFT very economical (and the
{\it periodogram a
good detector of hidden periodicities)}. Consider that if we where to
transmit the
signal, say using modems and a telephone line, it would be more
``economical'' to send $\ba$ instead of
the $\f$. Once $\ba$ is received, $\f=\bU\ba$ is reconstructed. This
is basically what data compression is all about.

Because we are dealing with equally spaced data, the coefficients of
the DFT are also related to smoothness. Notice that
the columns of $U$ are increasing in frequency and thus decreasing in
smoothness. This means that a ``smooth'' $\f$ should have only the
first $\ba = \bU'\f$
relatively different
from 0.

\begin{figure}[htb]
\begin{center}
\epsfig{figure=Plots/plot-06-09.ps,angle=270,width=.75\textwidth}
\end{center}
\end{figure}

A close relative of the DFT is the
Discrete Cosine Transform (DCT).
\begin{eqnarray*}
  U_1 &=& [n^{-1/2}: 1 \leq i \leq n] \\
  U_k &=& [(2/n)^{1/2}\cos\{\pi(2i-1)k/(2/n)\} : 1\ \leq i \leq n],
  k=2,\dots,n
\end{eqnarray*}

Economical bases together with ``shrinkage'' ideas can be used to
reduce risk and even to obtain estimates with minimax properties. We
will see this through an example

\subsection{An example}
We consider body temperature data taken from a mouse every 30 minutes
for a day, so we have $n=48$. We believe measurements will have
measurement error and maybe environmental variability so we use a
stochastic model  like
(\ref{lastmodel}). We expect body temperature to  change
``smoothly'' through-out the day  so we believe $f(x)$ is
smooth. Under this assumption $\bxx=\bU'\f$, with $\bU$ the DCT,
should have only a few coefficients that are ``big''.

Because the transformation is orthogonal we have that $\bz = \bU'\by$
is $N(\bxx,\sigma^2 {\mathbf I})$. An idea we learn from Stein (1956)
is to consider linear shrunken estimates $\hat{\bxx} = \{ \bw\bz; \bw
\in [0,1]^n\}$. Here the product $\bw \bz$ is taken component-wise
like in S-plus.

We can then choose the shrinkage coefficients that
minimize the risk
\[
E || \hat{\bxx} - \bxx||^2 = E || \bU \hat{\bxx}  - \f||^2.
\]
Remember that $\bU \bxx  = \bU \bU'\f = \f$.

Relatively simple calculations show that  $\tilde{\bw} = \bxx^2/(\bxx^2
+ \sigma^2)$ minimizes the risk over all possible $\bw \in {\mathbb
  R}^n$.  The MLE obtained, with $\bw = (1,\dots,1)'$, minimizes
the risk only if $\tilde{\bw} =  (1,\dots,1)'$ which only happens when
there is no variance!

\begin{figure}[htb]
\caption{\label{f6.4.2}Fitted curves obtained when using shrinkage
coefficients of the from $\bw =
(1,1,\dots,1,0,\dots,0)$, with $2m+1$ the number of 1s used.}
\begin{center}
\epsfig{figure=Plots/plot-06-10.ps,angle=270,width=.75\textwidth}
\end{center}
\end{figure}

Notice that $\tilde{\bw}$ makes sense because it shrinks
coefficients with small signal to noise ratio. By shrinking small
coefficients closer to 0 we reduce variance and the bias we add is not
very large, thus reducing risk. However, we don't know $\bxx$ nor
$\sigma^2$ so in practice we can't produce $\tilde{\bw}$. Here is
where having economical bases are helpful: we construct estimation
procedures that shrink more aggressively the coefficients for which we have
a-priori knowledge that they are ``close to 0'' i.e. have small signal
to noise ratio.  Two examples of such procedure are:

In Figure \ref{f6.4.1}, we show for the body temperature data the
the fitted curves obtained when using shrinkage
coefficients of the from $\bw =
(1,1,\dots,1,0,\dots,0)$.

\begin{figure}[htb]
\caption{\label{f6.4.2}Estimates obtained with harmonic model and with REACT. We
  also show the $\bz$ and how they have been shrunken.}
\begin{center}
  \epsfig{figure=Plots/plot-06-11.ps,angle=270,width=.75\textwidth}
\end{center}
\end{figure}

If Figure \ref{f6.4.2} we show the fitted curve obtained with $\bw =
(1,1,\dots,1,0,\dots,0)$ and using REACT.
In the first plot we show the coefficients shrunken to 0
with crosses. In the second $\bz$ plot we show $\bw \bz$ with
crosses. Notice that only the first few coefficients of the
transformation are ``big''. Here are the same pictures for data
obtained for 6 consecutive weekends.

Finally in Figure \ref{f6.4.3} we show the two fitted curves and
compare them to the average obtained from observing many days of data.

\begin{figure}[htb]
\caption{\label{f6.4.3} Comparison of two fitted curves to the
average obtained from observing many days of data.}

\begin{center}
\epsfig{figure=Plots/plot-06-12.ps,angle=270,width=.75\textwidth}
\end{center}
\end{figure}

Notice that using $\bw = (1,1,1,1,0,\dots,0)$ reduces to a parametric
model that assumes $f$ is a sum of 4 cosine functions.

Any smoother with a smoothing matrix $\bS$ that is a
projection, e.g. linear regression, splines, can be consider a special
case of what we have described here.

Choosing the transformation $\bU$ is an important step in these
procedure. The theory developed for Wavelets motivate a choice of
$\bU$ that is especially good at handling functions $\f$ that have
``discontinuities''.

\subsection{Wavelets}
The following plot show a nuclear magnetic resonance (NMR) signal.

\begin{figure}[htb]
\begin{center}
\epsfig{figure=Plots/plot-06-13.ps,angle=270,width=.5\textwidth}
\end{center}
\end{figure}

The signal does appear to have some added noise so  we could use
(\ref{lastmodel}) to model the process. However,  $f(x)$ appears to
have a peak at around $x=500$ making it not very smooth at that point.

Situations like these are where wavelets analyses is especially useful
for ``smoothing''. Now a more appropriate word is ``de-noising''.

The Discrete Wavelet Transform defines an orthogonal basis just like
the DFT and DCT. However the columns of DWT are locally smooth. This
means that the coefficients can be interpreted as local smoothness of
the signal for different locations.

Here are the columns of the Haar DWT, the simplest wavelet.

\begin{figure}[htb]
\begin{center}
\epsfig{figure=Plots/plot-06-14.ps,angle=270,width=.5\textwidth}
\end{center}
\end{figure}

%\centerline{\epsfig{figure=plot28.ps,angle=270,width=\textwidth}}

Notice that these are step function. However, there are ways (they
involve complicated math and no closed forms) to create ``smoother''
wavelets. The following are the columns of DWT using the Daubechies wavelets

\begin{figure}[htb]
\begin{center}
\epsfig{figure=Plots/plot-06-15.ps,angle=270,width=.5\textwidth}
\end{center}
\end{figure}

%\centerline{\epsfig{figure=plot29.ps,angle=270,width=\textwidth}}

The following plot shows the coefficients of the DWT by smoothness
level and by location:

\begin{figure}[htb]
\begin{center}
\epsfig{figure=Plots/plot-06-16.ps,angle=270,width=.5\textwidth}
\end{center}
\end{figure}

%\centerline{\epsfig{figure=plot30.ps,angle=270,width=\textwidth}}

Using wavelet with shrinkage seems to perform better at de-noising
than smoothing splines and loess as shown by the following figure.

\begin{figure}[htb]
\begin{center}
\epsfig{figure=Plots/plot-06-17.ps,angle=270,width=.5\textwidth}
\end{center}
\end{figure}

%\centerline{\epsfig{figure=plot31.ps,angle=270,width=\textwidth}}

\begin{figure}[htb]
\begin{center}
\epsfig{figure=Plots/plot-06-18.ps,angle=270,width=.5\textwidth}
\end{center}
\end{figure}

The last plot is what the wavelet estimate looks like for the temperature data


% Created 2020-12-26 Sat 19:42
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{mathbbol}
\usepackage{textcomp}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{bm}
\date{\today}

\hypersetup{
 pdfauthor={Rui Ying},
 pdftitle={Principal Component Analysis},
 pdfkeywords={PCA},
 pdfsubject={Statistic},
 pdfcreator={Emacs 27.1 (Org mode 9.4.2)},
 pdflang={English}}
\begin{document}

%\tableofcontents

\section{Definition of covariance matrix}
\indent

Suppose \textbf{X} is a d-dimensional random vector (with d random variables), and \bm{$X_1$},\dots,\bm{$X_n$} is n independent copies of \textbf{X}.

Write $\bm{X_i} = (X_i^1,\dots,X_i^d)^T$, the subscript means the $i_{th}$ copy, the superscript means the number of random variable (i.e. scala). %Then we can combine all the $\bm{X_i}$ together as a new matrix, $\mathbb{X}$ (n by d).

\begin{align}
  \bm{X} =
  \begin{pmatrix}
    X^1\\
    X^2\\
    \dots \\
    X^d
  \end{pmatrix}
  %
  %\mathbb{X} =
  %\begin{pmatrix}
  %  \dots & \bm{X_1}^T & \dots\\
  %  \dots & \bm{X_2}^T & \dots\\
  %  \dots & \bm{X_3}^T & \dots
  %\end{pmatrix}
\end{align}

Then we can know the covariance matrix, which means take two different scalas or coordinates (notice the superscript) from a vector and compute their covariance. For convenience, not use bold X again as before.
\begin{align}
  \Sigma & = cov(X^i,X^j)\\
         & = \mathbb{E}(XX^T)-\mathbb{E}(X)\mathbb{E}(X)^T\\
         & = \mathbb{E}[(X-\mathbb{E}(X))(X-\mathbb{E}(X))^T]
\end{align}
When it comes to empirical data, we use average $\bar{X}$ to replace expectation\footnote{Here can be a little comfused because in we used subscript before but here we have $X_i$. This is because in theory, $E(X^1)$ is the expectation of random variable $X^1$, but empirically we sampled many times and calculate their average} and use the empirical covariance matrix \textbf{S} to replace the $\Sigma$),

\begin{align}
  \mathbb{E}(X) & =
  \begin{pmatrix}
    \mathbb{E}(X^1)\\
    \vdots\\
    \mathbb{E}(X^d)
  \end{pmatrix}
  \rightarrow
  \begin{pmatrix}
    \frac{\sum}{n} X_i^1\\
    \vdots\\
    \frac{\sum}{n} X_i^d\\
  \end{pmatrix}\\
  S &= \frac{1}{n}\sum (X_iX_i^T) - \bar{X}\bar{X}^T \label{eq6}
\end{align}

In order to eliminate the sum character, we multiply a $\mathbb{1}$ to replace the average. $\mathbb{1}=(1,\dots,1)^T$
\begin{align}
  &\bar{X} = \frac{1}{n}\sum X_i \;\;\;\;\;\;
  \mathbb{X} =
  \begin{bmatrix}
    \vdots&\vdots&\vdots\\
    X_1&X_2&X_n\\
    \vdots&\vdots&\vdots
  \end{bmatrix}\\
  &\frac{1}{n}\mathbb{X}^T\mathbb{1}=\frac{1}{n}\sum X_i = \bar{X}
\end{align}
And we can see that
\begin{align}
  M_i &=
  \begin{bmatrix}
    0&\vdots & 0& 0\\
    0&X_i &0&0\\
    0&\vdots&0&0\\
  \end{bmatrix}\\
  \mathbb{X}^T \mathbb{X} &= \sum_i^n M_iM_i^T = \sum_i^nX_iX_i^T\\
  \mathbb{X}^T &= M_1 + M_2 + \dots + M_n
\end{align}

Then in Eq.6 can be transformed into
\begin{align}
  S = & \frac{1}{n}\mathbb{X}^T\mathbb{X} - \frac{1}{n^2}\mathbb{X}^T(\mathbb{1}\mathbb{1}^T)\mathbb{X}\\
  = & \frac{1}{n}\mathbb{X^T}(I_d - \frac{1}{n}\mathbb{1}\mathbb{1}^T)\mathbb{X}\\
  = & \frac{1}{n}\mathbb{X}^TH\mathbb{X}
\end{align}

So, obviously matrix $H$ is a prthogonal projector (you can proof by calculate $H^TH$), what's the subspace this projector project a vector to?
\begin{align}
  H & = (I_d - \frac{1}{n}\mathbb{1}\mathbb{1}^T)\\
& = \begin{bmatrix}
  1-\frac{1}{n} & \cdots & \frac{1}{n}\\
  \vdots & \ddots & \vdots\\
  \frac{1}{n} & \cdots & 1-\frac{1}{n}
\end{bmatrix}
\end{align}
so for any vector $\bm{v}$, we have
\begin{align}
  H\bm{v} & = \bm{v} - \frac{1}{n}(\bm{v}^T\mathbb{1})\mathbb{1}\\
    & = \bm{v} - \bar{\bm{v}}\mathbb{1}
\end{align}
which means a vector minus its means by all elements. And it's clear that
\begin{align}
  avg(H\bm{v}) = 0
\end{align}
means $H$ projects vector $\bm{v}$ to the subspace that has the mean of 0. Or in another words, Hv $\perp$ span of $\mathbb{1}$ because $(Hv)^T\mathbb{1} = 0$.

\section{Core: $u^T\Sigma u$}

Take a vector $\bm{u} \in \mathbb{R}^d$ (column vector), then
\begin{align}
  u^T\Sigma u & = u^T[E(XX^T) - E(X)E(X)^T]u\\
  & = E[(u^TX)(X^Tu)] - E(u^TX)E(X^Tu)\\
  & = E[(u^TX)^2] - [E(u^TX)]^2\\
  & = var(u^TX)
\end{align}

The transition to Eq.22 is because $u^TX = X^T$ = a number.
So this is the magic now, the covariance matrix is equal to the variance of $u^TX$. What's is $u^TX$?

$u^TX$ is the the inner product betwen u and X. Look at my handnote,in geometric, it means the length of red line. So with multiple points, the variance means \emph{the degree of dispersion along the vector u}.

\begin{figure}[hb!]
  \includegraphics[scale=0.3]{handnote.PNG}
\end{figure}

Therefore, we need to find the vector $\bm{u}$ to maxmize our variance, because we reduce the dimension but don't want to lose too much information (image a 3D olive, we cut it and wanna get the cross section with as long and wide as possible).

\section{Spectral decomposition/Eigendecomposition}
\subsection{Variance is eigenvalue}
Since $\Sigma$ and S are symmetric, we can decompose it into this form:
\begin{align}
  \Sigma = PDP^{T} \; (or PDP^{-1})
\end{align}
We know that matrix P consists of all eigenvectors of $\Sigma$, and
\begin{align}
  \Sigma v_1 = PDP^Tv_1=\lambda_1v_1\\
  v_1^T\Sigma v_1 = \lambda_1v_1^Tv_1=\lambda_1
\end{align}
Therefore, the variance along eigenvectors(here $v_1$ means the first and largest eigenvector) is simply the eigevalue $\lambda$.

Assume $\bar{X}=0$ to ensure $\bar{X}\bar{X}^T=0$ and make calculation easier, the Equation \ref{eq6} becomes
\begin{align}
  S=\Sigma X_i X_i^T
\end{align}

\subsection{Another way to proof}

Suppose %$\lambda_i$ are ordered by number (i.e. $\lambda_1 > \lambda_2 > \dots > \lambda_n$)
$y_i=P^TX_i$ (which is the projected vector). Then
\begin{align}
  \bar{y_i} & = \overline{P^TX_i} = P^T\bar{X_i} = 0\\
  S^\prime & = \frac{1}{n}\sum y_iy_i^T\\
  &=\frac{1}{n}\sum (P^TX_i)(P^TX_i)^T\\
  &=\frac{1}{n}\sum (P^TX_iX_i^TP)\\
  &=\frac{1}{n}\sum (P^TSP)
\end{align}
And because $S=PDP^T$, we have
\begin{align}
  S^\prime &= P^T(PDP^T)P\\
  & = D
\end{align}
We know D is a diagonal matrix made up of eigevalue $\lambda_i$. So $cov(y^i,y^j)=0$ when $i\neq j$. In other words, $lambda_i = var(P^TX_i)$.

\subsection{Why eigenvector is best?}
\indent
Here we need to proof why eigenvectors are the ones make variance largest, because there're so many choices.

Suppose $b=P^Tu$ and $u$ is unit vector.
\begin{align}
  u^TSu = b^TDb=\sum_{j=1}^d \lambda_j b_j^2 \leq \sum_{j=1}^d \lambda_1 b_j^2
\end{align}

$\lambda_1$ here still means the largest eigenvalue. So for any vector $u$, we can know that $\lambda_1$ is the largest variance and the Nth largest eigenvectors are called ($N_{th}$) Principal Components.

In extrme cases, if $n >> d$(much more data samples than dimension), then the empirical data converge to a consistent estimator (which means perfect). Otherwise, if $d >> n$, the angle between eigenvectors of $\Sigma$ and S will be very large (which means very bad estimator). And we need sparse PCA (I don't konw this either).

\end{document}


\documentclass{article}
\usepackage{amsmath}
\usepackage{url}
\usepackage{graphicx}
\usepackage{subcaption}
\title{Contextual BO experiment}
\author{Feng Zhao}

\begin{document}
\maketitle
\section{Experiment 1}
Using Contextual BO to find the maximal value of $f(x,a)$ for given $x$.
\begin{equation}\label{eq:f}
    f(x,a) = \cos(2 x) \cos(a) + \sin(x)
\end{equation}
Choosing $x,y \in [0,6]$.
In this experiment, $x$ is treated as task while $a$ is treated
as action.

Our goal is to provide a surrogate model of $z=g(x)=\arg\max_{a} f(x,a)$.
The exact solution is non-continuous, which means that it is very hard to estimate $z=g(x)$ at the
non-continuous points. Therefore, we need to sample more points in these places.
Bayesian optimization accomplishes this goal,
which can be verified by the histogram of samples $x$.

\begin{figure}[!ht]
    \begin{subfigure}[b]{0.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{./cbo_1.png}
        \caption{Fitting result}
        \label{fig:rk_order_compare}
    \end{subfigure}~
    \begin{subfigure}[b]{0.49\textwidth}
      \centering
      \includegraphics[width=\textwidth]{./cbo_1_hist.png}
      \caption{Histogram}
      \label{fig:time_equal_verify}
    \end{subfigure}
    \caption{}
\end{figure}

\section{Experiment 2}
Using the same object function but the adopted action should
satisfy the constraints.
\begin{equation}
    \cos(x)  \cos(a) - \sin(x) \sin(a) + 0.5 \leq 0
\end{equation}
The optimal action is not unique in this constraint
setting, we plot $f(x,z)$ against $x$ instead.

\begin{figure}[!ht]
    \centering
    \includegraphics[width=6cm]{cbo_2.png}
\end{figure}
\end{document}


\chapter{Matlab Basics and Programming }
Matlab is a commercial program\footnote{The homepage is www.mathworks.com.} which provides an integrated environment for ``Matrix Laboratory". Matlab is one of many softwares used most ubiquitously in areas of mathematics and engineering. Matlab is very compatible with areas of fields which require numerical simulations because the built-in functions and m-files are based on the standard library LIN-PACK and EISPACK\footnote{One can obtain the source files from www.netlib.org}.

\vv The filenames of Matlab files and script files always end in ``.m". The program is very convenient to use because almost all of the structures are composed of matrices. In addition, the graphic processing is set so that the numerical analysis results are expressed conveniently.

\vv One can get explanation regarding Help by typing
\matlabp\texttt{\textbf{help}} \vn in the Matlab command window, and one can get explanations regarding every available Matlab functions. For information regarding a specific command, type  \matlabp\texttt{\textbf{help command\_name}} \vn For instance, \matlabp\texttt{\textbf{help fft}} \vn gives explanation for \texttt{\textbf{fft}} command. To see the explanation in document form with Hypertext structure, use {\tt doc} instead of {\tt help}.

\vv This chapter deals with basic usage of Matlab and programming.
%For reference, basic Matlab command list is in appendix %\ref{A-Matlab}.

\section{Basic Command}
\subsection{Matrix}
Matlab has many different types of built-in matrices. For instance, let us try to make a $7 \times 7$ matrix with random numbers in each entries. \vv \texttt{\textbf{$\gg$ rand(7)}} \vn We can also make a random matrix with a different number of columns and rows by, for example, \matlabp\texttt{\textbf{rand(2,5)}} \vn For more explanations regarding the function rand, use help or doc command. Another specific matrix, Hilbert matrix, is a typical example used in numerical analysis. \matlabp\texttt{\textbf{hilb(5)}} \matlabp\texttt{\textbf{help hilb}} \vn $5 \times 5$ magic square can be made with the following command. \matlabp\texttt{\textbf{magic(5)}} \matlabp\texttt{\textbf{help magic}} \vn Magic square is a square matrix with the summation of the entries in the row, column, and diagonal all equal. This property of magic square will be explored in subsection 1.3 %\ref{Matlab-ft}
 using matrix multiplication. Many types of matrices used in numerical analysis can be made using built-in functions. \matlabp\texttt{\textbf{eye(6)}} \matlabp\texttt{\textbf{zeros(4,7)}} \matlabp\texttt{\textbf{ones(5)}} \vn Not only limited to these built-in matrices, one can make different matrices in any form. \matlabp\texttt{\textbf{[1 2 3 5 7 9]}} \matlabp\texttt{\textbf{[1, 2, 3; 4, 5, 6; 7, 8, 9]}} \matlabp\texttt{\textbf{[1 2 RET 3 4 RET 5 6]}} \vn Here {\tt RET} means one must press the "return key". Matlab grammatical system allows for easy usage of block matrices. \matlabp\texttt{\textbf{[eye(2); zeros(2)]}} \matlabp\texttt{\textbf{[eye(2); zeros(3)]}} \matlabp\texttt{\textbf{[eye(2), ones(2,3)]}} \vn The second example above gives an error. Why is that?

\subsection{Variable}
Matlab has built-in variables, such as {\tt pi}, {\tt eps}, and {\tt ans}. \matlabp\texttt{\textbf{pi}} \matlabp\texttt{\textbf{eps}} \matlabp\texttt{\textbf{help eps}} \vn \texttt{\textbf{who}} is a command which tells us which variables are currently being used. In addition, one can see which variables are being used in the Workspace. \matlabp\texttt{\textbf{who}} \matlabp\texttt{\textbf{help who}} \vn \texttt{\textbf{ans}} is a variable which has the value of the last calculated results that was not assigned a variable. \matlabp\texttt{\textbf{magic(6)}} \matlabp\texttt{\textbf{ans}} \matlabp\texttt{\textbf{x = ans}} \matlabp\texttt{\textbf{x = [x, eye(6)]}} \matlabp\texttt{\textbf{x}} \matlabp\texttt{\textbf{who}} \vn Since a new variable x has been created, x is a variable being used. In order to delete a variable, use the following command. \matlabp\texttt{\textbf{clear x}} \matlabp\texttt{\textbf{x}} \matlabp\texttt{\textbf{who}} \vn In order to erase all variables, use \matlabp\texttt{\textbf{clear}} \vn or \matlabp\texttt{\textbf{clear all}} \vn Use \texttt{\textbf{help}} or {\tt doc} to figure out the difference between the two.

\subsection{Functions} \label{Matlab-ft}
Let us try the following command. \matlabp\texttt{\textbf{a = magic(4)}} \vn Now let us find the transpose of a \matlabp\texttt{\textbf{a'}} \vn If {\tt a} was a complex matrix, then Matlab would calculate the conjugate transpose, not simply a transpose.

\vv Let us explore other arithmetic operations. \matlabp\texttt{\textbf{3*a}} \matlabp\texttt{\textbf{-a}} \matlabp\texttt{\textbf{a + (-a)}} \matlabp\texttt{\textbf{b = max(a)}} \matlabp\texttt{\textbf{max(b)}} \vn Some Matlab functions may have output with more than one values. If one uses \texttt{\textbf{max}} on matrix, then the output is maximum of each column and the indices of the row that contains that maximum. For vector case, the maximum value and the index of that value is presented. \matlabp\texttt{\textbf{[m, i] = max(b)}} \matlabp\texttt{\textbf{[m, i] = min(a)}}

\vn Let us try matrix multiplication in order to verify the ``magic" of magic square.  \matlabp\texttt{\textbf{A = magic(5)}}  \matlabp\texttt{\textbf{b = ones(5,1)}}  \matlabp\texttt{\textbf{A*b}}  \matlabp\texttt{\textbf{v = ones(1,5)}}  \matlabp\texttt{\textbf{v*A}}

\vv In Matlab, dot in front of an operation means entry-by-entry operation. In matrix multiplication, {\tt a.*b} is different from usual matrix multiplication in that the multiplication is done entry-by-entry.  \matlabp\texttt{\textbf{b = 2*ones(4)}}  \matlabp\texttt{\textbf{a.*b}}  \matlabp\texttt{\textbf{a*a}}  \matlabp\texttt{\textbf{a\defh 2}}  \matlabp\texttt{\textbf{a.\defh 2}} \vn The followings are many different arithmetic operations related to matrix.  \matlabp\texttt{\textbf{triu(a)}}  \matlabp\texttt{\textbf{tril(a)}}  \matlabp\texttt{\textbf{diag(a)}}  \matlabp\texttt{\textbf{diag(diag(a))}}  \matlabp\texttt{\textbf{c = rand(4,5)}}  \matlabp\texttt{\textbf{size(c)}}  \matlabp\texttt{\textbf{[m,n] = size(c)}}  \matlabp\texttt{\textbf{m}}  \matlabp\texttt{\textbf{d = .5-c}}

\vv Typically, Matlab commands are used for scalar, but there are many functions that can be applied for both scalars and matrices.  \matlabp\texttt{\textbf{sin(d)}}  \matlabp\texttt{\textbf{exp(d)}}  \matlabp\texttt{\textbf{log(d)}}  \matlabp\texttt{\textbf{abs(d)}}

\vv There are functions which translates decimal valued numbers into integers in Matlab. {\tt round}, {\tt fix}, {\tt ceil}, and {\tt floor} are some of these. For instance,  \matlabp\texttt{\textbf{f = [-.5 .1 .5]}}  \matlabp\texttt{\textbf{round(f)}}  \matlabp\texttt{\textbf{fix(f)}}  \matlabp\texttt{\textbf{ceil(f)}}  \matlabp\texttt{\textbf{floor(f)}}

\subsection{Logic operation}
Let us think of 1 as ``true", and 0 as ``false" in this subsection. \&, $|$, and $\sim$ are logic operations which mean ``and", ``or", and ``not", respectively. == is a logic operation which means equal.  \matlabp\texttt{\textbf{a = [1 0 1 0]}}  \matlabp\texttt{\textbf{b = [1 1 0 0]}}  \matlabp\texttt{\textbf{a == b}}  \matlabp\texttt{\textbf{a <= b}}  \matlabp\texttt{\textbf{$\sim$a}}  \matlabp\texttt{\textbf{a \$ b}}  \matlabp\texttt{\textbf{a \$ $\sim$a}}  \matlabp\texttt{\textbf{a $|$ b}}  \matlabp\texttt{\textbf{a $|$ $\sim$a}} \vn There is a comand named {\tt any} that checks whether the matrix has at least one non-zero entry or not. Not only that, there is also a command named {\tt all} that checks whether all the entries in the matrix are non-zero or not.  \matlabp\texttt{\textbf{a}}  \matlabp\texttt{\textbf{any(a)}}  \matlabp\texttt{\textbf{c = zeros(1,4)}}  \matlabp\texttt{\textbf{d = ones(1,4)}}  \matlabp\texttt{\textbf{any(c)}}  \matlabp\texttt{\textbf{all(a)}}  \matlabp\texttt{\textbf{all(d)}}  \matlabp\texttt{\textbf{e = [a', b', c', d']}}  \matlabp\texttt{\textbf{any(e)}}  \matlabp\texttt{\textbf{all(e)}}  \matlabp\texttt{\textbf{any(all(e))}}

\subsection{Colon}
Matlab provides a useful command for producing and dividng a matrix.  \matlabp\texttt{\textbf{x = -2:1}}  \matlabp\texttt{\textbf{length(x)}}  \matlabp\texttt{\textbf{-2:.5:1}}  \matlabp\texttt{\textbf{-2:.2:1}}  \matlabp\texttt{\textbf{a = magic(5)}}  \matlabp\texttt{\textbf{a(2,3)}} \vn Now let us try using colon to select specific rows and columns of {\tt a}.  \matlabp\texttt{\textbf{a(2,:)}} \matlabp\texttt{\textbf{a(:,3)}} \matlabp\texttt{\textbf{a(2:4,:)}} \matlabp\texttt{\textbf{a(:,3:5)}} \matlabp\texttt{\textbf{a(2:4,3:5)}} \matlabp\texttt{\textbf{a(1:2:5,:)}} \vn In addition, one can freely use row or column vectors in a matrix. \matlabp\texttt{\textbf{a(:,[1 2 5])}} \matlabp\texttt{\textbf{a([2 5],[2 4 5])}}

\vn And one can use assignment statements using vectors and matrices. \matlabp\texttt{\textbf{b = rand(5)}} \matlabp\texttt{\textbf{b([1 2],:) = a([1 2],:)}} \matlabp\texttt{\textbf{a(:,[1 2]) = b(:,[3 5])}} \matlabp\texttt{\textbf{a(:,[1 5]) = a(:,[5 1])}} \matlabp\texttt{\textbf{a = a(:,5:-1:1)}} \vn All of these are simple Matlab functions and examples of matrix multiplication. More functions can be found in the appendix \ref{A-Matlab}.

\subsection{Other features}
The default setting for Matlab is that the decimals are expressed up to 4 decimal digits. Even though the actual calculation is done up to 16 decimal digits, it is rounded and then expressed. The command \matlabp\texttt{\textbf{format long}} \vn changes so that all 16 decimal digits are displayed. And, \matlabp\texttt{\textbf{format short}} \vn changes back to the default setting. Of course, one can display scientific constants long or short, however one wishes, by using the following command \matlabp\texttt{\textbf{format short e}} \matlabp\texttt{\textbf{format long e}}

\vv It is not necessary to always display all the calculated values on the screen. If one attaches semicolon(;) at the end of the command, then calculation will proceed, but the values will not be displayed, just like the colon in Maple.

\vv Sometimes, a lot of time is spent on making matrices in Matlab session, and one might need to use these matrices next time. One can use Matlab command \matlabp\texttt{\textbf{save filename}} \vn to make a file named \texttt{\textbf{filename.mat}} that saves all the variable values made in the current session. If one does not wish to save all the variable values, then one can use \matlabp\texttt{\textbf{save filename x y z}} to save only variable {\tt x,y,z} in \texttt{\textbf{filename.mat}}. These saved variables can be reused next time using the command \matlabp\texttt{\textbf{load filename}}

\vv There are times when one must record all the keyboard inputs and results in Matlab session. The following command allows one to record all the process except for graphs. \matlabp\texttt{\textbf{diary filename}} \vn produces a file named \texttt{\textbf{filename}} and starts the record. \matlabp\texttt{\textbf{diary off}} \vn stops the record, \matlabp\texttt{\textbf{diary on}} \vn restarts the record. These processes are saved in text files, so one can edit them.

\section{Programming in Matlab}
Matlab is a language, with which one is able to program, just like Maple. A person who wants to write the files can easily do so by using .m files toy write a program and execute it. If one wrote {\tt myfile.m}, one can execute {\tt myfile.m} by using the command {\tt myfile}, just like other Matlab commands. Matlab is an interpreter language, so one can execute a written program without compiling.

\subsection{Assignment Statement}
Assignment means one can assign a value to a variable. In other words, {\tt x=a} means that one is assigning the value {\tt a} to the variable {\tt x}. Let us see the following simple program which uses assignment statement.

\vv
\begin{center}
\fbox{\parbox{10.5cm}{\begin{center}
\parbox{9.5cm}{\tt function r=mymod(a,d)\\ \\
\% r=mymod(a,d). If a and d are integers, then \\
\% r is the integer remainder of a after\\
\% division by d. If a and d are integer matrices, \\
\% then r is the matrix of remainders after division \\
\% by corresponding entries. Compare with REM.\\ \\
r=a-d.*floor(a./d);} \end{center} }}\end{center} \vn Make the above {\tt mymod.m} file, and assign an integer value to {\tt a} and {\tt d}. Then, if one uses

\matlabp \texttt{\textbf{mymod(a,d)}}

\vn it is executed as if it were a built-in Matlab command. Let us enter the following command.

\matlabp \texttt{\textbf{help mymod}}

\vn Then the 5-lined information above that start with \% will show up. \% means that the statements that come after it are ignored when a program is executed. When help command is executed in Matlab, the information on the uppermost part of the announced part of the function is displayed. Using this method, the help command provides a help function that let us know the properties of the function quickly. Let us enter the following.

\matlabp\texttt{\textbf{type mymod}}

\vn This command displays the entire details of the file on the screen for convenient reading. Now let us examine the details of {\tt mymod.m}. The first row corresponds to ``function announcement statement". In here, name of the file(always the same as the file name without m), input variable(In this case, {\tt a} and {\tt d}), and output variable({\tt r}) are announced. The next is the `help" aforementioned. Last, the middle part of the program is displayed on the screen. The variable {\tt r} is assigned the value {\tt a-d.*floor(a./d)}. Out of the operations on the right hand side, ``." means entry-by-entry operation. Last, ``;" means that until the last part of the execution, the result is blocked from being printed on the screen. Try executing the program after deleting ``;". A quite different result will be printed.

\subsection{Conditional Statement}
Conditional statement has the following structure. \vv

\texttt{\textbf{if <condition>, <program> end}} \vn \texttt{\textbf{<condition>}} above is a MATLAB function, but it is not a must to have values only 0 and 1. In the conditional statement, \texttt{\textbf{<program>}} is executed only when \texttt{\textbf{<condition>}} has a non-zero value, and proceeds to the next. Let us not forget that {\tt a==b} and {\tt a<=b} are functions perceived as having values 0 or 1. Often, conditional statements have the following form. \vv \texttt{\textbf{if <condition1>, <program1> else <program2> end}} \vn In this case, if {\tt <condition1>} has the value 0, then {\tt <program2>} is performed. There is another form \vv \texttt{\textbf{if <condition1>, <program1>}} \par \texttt{\textbf{elseif <condition2>, <program2>}} \par \texttt{\textbf{end}} In this case, if {\tt <condition1>} is non-zero, then {\tt <program1>} is performed, if {\tt <condition1>} is 0 and {\tt <condition2>} is non-zero, then {\tt <program2>} is performed. In other cases, the program exits the conditional statement and proceeds to the next. The following is a simple program that uses conditional statement. \vv
\begin{center}
\fbox{\parbox{10.5cm}{\begin{center}
\parbox{9.4cm}{\tt function b=even(n)\\ \\
\% b=even(n). If n is an even integer, then b=1 \\
\% otherwise b=0 \\ \\
if mymod(n,2) == 0\\
\phantom{ab}b=1; \\
\phantom{ab}else b=0; \\
end} \end{center} }}\end{center}

\subsection{For Loop}
For loop has the following structure.

\vv \texttt{\textbf{for i=1:n, <program>, end}} \vn Depending on the value of {\tt i}, \texttt{\textbf{<program>}} is repeatedly executed each time. Let us introduce some simple program. The first is matrix multiplication. \vv
\begin{center}
\fbox{\parbox{10.5cm}{\begin{center}
\parbox{10.2cm}{\tt function c=add(a,b)\\ \\
\% c=add(a,b). This is the function which adds \\
\% the matrices a and b. It duplicates the MATLAB \\
\% function a+b.\\ \\
$[$m,n$]$ = size(a);\\
$[$k,l$]$ = size(b);\\
if m$\sim$=k $|$ n$\sim$=l, \\
\phantom{ab}r='ERROR using add: matrices are not the same size',\\
\phantom{ab}return,\\
end \\
c=zeros(m,n); \\
for i=1:m,\\
\phantom{ab}for j=1:n,\\
\phantom{ab}\phantom{ab}c(i,j)=a(i,j)+b(i,j);\\
\phantom{ab}end \\
end} \end{center} }}\end{center}

\vn The next is a program related to matrix multiplication. \vv

\begin{center}
\fbox{\parbox{10.5cm}{\begin{center}
\parbox{9.5cm}{\tt function c=mult(a,b)\\ \\
\% c=mult(a,b). This is the matrix product of  \\
\% the matrices a and b. It duplicates the MATLAB  \\
\% function c=a*b.\\ \\
$[$m,n$]$=size(a);\\
$[$k,l$]$=size(b);\\
if n$\sim$=k  \\
\phantom{ab}r='ERROR using mult: matrices are not compatible \\
\phantom{ab}\phantom{ab}for multiplication',\\
\phantom{ab}return,\\
end \\
c=zeros(m,l); \\
for i=1:m,\\
\phantom{ab}for j=1:l,\\
\phantom{ab}\phantom{ab}for p=1:n,\\
\phantom{ab}\phantom{ab}\phantom{ab}c(i,j)=c(i,j)+a(i,p)*b(p,j);\\
\phantom{ab}\phantom{ab}end \\
\phantom{ab}end \\
end} \end{center} }}\end{center} \vn  Let us look carefully at the conditional statement after the {\tt size} statement in both programs. It is there to print error message. In {\tt add} case, adding matrices with different dimensions results in printing error message, and in {\tt mult} case, if the dimension of the column of the left matrix and the dimension of the row of the right column do not match, error message prints. If there is an error even when there is no such error messages, then MATLAB will print a strange calculation result. Observe the single quotation marks in the error message part. The sentence indicated by the quotation marks is regarded as text and is displayed as a value of the variable {\tt r}. After the error message is the return command. This is an instruction statement which tells us to return back to the function or prompt that called {\tt add} or {\tt mult}. Return command is very useful in error message statement.

\vv {\tt i} in the next loop statement

\vv \texttt{\textbf{for i=1:n, <program>, end}} \vn can be handled in many different ways in the program. There is no problem in writing a vector in place of {\tt 1:n} in MATLAB. In the case of loop statement

\vv \texttt{\textbf{for i=[2,4,5,6,10], <program>, end}} \vn the program will be executed 5 times repeatedly, with i having the values $2,4,5,6,10$ each time. The developers of MATLAB went further. It's possible to use vector, but what about matrix? Thus the loop statement like the following

\vv \texttt{\textbf{for i=magic(7), <program>, end}} \vn is also possible. This program will be executed 7 times (the dimension of the column), with the variable {\tt i} being the column of {\tt magic(7)} each time.

\subsection {While Loop Statement}
While loop statement takes the following form

\vv \texttt{\textbf{while <condition>, <program>, end}}

\vn \texttt{\textbf{<condition>}} becomes MATLAB function, just like it did in conditional statement. The program keeps executing as long as \texttt{\textbf{<condition>}} has non-zero value. However, there is a risk in using while statement because there is no way to forcefully terminate the while statement. Next is a simple program using a while statement. \vv
\begin{center}
\fbox{\parbox{10.5cm}{\begin{center}
\parbox{8.2cm}{\tt function l=twolog(n) \\ \\
\% l=twolog(n). l is the floor of the base 2 \\
\% logarithm of n.  \\ \\
l=0; \\
m=2; \\
while m<=n\\
\phantom{ab}l=l+1; \\
\phantom{ab}m=2*m; \\
end} \end{center} }}\end{center}

\subsection{Recursion}
Recursion means that the function calls upon itself. Next is a simple example that uses recursion. \vv
\begin{center}
\fbox{\parbox{10.5cm}{\begin{center}
\parbox{10cm}{\tt function y=twoexp(n) \\ \\
\% y=twoexp(n). This is a recursive program for \\
\%\phantom{ab}\phantom{ab}computing \\
\% y=2\defh n. The program halts only if n is a nonnegative \\
\%\phantom{ab}\phantom{ab}integer. \\ \\
if n==0, y=1;\\
\phantom{ab}else y=2*twoexp(n-1); \\
end} \end{center} }}\end{center} \vn Many recursive programs consist of conditional statement just like this program. The condition {\tt n==0} is the basic part of recursion, and it is the only way for the program to restrict itself from calling itself up. The ``else" part is the part that shows the recursion. Let us take note on how {\tt twoexp(n-1)} is executed in a program that calculates {\tt twoexp(n)}. The principle is calling upon smaller number {\tt n-1}, and continuing this until n=0 is called upon. Successful recursion means continuously calling upon smaller number.

\vv However, there are many dangers in using recursion. First, just like while loop statement, the function can continuously call itself up. Second, although it can be stopped, it might calculate unneccesarily, wasting time, and third, while the recursive program is executing, extra memory is required. In massive calculation, the memory storage is crucial, and it must not be unnecessarily wasted. Then, with all these negative sides, why is recursion used? Actually, users who are familiar with using recursive programs can utilize its merits while avoiding these problems. When using recurisve function, one can program easier than when one does not use recursive function.

\subsection{Other Variety of Programs}
One can use matrix-valued functions as conditional statement or conditions of loop statement. In other words, conditions can have matrices such as {\tt ones(2)}, {\tt zeros(2)}, or {\tt eye(2)} in them. If {\tt <condition> = eye(2)} in the following program, how will this program execute?\vv
\texttt{\textbf{if <condition>, <program1>,}}
\par \texttt{\textbf{else <program2>, end}}
\vn Here, {\tt <program1>} is executed when all the entries in {\tt <condition>} is non-zero. Hence, {\tt <program1>} is executed if {\tt <condition>} is {\tt magic(2)}, and {\tt <program2>} is executed if {\tt <condition>} is {\tt eye(2)}.

\vv Now let us predict how the following program will be executed. \vv
\texttt{\textbf{if A $\sim$= B, <program>, end}} \vn Here, {\tt <program>} will only be executed when the entries of {\tt A} and {\tt B} are all different. If we wanted to execute {\tt <program>} even when only one entry of {\tt A} and {\tt B} are different, then how can we do this? There are many ways, but first, we can do \vv \texttt{\textbf{if A == B, else $<$program$>$, end}} \vn With this program, the ``else" part will be executed when at least one entry of {\tt A} and {\tt B} is different. For a different way, we can use {\tt A==B} as {\tt all(all(A==B))} to transform it into binary function. The inner {\tt all} will produce a binary vector with all the entries as 1 if the {\tt i}-th column of {\tt A} and the {\tt i}-th column of {\tt B} are the same. If all the entries of this vector is 1, then the outer {\tt all} will produce 1. Therefore, if at least one entry of {\tt A} and {\tt B} is different, then {\tt all(all(A==B))=0}. Therefore, the following program \vv \texttt{\textbf{if $\sim$all(all(A==B)), <program>, end}} \vn will be executed as we want.

\vv Very similar method is also used in while loop statement.
\vv \texttt{\textbf{while <condition>, <program>, end}} \vn With this program, when {\tt <condition>} takes a matrix value, the program is continuously executed if all the entries are non-zero, and the program exits the loop statement if at least one of the entry is 0.

\vv The following program uses many conditional statements at the same time. Let us predict how this program is executed.  \vv \texttt{\textbf{if <condition1> \& <codition2>,}}
\par \texttt{\textbf{<program>, end}} \vn Of course, the program is executed when both {\tt <condition1>} and {\tt <condition2>} are non-zero. But, does this program always work without any problems? In reality, there are times when {\tt <condition1> = 0} and {\tt <condition2>} causes an error during the execution of a program.  For instance, this happens in the following program.  \vv \texttt{\textbf{if i $<=$ m \& A(i,j)==0, <program>, end}} \vn Here, {\tt m} is the dimension of the column of {\tt A}. If {\tt i>m}, then we would like the program to proceed on. However, the error message will be displayed on the screen because of {\tt A(i,j)} part. To avoid this, one can change the conditional statement as follows. \vv \texttt{\textbf{if i$<=$m,}}
\par
 \quad \texttt{\textbf{if A(i,j)==0,}} \par
\qquad \texttt{\textbf{<program>}} \par \quad
\texttt{\textbf{end}} \par \texttt{\textbf{end}}

\subsection{Script}
Script is m-file without function declaration statement and executes differently from m-file defined by function. Let us assume {\tt x} is one of the variable in use this session. If we write a program using a function(-defined) file, and we use {\tt x} in the program even if {\tt x} is not an input variable of the function, then the program does not use the value of {\tt x} that was defined during the session, but it takes on the value that was locally allocated in the program. Furthermore, the program does not change the value of {\tt x} in the session. Therefore, during the execution of function file, it is very convenient in that one does not need to take heed of the variable declared in the session, and this is because of the function that was declared. If there is no part about the declaration, then that script is regarded as a continuous part of the session. Hence, if a used variable is changed in the script during the session, then the variable is changed for the entire session.

\subsection{Proposal for Better Programming}
Pay special attention to the following details when programming with MATLAB. Of course, these details can be applied with not only Matlab, but also for programming in Maple as well.

\begin{enumerate}
\item Just like the examples until now, it is recommended to use indent while programming. This way, it is easier to read, easier to find  grammatical errors, and easier to think of programming in group.

\item Be precise with writing footnote. This is because there can be times when one cannot understand the program that he or she has written. If one uses \% in a sentence, then everything after \% in that line becomes footnote.

\item Insert the error message just like the examples above. These error messages are very helpful when debugging.

\item Always structuralize the output as the same as the form of input of other functions. For example, if the form of output of a program is ``yes-no", then change it to 1 or 0 instead of ``yes" and ``no". This way, they can be used as conditions for conditional statement or loop statement.

\item Try to avoid loop statement in MATLAB as much as possible and use optimized built-in MATLAB functions. For example, try to see how much faster {\tt A*B} is compared to {\tt mult(A,B)}. One will probably be surprised by the difference in speed.

\item If there is a difficult in writing a program, then try taking out the suspicious part and execute it separately. Then come back to the original program and then fix it.
\end{enumerate}
\newpage
\section{Graphics}
A well drawn picture is better than 100-words long explanation. Matlab has powerful graphics processing system that is convenient to use and that expresses given data very well. (The pictures used in this chapter are all drawn using Matlab.)  This section will explain the high-level 2-D and 3-D graphic fuction of Matlab. The low-level factors like graphic objects are not dealt with here. This section deals with only showing a little bit of the power of Matlab graphics. For more details, use {\tt help} or {\tt doc}, or reference \textbf{Matlab User's Guide} or \textbf{Reference Guide}.

\subsection{2-D Graph}

\begin{figure}
\centering
\mbox{%
\subfigure[]{%
\epsfig{file=examplesofplota.eps,width=0.45\textwidth}} \quad
\subfigure[]{%
\epsfig{file=examplesofplotb.eps,width=0.45\textwidth}}} \quad
\caption{{\tt plot} 명령의 예} \label{plot}
\end{figure}

2-D graph is drawn using the {\tt plot} function. In the simplest form, one can display graphs that has only one vector value like {\tt plot(y)}. In this case, the elements of {\tt y} are drawn in the order of index. In other words, {\tt plot(rand(1,20))} graphs 20 random numbers in the order of $1\sim20$, connects all the consecutive points in lines, and prints the picture on the screen just like picture~\ref{plot}(a). If {\tt y} was a matrix, each elements of columns are drawn in order, drawing as many as the number of columns of the matrix. The axis are adjusted with the maximum and minimum values of the data and printed on the screen.

\vv The typical form of {\tt plot} is {\tt plot(x,y)}. Here, {\tt x} and {\tt y} are vectors with same dimensions. Let us execute the following command. \matlabp\texttt{\textbf{x = 0:pi/40:4*pi;}} \matlabp\texttt{\textbf{plot(x, sin(x))}} \vn In this graph, the coordinate of {\tt i}-th point is $(\tt x_{i}, y_{i})$.

\vv A line is drawing using the coordinates of {\tt x} and {\tt y} vectors. For instance, to draw a line connecting $(0,1)$ and $(4,3)$, type \matlabp\texttt{\textbf{plot([0,4], [1,3])}} \vn Here, {\tt [0,4]} mean {\tt x} coordinates of two points, and {\tt [1,3]} represent  the associated {\tt y} coordinates.

\vn \textbf{Practice question:} Draw a line connecting the following each points: $(0,1), (4,3), (2,0), (5,-2)$ \vn \textbf{Practice question:} Draw a house similar to the picture~\ref{plot}(b).

\subsubsection{Label}
One can attach label to the graph using the following commands.

\begin{description}
\item[\tt gtext('text')] \hfil \par
After executing this command, when one moves the mouse on the graph window, a cross-shaped thing is shown on the graph and waits for mouse click. After choosing the location by moving the mouse, the sentence {\tt 'text'} pops up on the graph window upon clicking.

\item[\tt text(x, y, 'text')] \hfil \par
Prints {\tt 'text'} on {\tt (x,y)} location on the graph window. If {\tt x} and {\tt y} are vectors, {\tt 'text'} is printed on each points.

\item[\tt title('text')] \hfil \par
Prints the title {\tt 'text} at the very top part of the graph window.

\item[\tt xlabel('text')] \hfil \par
Puts an explanation on the x-axis.

\item[\tt ylabel('text')] \hfil \par
Puts an explanation on the y-axis.
\end{description}

\subsubsection{Drawing Many Graphs on the Same Axis}
There are at least 3 ways to draw many graphs on the same axis. However, if the recent graph contains data with bigger range than the previous data, then the graph can be rescaled.
\begin{enumerate}
\item The easiest way is to the command {\tt hold}, which holds the current graph on the graphic window. The graphs that are drawn after that are overlapped until the {\tt hold} status is turned off. The command that turns off {\tt hold} is {\tt hold off}.

\item The second way is to use the {\tt plot} command the following way. \vv \texttt{\textbf{plot(x1, y1, x2, y2, x3, y3, $\ldots$)}} \vn The merit of this way is that each vector pair is represented with different color/line forms.

\item The third way is using {\tt plot(x,y)}. Here, {\tt x} and {\tt y} are either both matrices or one is vector and the other is matrix. If one of them is matrix and the other vector, then the column or row that matches the vector is printed with different color/line form. The column or row is chosen to match the vector. In case of square matrix, the column is selected. If both {\tt x} and {\tt y} are matrices that have same dimensions, the column of {\tt x} is matched with the column of {\tt y} and printed. If {\tt x} is not predetermined (just like {\tt plot(y)}), then the column of {\tt y} is matched with the index of the row and then drawn.

\end{enumerate}

\subsubsection{Form of Line, Output Symbol, and Color}
The form of line and output symbol can be selected by adding sentences to the {\tt plot} command.

\begin{description}
\item[\tt plot(x,y, '--')] \hfil \par
{\tt y} is plotted in point-line with respect to {\tt x}.
\item[\tt plot(x,y, 'o')] \hfil \par
Instead of connecting each points with line, the points are displayed in circle.
\item[\tt plot(x,y, '--o')] \hfil \par
Each data is connected with point-line and the points are displayed in circle.
\item[\tt plot(x,y, '--m')] \hfil \par
Displays magenta colored point-line. \end{description} \vn For diverse usage, refer to {\tt help plot}.

\subsubsection{Axis Scale}
In Matlab, the axis are automatically adjusted when drawing the graph. One can use command \vv \texttt{\textbf{axis([xmin, xmax, ymin, ymax])}} \vn to adjust the axis. One can return to the default setting using \texttt{\textbf{axis('auto')}}. The next command \vv \texttt{\textbf{v = axis}} \vn adjusts the current axis scale to the vector {\tt v}. One can maintain the current axis scale using {\tt axis(axis)}. Then, executing {\tt hold}, the next graph is printed with the current scale.

\vv Let us draw the following circle. \matlabp\texttt{\textbf{x = 0:pi/40:2*pi;}} \matlabp\texttt{\textbf{plot(cos(x), sin(x))}} \vn However, the thing that is shown in the screen does not look like circle. Why? The default setting for the length of each axis in Matlab is different. The command {\tt axis('equal')} equalizes the visual scale of {\tt x} and {\tt y} on the screen so that the circle shape will be printed. Use {\tt axis('normal')} to turn off this setting. Use the command {\tt axis('on')} and {\tt axis('off')} to set/erase the axis marking.

\subsubsection{Drawing Many Axes Using Subplot}

\begin{figure}[]
\center \epsfig{file=foursubplots.eps,%height=7cm ,
width=11cm, angle=0} \caption{\label{subplot}4개의 subplot:
3차원상에서 곡면의 회전}
\end{figure}

One can draw graphs with many different axes in one graphic window using {\tt subplot}. The command \texttt{\textbf{subplot(m, n, p)}} divides the current graphic window into $m \times n$ small axis, (starting from top left following the row) sets the {\tt p}-th graph as the current graph. For example, the following program makes four axes just like the picture~\ref{subplot}.

\begin{center}
\fbox{\parbox{10.5cm}{\begin{center}
\parbox{7.6cm}{\tt \% test\!$\_{}$subplot.m \\ \\
\tt [x,y] = meshgrid(-3:0.3:3, -3:0.3,3); \\
z = x.* exp(-x.\^{}2-y.\^{}2); \\
subplot(2,2,1) \\
mesh(z), title('subplot(2,2,1)') \\
subplot(2,2,2) \\
mesh(z) \\
view(-37.5, 70), title('subplot(2,2,2)') \\
subplot(2,2,3) \\
mesh(z) \\
view(-37.5, -10), title('subplot(2,2,3)') \\
subplot(2,2,4) \\
mesh(z) \\
view(0,0), title('subplot(2,2,4)')}  \end{center} }}
\end{center}
\vn The command {\tt subplot(1,1,1)} sets the graphic axis back to one.

\subsubsection{figure, clf, cla}
Command {\tt figure} produces a new figure window. \par

\begin{description}
\item[{\tt figure(N)}] \hfil\par
Produces {\tt N}-th figure window. Commands related to graphic after this will be executed in this window.
\item[{\tt clf}] \hfil\par
Everything except the window of the current figure window will be deleted. Thus, the properties of the current window will also be deleted.
\item[{\tt cla}] \hfil\par
Deletets all the lines, symbols, texts except the axis and axis markings in the current figure window.
\end{description}

\subsubsection{Inputs related to graphics}
The command \vv \texttt{\textbf{[x, y] = ginput}} \vn saves all the points that are inputted by the mouse on the current window. Cross shape is shown on the screen, and saves the points the mouse clicks. {\tt Enter} finishes this command. The command \vv \texttt{\textbf{[x, y] = ginput(n)}} \vn is exactly the same as {\tt ginput} except it only saves {\tt n} points. Use {\tt help} or {\tt doc} to earn more information..

\subsubsection{Logarithmic plot}

\begin{figure}[]
\center \epsfig{file=logarithmicplot.eps, height=7cm, %width=10cm,
angle=0} \caption{\label{log}Logarithmic plot}
\end{figure}

Command \texttt{\textbf{semilogy(x, y)}} displays the graph {\tt y} with $\log_{10}$ scale and {\tt x} with linear scale. For instance, \matlabp\texttt{\textbf{x = 0:0.01:4;}} \matlabp\texttt{\textbf{semilogy(x, exp(x)), grid}} \vn draws the graph like picture~\ref{log}. The increase of equidistant interval of {\tt y}-axis is expressed in exponent of 10. In addition, the marking in the {\tt y}-axis are drawn to show 1, 2, 3, $\ldots$, 10, 20, 30, $\ldots$, 100, $\ldots$ starting from bottom. There are also similar commands like {\tt semilogx} and {\tt loglog}. {\tt x} and {\tt y} can be vector or matrix just like they were with {\tt plot}.

\vn \textbf{Practice question:} Draw graph of $x^{2}, x^{3}, x^{4}, \exp{x^{2}}$ with $ 0\leq x \leq 10$ using {\tt semilogy}.

\subsubsection{Polar Coordinate}

\begin{figure}[]
\center \epsfig{file=polarplot.eps,height=7cm, %width=10cm,
angle=0} \caption{Polar plot: $r=\sin 2\theta$ } \label{polar}
\end{figure}

The command \texttt{\textbf{polar(theta, r)}} uses angle $\theta$ and size $r$ to show the position of the point. For instance, \matlabp\texttt{\textbf{x = 0:pi/40:2*pi;}} \matlabp\texttt{\textbf{polar(x, sin(2*x)), grid}} \vn produces graph like picture~\ref{polar}.

\subsubsection{Drawing a Graph of a Function that Changes Quickly}

\begin{figure}
\centering
\mbox{%
\subfigure[]{%
\epsfig{file=ysina.eps,width=0.45\textwidth}} \quad
\subfigure[]{%
\epsfig{file=ysinb.eps,width=0.45\textwidth}}} \caption{$y
=\sin(1/x)$} \label{sin}
\end{figure}

Until now, the graphs were drawn with data that has $x$-axis all distributed equally, like the example \texttt{\textbf{x = 0:0.01:4}}. If a function to be drawn rapidly changes in a certain domain, then the distribution of $x$-axis will be inefficient, and the graph will not be drawn properly. For instance, \matlabp\texttt{\textbf{x = 0.01:0.001:0.1;}} \matlabp\texttt{\textbf{plot(x, sin(1./x))}} \vn will draw the graph like picture~\ref{sin}(a). However, if the increment of $x$ is reduced to 0.0001, then the graph like picture~\ref{sin}(b) will be drawn. The two graphs are clearly different in the domain $x < 0.04$.

\vv Matlab provides {\tt fplot}, which is a more efficient function. When it comes to drawing a function like $\sin(1/x)$, {\tt fplot} calculates rapid changing places more frequently. However, the command {\tt fplot} has a demerit, which is it must use function file.

\subsubsection{Many Commands related to 2 Dimensional Graphs}
Matlab provides many commands that express functions into graphs. Here, we state some examples, but we wish for the reader to use {\tt help} or {\tt doc} to get more detailed information.

\begin{description}
\item[\tt bar] \hfil\par Draws bar graph. \item[\tt compass] \hfil\par Displays the vector with entries size and direction of complex number with an arrow starting from the origin.
\item[\tt errorbar] \hfil\par Displays error bar. \item[\tt hist] \hfil\par Draws histogram. \item[\tt quiver] \hfil\par Draws many different types of vector fields(for instance, gradient) using little arrows.
\item[\tt fill] \hfil\par Draws polygon and fills in with given color.
\end{description}

\subsection{3 Dimensional Graph}
Matlab provides many functions that can express 3 dimensional graphs. This susbsection will be a brief introduction to these functions.

\subsubsection{Plot3}

\begin{figure}
\centering
\mbox{%
\subfigure[]{%
\epsfig{file=plot3a.eps,width=0.45\textwidth}} \quad
\subfigure[]{%
\epsfig{file=exampleofplot3b.eps,width=0.45\textwidth}}} \quad
\caption{example of command plot3}\label{plot3}
\end{figure}

The command {\tt plot3} is a 3 dimensional version of {\tt plot}, and if we write \matlabp\texttt{\textbf{plot3(x, y, z)}} \vn then a line connecting the points $(x_i, y_i, z_i)$ will be drawn in 3 dimension. For instance, \matlabp\texttt{\textbf{plot3(rand(1,10), rand(1,10), rand(1,10))}} \vn uses 10 random points to draw a line in 3 dimension, like picture~\ref{plot3}(a). And another example, \matlabp\texttt{\textbf{t = 0:pi/50:10*pi;}} \matlabp\texttt{\textbf{plot3(exp(-0.02*t).*sin(t), exp(-0.02*t).*cos(t), t), \ldots}} \par\texttt{\textbf{xlabel('x-axis'), ylabel('y-axis'), zlabel('z-axis') }}\vn draws a dwindling spiral like picture~\ref{plot3}(b). Be careful on the direction of $x$-axis, $y$-axis, $z$-axis, and pay attention to the fact that label was marked for each axis.

\subsubsection{Mesh Surface}
The following is an example regarding mesh surface.

\begin{center}
\fbox{\parbox{10.5cm}{\begin{center}
\parbox{8.4cm}{\tt \% Mexican\!$\_{}$hat.m \\ \\
\tt [x y] = meshgrid(-7.5:0.5:7.5, -7.5:0.5:7.5);  \\
r = sqrt(x.\^{}2 + y.\^{}2) + eps; \\
z = sin(r)./r; \\
mesh(z);}  \end{center} }}
\end{center}

\vv To know how these surfaces are drawn, let us study a simple example like $z = x^{2} - y^{2}$. We want a graph that shows the change in $z$ value when there is a change in values in $x$-$y$ plane. Let us think only in the domain $0 \leq x \leq 5, 0 \leq y \leq 5$ for this example. First use Matlab command {\tt meshgrid} to produce grid on the $x$-$y$ plane where the surface will be drawn. \matlabp\texttt{\textbf{[x y] = meshgrid(0:5, 0:5)}} \vn This command produces two matrices {\tt x, y} like the following.
$$\begin{array}{ccccccc}
{\tt x =} & & & & & & \\
& 0 & 1 & 2 & 3 & 4 & 5 \\
& 0 & 1 & 2 & 3 & 4 & 5 \\
& 0 & 1 & 2 & 3 & 4 & 5 \\
& 0 & 1 & 2 & 3 & 4 & 5 \\
& 0 & 1 & 2 & 3 & 4 & 5 \\
& 0 & 1 & 2 & 3 & 4 & 5
      \end{array}\qquad\qquad
\begin{array}{ccccccc}
{\tt y =} & & & & & & \\
& 0 & 0 & 0 & 0 & 0 & 0 \\
& 1 & 1 & 1 & 1 & 1 & 1 \\
& 2 & 2 & 2 & 2 & 2 & 2 \\
& 3 & 3 & 3 & 3 & 3 & 3 \\
& 4 & 4 & 4 & 4 & 4 & 4 \\
& 5 & 5 & 5 & 5 & 5 & 5
\end{array}$$

\vn As we can see from above, matrix {\tt x} represents each grid of $x$-axis, and matrix {\tt y} represents each grid of $y$-axis. If the grid of $x$-direction and that of $y$-direction are of same shape, then we can write in the following short form. \matlabp\texttt{\textbf{[x y] = meshgrid(0:5)}} \vn And as can be predicted with the Matlab matrix operation, the command \texttt{\textbf{z = x.\^{}2-y.\^{}2}} produces the following matrix.
$$\begin{array}{rrrrrrr}
{\tt z =} & & & & & & \\
& 0  &  1  &  4  &  9  &  16  &  25 \\
& -1  &  0  &  3  &  8  &  15  &  24 \\
& -4  &  -3  &  0  &  5  &  12  &  21 \\
& -9  &  -8  &  -5  &  0  &  7  &  16 \\
& -16  &  -15  &  -12  &  -7  &  0  &  9 \\
& -25  &  -24  &  -21  &  -16  &  -9  &  0
\end{array}$$

\vn For instance, at the point $(5, 2)$, z takes the value $5^{2}-2^{2} = 21$. Fortunately, one does not need to be concerned with the precise relationship between the coordinate system of the grid and the index of the matrix. This is automatically adjusted by {\tt meshgrid}.

\begin{figure}[]
\center \epsfig{file=surface.eps, %fig7surf.eps,
height=7cm,%width=10cm,
angle=0} \caption{curved surface $z = x^{2}-y^{2}$} \label{surf}
\end{figure}

\vv The command {\tt mesh(z)} produces graph with lattice-like surface, where the points on the grid are raised to the surface and then connected to form the lattice. In other words, {\tt mesh} draws a `wire mesh'-like surface. If one does not want color, then one can type \matlabp{\tt mesh(z,'EdgeColor','black')} \vn In addition, another command {\tt surf} draws a lattice-like surface composed of small colored tiles. Use {\tt help} or {\tt doc} to learn more about {\tt mesh} and {\tt surf}.

If one is using Matlab student edition, then one must know that there is a limit to grid size when using {\tt meshgrid}. The limit is that the size of the row or column of matrix must be at most 32, and the size of matrix must not exceed 8192.

\vn \textbf{Practice question:} Use the command  \vv \texttt{\textbf{[x y] = meshgrid(0:0.25:5);}} \vn to draw a denser mesh than picture~\ref{surf}.

\vn \textbf{Practice question:} The distribution of temperature on the iron plate is as follows.
$$u(x, y) = 80 y^{2} e^{-x^{2}-0.3y^{2}}$$ \noindent With the domain $-2.1 \leq x \leq 2.1, -6 \leq y \leq 6$, draw the curved surface $u$ with the grid size of each direction as 0.15.

\subsubsection{Drawing Contour}
\begin{figure}
\centering
\mbox{%
\subfigure[]{%
\epsfig{file=contoura.eps,width=0.45\textwidth}} \quad
\subfigure[]{%
\epsfig{file=contour3b.eps,width=0.45\textwidth}}} \caption{Contour
plot} \label{contour}
\end{figure}

After solving the practice questions above, execute the following command. \matlabp\texttt{\textbf{ contour(u)}} \vn Then, one can earn a contour(isothermal line) about the distribution of temperature like picture~\ref{contour}(a). The command {\tt contour} can take second input variable. For this second variable, one inputs how many lines the contour will draw or a vector with specific values for drawing contour. Use command {\tt contour3} to draw a 3 dimensional contour like picture~\ref{contour}(b). One can make contour label with the command {\tt clabel}.

\begin{figure}
\centering
\mbox{%
\subfigure[]{%
\epsfig{file=fig710a.eps,width=0.45\textwidth}} \quad
\subfigure[]{%
\epsfig{file=fig710b.eps,width=0.45\textwidth}}} \caption{(a)
meshc \quad (b) Erasing part of curved surface} \label{meshc}
\end{figure}

\vv To display both contour and mesh together, one can use {\tt meshc} or {\tt surfc}. For instance, the following program \matlabp\texttt{\textbf{[x y] = meshgrid(-2:0.2:2); }} \matlabp\texttt{\textbf{ z = x.*exp(-x.\^{}2 - y.\^{}2); }} \matlabp\texttt{\textbf{meshc(z);}} \vn draws a graph like picture~\ref{meshc}(a).

\subsubsection{Deletion of Curved Surface Due to NaN(Not a Number)}
If the matrix that holds information on the curved surface contains NaN, then this value does not appear in the graph, and because of this, a part of the curved surface will be omitted. Let us study the following example.

\begin{center}
\fbox{\parbox{10.5cm}{\begin{center}
\parbox{8.0cm}{\tt \% cropping.m \\ \\
\tt [x y] = meshgrid(-2:.2:2);  \\
z = x.*exp(-x.\^{}2 - y.\^{}2); \\
c = z;\qquad \% preserve the original surface \\
c(1:11, 1:21) = nan; \\
mesh(c), xlabel(`x-axis'), ylabel(`y-axis')}  \end{center} }}
\end{center}
\vn The above program will display the graph like picture~\ref{meshc}(b).

\subsubsection{quiver}
The command {\tt quiver} draws a vector that starts at 2 dimensional point. Although it is drawn in 2 dimensional graph, it is occasionally used with {\tt contour}, which helps understanding changes in 3 dimensional curved surfaces. For instance, let us think about $V = x^{2} + y$, which is a scalar function with 2 variables for input. The gradient of V is defined as the following vector field.
\begin{eqnarray*}
\nabla V & = & \left(\frac{\partial V}{\partial x}, \frac{\partial
V}{\partial y}\right) \\
         & = & (2x, 1)
\end{eqnarray*}

\begin{figure}[]
\center \epsfig{file=fig711.eps,height=7cm, %width=10cm,
angle=0} \caption{Gradient and contour} \label{grad}
\end{figure}

\noindent The following program draws the direction of $\nabla V$ for each points in $x$-$y$ plane(refer to picture~\ref{grad}).

\begin{center}
\fbox{\parbox{10.5cm}{\begin{center}
\parbox{5.6cm}{\tt \% test\!$\_{}$quiver.m \\ \\
\tt [x y] = meshgrid(-2:.2:2);  \\
V = x.\^{}2 + y; \\
dx = 2*x; \\
dy = ones(size(y)); \\
axis equal \\
contour(x, y, V), hold on \\
quiver(x, y, dx, dy), hold off}  \end{center} }}
\end{center}

`Contour' is a series of {\tt level surface}. Gradient of a random point is perpendicular to the level surface that passes through that point. When drawing a contour, the vectors {\tt x} and {\tt y} are required for labeling the axes. What will happen if take this out and just use {\tt contour(V)} and execute the above {\tt test\!$\_{}$quiver.m}? Let us try to predict the result before we execute it.

Another option regarding {\tt quiver} is that one can change the size of the arrows. See {\tt help} or {\tt doc}.

If it is not possible to differentiate the vector $V$ or if one does not wish to differentiate it, then one can use the command {\tt gradient} to calculate the derivative.
\matlabp\texttt{\textbf{[dx dy] = gradient(V, 0.2, 0.2);}} \vn 0.2 means the increment with respect to $x$ and $y$ directions for approximate calculation.

\subsubsection{Pseudocolor}
The following program  \matlabp\texttt{\textbf{[x, y] = meshgrid(-2:.2:2); }} \matlabp\texttt{\textbf{z = x.*exp(-x.\^{}2 - y.\^{}2); }} \matlabp\texttt{\textbf{pcolor(z), shading flat, colormap(hot) }} \vn draws a contour that expresses height using mixture of red, orange, and yellow. The command {\tt shading flat} eliminates the grid line. {\tt pcolor} means pseudocolor. Each element of the matrix {\tt z} is used as index of color map(in this case hot) that determines the color which will express the element. If one wants a cool color, then try {\tt colormap(cool)}.\^{}\^{} Does it feel cool? There is yet another color map, {\tt colormap(hsv)}, where {\tt hsv} stands for huge-saturation-value.

\subsubsection{Visualization of matrix}

\begin{figure}[]
\center \epsfig{file=viewmat.eps,height=7cm,%width=10cm,
angle=0} \caption{visualization of matrix} \label{mat}
\end{figure}

The command {\tt mesh} can `visualize' the matrix. The following program displays the graph like picture~\ref{mat}.

\begin{center}
\fbox{\parbox{10.5cm}{\begin{center}
\parbox{4.7cm}{\tt \% visual\!$\_{}$mat.m \\ \\
\tt a = zeros(30);  \\
a(:,15) = 0.2*ones(30,1); \\
a(7,:) = 0.1*ones(1,30); \\
a(15,15) = 1; \\
mesh(a)}  \end{center} }}
\end{center}
\vn The size of the matrix {\tt a} is $30 \times 30$. The middle element {\tt a(15,15)} is 1, and every element of the 7-th row is 0.1, and the remaining elements of the 15-th row is 0.2. {\tt mesh(a)} cognizes every rows and columns of matrix a as coordinates of $x$-$y$. In other words, the value of {\tt a(i,j)} is the height of the curved surface mesh at point {\tt (i,j)}.

\subsubsection{Rotating 3 Dimensional Graph}
{\tt view} is a command which designates observation point when viewing a 3 dimensional graph. To see how this works, let us execute the following program which rotates the visualized matrix picture~\ref{mat}.

\begin{center}
\fbox{\parbox{10.5cm}{\begin{center}
\parbox{5cm}{\tt \% rotation.m \\ \\
\tt a = zeros(30);  \\
a(:,15) = 0.2*ones(30,1); \\
a(7,:) = 0.1*ones(1,30); \\
a(15,15) = 1; \\
el = 30; \\
for az = -37.5:15:-37.5+360 \\
\phantom{for}mesh(a), view(az, el) \\
\phantom{for}pause(0.5) \\
end}  \end{center} }}
\end{center}
\vn The command {\tt view} requires two angles. The first, as can be seen in the example, is azimuth {\tt az} on the $x$-$y$ plane that has degree as its unit. {\tt az} rotates the observation point about $z$-axis - in other words, the 'sharp point' at $(15,15)$ in picture~\ref{mat} - counterclockwise. The default value for {\tt az} is $-37.5^{\circ}$. Therefore, the above program rotates the observation point about $z$-axis $15^{\circ}$ each time from the default value. The second angle of {\tt view} is {\tt el} which expresses altitude with degree as its unit. This means the angle between the $z$-axis and $x$-$y$ plane. For instance, $90^{\circ}$ represents 2 dimensional graphic, in other words, looking down from above. If the value of altitude is positive, then the observer is above the $x$-$y$ plane, and if negative, then is below the plane. The default value is $30^{\circ}$.

\vv The command {\tt pause(n)} stops the execution for {\tt n} seconds.

\vn \textbf{Practice question:} Fix the above program so that the value of {\tt az} is fixed as the default value and and the value of {\tt el} is gradually changing.

\subsubsection{Lighting}
One can materialize lighting and shadow effect using the command {\tt surfl}. Try the following.

\matlabp\texttt{\textbf{[x, y] = meshgrid(-2:0.05:2);}}
\matlabp\texttt{\textbf{z = x.*exp(-x.\^{}2 - y.\^{}2); }}
\matlabp\texttt{\textbf{surfl(z, [-20 50]), colormap(gray),
shading flat}} \vn The location of the source of light is determined by the second variable of {\tt surfl}, with the first value being azimuth and the second being altitude. To make a natural reflection light, one must set the grid compact so that the grid is not visualizable. (In this case, $81\times 81$)

_codes/intro/Learning MATLAB with Linear Algebra (Jeon, Lee)/Matlabe.tex

files/intro/Learning MATLAB with Linear Algebra (Jeon, Lee)/Matlabe.tex


% !TEX root = ./Basilisk-Integrators20170724.tex

\begin{figure}[htb]
	\centerline{
	\includegraphics[]{Figures/integratorDiagram}
	}
	\caption{Illustration of the Integrator Diagram}
	\label{fig:intDiag}
\end{figure}

\section{Model Description}

\subsection{Overview}
The Basilisk integration capability is implemented in a modular manner such that different integrators can be assigned to the equations of motion that must be solved.  Figure~\ref{fig:intDiag} illustrates how the integrator functions relative to a dynamical systems model, here the {\tt SpacecraftPlus()} object.  The ODE's are provided by a sub-class of {\tt DynamicObject} which must be able to respond to the {\tt equationsOfMotion()} method.  Integration of the state vector forward one time step is then handled by the {\tt integrate} method of the integrator class.  By default the {\tt DynamicObject} is integrated using a fixed time step 4th order Runge-Kutta method.

Assume the dynamical system is given by
\begin{equation}
	\dot{\bm x} = \bm f(t, \bm x)
\end{equation}
The initial conditions are specified through $\bm x_{0} = \bm x(t_{0})$.  In integration time step is given through $h$.

\subsection{Implemented Integrators}

\subsubsection{4th Order Runge Kutta - Default Integrator}
A standard fixed time step 4th order Runge Kutta integrator is enabled by default.  The 4 $k_{i}$ values are defined as
\begin{align}
	\bm k_{1} &= \bm f(t_{n}, \bm x_{n}) \\
	\bm k_{2} &= \bm f(t_{n} + \frac{h}{2}, \bm x_{n} + \frac{h}{2} \bm k_{1}) \\
	\bm k_{3} &= \bm f(t_{n} + \frac{h}{2}, \bm x_{n} + \frac{h}{2} \bm k_{2}) \\
	\bm k_{4} &= \bm f(t_{n} + h, \bm x_{n} + h \bm k_{3})
\end{align}
The states at the next integration time $t_{n+1} = t_{n} + h$ is
\begin{equation}
	\bm x_{n+1} = \bm x_{n} + \frac{h}{6} \left(
		\bm k_{1} + 2 \bm k_{2} + 2 \bm k_{3} + \bm k_{4}
	\right)
\end{equation}

\subsubsection{2nd Order Runge Kutta (Heun's Method)}
A 2nd order Runge-Kutta method is implemented through Heun's method.\footnote{\url{http://goo.gl/SWdyBZ}} The 2 $k_{i}$ values are defined as
\begin{align}
	\bm k_{1} &= \bm f(t_{n}, \bm x_{n}) \\
	\bm k_{2} &= \bm f(t_{n} + h, \bm x_{n} + h \bm k_{1})
\end{align}
The states at the next integration time $t_{n+1} = t_{n} + h$ is
\begin{equation}
	\bm x_{n+1} = \bm x_{n} + \frac{h}{2} \left(
		\bm k_{1} +  \bm k_{2}
	\right)
\end{equation}

\subsubsection{1st Order Runge Kutta (Euler's Method)}
A first order Runge-Kutta method is implemented through Euler's method. The one $k_{1}$ value is defined as
\begin{align}
	\bm k_{1} &= \bm f(t_{n}, \bm x_{n})
\end{align}
The states at the next integration time $t_{n+1} = t_{n} + h$ is
\begin{equation}
	\bm x_{n+1} = \bm x_{n} + h
		\bm k_{1}
\end{equation}


\section{Representation}

\subsection{Nodes}
The possible nodes for each individual, or parse tree, comprise nodes from a function set and terminal set. As per the implementation of the EvoHyp \cite{pillay2017evohyp} toolset, the function set - used for possible internal nodes - contains simple addition, subtraction, multiplication and division arithmetic operators. In the case of the division operator, a value of 1 is returned if the denominator is 0. In addition, arithmetic rules were used so the function set also comprises an \emph{ifelse} operator and
relational operators ($<$, $>$, $<=$, $>=$, $==$ and $!=$).

In terms of the terminal set, there are 3 possible nodes - \emph{A}, \emph{C} and \emph{F} - which represent attributes of a specific city, \emph{n}, in the TSP instance. \emph{A} represents the average distance from \emph{n} to each of its neighbors. \emph{C} represents the distance between \emph{n} and its closest neighbor. \emph{F} represents the distance between \emph{n} and its furthest neighbor. These end nodes, as well as the aforementioned internal nodes, are described in more detail in table \ref{tab:nodes}.

\begin{table}[H]
\resizebox{\textwidth}{!}{\begin{tabular}{|l|l|l|}
\hline
\textbf{Symbol} & \textbf{Arity} & \textbf{Description}                                                                                \\ \hline
+               & 2              & Adds its 2 children and returns the result                                                          \\ \hline
-               & 2              & Subtracts the first child from the second child and returns the result                              \\ \hline
*               & 2              & Multiplies its 2 children and returns the result                                                    \\ \hline
/               & 2              & Divides the 1st child by the 2nd child and returns the result; or, returns 1 if the 2nd child is 0  \\ \hline
\textit{ifelse} & 3              & If the 1st child evaluates to true the 2nd child is returned; otherwise,  the 3rd child is returned \\ \hline
\textless{}     & 2              & True if the 1st child is less than the 2nd child; otherwise, evaluates to false                     \\ \hline
\textgreater{}  & 2              & True if the 1st child is greater than the 2nd child; otherwise, evaluates to false                  \\ \hline
\textless{}=    & 2              & True if the 1st child is less than or equal to the 2nd child; otherwise, evaluates to false         \\ \hline
\textgreater{}= & 2              & True if the 1st child is greater than or equal to the 2nd child; otherwise, evaluates to false      \\ \hline
==              & 2              & True if the 1st child is equal to the 2nd child; otherwise, evaluates to false                      \\ \hline
!=              & 2              & True if the 1st child is not equal to the 2nd child; otherwise, evaluates to false                  \\ \hline
A               & 0              & Returns the average distance from a specific city in the TSP and each of its neighbors              \\ \hline
C               & 0              & Returns the distance from a specific city in the TSP and its closest neighbor                       \\ \hline
F               & 0              & Returns the distance from a specific city in the TSP and its furthest neighbor                      \\ \hline
\end{tabular}}
\caption{Function and Terminal Set}
\label{tab:nodes}
\end{table}

\subsection{Individuals}\label{sec:individuals}
Each chromosome within a population consists of a parse tree for calculating a priority value for each city in the TSP. To clarify, when evaluating the fitness of an individual, the parse tree will be used to calculate a priority value for all cities in the TSP; then, the cities will be visited in order of these priority values and the total distance determined. Given the possible nodes identified in table \ref{tab:nodes}, an example of one such parse tree is depicted in figure \ref{fig:decision_tree}.

% minimum distance between nodes on the same line
\setlength{\GapWidth}{1em}
% draw with a thick dashed line, very nice looking
\thicklines \drawwith{\dottedline{2}}
% draw an oval and center it with the rule.  You may want to fool with the
% rule values, though these seem to work quite well for me.  If you make the
% rule smaller than the text height, then the GP nodes may not line up with
% each other horizontally quite right, so watch out.
\newcommand{\gpbox}[1]{\Ovalbox{#1\rule[-.7ex]{0ex}{2.7ex}}}

\begin{figure}[H]
    \centering
    \resizebox{\textwidth}{!}{\begin{bundle}{\gpbox{+}}\chunk{\begin{bundle}{\gpbox{ifelse}}\chunk{\begin{bundle}{\gpbox{$<$}}\chunk{\begin{bundle}{\gpbox{-}}\chunk{\gpbox{A}}\chunk{\gpbox{C}}\end{bundle}}\chunk{\begin{bundle}{\gpbox{-}}\chunk{\gpbox{F}}\chunk{\gpbox{A}}\end{bundle}}\end{bundle}}\chunk{\begin{bundle}{\gpbox{/}}\chunk{\gpbox{A}}\chunk{\gpbox{C}}\end{bundle}}\chunk{\begin{bundle}{\gpbox{/}}\chunk{\gpbox{A}}\chunk{\gpbox{F}}\end{bundle}}\end{bundle}}\chunk{\begin{bundle}{\gpbox{*}}\chunk{\gpbox{A}}\chunk{\begin{bundle}{\gpbox{/}}\chunk{\gpbox{C}}\chunk{\gpbox{F}}\end{bundle}}\end{bundle}}\end{bundle}}
    \caption{Example Individual}
    \label{fig:decision_tree}
\end{figure}

\subsection{Populations}
Of course, a population consists of a collection of the individuals that have been described. The genetic algorithm applied only made use of a single population consisting of 256 individuals. This means that only a single population was evolved and cross-population breeding was not applied.

\section{Initial Population Generation}

As implemented using EvoHyp \cite{pillay2017evohyp}, the initial population was generated using the \emph{grow} method proposed by Koza \cite{koza1992genetic}. At the maximum depth the elements from the terminal set are selected; but, at other levels, elements are selected from both the function and terminal sets, whilst abiding by constraints such that the child of an \emph{ifelse} node must be one of the conditional operator nodes. The maximum tree depth allowed during the initial population generation was a tree depth of 4.


\appendix
\section{$\hat{\mu}$ bias by FBMP}
\label{sec:mubias}

\subsection{Model}

The model is described in section~\ref{subsec:fbmp}.

\begin{equation}
\begin{aligned}
    \left.
    \begin{bmatrix}
        \bm{w} \\
        \bm{q}'
    \end{bmatrix}
    \right\vert\bm{z}
    &\sim \mathrm{Normal}\left(
    \begin{bmatrix}
        \bm{V}_\mathrm{PE}\bm{z} \\
        \bm{z}
    \end{bmatrix},
    \begin{bmatrix}
        \bm{\Sigma}_z & \bm{V}_\mathrm{PE}\bm{Z} \\
        \bm{Z}\bm{V}_\mathrm{PE}^\intercal & \bm{Z}
    \end{bmatrix}
    \right) \\
    \bm{\Sigma}_z &= \bm{V}_\mathrm{PE}\bm{Z}\bm{V}_\mathrm{PE}^\intercal+\sigma_\epsilon^2\bm{I}_M
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
    \nu =& \log[p(\bm{w},\bm{z})] = \log[p(\bm{w}|\bm{z})p(\bm{z})] \\
    =& -\frac{1}{2}(\bm{w}-\bm{V}_\mathrm{PE}\bm{z})^\intercal\bm{\Sigma}_z^{-1}(\bm{w}-\bm{V}_\mathrm{PE}\bm{z})-\frac{1}{2}\log\det\bm{\Sigma}_z-\frac{N}{2}\log2\pi \\
    &+ \sum_{i}\log{\mathrm{Poisson}(z_i,p_i)}
\end{aligned}
\end{equation}

$\bm{V}_\mathrm{PE}$ is a $M\times N$ matrix, where $N=N_\mathrm{PE}$ is the simulation truth of PE number.

\subsection{Approximation}

\begin{itemize}
    \item Poisson $\rightarrow$ Bernoulli, $z_i=0,\bm{Z}_{ii}=0$ or $z_i=1,\bm{Z}_{ii}=\sigma_q^2$
    \item SPE approximation
    \begin{itemize}
    \item For \emph{delta} approximation, $\bm{V}_{PE}=\begin{bmatrix}\bm{I} \\\bm{O}\end{bmatrix}$, $w_i\sim \mathrm{Normal}(1,\sigma_q^2+\sigma_\epsilon^2)$
    \item For \emph{flat} approximation, $\bm{V}_{PE}=\frac{1}{\sqrt{M}}\bm{J}_{M,N}$, $w_i\sim \mathrm{Normal}(\frac{N}{\sqrt{M}},\frac{N}{\sqrt{M}}\sigma_q^2+\sigma_\epsilon^2)$, $\sum_i^M w_i\sim \mathrm{Normal}(N\sqrt{M},M^{\frac{3}{2}}N\sigma_q^2+M\sigma_\epsilon^2)$
    \end{itemize}
    \item flat prior, $P(z_i=1)=\lambda,P(z_i=0)=1-\lambda$
\end{itemize}

The factor $\frac{1}{\sqrt{M}}$ of $\bm{V}_{PE}$ in flat approximation is required by FBMP's repeated greedy search (RGS).

\subsection{Evaluation}

For a certain $(\bm{w}, \bm{z})$, let $K=\{i|z_i=1\}$, $|K|=n$, we evaluate the evolution process of $\nu$ in 2 approximations respectively.

\subsubsection{Delta approximation}

\begin{equation}
\begin{aligned}
    \bm{\Sigma}_z &= \begin{bmatrix}
        \bm{Z} & \bm{O} \\
        \bm{O} & \bm{O}
    \end{bmatrix} + \sigma_\epsilon^2\bm{I}_M \\
    \det\bm{\Sigma}_z &= \Pi_i (z_i\sigma_q^2+\sigma_\epsilon^2) \\
    \bm{\Sigma}_{zij}^{-1} &= \begin{cases}
        \frac{1}{z_i\sigma_q^2+\sigma_\epsilon^2} & \text{ if } i=j \\
        0 & \text{ if } i\neq j
    \end{cases}
\end{aligned}
\end{equation}

Thus,

\begin{equation}
\begin{aligned}
    -2\nu =& \sum_{i\in K}\frac{(w_i - 1)^2}{\sigma_q^2+\sigma_\epsilon^2} + \sum_{i\in \bar{K}}\frac{w_i^2}{\sigma_\epsilon^2} \\
    &+ n\log(\sigma_q^2+\sigma_\epsilon^2) + (N-n)\log\sigma_\epsilon^2 - 2n\log\lambda - 2(N-n)\log(1-\lambda) + C.
\end{aligned}
\end{equation}

Additionally,

\begin{equation}
    \frac{w_i^2}{\sigma_\epsilon^2} = \frac{1}{\sigma_\epsilon^2}\left((\sigma_q^2+\sigma_\epsilon^2)\frac{(w_i - 1)^2}{\sigma_q^2+\sigma_\epsilon^2} + 2(w_i - 1) + 1\right).
\end{equation}

Some statistics we use below,

\begin{equation}
    \sum_{i\in K}\frac{(w_i - 1)^2}{\sigma_q^2+\sigma_\epsilon^2} \sim \chi^2(n).
\end{equation}

Calculate expectation on distribution of $\bm{w}$ and $\bm{z}$ with $|K|=n$,

\begin{equation}
\begin{aligned}
    -2E(\nu)_n =& n + (N - n)\frac{\sigma_q^2+\sigma_\epsilon^2}{\sigma_\epsilon^2} + (N - n)\frac{1}{\sigma_\epsilon^2} \\
    &+ n\log(\sigma_q^2+\sigma_\epsilon^2) + (N-n)\log\sigma_\epsilon^2 - 2n\log\lambda - 2(N-n)\log(1-\lambda) + C.
\end{aligned}
\end{equation}

Thus,

\begin{equation}
\begin{aligned}
    -2D(\nu)_n =& -2E(\nu)_n - (-2E(\nu)_{n-1}) \\
    =& 1 - \frac{\sigma_q^2+\sigma_\epsilon^2}{\sigma_\epsilon^2} - \frac{1}{\sigma_\epsilon^2} + \log(\sigma_q^2+\sigma_\epsilon^2) - \log\sigma_\epsilon^2 - 2\log\lambda + 2\log(1-\lambda).
\end{aligned}
\end{equation}

Remember that $\sigma_q^2 \gg \sigma_\epsilon^2$ and $\lambda \ll 1$, set $x=\sigma_q^2/\sigma_\epsilon^2$, then,

\begin{equation}
    D(\nu)_n \approx \frac{1}{2}x - \frac{1}{2}\log x + \log\lambda
\end{equation}

while $x \gg 1$, $\frac{1}{2}x - \frac{1}{2}\log x \gg 1$, the sign of $D(\nu)_n$ is defined by the relative magnitude of $\frac{1}{2}x - \frac{1}{2}\log x$ and $\log\lambda$.

\subsubsection{Flat approximation}

\begin{equation}
\begin{aligned}
    \bm{\Sigma}_z &= \frac{1}{M}\sigma_q^2\sum_i^N z_i\bm{J}_M + \sigma_\epsilon^2\bm{I}_M = \frac{\sigma_q^2n}{M}\bm{J}_M+\sigma_\epsilon^2\bm{I}_M \\
    \det\bm{\Sigma}_z &= (\sigma_\epsilon^2 + \sigma_q^2\sum_i^N z_i)\sigma_\epsilon^{2(M-1)} = (\sigma_\epsilon^2 + \sigma_q^2n)\sigma_\epsilon^{2(M-1)} \\
    \bm{\Sigma}_z^{-1} &= \frac{1}{\sigma_\epsilon^2}\left(\bm{I}_M - \frac{1}{M}\frac{\sigma_q^2\sum_i^N z_i}{\sigma_q^2\sum_i^N z_i+\sigma_\epsilon^2}\bm{J}_M\right) = \frac{1}{\sigma_\epsilon^2}\left(\bm{I}_M - \frac{1}{M}\frac{1}{1+\frac{\sigma_\epsilon^2}{\sigma_q^2n}}\bm{J}_M\right)
\end{aligned}
\end{equation}

set $M^\ast = M(1+\frac{\sigma_\epsilon^2}{\sigma_q^2n})$, regarding to $\sigma_q\gg\sigma_\epsilon$, thus $M^\ast\approx M$, and

\begin{equation}
    \bm{\Sigma}_z^{-1} = \frac{1}{\sigma_\epsilon^2}\left(\bm{I}_M - \frac{1}{M^\ast}\bm{J}_M\right).
\end{equation}

Thus,

\begin{equation}
\begin{aligned}
    -2\nu =& \frac{1}{\sigma_\epsilon^2}\left[\sum_i^M\left(w_i-\frac{n}{\sqrt{M}}\right)^2-\frac{1}{M^\ast}\left(\sum_i^M w_i-n\sqrt{M}\right)^2\right] \\
    &+ \log(\sigma_q^2n+\sigma_\epsilon^2) - 2n\log\lambda - 2(N-n)\log(1-\lambda) + C.
\end{aligned}
\end{equation}

Additionally,

\begin{equation}
\begin{aligned}
    \left(w_i-\frac{n}{\sqrt{M}}\right)^2 =& \left(w_i - \frac{N}{\sqrt{M}}\right)^2 + 2\left(w_i - \frac{N}{\sqrt{M}}\right)\frac{N-n}{\sqrt{M}} + \frac{(N-n)^2}{M} \\
    \left(\sum_i^M w_i-n\sqrt{M}\right)^2 =& \left(\sum_i^M w_i-N\sqrt{M}\right)^2 + 2\left(\sum_i^M w_i-N\sqrt{M}\right)(N-n)\sqrt{M} + (N-n)^2M.
\end{aligned}
\end{equation}

Some statistics we use below,

\begin{equation}
\begin{aligned}
    \sum_i^M\frac{(w_i - \frac{N}{\sqrt{M}})^2}{\frac{N}{\sqrt{M}}\sigma_q^2+\sigma_\epsilon^2} &\sim \chi^2(M) \\
    \frac{(\sum_i^M w_i-N\sqrt{M})^2}{M^{\frac{3}{2}}N\sigma_q^2+M\sigma_\epsilon^2} &\sim \chi^2(1).
\end{aligned}
\end{equation}

Calculate expectation on distribution of $\bm{w}$ and $\bm{z}$ with $|K|=n$,

\begin{equation}
\begin{aligned}
    -2E(\nu)_n =& \frac{1}{\sigma_\epsilon^2}\left[\left(N\sqrt{M}\sigma_q^2+(N-n)^2\right)\left(1-\frac{M}{M^\ast}\right)+\left(M-\frac{M}{M^\ast}\right)\sigma_\epsilon^2\right] \\
    &+ \log\left(\frac{\sigma_q^2}{\sigma_\epsilon^2}n+1\right) - 2n\log\lambda - 2(N-n)\log(1-\lambda) + C.
\end{aligned}
\end{equation}

Thus,

\begin{equation}
\begin{aligned}
    E(\nu)_n' =& -\frac{1}{2}\Biggl\{\frac{1}{\sigma_\epsilon^2}\Bigl[2(n-N)\left(1-\frac{M}{M^\ast}\right) \\
    &+\left(N\sqrt{M}\sigma_q^2+(N-n)^2+\sigma_\epsilon^2\right)\frac{M}{M^{\ast2}}M^{\ast'}\Bigr] +\frac{1}{n+\frac{\sigma_\epsilon^2}{\sigma_q^2}}\Biggr\} + \log\lambda - \log(1-\lambda)
\end{aligned}
\end{equation}
where $M^{\ast'}=-\frac{M\sigma_\epsilon^2}{\sigma_q^2n^2}$.

When $n=N$,

\begin{equation}
\begin{aligned}
    E(\nu)_n'|_N =& \frac{1}{2}\frac{N}{\left(N+\frac{\sigma_\epsilon^2}{\sigma_q^2}\right)^2}(\sqrt{M}-1) + \log\lambda - \log(1-\lambda).
\end{aligned}
\end{equation}

Remember that $M \gg 1$, then,

\begin{equation}
    C = \frac{1}{2}\frac{N}{\left(N+\frac{\sigma_\epsilon^2}{\sigma_q^2}\right)^2}(\sqrt{M}-1) > 0
\end{equation}

the sign of $E(\nu)_n'|_N$ is defined by the relative magnitude of $C$ and $\log\lambda$.

\subsection{Discussion}

When the time bin is very thin, $\log\lambda \ll 0$, $D(\nu)_n < 0$. When Bernoulli approximation fails, $D(\nu)_n > 0$.

While we easonably assume $E(\nu)_n$ is near its maximum when $n=N_\mathrm{PE}$, considering the fact that RGS in FBMP has imperfect ergodicity, the bias is positive given $D(\nu)_n > 0$ (or $E(\nu)_n'|_N > 0$) as RGS does \emph{not} stop immediately when $n=N_\mathrm{PE}$. Given $D(\nu)_n < 0$ (or $E(\nu)_n'|_N < 0$), the bias is be negative.