\RequirePackage[l2tabu, orthodox]{nag}
%% Checks for obsolete LaTeX packages and outdated commands. 
%% Does nothing as long as your syntax is right.

\documentclass{amsart}
% Document class possibilities:
%   amsart, article, book, beamer, report, letter
% Options:
%   letterpaper, a4paper,11pt,oneside, twoside, draft, twocolumn, landscape

%% For beamer class, see my beamer template.

%% ========== Options to Toggle When Compiling ==============
%\usepackage[notcite]{showkeys} %% Show tags and labels.
%\usepackage{layout}            %% Show variable values controlling page layout.
%\allowdisplaybreaks[1]         %% Allow multiline displays to split.
%\nobibliography     %% Use proper citations, but do not generate bibliography.

%% ========== Select *.tex file encoding and language ==============
%\usepackage[language]{babel} %% Takes care of all language requirements.

%\usepackage[latin1]{inputenc}  %% Use with PuTTY or TeXMaker
\usepackage[utf8]{inputenc}  %% Use on most OS's, such as Ubuntu.
  \usepackage[T1]{fontenc}
%   \usepackage{libertine} % or any other font package (or none)
  % ============== Quote Code =================
% \usepackage{ifxetex}
% \usepackage[svgnames]{xcolor}
% \ifxetex{%
%   \usepackage{fontspec}
%   \setmainfont{Linux Libertine O} % or any font on your system
%   \newfontfamily\quotefont[Ligatures=TeX]{Linux Libertine O} % or any font on your system
% \else
%   \newcommand*\quotefont{\fontfamily{fxl}} % selects Libertine for quote font
% \fi
%  \usepackage{tikz}
% \usepackage{framed}
% % Make commands for the quotes
% \newcommand*{\openquote}{\tikz[remember picture,overlay,xshift=-15pt,yshift=-10pt]
%      \node (OQ) {\quotefont\fontsize{60}{60}\selectfont``};\kern0pt}
% \newcommand*{\closequote}{\tikz[remember picture,overlay,xshift=15pt,yshift=10pt]
%      \node (CQ) {\quotefont\fontsize{60}{60}\selectfont''};}
% % select a colour for the shading
% \definecolor{shadecolor}{named}{Azure}
% % wrap everything in its own environment
% \newenvironment{shadequote}%
% {\begin{snugshade}\begin{quote}\openquote}
% {\hfill\closequote\end{quote}\end{snugshade}}
% ============== End Quote Code =================
  
%% ============== Page Layout ==============
%% Allow extra space at the bottom of pages.
\raggedbottom     

%% Use smaller margins.
%\usepackage{fullpage}

%%Control page number placement.  \thepage is the current page number.
% \renewcommand{\headrulewidth}{0pt}
% \lhead{}
% \chead{}
% \rhead{}
% \lfoot{}
% \cfoot{\thepage}
% \rfoot{}

%\usepackage{geometry}  %% Can adjust the margins of individual pages
%% Use it like this:
%% \newgeometry{left=3cm,bottom=0.1cm}
%%     ... Lines that require margins adjusted ...
%% \restoregeometry

%% ============== Page Styles ==============
% \usepackage{fancyhdr}
% \pagestyle{fancy}
% \pagestyle{empty}

%% ============== Math Packages ==============
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools} % An improvement of amsmath
\usepackage{latexsym}

%% ============ Typesetting add-ons ============
\usepackage{siunitx} %Support for SI units, \num, \SI, etc.

%% ============== Single-Use Packages ==============
\usepackage{enumerate}
\usepackage{cancel}
\usepackage{cases}
\usepackage{empheq}
\usepackage{multicol}
\usepackage{wrapfig}

%% ============== Graphics Packages ==============
%\usepackage{graphicx} %% Conflicts with pdflatex.
%\usepackage{graphics} %% Conflicts with eps files.
%\usepackage{epsfig} Allows eps files (?)

%% Note: For using .eps graphics, use the graphicx package,
%% and in the document use, for example:
%% \begin{figure}
%%  \includegraphics[scale=0.5]{my_picture.eps}
%% \end{figure}

%% Prevent figures from appearing on a page by themselves:
%\renewcommand{\topfraction}{0.85}
%\renewcommand{\textfraction}{0.1}
%\renewcommand{\floatpagefraction}{0.75}

%% Force floats to always appear after their definition: 
%\usepackage{flafter}

%% ============== tikZ and PGF packages ==============
\usepackage{ltxtable,tabularx,tabulary}

% \usepackage{tikz} % Already in the Quotes section
% \usepackage{pgf}
% \usepackage{pgfplots} %% Requires pgf 2.0 or later.
% \usetikzlibrary{arrows, automata, backgrounds, calendar, chains,
% matrix, mindmap, patterns, petri, shadows, shapes.geometric,
% shapes.misc, spy, trees}


%% ============== Colors ==============
%% Warning: These are often a source of conflicts during compilation.
\usepackage{color}
\newcommand{\blue}[1]{{\color{blue} #1}}
\newcommand{\red}[1]{{\color{red} #1}}

%% ============== Notes ==============
%% Use in document as \mnote{My note here.}
\newcounter{mnote}
 \setcounter{mnote}{0}
 \newcommand{\mnote}[1]{\addtocounter{mnote}{1}
   \ensuremath{{}^{\bullet\arabic{mnote}}}
   \marginpar{\footnotesize\em\color{red}\ensuremath{\bullet\arabic{mnote}}#1}}

\usepackage{todonotes}
% \listoftodos, \todo[noline]{}, \todo[inline]{}, 
% \todo{}, \missingfigure{}
   
%% ============== Fonts ==============
% \usepackage{bbm}  %% Non-Vanilla: Not include in many LaTeX distros.
\usepackage{mathrsfs}
\usepackage{fontenc} %T1 font encoding
\usepackage{inputenc} %UTF-8 support
%\usepackage{babel} %Language specific commands, shortcuts, hyphenation.

\usepackage{verbatim}

%% Microtype improves spacing.  Load after fonts.
\usepackage{microtype}

%% ============== Theorem Styles ==============
%% Note: newtheorem* prevents numbering.

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem*{claim}{Claim}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem{exercise}[theorem]{Exercise}
\newtheorem{axiom}[theorem]{Axiom}

\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}

%% ============== References ==============
\numberwithin{equation}{section} %% Equation numbering control.
\numberwithin{figure}{section}   %% Figure numbering control.

\usepackage[square,comma,numbers,sort&compress]{natbib}
\usepackage[colorlinks=true, pdfborder={0 0 0}]{hyperref}
\hypersetup{urlcolor=blue, citecolor=red}
\usepackage{url}

%% Reference things as 'fig. 1', 'Lemma 7', etc.
%% Note: some conflict with section labeling
% \usepackage{cleveref}

%% Create references like 'on the following page', 'on page 23'
\usepackage{varioref} 

% usepackage[refpages]{gloss} %% Glossary

%%%%%%%%%%%%%%%%%%%%% MACROS %%%%%%%%%%%%%%%%%%%%%

% ============================== Vectors ==============================
\newcommand{\vect}[1]{\mathbf{#1}}
\newcommand{\bi}{\vect{i}}
\newcommand{\bj}{\vect{j}}
\newcommand{\bk}{\vect{k}}

\newcommand{\bu}{\vect{u}}
\newcommand{\bv}{\vect{v}}
\newcommand{\bw}{\vect{w}}
\newcommand{\boldm}{\vect{m}}
\newcommand{\bx}{\vect{x}}
\newcommand{\by}{\vect{y}}
\newcommand{\bz}{\vect{z}}

\newcommand{\be}{\vect{e}}

% ==================== Fields ==================
\newcommand{\field}[1]{\mathbb{#1}}
\newcommand{\nC}{\field{C}}
\newcommand{\nF}{\field{F}}
\newcommand{\nK}{\field{K}}
\newcommand{\nN}{\field{N}}
\newcommand{\nQ}{\field{Q}}
\newcommand{\nR}{\field{R}}
\newcommand{\nT}{\field{T}}
\newcommand{\nZ}{\field{Z}}

% ======================== Script Symbols  ========================
\newcommand{\sH}{\mathscr H}
\newcommand{\sL}{\mathscr L}

% ====================== Caligraphic Symbols ======================
\newcommand{\cA}{\mathcal A}
\newcommand{\cB}{\mathcal B}
\newcommand{\cC}{\mathcal C}
\newcommand{\cD}{\mathcal D}

\newcommand{\cL}{\mathcal L}

% ======================== Fraktur Symbols  ========================
% Note: Use mathrsfs package.

%\newcommand{\fM}{\mathfrak M}

% ========================== Bold Symbols ==========================
\newcommand{\bvphi}{\boldsymbol{\vphi}}
\newcommand{\bPhi}{\boldsymbol{\Phi}}

% ======================== Misc. Symbols ========================

\newcommand{\vphi}{\varphi}
\newcommand{\maps}{\rightarrow}
\newcommand{\Maps}{\longrightarrow}
\newcommand{\sand}{\quad\text{and}\quad}
\newcommand{\QED}{\hfill$\blacksquare$}
\newcommand{\tac}{\textasteriskcentered}
%\newcommand{\dhr}{\m\athrel{\lhook\joinrel\relbar\kern-.8ex\joinrel\lhook\joinrel\rightarrow}}

% ========================== Operations ==========================
\newcommand{\cnj}[1]{\overline{#1}}
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\npd}[3]{\frac{\partial^#3 #1}{\partial #2^#3}} %\npd{f}{x}{2}
\newcommand{\abs}[1]{\left\lvert#1\right\rvert}
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
%\newcommand\norm[1]{\left\vert\mkern-1.7mu\left\vert#1\right\vert\mkern-1.7mu\right\vert}
%\newcommand\bnorm[1]{\bigl\vert\mkern-2mu\bigl\vert#1\bigr\vert\mkern-2mu\bigr\vert}
\newcommand{\set}[1]{\left\{#1\right\}}
\newcommand{\ip}[2]{\left<#1,#2\right>}
\newcommand{\pnt}[1]{\left(#1\right)}
\newcommand{\pair}[2]{\left(#1,#2\right)}

%Advection operators:
\newcommand{\adv}[2]{(#1 #2)}
\newcommand{\vectadv}[2]{\;#1 \otimes#2\;}

% ============ Special Macros For This Paper ==================
% \newcommand{}[]{}

% % ========================== Norms ==========================
% \newcommand{\norm}[1]{\|#1\|}
% \newcommand{\snorm}[1]{|#1|}
% \newcommand{\normLp}[2]{\|#2\|_{L^{#1}}}
% \newcommand{\normHs}[2]{\|#2\|_{H^{#1}}}
% \newcommand{\normLL}[3]{\|#3\|_{L^{#1}([0,T],L^{#2})}}
% \newcommand{\normLH}[3]{\|#3\|_{L^{#1}([0,T],H^{#2})}}
% \newcommand{\normCL}[3]{\|#3\|_{C^{#1}([0,T],L^{#2})}}
% \newcommand{\normCH}[3]{\|#3\|_{C^{#1}([0,T],H^{#2})}}


%% ============== Counters ==============
\newcounter{my_counter}
\setcounter{my_counter}{1} 

%


\usepackage{fullpage}
\textheight 9.5in



% ============== Matlab =================
\usepackage{listings} % Use for code.
\usepackage{textcomp} % Used for upquote.
\usepackage{color} 
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
% ============== Matlab End =================

\begin{document}

\title{Math 934\\Topics in Differential Equations\\$\sim$Visions of Chaos$\sim$}
% \date{Friday, 19 October 2012}  %Date assigned
\date{} 
\author{Instructor: Dr. Adam Larios}
\maketitle


\thispagestyle{empty}
% \pagestyle{empty}

% \begin{center}
% \textbf{Due date: Monday, 5 Nov. 2012} 
% \end{center}
 
 
%----------------------------------------------------

% \bigskip


\lstset{language=Matlab,
   keywords={break,case,catch,continue,else,elseif,end,for,function,
       global,if,otherwise,persistent,return,switch,try,while},
   basicstyle=\ttfamily,
   keywordstyle=\color{blue},
   commentstyle=\color{red},
   stringstyle=\color{dkgreen},
   numbers=left,%    numberstyle=\tiny\color{gray},none
   stepnumber=1,
   numbersep=10pt,
   backgroundcolor=\color{white},
   tabsize=4,
   showspaces=false,
   showstringspaces=false,
   }

% \noindent\underline{\textbf{Part 1: Visions of Chaos}}
% \begin{shadequote}
\noindent
``Chaos is the score upon which reality is written.''
 \par\emph{-Henry Miller}
 \\
 
 \noindent
 ``Chaos is a friend of mine.''  \par\emph{-Bob Dylan}
% \end{shadequote}
% ``Chaos is the score upon which reality is written.''
% -Henry Miller 
% 
% ``Chaos is a friend of mine'' -Bob Dylan

% In all chaos there is a cosmos, in all disorder a secret order.
% 
% CARL JUNG, 

\bigskip

\noindent\underline{\textbf{Note.}} This is a little thing for you to try out after you finish the Matlab introduction worksheet.  It should go quickly, but it is fun to try out.

\noindent\underline{\textit{Discrete Dynamical Systems}}\\
Differential equations are ``continuous'' systems, but there are also ``discrete'' systems, which are called ``discrete dynamical systems.''  Consider the familiar exponential growth population model with growth rate $r$.  If we let $P_n$ denote the population at the $n^{\text{th}}$ time step, then $P_{n+1}$, the population at the $(n+1)^{\text{st}}$ time step is given by
\begin{align*}
   P_{n+1} = rP_n
\end{align*}

\noindent\underline{\textit{Warm up}}\\
Suppose $r=0.17$, and the population starts at the ``seed value'' given by $P_0 = 3$. In Matlab, compute $P_{10}$ as follows.

\begin{minipage}[h]{5in}
\centering
\begin{lstlisting}
P = 3; 
for n = 1:10
  P = 0.17*P;
end
P
\end{lstlisting}
\end{minipage}

Of course, this will not store any of the values, so if we want to store them (so we can use them to plot, for example), we can do this:

\begin{minipage}[h]{5in}
\centering
\begin{lstlisting}
P(1) = 3; 
for n = 1:10
  P(n+1) = 0.17*P(n);
end
plot(P);
\end{lstlisting}
\end{minipage}

\noindent
(If we only pass one vector \texttt{P} to \texttt{plot}, then Matlab uses just the usual counting numbers for the x-axis, \texttt{[1 2 3 ... length(P)]}).  Notice that, since Matlab can't start the index at $0$, we have to start it at $1$.  Also, it is better to preallocate \texttt{P} by setting \texttt{P=zeros(1,10)} before the loop.  This makes Matlab run faster, since it sets aside the space it needs beforehand.

\pagebreak

Recall the logistic equation mentioned in class:

\begin{align*}
 \frac{dP}{dt} = r\cdot P\cdot\pnt{1-\frac{P}{K}}
\end{align*}
where $r$ is the effective growth rate, and $K$ is the carrying capacity.   We can consider a discrete version of this equation (setting $K=1$ for simplicity), namely 
\begin{align*}
 P_{n+1} = r\cdot P_n\cdot\pnt{1-P_n}
\end{align*}
This seemingly simple dynamical system exhibits chaotic behavior, in the sense that tiny changes in the parameters can lead to very different long-term  outcomes.  This means that, if we want to predict the behavior, even if our measurements of these parameters is very precise, but not perfect, the long-term behavior is essentially unpredictable.  We explore this below.

\bigskip

\underline{\textit{Things to try}}
\begin{enumerate}
\item 
To get started, choose the seed value $P_0=0.5$, and set $r=1.61$, and repeat this process, say, $250$ times to find $P_{250}$.  Remember to only output $P_{250}$, since you don't want a mess showing up.  (There is nothing special about the $250$ here, we just need a large number.)

\item Next, let's try varying $r$.  Try the above exercise plugging in $r=2.6$, $r=2.61$, $r=2.611$, $r=2.6111$, $r=2.61111$, $r=2.611111$, outputting only $P_{250}$ each time.  Pay attention to (and record) the values as you go.  We are only slightly varying $r$, and the output results are not very surprising.

\item Try the above exercise again, but this time with $r=3.6$, $r=3.61$, $r=3.611$, $r=3.6111$, $r=3.61111$, $r=3.611111$.  What the heck just happened?

\item OK, that was weird.  Let's try to get a better picture of things by looping over a wide range of $r$ values.  Calculate $P_{250}$ for 1000 different $r$ values, ranging between $3\leq r\leq 4$ (\texttt{linspace} would be a good tool to use here), and save them as you go in a big vector.  Plot your $r$ values against your $P_{250}$ values.  What do you see? How are the points changing as $r$ increases?

\item Try changing your seed value $P_0=0.5$ a little.  Do you see different behavior?  If not, try $P_0=0.7$ and $P_0=0.9$.  What do you notice?  

\item OK, it's time to sort everything out.  Let's make a big loop that runs over everything, looping from seed $P_0$ from $0.1$ to $0.9$ using, about 1000 values or so.  Put this all on the same plot by declaring ``\texttt{hold on}''  somewhere near the top of your document. If you want to watch it draw each plot, put \texttt{pause(0.1)} in your outer loop.  \textbf{Don't forget to go full screen and zoom in!}

Matlab automatically connects the dots between plotted points. Try plotting with points, to see things easier, like this:

\lstset{numbers=none}

\begin{minipage}[h]{5in}
\centering
\begin{lstlisting}[upquote=true]
plot(r_vals,P_vals,'.');
\end{lstlisting}
\end{minipage}

\lstset{numbers=left}

What you just built is called ``The orbit diagram for the logistic family.''  It illustrates that the long-term behavior of this system is independent of initial conditions, and chaotically dependent on the parameters.


\end{enumerate}
%----------------------------------------------------
\end{document}
