% !TeX program = pdfLaTeX
\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{graphicx,psfrag,epsf}
\usepackage{enumerate}
\usepackage{natbib}
\usepackage{url} % not crucial - just used below for the URL

%\pdfminorversion=4
% NOTE: To produce blinded version, replace "0" with "1" below.
\newcommand{\blind}{1}

% DON'T change margins - should be 1 inch all around.
\addtolength{\oddsidemargin}{-.5in}%
\addtolength{\evensidemargin}{-.5in}%
\addtolength{\textwidth}{1in}%
\addtolength{\textheight}{1.3in}%
\addtolength{\topmargin}{-.8in}%

\begin{document}

\def\spacingset#1{\renewcommand{\baselinestretch}%
{#1}\small\normalsize} \spacingset{1}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\if0\blind
{
  \title{\bf Kaggle-in-class Data Challenges Can Boost Student Learning}

  \author{
        Julia Polak \\
    Department of Statistics, University of Melbourne\\
     and \\     Dianne Cook \\
    Department of Econometrics and Business Statistics, Monash University\\
      }
  \maketitle
} \fi

\if1\blind
{
  \bigskip
  \bigskip
  \bigskip
  \begin{center}
    {\LARGE\bf Kaggle-in-class Data Challenges Can Boost Student Learning}
  \end{center}
  \medskip
} \fi

\bigskip
\begin{abstract}
Kaggle is a data modeling competition service, where participants
compete to build a model with lower predictive error than other
participants. Several years ago they released a simplified service that
is ideal for instructors to run competitions in a classroom setting.
This paper describes the results of an experiment to determine if
participating in a predictive modeling competition enhances learning.
The evidence suggests it does. In addition, students were surveyed to
examine if the competition improved engagement and interest in the
class.
\end{abstract}

\noindent%
{\it Keywords:} instructional technology, statistical modeling, data science, statistics education, data mining
\vfill

\newpage
\spacingset{1.45} % DON'T change the spacing!

\providecommand{\tightlist}{%
    \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

\section{Introduction}\label{introduction}

Kaggle \citep{kaggle} is a platform for predictive modelling and
analytics competitions where participants compete to produce the best
predictive model for a given data set. It is well-known for its
competitions (e.g. \citet{darkmatter}), some of which come with rich
monetary prizes (e.g. \citet{heritage}). There are also learning
competitions \citep{kagglelearn}, designed to help novices hone their
data mining skills. Winners are typically expected to share their code,
and occasionally new algorithms have emerged, for example, deep neural
networks \citep{HintonDahi12} and XGBoost \citep{ChenXGBoost}.

In 2015, Kaggle InClass was introduced, as a self-service platform to
conduct competitions. These competitions can be private, limited to
members of a university course, and are easy to setup. This is an
opportunity for educators to provide a vehicle for students to
objectively test their learning of predictive modelling. As a
competition, with an independent clear performance metric, along with a
dynamic leader board, students can see how their model predictions
compare with the models produced by other students. Being able to make
multiple submissions over a several week time frame, enables them to try
out approaches to improve their models. This paper examines the
educational benefits of conducting predictive modeling competitions in
class on performance, engagement and interest.

In the past few years, the educational community started to collect
positive evidence on including competitions in the classroom. None of
these were data analysis competitions. \citet{VanNuland15} ran a
competition assessing anatomical knowledge, as part of an undergraduate
anatomy course. \citet{Calnon12} discusses robotics competitions as part
of computer science education. \citet{Canada15} discusses the
participation of students in externally run artificial intelligence
competitions. All of these studies found significant improvement in
student exam marks accredited to participation in competition.

Classroom competition is an example of active learning, which has been
shown to be pedagogically beneficial. \citet{Prince04} surveyed the
literature and found that all forms of active learning have positive
effect on the learning experience and student achievement. The magnitude
of the effect of different approaches, though, varies. What's more,
\citet{Freeman14} examined 158 studies published in about 50 STEM
educational journals. The authors found that student exam scores
increased by almost half a standard deviation through active learning.
Moreover, students in classes with traditional lecturing were 1.5 times
more likely to fail than their peers in classes with active learning.

A competition, like any other assessment method has its advantages and
disadvantages. It brings the `game' feeling, increases the interest
level among students and motivates for higher performance
(\citet{Shindler09}, p 105). But, it may have negative influence if
constructed poorly. Among the negative influences are increased stress
and anxiety, induced by fearing a low ranking, failure or technology
barriers. In addition, students may invest a disproportionate amount of
time and effort into competition. Despite some received criticism, a
properly set competition can benefit the students greatly. The
competition should be relatively short in duration to avoid consuming
undue energy. Students should be clear about the rules and the goal.
They should be properly rewarded and most important, feel that they have
reasonable chance to win or achieve high mark \citep{Shindler09}.

\citet{Shelley08} raised the need for more quantitative and statistical
analysis of evidence in science education. This paper contributes to
this call by offering statistical analysis of theffects on learning of
classroom data competitions.

\section{Experimental setup}\label{experimental-setup}

\subsection{Data collection}\label{data-collection}

The experiment was conducted during Semester 2 2017. Data was collected
during two classes, one at the University of AB (MAST90083), and one at
CD University (ETC2420/5242).

\subsection{Competition data}\label{competition-data}

Two data sets were compiled for the kaggle challenges: Melbourne
property auction prices and spam classification. The Melbourne auction
price data was collected by extracting information from real estate
auction reports (pdf) collected between Feb 2, 2013 and Dec 17, 2016.
The spam classification data was compiled by graduate students at Iowa
State University as part of a data mining class, in 2009. Data was
compiled by monitoring and extracting information from their emails by
class members, over a period of a week, and manually tagging them as
spam or ham.

Both data sets were split into training and test sets, for the kaggle
challenge. Students had access to the true response variable only for
the training data. For the Melbourne housing data, students were
expected to predict price based on the property characteristics. For the
spam data, students were expected to build a classifier to predict
whether the email as spam or not.

Both data sets are challenging for prediction, with relatively high
error rates.

The training and the testing data sets of the Melbourne auction price
data were similar but not identical across the two institutions. Some of
the variables in the data set were simulated, e.g.~property land size
and house size. The simulated data was generated slightly differently
for different institutions. This was done deliberately to prevent
students passing answers from one institution to another.

\subsection{Participants}\label{participants}

Computational Statistics and Data Mining (MAST90083) is designed for
postgraduate level students with math, statistics, information
technology or actuarial backgrounds. It covers modelling both continuous
(regression) and categorical (classification) response variables. The 63
students were randomized into one of two kaggle competitions, one
focused on regression (R) and the other classification (C). This setup
mimics randomized control trials, which is the 'gold standard, in
experiment design (\citet{Shelley09CH1}, ch.~1). Students built
prediction models and made submissions individually for 16 days, and
then were allowed to form groups to compete for another 7 days.

The reason for this strategy was first to motivate each of the students
to think about modelling and be actively engaged in the competitions
through individual submission. The lecturer allowed participants to
create groups towards the end of the competition to illustrate the
advantages of group work and mixing models. Another motivation for this
stratagem was the university policy, requiring a strategy to assign
students individually in group assignments.

One of the 63 students elected not to take part in the competition, and
another student didn't sit the exam, producing a final sample size of
61.

Statistical Thinking (ETC2420/5242), covers regression, but not
classification, and has a mix of undergraduate and postgraduate
students. Only the 34 postgraduate (5242) students were required to
participate in the kaggle competition, and competed in the regression
(R) challenge. This was run independently from the MAST90083
competition. The 145 undergraduate (2420) students were used as controls
for examining performance of the postgraduate students. Although, it may
be surprising at first glance, the undergraduate students provide a
reasonable control for the graduate students. The class is taught to
both cohorts simultaneously. The entry requirements to the Bachelors of
Commerce at Monash is high, and these students have strong mathematics
backgrounds. In the years prior to this experiment, the undergraduate
scores on the final exam are indistinguishable from those of the
graduate students. The competition ran for one month. Students formed
their own teams of 2-4 members to compete.

\section{Methodology}\label{methodology}

\subsection{Performance}\label{performance}

Better performance is equated to better understanding of the material,
as measured in the final exam. MAST90083 and ETC2420/5242 included
questions, with several parts, on the final exam related to kaggle
challenges. These questions were identified prior to data analysis.

For all questions in the exam, difficulty and discrimination scores were
computed, using the mean and standard deviations. Of the questions
pre-identified as being relevant to the data challenges, only the parts
that corresponded to high level of difficulty and high discrimination
were included in the comparison of performance.

Scores for the relevant questions were summed, and converted into
percentage of the possible score. The total exam score was converted to
a percentage. One can expect, that on average, student's success rate
for each question will be about the same as the success rate in the
total exam. Understanding one topic better than another will result in
higher success rate for questions asking about the better understood
topic compared to the scores for other topics. For example, we would
expect from a student with a 70\% exam mark to get 70\% marks on each of
the questions in the exam, if she has similar knowledge level on all the
exam topics. If in some topic, say regression, the student has better
knowledge, she will perform better on the regression questions. Her
success rate on regression question will be higher than 70\%.
Consequently, her performance on some other questions should be below
70\% which is associated with lesser understanding of these topics.

Therefore, performance for each student was computed as the ratio of
these two numbers, percentage success in the regression (classification)
questions and percentage success in the total exam. A value of 1 would
indicate that the student's performance on that set of questions was
consistent with their overall exam performance, greater than 1 that they
performed better than expected, and lower than 1 meant less than
expected on that topic.

Using only the percentage of successes for each set of questions,
instead of the proposed ratio, will not differentiate between a better
performance and just a better student. Especially in the case of
ETC2420/5242 that have a mixed population of master and undergraduate
student.

The distribution of the performance scores by group is shown as a
boxplot. Focus is on the difference in median between the groups.
Permutation tests were conducted to examine difference in median scores
for students participating or not in a competition.

\subsection{Engagement}\label{engagement}

The students were allowed to submit at most one prediction per day,
while the competitions were open. The frequency of submissions, and the
accuracy (or error) of their predictions, made by individual students,
is recorded as a part of the kaggle system. To examine whether
engagement improved performance, scores on the questions related to the
competition normalised by total exam score (as computed in the
performance section) is examined in relation to frequency of submissions
during the competition. In addition, performance in the competition as
measured by accuracy or error, is also examined in relation to the
number of submissions. Scatterplots, correlation and linear models are
used to examine the associations.

\subsection{Interest}\label{interest}

Students in MAST90083 and ETC5242 were invited to give feedback about
the course, in particular about the data competitions, before the final
exam. This information was voluntary, and students who completed the
questionnaire were rewarded with a coupon for a free coffee. The data
from this survey was viewed by the researchers after all course grades
had been reported. To reduce potential bias in students replies, we
emphasize this point as part of the instruction at the beginning of the
survey.

\section{Results}\label{results}

\subsection{Performance}\label{performance-1}

Figure \ref{fig:MAST90083} shows the data collected in MAST90083.
Performance is plotted against type of question, separately for the
competition they completed. The difference in median scores indicates
performance improvement. Students who completed the classification
competition (left) performed relatively better on the classification
questions than the regression questions in the final exam. Conversely,
students who participated in the regression competition performed
relatively better on the regression questions.

\begin{figure}
\centering
\includegraphics{paper-kaggle_files/figure-latex/Melb-1.pdf}
\caption{\label{fig:MAST90083} Boxplots of performance on regression and
classification questions in the final exam, by type of data competition
completed in MAST90083. Students generally performed better on the
questions corresponding to the competition they participated in.}
\end{figure}

\begin{table}[h]
\begin{center}
\begin{tabular}{|l|r|r|}\hline
Question Set & Median difference & Permutation $p$-value \\\hline
Classification & 0.250 & 0.033\\
Regression & 0.104 & 0.000\\\hline
\end{tabular}
\caption{Comparison of median difference in performance by competition group, for MAST90083 students, using permutation tests. Both sets of medians are significantly different, indicating improved scores for questions on the topic related to the Kaggle competition.}
\end{center}
\label{tab:Melb_Perm}
\end{table}

Table 1 shows the results of permutation testing of median difference
between the groups. Generally the results support the competition
improved performance. Students who participated in the kaggle challenge
for classification scored higher than those that did the regression
competition, on the classification problem. Using a permutation test,
this corresponds to a significant difference in medians. Similarly the
results show that students who did the regression challenge, performed
better on these exam questions.

Figure \ref{fig:ETC5242} shows the results for students ETC2420/5242.
The boxplots suggest that the students who participated in the challenge
performed relatively better than those that didn't on the regression
question than expected given their total exam performance.

Only the post-graduate students participated in the regression
competition, as their additional assessment requirement. Scores for the
question on regression (Q7a,b,c) in the final exam were compared with
the total exam score (RE). On these question parts, a, b, c, over all
the students all three were in the top 10 of difficulty, with students
scoring less than 70\%, on average. Parts b, c were in the top 10 for
discrimination, and part a was at rank 13.

\begin{figure}
\centering
\includegraphics{paper-kaggle_files/figure-latex/ETC5242-1.pdf}
\caption{\label{fig:ETC5242} Performance for regression question
relative to total exam score for students who did and didn't do the
regression data competition in ETC2420/5242.}
\end{figure}

Based on the median, the students who participated in the kaggle
challenge scored 0.09 higher than those that didn't, a median of 1.01 in
comparison to 0.92. Using a permutation test, this corresponds to a
significant difference in medians, with \(p\)-value of 0.015.

\begin{figure}
\centering
\includegraphics{paper-kaggle_files/figure-latex/crossperformances-1.pdf}
\caption{\label{fig:crossperformances} Students performance in
classification and regression questions by competition type.}
\end{figure}

Figure \ref{fig:crossperformances} presents students' scores for
classification and regression questions. A score over 1 is considered
outperformance (relative to the expectation). Quarters one and three
include students that underperform or outperform on both types of
questions, respectively. In both cases the number of students that
participated in the classification competition is very close to the
number of students that participated in the regression competition
(excluding a few regression students on the border of score 1). Students
in quarters two and four outperform on one type of questions but not on
the other type. We can see that more regression students outperform on
regression questions than classification students (12 vs. 7). Similarly,
classification students do better on classification questions (11 vs.
3). This is another evidence towards positive influence of the data
competition on student's performances.

\subsection{Engagement}\label{engagement-1}

The number of submissions that a student made, may be an indicator of
performance on the exam questions related to the competition. A student
who is more engaged in the competition may learn more about the
material, and consequently perform better on the exam. Figure
\ref{fig:numsubmition} (top row) shows performance on the classification
and regression questions, respectively, against their frequency of
prediction submissions for the three student groups (MAST90083
classification and regression, ETC5242 regression) competitions. The
relationship is weak in all groups, and this mirrors insignificant
results from a linear model fit to both subsets. On the other hand, the
predictive accuracy improved with the number of submissions for the
regression competitions. There appears to be some nonlinearity present
in these plots, suggesting reduced returns. That is reasonable to
expect. Also, some students strategically make very poor initial
predictions, to get a baseline on error equivalent to guessing.

The competition performance relative to number of submissions is shown
in plots (d)-(f). Each point corresponds to one student, and accuracy or
error of the best predictions submitted is used. The regression
competition seemed to engage students more than the classification
challenge. Students submitted more predictions, and their models
improved with more submissions.

\begin{figure}
\centering
\includegraphics{paper-kaggle_files/figure-latex/numsubmition-1.pdf}
\caption{\label{fig:numsubmition} Scatterplots of the exam performance
(a-c) and competition performance (d-f) by number of prediction
submissions, for the three student groups. The relationships with exam
performance are weak. For the MAST90083 and ETC5242 regression
competitions, a clear pattern is that predictions improved substantially
with more submissions. (House price in ETC5242 were divided by 100,000,
explaining the difference in magnitude of error between two
competitions.)}
\end{figure}

\subsection{Interest}\label{interest-1}

Figure \ref{fig:likert} shows the survey responses related to the kaggle
competition, for MAST90083 and ETC5242. The response rate for MAST90083
was 55\%, with 34 of 61 students completing the survey. The response
rate for ETC5242 was 50\%, 17 students out of 34 completed the survey.
Overwhelmingly, students reported that they found the competition
interesting and helpful for their learning in the course.

\begin{figure}
\centering
\includegraphics{paper-kaggle_files/figure-latex/likert-1.pdf}
\caption{\label{fig:likert}Summary of responses to survey of kaggle
competition participants. Overwhelmingly the response to the competition
was positive in both classes, especially the questions on enjoyment and
engagement in the class, and obtaining practical experience. (Table 2
lists the questions.)}
\end{figure}

\begin{table}[b!]
\begin{center}
\begin{tabular}{|l|p{15cm}|}\hline
Label & Question \\\hline
Q1 & I found the data competition is great fun.\\\hline
Q2 & Taking part in the data competition contributed a lot to my engagement with the subject.\\\hline
Q3 & Taking part in the data competition improved my confidence in my understanding of the covered material.\\\hline
Q4 & Taking part in the data competition improved my confidence in my success in the final exam.\\\hline
Q5 & Taking part in the data competition improved my confidence in my ability to use the acquired knowledge in practical applications.\\\hline
Q6 & I feel that the required time investment in the data competition was worthy. \\\hline
\end{tabular}
\end{center}
\caption{Questions asked in the survey of competition participants.}
\label{survey_Q}
\end{table}

After collecting the survey from the students we were told that the
questions about students engagement were positively worded. This may
have potentially led to some bias. An improved way could be to ask
directly about student's engagement, e.g. ``How would you rate your
level of engagement in this course?'' with set answer options of ``not
at all engaged'' -- up to ``extremely engaged'' with several choices in
between. Another improvement could be asking ETC2420 students that
didn't take part in the competition about their level of engagement and
compare the answers with other students of ETC5242. We acknowledge that
the differences in the engagement levels may not necessarily be a result
of participation in the competition but it is still an interesting
aspect.

The survey was not anonymous. However, the results became available to
the lectures only after all the grades were realised to the students.
This point was emphasized in the instructions to the students at the
beginning of the survey.

\section{Teachers' corner}\label{teachers-corner}

Creating a new competition is surprisingly easy. Information on setting
up a Kaggle InClass challenge is available on the service's web site
(\url{https://www.kaggle.com/about/inclass/overview}). There is a setup
wizard for step-by-step guidance on getting your competition underway.
We have created a short video illustrating the steps to establish a new
competition, available on the web (\url{https://www.youtube.com/XXXX}).

The kaggle service provides some data sets, primarily for student
self-learning. These are not suitable for use in a class challenge,
because all the data is available, and solutions are also provided. We
recommend providing your own data for the class challenge. Finding a
suitable data set for a competition can be a difficult task. The
criteria for a good data set are:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
  the full set is not available to the students, to avoid cheating.
\item
  the data is not too easy, or too hard, to model so that there is some
  discriminatory power in the results.
\item
  data should be relatively clean, to the point where the instructor has
  tested that a model can be fitted.
\item
  contains some challenges, that make standard off-the-shelf modeling
  less successful, like different variable types that need processing or
  transforming, some outliers, large number of variables.
\item
  if it is a classification challenge, it will work better with
  relatively balanced classes, because the overall accuracy is the
  easiest metric to use.
\end{enumerate}

Choosing the metric upon which to evaluate the model is another
decision. Our advice is to keep it simple, so you, and the students, can
understand the student scores. If you are running a regression
challenge, then ``Root Mean Squared Error (RMSE)''. It is is a
classification challenge, then ``Categorization Accuracy'', the
percentage correct is reasonable.

The data needs to be split into training and testing sets. Kaggle will
then split your test set into two, a public set that is used to provide
ongoing scores to participants, and a private set, on which performance
is revealed only after the competition closes. If you have categorical
variables in the data set, you will want to make sure that all
categories are present in both training and test sets. The training set
will have both predictors and response, but the test set will have the
response variable removed. Each observation needs to be assigned an id,
because this will be needed to evaluate predictions. The solution file,
containing the id and the true response, is provided to the system for
evaluating submissions, and is kept private. A sample submission file
needs to be provided. Participants will submit their solutions in the
same format.

It is a good idea to build a basic model yourself on the training data,
and predict the test data. The performance of this model can be provided
to the participants as baseline to beat.

The competition needs to run without any intervention from the
instructor. The instructor can monitor students progress: the number of
submissions, student scores and even the uploaded data at any time. When
the competition ends the ``Leaderboard'' page provides a list of
students ordered by the final score. It also provides all the scores
from all past submissions (under ``Raw Data'' on ``Public
Leaderboard'').

It may be recommended to limit students to one submission per day. It
encourages students to think about more efficient improvement of their
model before the next submission. It also prevents the student spending
too much time building and submitting models. Some students will become
so engaged in the competition that they might neglect their other
coursework. About halfway through the coompetition, students might be
allowed to form teams, to learn how averaging models can boost
performance.

In awarding course points to student effort, we typically align it to
performance. Participant ranks based on their performance on the private
part of the test data are recorded. Performance scores that are pretty
close to each other should be given the same rank, reflecting that there
may not be a significant difference between them. The best gets perhaps,
5 points, then a half a point drop until about 2.5 points, so that the
worst performing students still get 50\% for the task. Kaggle does not
allow you to download participants email addresses. All you see is their
kaggle name. Record the student names in kaggle to match with your class
records.

Along with the competition, students were expected to submit a report
that explained their modeling strategy, and what they had learned about
the data beyond the modeling. The overall score for this part of the
course was a combination of the mark for their report and their
performance in the challenge. In both courses this accounted for 10\% of
the final mark.

From an instructor perspective, its very rewarding watching the students
participate in the competition. It provides a truly objective way to
assess their ability to model in practice. Students are often motivated
to consult with the instructor about why their model is underperforming,
or what other approaches might produce better results.

\section{Discussion}\label{discussion}

This paper has described the setup and results of an experiment to
examine the effectiveness of data competitions on student learning,
using Kaggle InClass as the vehicle for conducting the competition. The
experiment was conducted in the classroom settings as part of the normal
teaching of the courses, which imposes limitations on the design.
However, with both MAST90083 and ETC2420/5242, either a randomized
assignment of students to two topic groups, or a control group, was
possible, which enabled comparing performance.

The primary finding, is that participating in a data challenge
competition, produces a statistically significant improvement in the
learning of the topic, although the effect size is small. Secondarily,
anecdotally, the competitions enhanced interest and engagement in the
course.

\section{Future work}\label{future-work}

This work is one of a few quantitative analyses of data competition
influences on students' performance. More evidence needs to be collected
from other STEM courses to explore consistent positive influence.
Moreover, future investigation is required to understand the influence
of the different aspects of data competition implementation on the
magnitude of the performance improvement. For example, the competition
duration, availability and accessibility of additional material,
requirement of writing a final report or giving a short oral
presentation are elements worth investigating. Prior and post testing of
students might provide a better experimental design.

\section{Acknowledgments}\label{acknowledgments}

This project (title: Effect of Data Competition on Learning Experience)
has been approved by the Faculty of Science Human Ethics Advisory Group
University of AB (ID: 1749858.1 on September 4, 2017) and by CD
University Human Research Ethics Committee (ID: 9985 on August 24,
2017).

This document was produced in R \citep{R} with the package knitr
\citep{knitr}. Data cleaning was conducted using tidyr \citep{tidyr},
dplyr \citep{dplyr} and plots were made with ggplot2 \citep{ggplot2}.
The materials to reproduce the work are available at
\url{https://github.com/XXX}.

\bibliographystyle{agsm}
\bibliography{bibliography.bib}

\end{document}


\section{Introduction}

Before introduce how to write a good introduction, I want first emphasize the importance of introduction section as below (for the details, see the slides in ``Slides'' folder of this project):

Spend some time to pick a good title for your paper!!! If your paper later has 1000 hundred of readers, 1000 will read your title, 100 read abstract, 100 read introduction, 10 read your problem definition, 10 read related work, maybe just 10 or less than 10 will read your solutions and only 5 read the details of your solution at the end.

Usually, you should write the introduction section two times! 1) Write it first: tell a clear story; 2) write it last: make sure it is telling what you really do in your finished paper.

Here is a good structure for introduction section:

Paragraph 1: Context. What is the background? At most 4 sentences please!

Paragraph 2: Problem area. Some related studies. What is the problem in this area? Don't tell too detailed about the related studies, which is what ``Related Work'' will do.  About 3 sentences are enough.

Paragraph 3-4: What you do in this paper? What is the key point of this paper? You can have a motivation example to show your niche! Usually, people can understand example better than what you explained. Better to draw an interesting figure for you problem. Like the example \cite{cheng2016task} below:

\begin{figure}[h!]\centering
	\scalebox{0.7}[0.7]{\includegraphics{../figures/motivation_example.eps}}
	\caption{\small An Example of Motivation Example.}
	\label{fig:bbq_example}
\end{figure}

Paragraph 5: challenges in this novel problem. Why it is hard? Why existing solutions cannot solve it?

Paragraph 6: What you have done on solving it?  Similar to the introduction, but more details. Brief summary of results.

Paragraph 7: Outline of this paper. You can modify the example below:

To summarize, we make the following contributions in the paper:
\begin{itemize}[leftmargin=*]
	\item We propose a XXXX problem and prove it is NP-hard in Section \ref{sec:problemDefinition}.\itemMargin
	\item  We proposed solution1 and solution2 in Section \ref{sec:solution1} and \ref{sec:solution2}, respectively.\itemMargin
	\item We have  conducted extensive experiments on real and synthetic data sets, to show the efficiency and effectiveness of our XXX framework/algorithm in Section \ref{sec:experimental}.\itemMargin
\end{itemize}

In addition, the remaining sections of the paper are arranged as
follows. We review and compare previous studies on queueing theory and vehicle dispatching in Section \ref{sec:related} and conclude the work in Section \ref{sec:conclusion}.


% This is samplepaper.tex, a sample chapter demonstrating the
% LLNCS macro package for Springer Computer Science proceedings;
% Version 2.20 of 2017/10/04
%
\documentclass[runningheads]{llncs}
%
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage{float}
\usepackage{amsmath}
\usepackage{verbatimbox}
\usepackage[dvipsnames]{xcolor}
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=blue,
    pdftitle={Overleaf Example},
    pdfpagemode=FullScreen,
    }
% Used for displaying a sample figure. If possible, figure files should
% be included in EPS format.
%
% If you use the hyperref package, please uncomment the following line
% to display URLs in blue roman font according to Springer's eBook style:
% \renewcommand\UrlFont{\color{blue}\rmfamily}

\DeclareMathOperator{\dequeue}{dequeue}
\DeclareMathOperator{\enqueue}{enqueue}

\newcommand{\todo}[1]{{\leavevmode\textcolor{red}{\sf \bf [#1]}}}

\begin{document}
%
\title{Functional Queue Visualization}
%
%\titlerunning{Abbreviated paper title}
% If the paper title is too long for the running head, you can set
% an abbreviated paper title here
%
\author{Gaurav Arya \and Shana Mathew \and Stuti Vishwabhan}
%
% First names are abbreviated in the running head.
% If there are more than two authors, 'et al.' is used.
%
%
\maketitle              % typeset the header of the contribution
%
%
%
%

In our 6.851 Advanced Data Structures class, we learned about functional data structures. A functional data structure can never be modified. Instead, when performing an operation, a new copy of the data structure is returned. This is a neat concept: previous versions of the data structure remain accessible forever, implying other nice properties such as full persistence.

A functional stack is probably the simplest example of a functional data structure. It consists solely of a pointer to the head of a one-way linked list, which contains the stack's elements from top to bottom. To push, we add a new element to the one-way linked list, and return the pointer to this new element. To pop, we return a pointer to the second element of the linked list.

Functional queues are a natural next step. Indeed, to design one was the task given to us in Problem Set 2 of 6.851. We were to achieve the same runtime as functional stacks, namely $O(1)$ for both enqueue and dequeue. However, this turns out to be much harder. When attempting a linked-list solution analagous to stacks, we are forced to grow the list in both directions -- this is impossible, since we cannot add pointers to a node that has already been created.

Remarkably, there is a solution: using our functional stacks as a black box, we can simulate each queue operation using $O(1)$ operations on a set of \emph{six} stacks. These operations can be confusing, as we discovered when working on Problem Set 2. The primary goal of our visualizer is to illustrate how our queue uses the six stacks to achieve $O(1)$ for enqueue and dequeue. To demonstrate the full persistence of functional queues, we also provide a version history which the user can use to view and apply operations to previous versions.

In this write-up, we first discuss the design process of the visualizer and the visualizer's final functionality. We then discuss implementation details, and the challenges we faced during the course of our project. We conclude by discussing scope for future improvements. % can change this order later

% note: include below stuff later after reorganizing

%Our own experience when working on Problem Set 2 (Functional Queues) this term led us to this realization as we struggled to draw our own diagrams to understand how the functional queue data structure can be represented as multiple stacks that we could perform \texttt{delete-first(Q)} and \texttt{insert-last(Q,x)} operations on.

% In this write-up, we introduce a Functional Queue Visualization that has 2 main purposes: 1) visualizing stack changes as new operations are performed and 2) visualizing a history of the queues that maintain full persistence of the data structure. Visualizing the stack changes allows users to perform the \texttt{delete-first(Q)} and \texttt{insert-last(Q,x)} operations and visualize through our interface how elements in each of the intermediary stacks change. Each operation is accompanied with a series of steps that provide detailed explanation as to how stacks are changing as well as helpful highlighting and animation. Visualizing the history allows users to see all versions of the queue and update any version as well. Our interface allows users to click on a particular version of the queue and perform new operations on it while preserving previous versions.

%

\section{Design}

\subsection{Functional Queue Data Structure}

The first goal of our project was to implement the functional queue data structure. More precisely, we wanted to implement the following interface:
\begin{verbatim}
    Q' = Queue.enqueue(Q, x); // Q' is new functional queue with value x enqueued.
    Q' = Queue.dequeue(Q); // Q' is new functional queue with head of Q dequeued.
    val = Queue.head(Q); // val is head element of the queue.
\end{verbatim}

Given that our main goal was to visualize the functional queue rather than implement it, it was not strictly necessary to provide a functional interface like the one above. However, for a multitude of reasons such as conceptual simplicity and supporting the version history, we decided that this would be the best choice.

Since each functional queue consists of multiple functional stacks, we also implemented the following interface for a functional stack:
\begin{verbatim}
    S' = Stack.push(S, x); // S' is new functional stack with value x pushed.
    S' = Stack.pop(S); // S' is new functional stack with head of S popped.
    val = Stack.head(S); // val is head element of the stack.
\end{verbatim}

The full functional queue implementation was based directly on one of our problem set writeups, and we also made reference to \cite{hood}. The details are in \texttt{writeups/functional.pdf} in the Git repository.

\subsection{Augmenting the Functional Queue Data Structure}

While it was a good start, the above interface did not provide sufficient information for visualization. Firstly, given a functional queue, the visualizer needed the ability to look at its inner state. For this purpose, we allowed the visualizer to access each functional stack of the functional queue, and implemented the utility function \texttt{Stack.listAllElements} to turn a functional stack into a Javascript list. Now, the visualizer would have sufficient information to show the state of all six stacks before and after an operation.

However, although each operation uses $O(1)$ pointer operations, it is very difficult to see how these were done by looking at only the beginning and end state of the functional queue. Instead, we wanted to break up each operation into $O(1)$ ``simple'' moves for which it is clear to the user what happened. To do this, we augmented each \texttt{Queue.enqueue} and \texttt{Queue.dequeue} operation to also populate an array of \texttt{moves}. Each \texttt{move} object stores the queue state before and after the move, as well as metadata such as the ``type'' of move (push/pop to a single stack, moving a value between stacks, and various moves for beginning and ending transfer mode) and the stacks involved. This gives the visualizer sufficient information to break each operation into multiple simple steps, and to improve the visualization of each step through explanations and animations.

\subsection{User Input}

\begin{figure}[H]
    \centering
    \includegraphics[width=0.8\textwidth]{input.png}
    \caption{Enqueue and dequeue operations}
\end{figure}

Thanks to the simplicity of the queue interface, handling user input was the simplest part of our project. When a particular queue version $Q_i$ is selected, the user can either create $\dequeue(Q_i)$ or $\enqueue(Q_i, x)$ for some inputted value $x$ by using the input box and the two buttons.

\subsection{Basic Stacks View}

In visualizing the state of the six stacks, we decided to use a row for each stack, and display it using a minimalist design with arrows and boxes. We elected to show all the pointers within each stack (which is what the arrows are for).

An alternative would have been to take the use of functional stacks as a black box even further, and visualize each stack as a literal stack of values. We decided on the pointer approach because it makes it clearer why the queue is functional, and also because it makes it more apparent how some operations (such as copying the elements of one stack into another) are able to be accomplished in $O(1)$ by pointer assignment. However, we elected not to show the entire hidden web of pointers and all the elements not currently in our queue because it would be difficult for the user to follow. It would also go against our minimalist pure HTML design.

\begin{figure}[H]
    \centering
    \includegraphics[width=0.8\textwidth]{stacks.png}
    \caption{The stacks view after performing some push operations on the queue.}
\end{figure}

\subsection{Individual Moves and Animations}

As discussed earlier, we would like to visualize each simple move in an operation. To do this, we needed our stacks view to process a \texttt{move} object and visualize it a nice way. First, we used the metadata in each move to provide an explanation of what the move does (i.e. what pointer operation or functional stack operation it does). For pushing, popping, and moving elements, we highlighted the appropriate elements in green/red and faded them in and out.

For the moves for begin transfer and end transfer, the situation was slightly more complicated, since this consisted of assigning a pointer for a stack to another pointer. Originally, this resulted in a very abrupt visual change. We decided to split up end transfer into 3 moves, such that each move only made one stack assignment. We then highlighted all the elements that get ``added'' to the new stack in green, and highlighted the elements of the stack that get copied into the ``new stack'' in blue. After playing around with this effect, with the help of the explanatory text above it became quite clear that the operation was actually a single pointer assignment.

\begin{figure}[H]
    \centering
    \includegraphics[width=0.6\textwidth]{highlight.png}
    \caption{The state of the stacks view when visualizing the move for begin transfer.}
\end{figure}

Finally, to navigate between the moves of each operation, we added three modes: manual, automatic, and skip to end. In manual mode, the user must use arrow buttons to go forwards/backwards between the moves. In automatic mode, we automatically step forward at regular intervals, and the pace is controlled by a speed slider. In skip to end mode, the queue immediately jumps to its final state after the operation.

\subsection{Version History}

The version history was more difficult than expected because of the large number of versions that are created. We eventually decided to implement both a linear version history and graph version history. Users can toggle a switch to choose which view they would like to see.

\subsubsection{Linear Version History}

In the linear version history, new versions are added to a bottom of a scrolling list of versions. When clicking on a version, all of its ancestors (the queues that were operated on to eventually create this version) are highlighted. The version history is written to mimic the style of pseudocode, and the user is able to see what operations created each queue and a list of elements for the current queue.

\begin{figure}[H]
    \centering
    \includegraphics[width=0.6\textwidth]{linear.png}
    \caption{The linear version history view with version Q\textsubscript{8} selected.}
\end{figure}

\subsubsection{Graph Version History}

In the graph version history, each version is represented as a node. The versions are laid out vertically as a tree, where an edge connects a parent version to the child version that was created from the parent. Similar to the linear version history, the current version that is being displayed in the stack view is highlighted in green. Users can click on a different version to display a new version in the stack view and can perform enqueue/dequeue operations on the selected version.

When the user hovers over a particular version with their mouse, a tooltip displaying the queue version number appears. This is particularly useful in identifying versions when there are so many nodes in the tree that they overlap. While we considered maintaining enough space between each node to prevent overlap, this came at the expense of the user having to scroll up and down the page to view different sections of the tree. We felt like the main benefit of the graph view was that a user can easily see the history of every version, and so seeing the entire graph at once without scrolling was important.

\begin{figure}[h!]
    \centering
    \includegraphics[width=.9\textwidth]{graph-version-history.PNG}
    \caption{The graph version history view with version Q\textsubscript{8} selected.}
\end{figure}

\subsection{Other Design Choices}

At the start of the project, as we were considering what language to base our code in, we decided between Python and JavaScript. While some of us had more experience with Python, we ultimately chose to code the project in JavaScript since it had better support with the framework and libraries we planned on using. In particular, we chose to use React since it allowed us to create an interactive UI with minimal setup time. Additionally, using React allowed us to take advantage of a UI library called Ant Design which is well-known its sleek, minimal icons and components.

As we were developing the version history, we noticed that users would have to scroll back and forth from the version history at the bottom to the input \& stack view at the top. We ended up implementing a version where the stack view and version history were side-by-side so that users could see both at the same time, but in the end, decided to revert this change since we felt like the primary focus should be on the stack view. Having these two components next to each other minimized both of their horizontal real estate, and ended up causing users to have to scroll horizontally in order to get a sense of the stack structure.

%\todo{not necessary, but we could talk about the color scheme (seagreen chosen to not distract, etc) and/or why we chose react over other frameworks}

\section{Implementation details}
This interface is a React application with embedded Ant Design components and vanilla HTML/CSS/JavaScript.

For the graph version of history, we used visx, a collection of low-level visualization primitives for React that build on top of D3.js. In particular, we used the visx tree example~\cite{vx-example} as a framework for our own code.

\subsection{Instructions for local setup}

This application requires that Node.js and npm be installed. Once npm is installed, the command \texttt{npm install yarn} can be run in order to install the package manager used to easily launch the code locally. The repository containing the code can be found at \href{https://github.com/6851-2021/functional-queue-visualization}{https://github.com/6851-2021/functional-queue-visualization}. Once it is cloned/downloaded, navigate to the \texttt{/app} directory, run \texttt{npm install} to install any package dependencies, and then run \texttt{yarn start} to launch the application. The app should open at \texttt{localhost:3000} in your browser. We recommend using Chrome as we primarily developed and tested using this browser. %\todo{were other browsers developed on?}

\section{Future Work}

As hinted at earlier, a really interesting additional feature would be to visualize the network of all pointers and nodes ever created, rather than just those relevant to the currently selected queue. This visualization could be done with an external library, similar to the graphical version history.

Internally, visualizing a queue takes $O(n)$ time, since we internally list all the elements of each stack and perform operations on them. The version history, as implemented, also takes $O(n)$. It would be interesting to try to update the visualizer's entire state in $O(1)$. However, since DOM operations will likely always be the bottleneck, it is unclear whether it would be possible to achieve significant performance improvements.

Overall, we are very happy with how this project has turned out and hope it can be published and put to good use in helping others understand how to implement functional queues in $O(1)$ per operation.

% visualization is not  O(1)
% showing hidden pointers

% \section{Section Sample}
% \subsection{A Subsection Sample}
% Please note that the first paragraph of a section or subsection is
% not indented. The first paragraph that follows a table, figure,
% equation etc. does not need an indent, either.

% Subsequent paragraphs, however, are indented.

% \subsubsection{Sample Heading (Third Level)} Only two levels of
% headings should be numbered. Lower level headings remain unnumbered;
% they are formatted as run-in headings.

% \paragraph{Sample Heading (Fourth Level)}
% The contribution should contain no more than four levels of
% headings. Table~\ref{tab1} gives a summary of all heading levels.

% \begin{table}
% \caption{Table captions should be placed above the
% tables.}\label{tab1}
% \begin{tabular}{|l|l|l|}
% \hline
% Heading level &  Example & Font size and style\\
% \hline
% Title (centered) &  {\Large\bfseries Lecture Notes} & 14 point, bold\\
% 1st-level heading &  {\large\bfseries 1 Introduction} & 12 point, bold\\
% 2nd-level heading & {\bfseries 2.1 Printing Area} & 10 point, bold\\
% 3rd-level heading & {\bfseries Run-in Heading in Bold.} Text follows & 10 point, bold\\
% 4th-level heading & {\itshape Lowest Level Heading.} Text follows & 10 point, italic\\
% \hline
% \end{tabular}
% \end{table}

% \noindent Displayed equations are centered and set on a separate
% line.
% \begin{equation}
% x + y = z
% \end{equation}
% Please try to avoid rasterized images for line-art diagrams and
% schemas. Whenever possible, use vector graphics instead (see
% Fig.~\ref{fig1}).

% \lipsum[44]

% \begin{figure}
% \includegraphics[width=\textwidth]{fig1.eps}
% \caption{A figure caption is always placed below the illustration.
% Please note that short captions are centered, while long ones are
% justified by the macro package automatically.} \label{fig1}
% \end{figure}

%
% ---- Bibliography ----
%
% BibTeX users should specify bibliography style 'splncs04'.
% References will then be sorted and formatted in the correct style.
%
\bibliographystyle{refs-style}
\bibliography{refs}
%
\end{document}


%% State Space Modelling of Dynamic Systems
%% Lecture 1: Introduction to State-Space Models
\def\FileDate{9/9/29}
\def\FileVersion{1.1}
% ----------------------------------------------------------------
% Notes pages *********************************************************
% ----------------------------------------------------------------
\section*{Introduction to State Space}

\begin{slide}\label{slides:l13s1}
\heading{Introduction to State Space}
\begin{itemize}
	\item \emph{Classical control system theory} is based on the Transfer Function model.
	\item Can only be used for systems that are defined by LTI differential equations.
	\item \emph{Modern control systems theory} is based on a time-domain description of systems.
	\item Convenient matrix form makes the model attractive for simulation, analysis and design using packages like Matlab.
	\item All classical control methods seen so far also apply to state-space models.
\end{itemize}
\end{slide}

Classical control system theory is based on the Transfer Function (TF) model. This applies only to those dynamic systems which can be described by linear time-invariant (LTI) differential equations. TF models are usually applied to Single Input Single Output (SISO) systems. Since the TF is expressed in terms of the Laplace Transform variable, $s$, analysis and design is mostly carried out in the $s$-domain or the frequency domain ($s=j\omega$).

Modern control systems theory is based on a time domain description of a system in terms of a set of first order coupled differential equations in the so-called \emph{state variables} of the system.

State space models can do anything that classical TF models can do.

In addition state space models have allowed control theory to be extended in various directions as shown in \sref{slides:l13s2}

\begin{slide}\label{slides:l13s2}
	\heading{The Advances Enabled by State Space Models}
\begin{enumerate}
\item Handling Multiple Input Multiple Output (MIMO) systems.
\item Extension to linear time-varying systems.
\item Clarification of issues of controllability and observability.
\item Development of powerful compensation methods via state feedback and observers.
\item Development of optimal control theory.
\end{enumerate}
\end{slide}
In this module we will restrict ourselves to the study of mostly to SISO LTI systems, but is important to note the possibility of its extension to more challenging areas.

\subsection*{Construction of State Space Models}

The state-space model is a form of system representation that is used
in several engineering disciplines. It is particularly used in control
and in signal processing.

The state-space model is a form of differential equation
representation and it is principally used when an analysis of the
system behaviour is required in terms of time responses. That
stated, it is relatively easy to convert a state-space model into
a transfer function, to allow the frequency response analysis of a
system. It is however not necessary to do this conversion if the
time-response behaviour is all that is required.

The state space model is easily extended to cope with models with more
than one input and more than one output. It also has more favourable
numerical properties that make it more attractive as a representation
for high order systems\footnote{Systems with large numbers of
  derivative terms} than the polynomial representation provided by the
transfer function. State-space models are easily simulated by the
straightforward application of numerical integration.

\sref{slides:l13s1} shows a simple electrical circuit. We shall develop
this circuit into a block diagram and from the block-diagram we shall
develop the state-space model. Later we will generalise this result.

\begin{slide}\label{slides:l13s1}
\heading{Example 1}
\begin{center}
\resizebox{150pt}{!}{\includegraphics{pictures/circuit.pdf}}
\end{center}
\end{slide}

\ifslidesonly
\begin{slide}\label{opteq:l13e1a}
\heading{Equations}
\input{frag1a}
\end{slide}
\fi\input{frag1a}

Since the system ``source'' is $u$, we can construct the block diagram
systematically by tracing the equations through from the source. We
also introduce the additional constraint that we would like the derivative terms
$dv_{31}/dt$ and $di_1/dt$ to
appear as inputs to integrator blocks whose outputs are therefore
\ifslidesonly
\begin{slide}\label{opteq:l13e1b}
\heading{Equations (continued)}
Integrator equations:
\input{frag1b}
\end{slide}
\fi\input{frag1b}
The other
components of equations (\ref{eq:l13e1}) to (\ref{eq:l13e3}) appear as
gain blocks and (\ref{eq:l13e4}) and (\ref{eq:l13e5}) appear as summing
junctions.  With these constraints, the resulting block diagram is
that shown in \sref{slides:l13s2}.

We can model these equations in \Matlab{}/\Simulink{}\footnote{Note
  that in \Simulink{} triangular blocks are used for gains and that the transfer function
  block $1/s$ represents the integral operator $\int$. The small
  elliptical blocks represent input and output ports.} as shown in \sref{slides:l13s1a}.
\begin{slide}\label{slides:l13s1a}
\heading{Modelled Equations}
\resizebox{300pt}{!}{\rotatebox{90}{\includegraphics{pictures/blocks.pdf}}}
\end{slide}
Combining these blocks such that the input is $u$ and the output is
the current flowing through the inductance $i_1$\footnote{We could have used any signal as an output as we shall
  see later.} we obtain the block diagram shown in \sref{slides:l13s2}.
\begin{slide}\label{slides:l13s2}
\heading{Example as a Block Diagram}
\resizebox{300pt}{!}{\rotatebox{90}{\includegraphics{pictures/blockdiag.pdf}}}
\end{slide}

Having constructed a block diagram that allows us the visualize
the structure of the differential equations, we now go on to
create the state-space model of the system. To do this we first
identify the ``\emph{state-variables}'' which are (in this case)
the physical quantities that are changing with time, i.e. the
voltage across the capacitor $v_{31}$ and the current through the
inductor $i_1$\footnote{A state variable that can be related to a
physical quantity is called a ``\emph{physical state
variable}.''}. The derivatives of these state variables become the
left-hand-side of the ``\emph{state equations}''. These equations
have apparently already been written down as (\ref{eq:l13e1}) and
(\ref{eq:l13e2}), but we impose an additional condition that the
state equations can only involve the state-variables, their
derivatives and the system input. Thus we have to trace the path
back through the block diagram from the inputs to the integrator
blocks to the (nearest) state variable(s). The result is
\ifslidesonly
shown in \sref{slide:ss1}
\begin{slide}\label{slide:ss1}
\heading{State Equations for the Example}
\input{frag2}
\end{slide}
\fi\input{frag2}

Equations (\ref{eq:l13e8}) and (\ref{eq:l13e9}) together form a pair
of simultaneous equations (they must both be satisfied by the
dynamic response of the circuit voltages and currents to the
input) and they may therefore be written in vector
form\footnote{You should expand this matrix equation
(\ref{eq:l13e8}) out to verify that it is equivalent to
(\ref{eq:l13e6}) and (\ref{eq:l13e7}).}:
\begin{equation}\label{eq:l13e10}
\input{frag3}
\end{equation}
\ifslidesonly
\begin{slide}
\heading{State equations in vector form}
\begin{displaymath}
\input{frag3}
\end{displaymath}
\input{frag4}
\end{slide}
\fi
\input{frag4}\footnote{The matrix
operator $[]^T$ is the ``transpose'' operator. In this case it
converts the row vector shown into the column vector actually used
in the state equations. When applied to a matrix, the rows of the
matrix become the columns of the transposed matrix. We shall use
the transpose operator in the discussion of state equations to
avoid messy attempts to write column vectors in the body of a
sentence!}

We can generalize this result by defining general state variables
$x_1=v_{31}$ and $x_2=i_1$. Using the notational shorthand
$\dot{x}=dx/dt$ we then get:
\begin{equation}\label{eq:l13e11}
  \left[\begin{array}{c}
    \dot{x_1} \\
    \dot{x_2} \\
    \end{array}\right]=\left[\begin{array}{cc}
      0 & -1/C \\
      1/L & -R/L \\
    \end{array}\right]\left[\begin{array}{c}
      x_1 \\
      x_2 \\
    \end{array}\right]+\left[\begin{array}{c}
      1/C \\
      0 \\
    \end{array}\right]\ u
\end{equation}
\ifslidesonly
\begin{slide}
\heading{Generalising the Equations}
We can generalize the result by defining general state variables
$x_1=v_{31}$ and $x_2=i_1$. Using the notational shorthand
$\dot{x}=dx/dt$ we then get:
\begin{displaymath}
  \left[\begin{array}{c}
    \dot{x_1} \\
    \dot{x_2} \\
    \end{array}\right]=\left[\begin{array}{cc}
      0 & -1/C \\
      1/L & -R/L \\
    \end{array}\right]\left[\begin{array}{c}
      x_1 \\
      x_2 \\
    \end{array}\right]+\left[\begin{array}{c}
      1/C \\
      0 \\
    \end{array}\right]\ u
\end{displaymath}
\end{slide}
\fi

\subsection*{General State Space Models of Dynamic Systems}

An $n^\mathrm{th}$ order dynamic system can be described by an $n^\mathrm{th}$ order differential equation in one dependent variable, $y(t)$, and an input forcing function, $u(t)$, with time, $t$, as the independent variable.

\begin{equation}
\frac{d^ny}{dt^n}=f\left(\frac{d^{n-1}y}{dt^{n-1}}, \frac{d^{n-2}y}{dt^{n-2}}, \ldots, \frac{dx_2}{dt}, \frac{dy}{dt}, y, u(t), t \right) \label{eq:l13e12}
\end{equation}
\ifslidesonly
\begin{slide}
	\heading{General State Space Models (1)}
	\begin{equation}
	\frac{d^ny}{dt^n}=f\left(\frac{d^{n-1}y}{dt^{n-1}}, \frac{d^{n-2}y}{dt^{n-2}}, \ldots, \frac{dx_2}{dt}, \frac{dy}{dt}, y, u(t), t \right) \nonumber
	\end{equation}
\end{slide}
\fi
Alternatively, in a state space model extra variables, called \textbf{states}, are introduced to create an equivalent description, but this time involving only 1st order differential equations.

\begin{eqnarray}
	x_1 & = & y \label{eq:l13e13a}\\
	\frac{dx_1}{dt} & = & \frac{dy}{dt} = x_2  \label{eq:l13e13b} \\
    \frac{dx_2}{dt} & = & \frac{d^2y}{dt^2} = x_3   \\
	 \vdots      \nonumber \\
	\frac{dx_{n-2}}{dt} & = & \frac{d^{n-2}y}{dt^{n-2}} = x_{n-1}   \\
	\frac{dx_{n-1}}{dt} & = & \frac{d^{n-1}y}{dt^{n-1}} = x_n   \\
    \frac{dx_n}{dt} &=& \frac{d^{n}y}{dt^{n}} =  f\left(x_n, x_{n-1}, \ldots, x_3, x_2, x_1, u(t), t \right) \label{eq:l13e13c}
\end{eqnarray}
\ifslidesonly
\begin{slide}
	\heading{General State Space Models (2)}
	\begin{eqnarray}
		x_1 & = & y \\
		\frac{dx_1}{dt} & = & \frac{dy}{dt} = x_2  \\
	    \frac{dx_2}{dt} & = & \frac{d^2y}{dt^2} = x_3   \\
		 \vdots      \nonumber \\
		\frac{dx_{n-2}}{dt} & = & \frac{d^{n-2}y}{dt^{n-2}} = x_{n-1}   \\
		\frac{dx_{n-1}}{dt} & = & \frac{d^{n-1}y}{dt^{n-1}} = x_n   \\
	    \frac{dx_n}{dt} &=& \frac{d^{n}y}{dt^{n}} =  f\left(x_n, x_{n-1}, \ldots, x_3, x_2, x_1, u(t), t \right)
	\end{eqnarray}
\end{slide}
\fi
An nth order system gives rise to a state space model consisting of n coupled 1st order differential equations (the \emph{state equations} (\ref{eq:l13e13b})--(\ref{eq:l13e13c})) in terms of $n$ state variables and the input forcing function(s) . In addition there are the \emph{output equations} (\ref{eq:l13e13a}) expressing other variables, or outputs, of interest, also in terms of the states and inputs.

You should note that the formulation used in the differential equation (\ref{eq:l13e12}) and its equivalent state equations (\ref{eq:l13e13a})--(\ref{eq:l13e13c}) is completely general and makes no assumptions about the nature of $f$. In fact the only condition, is that it represents a lumped parameter system rather than a distributed parameter system. The need to solve such equations by simulation is the basis of the integral models we introduced in Lecture 2.

\subsubsection*{Example 2}

Derive a state space model for the system described by the differential equation:

$$\frac{d^2y}{dt^2}+3\frac{dy}{dt}+2y=u(t)$$
\ifslidesonly
\begin{slide}
	\heading{Example 2}

	Derive a state space model for the system described by the differential equation:

	$$\frac{d^2y}{dt^2}+3\frac{dy}{dt}+2y=u(t)$$

	You should try this now. We will give the solution in the live class.
\end{slide}
\fi
Introducing state variables $x_1=y$ and $x_2=dy/dt$ then the state equations are:
$$\frac{dx_1}{dt}=\frac{dy}{dt}=x_2$$
$$\frac{dx_2}{dt}=\frac{d^2y}{dt^2}=-2x_1 - 3x_2 + u$$
and the output equation is:
$$y=x_1.$$

\begin{slide}
\heading{Physical states}
The states are often chosen to have a direct physical significance such as:

\begin{itemize}
	\item \emph{Electrical Systems} -- the charge or voltage on a capacitor or the current in an inductor.
    \item \emph{Mechanical Systems} -- the force in springs or the velocity of mass or angular velocity of moments of inertia.
    \item \emph{Aerospace Systems} -- forward velocity, thrust, pitch and pitch rate, yaw and yaw rate, position and velocity of control surfaces.
    \item \emph{Chemical Process Plant} -- chemical composition, levels, temperatures, pressures and flows.
\end{itemize}
\end{slide}

\begin{slide}
	\heading{Example 3}
	Derive the state equations for the Spring-Mass-Damper system shown in the figure.
	\begin{center}
		\resizebox{100pt}{!}{\includegraphics{pictures/smd.pdf}}
	\end{center}

	You should try this now. We will give the solution in the live class.
\end{slide}

For this system, summing the forces in the direction $x$ we have

$$F = m\frac{d^2x}{dt^2} + b\frac{dx}{dt} + kx$$

If we chose the position of the mass $x$ and it's velocity $dx/dt$ to be the states, and let the force $F$ be the system input, then $x_1 = x$, $x_2 = dx/dt$ and $u=F$ and the state equations are:

\begin{eqnarray*}
	\frac{dx_1}{dt} & = & x_2 \\
	\frac{dx_2}{dt} & = & -\frac{k}{m} x_1 -\frac{b}{m} x_2 + \frac{1}{m} u
\end{eqnarray*}

You should construct the Simulink model represented by these equations and compare the results with those shown in Lecture 2.

\subsection*{The nature of the states}

\begin{itemize}
	\item The state equations can be solved uniquely when the input functions are given together with the initial values of all the states.
	\item The solutions for the states are substituted into the output equations to obtain the solutions for the other variables in the system.
	\item In the above examples the states are physical quantities associated with energy storage in the system. The energy stored in a inductor is $E_L=Li_L^2/2$, and the energy stored in a capacitor is $E_C=CV_C^2/2$.
	\item Similarly for mechanical systems the velocity of a mass relates to its kinetic energy and the force in a spring relates to its elastic potential energy.
	\item The states encode the configuration of the system, or its whole past history.
	\item The choice of states is not unique, but they must be chosen such that the configuration of the system can be unambiguously and completely determined from them.
\end{itemize}
\ifslidesonly
\begin{slide}
	\heading{State Properties}
	\begin{itemize}
	\item The state equations can be solved for a given input function and initial conditions.
	\item Other variables in the system can be determined from the state solutions and the output equations.
	\item Physical states are associated with energy storage.
	\item The states encode a system's whole past history.
	\item The choice of states is not unique.
\end{itemize}
\end{slide}
\fi

\subsection*{State Space Models for Linear Systems}

Linear systems are the most important ones for control system analysis and design as the best methods and techniques apply to these.

Non-linear systems are often approximated about a suitable operating point by the nearest equivalent linear system allowing linear techniques to be used in the design of a suitable controller.

In the case of linear systems, each state equation expresses the derivative of one of the states as a linear function of the states and inputs. The output equations also express the output variables linearly. As a consequence the natural mathematical notation to use is that of vectors and matrices.
Thus state, input and output variables are grouped in column vectors and are multiplied by matrices in the state and output equations.

For linear time varying systems, the matrices have elements which are functions of time, but for time invariant systems all the matrices are constant.
\ifslidesonly
\begin{slide}
	\heading{State Space Models for Linear Systems}
	\begin{itemize}
	 \item Linear systems most important for control systems analysis and design.
	 \item Best control methods and techniques apply to linear systems.
	 \item Nonlinear systems can often be \emph{linearized} around a given operating point allowing linear methods to be used.
	 \item Linear models can be modelled with matrix equations with makes them attractive to tools like Matlab.
	 \item Linear time-varying systems have matrix elements which are functions of time.
	 \item Linear time invariant systems have constant matrix elements.
  \end{itemize}
\end{slide}
\fi

\subsubsection*{A vector/matrix notation}

In general, we will have $n$ state variables and $q$ system
inputs. In such a case we can write down a matrix state
equation as developed in \sref{slides:l13s3} and \sref{slides:l13s4}.
\begin{slide}\label{slides:l13s3}
\heading{Matrix State Equation (1)} Let us define a general
$n$-dimensional state vector
\[\mathbf{x} = \left[x_1,\ x_2,\ \ldots, x_n\right]^T\] Its
derivative is
\[\frac{d\mathbf{x}}{dt} = \left[\frac{dx_1}{dt},\ \frac{dx_2}{dt},\
\ldots,\
\frac{dx_n}{dt}\right]^T\] or more compactly
\[\dot{\mathbf{x}}=\left[\dot{x_1},\ \dot{x_2},\ \ldots,\
\dot{x_n}\right]^T.\]

There may be any number of inputs to a system, so we also assume a
general vector of $q$ inputs
\[\mathbf{u}=\left[u_1,\ u_2,\ \ldots,\ u_q\right]^T\]
\end{slide}

\begin{slide}\label{slides:l13s4}
\heading{Matrix State Equation (2)}
\[
\dot{\mathbf{x}}=\left[\begin{array}{cccc}
  a_{11} & a_{12} & \cdots & a_{1n} \\
  a_{21} & a_{22} & \cdots & a_{2n} \\
  \vdots & \vdots & \ddots & \vdots \\
  a_{n1} & a_{n2} & \cdots & a_{nn}
\end{array}\right]\ \mathbf{x} + \left[\begin{array}{cccc}
  b_{11} & b_{12} & \cdots & b_{1q} \\
  b_{21} & b_{22} & \cdots & b_{2q} \\
  \vdots & \vdots & \ddots & \vdots \\
  b_{n1} & b_{n2} & \cdots & b_{nq}
\end{array}\right]\ \mathbf{u} \\
\]
or more succinctly
\[
\dot{\mathbf{x}}=\mathbf{A}\mathbf{x}+\mathbf{B}\mathbf{u}
\]
Where $\mathbf{A}$ is the $n\times n$ ``\emph{system matrix}'' and
$\mathbf{B}$ is the $n\times q$ ``\emph{input matrix}''.
\end{slide}

The state equations allow us to describe the internal behaviour of
the system when subjected to stimuli from the inputs. In
example 1, we need nothing more if we wish to describe the way that
the capacitor voltage $v_{31}$ and inductor current $i_1$ change
with time under the influence of the input current $u$. However,
if we wish to describe the behaviour of the other variables in the
circuit we need to complete the state space model with a set of
``\emph{output equations}.''

Let us consider every possible output. From the block diagram, we
see that, in terms of the state variables and the system input
\begin{eqnarray}
\label{eq:l13y1} v_{31}&=&1\times v_{31} \\
i_1 & = & 1\times i_1 \\
v_{32} &=& 1\times v_{31} - R \times i_1 \\
v_{21} & = & R \times i_1 \\
\label{eq:l13yn} i_2 & = & u - 1\times i_1.
\end{eqnarray}
\ifslidesonly
\begin{slide}
\heading{Output Equations}
For illustration purposes we write an ``output equation'' for every
possible signal.
\begin{eqnarray*}
v_{31}&=&1\times v_{31} \\
i_1 & = & 1\times i_1 \\
v_{32} &=& 1\times v_{31} - R \times i_1 \\
v_{21} & = & R \times i_1\\
i_2 & = & u - 1\times i_1.
\end{eqnarray*}
\end{slide}
\fi
Arranging these equations in vector form we have:
\begin{equation}
\left[\begin{array}{c}
  v_{31} \\
  i_1 \\
  v_{32} \\
  v_{21} \\
  i_2
\end{array}\right] = \left[\begin{array}{cc}
  1 & 0 \\
  0 & 1 \\
  1 & -R \\
  0 & R \\
  0 & -1
\end{array}\right]\ \left[\begin{array}{c}
  v_{31} \\
  i_1
\end{array}\right]+\left[\begin{array}{c}
  0 \\
  0 \\
  0 \\
  0 \\
  1
\end{array}\right] u
\end{equation}
\ifslidesonly
\begin{slide}
\heading{Vectorised form...}
Arranging the output equations in vector form we have:
\begin{displaymath}
\left[\begin{array}{c}
  v_{31} \\
  i_1 \\
  v_{32} \\
  v_{21} \\
  i_2
\end{array}\right] = \left[\begin{array}{cc}
  1 & 0 \\
  0 & 1 \\
  1 & -R \\
  0 & R \\
  0 & -1
\end{array}\right]\ \left[\begin{array}{c}
  v_{31} \\
  i_1
\end{array}\right]+\left[\begin{array}{c}
  0 \\
  0 \\
  0 \\
  0 \\
  1
\end{array}\right] u
\end{displaymath}
\end{slide}
\fi
In general, we can describe a system with $r$ inputs in terms of
the generic output variables $y_1, y_2,\ldots,\ y_r$ as shown in
\sref{slides:l13s5} and \sref{slides:l13s6}.
\begin{slide}\label{slides:l13s5}
\heading{General Output Equation (1)} Let us define a general
output vector
\[\mathbf{y} = \left[y_1,\ y_2,\ \ldots, y_r\right]^T\]

Given that some of the inputs to the system may be directly
connected to the output, the input vector may also appear in the
output general equation.
\end{slide}
\begin{slide}\label{slides:l13s6}
\heading{General Output Equation (2)}
\[
\mathbf{y}=\left[\begin{array}{cccc}
  c_{11} & c_{12} & \cdots & c_{1n} \\
  c_{21} & c_{22} & \cdots & c_{2n} \\
  \vdots & \vdots & \vdots & \vdots \\
  c_{r1} & c_{r2} & \cdots & c_{rn}
\end{array}\right]\ \mathbf{x} + \left[\begin{array}{cccc}
  d_{11} & d_{12} & \cdots & d_{1q} \\
  d_{21} & d_{22} & \cdots & d_{2q} \\
  \vdots & \vdots & \vdots & \vdots \\
  d_{r1} & d_{r2} & \cdots & d_{rq}
\end{array}\right]\ \mathbf{u} \\
\]
or more succinctly
\[
\mathbf{y}=\mathbf{C}\mathbf{x}+\mathbf{D}\mathbf{u}
\]
Where $\mathbf{C}$ is the $n\times r$ ``\emph{output matrix}'' and
$\mathbf{D}$ is the $r\times q$ ``\emph{feedforward matrix}''.
\end{slide} This
equation relates the states and inputs to the outputs. There are
no dynamic terms!
\begin{slide}\label{slides:l13s7}
\heading{The State Space Model}
\begin{eqnarray*}
\dot{\mathbf{x}}&=&\mathbf{A}\mathbf{x}+\mathbf{B}\mathbf{u}\\
{y}&=&\mathbf{C}\mathbf{x}+\mathbf{D}\mathbf{u}
\end{eqnarray*}
Can always be developed from a system with physically realizable
states and physical realistic sources. Such a system is called
``\emph{proper}''. If $\mathbf{D}$ is null (matrix of zeros) the
system is called ``\emph{strictly proper}''.
\end{slide}
A block diagram representation of the state space model is shown
in \sref{slides:l13s8}. The block diagram of the circuit, rearranged
to match the general model is shown in \sref{slides:l13s9}.
\begin{slide}\label{slides:l13s8}
\heading{Block Diagram of a State-Space Model}
\resizebox{300pt}{!}{\includegraphics{pictures/ssmodel}}
\end{slide}
\begin{slide}\label{slides:l13s9}
\heading{State Space Model of Example 1}
\resizebox{300pt}{!}{\rotatebox{90}{\includegraphics{pictures/statemodel.pdf}}}
\end{slide}

\subsubsection*{Modelling State Space Systems in Matlab}

\begin{slide}
	\heading{Modelling State Space Systems in Matlab}
	For the example above
	\begin{verbatim}
	A = [0 -1/Cap; 1/L -R/L];
	B = [1/Cap; 0];
	C = [1 0; 0 1; 1 -R; 0 R; 0 -1];
	D = [0; 0; 0; 0; 1];
	circ_ss = ss(A, B, C, D,...
	   'statename',{'v31' 'i1'}, 'inputname','u',...
	   'outputname', {'v31' 'i1' 'v32' 'v21' 'i2'});
	\end{verbatim}
	Once you have the state-space model, all the analysis techniques seen so far are open to you.
\end{slide}

Important note, for this to work you must assign numerical values to the the parameters \texttt{Cap}, \texttt{R} and \texttt{L}.

\subsubsection*{In class activity}

In class we will construct the state-space model for the Antenna Azimuth Case Study Problem from Nise.
\ifslidesonly
\begin{slide}
\heading{Example 4}
Construct the state-space model for the Antenna Azimuth Case Study Problem from Nise.
\end{slide}
\fi

%----------------------------------------------------------------
% The end of slides
% ----------------------------------------------------------------
\endinput

% Local Variables:
% TeX-master: "lecture1"
% End: