\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphics}
\usepackage{listings}
\usepackage{tcolorbox}
\usepackage{verbatim}

\DeclareMathOperator*{\argmin}{arg\,min}

\marginparwidth 0pt
\oddsidemargin  0pt
\evensidemargin  0pt
\marginparsep 0pt
\topmargin   0pt
\textwidth   6.5in
\textheight  8.5in
\setlength{\parindent}{0em}

\newenvironment{packed_itemize}{
\begin{itemize}
  \setlength{\itemsep}{0pt}
  \setlength{\parskip}{0pt}
  \setlength{\parsep}{0pt}
}{\end{itemize}}

\newenvironment{packed_enumerate}{
\begin{enumerate}
  \setlength{\itemsep}{1pt}
  \setlength{\parskip}{0pt}
  \setlength{\parsep}{0pt}
}{\end{enumerate}}

\definecolor{codeback}{RGB}{235,255,255}

\lstset{ %
language=Matlab,                % choose the language of the code
basicstyle=\footnotesize,       % the size of the fonts that are used for the code
numbers=left,                   % where to put the line-numbers
numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
stepnumber=1,                   % the step between two line-numbers. If it is 1 each line will be numbered
numbersep=10pt,                  % how far the line-numbers are from the code
backgroundcolor=\color{codeback},  % choose the background color. You must add \usepackage{color}
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular underscores
frame=single,           % adds a frame around the code
tabsize=2,          % sets default tabsize to 2 spaces
captionpos=b,           % sets the caption-position to bottom
breaklines=true,        % sets automatic line breaking
breakatwhitespace=false,    % sets if automatic breaks should only happen at whitespace
escapeinside={\%*}{*)}          % if you want to add a comment within your code
}

\begin{document}

\begin{center}
  \Huge{MATLAB Introduction}\\
  \Large{MATH2070: Numerical Methods in Scientific Computing I}
\end{center}
Location: http://people.sc.fsu.edu/$\sim$jburkardt/classes/math2070\_2019/matlab\_intro/matlab\_intro.pdf
\vskip 0.1in
\hrule
\vskip 0.1in

\begin{center}
  \includegraphics[scale=0.20]{hail.png}
  \vskip 0.1in
  {\it{Hailstone numbers cycle for a while and then drop unpredictably!}}
\end{center}
\vskip 0.1in
\begin{tcolorbox}[colback=red!5,title=MATLAB Intro]
{\it{
  Let's model a particular problem, and see how MATLAB can help us.
}}
\end{tcolorbox}

\section{The HUMP function}

Suppose we are interested in the function:
\begin{displaymath}
  y = \frac{1}{(x-0.3)^2+0.01} + \frac{1}{(x-0.9)^2+0.04} - 6.0
\end{displaymath}
We can create a MATLAB function {\it{hump.m}}, so that we only have
to type this formula correctly once.  Note that division is done with 
a period-slash, squaring is done with a ``period-caret'', and we have 
to be careful about the parentheses:
\begin{lstlisting}
function y = hump ( x )
  y = 1.0 ./ ( ( x - 0.3 ).^2 + 0.01 ) ...
    + 1.0 ./ ( ( x - 0.9 ).^2 + 0.04 ) ...
    - 6.0;
  return
end
\end{lstlisting}
Then we can ask for the value at 2.0 by writing {\tt{y=hump(2.0)}}, or the
value at a variable {\tt{x}} by writing {\tt{y=hump(x)}} and so on.
\vskip 0.1in
Suppose we wanted to know the value of the function at 6 equally spaced
points in the interval $[0,1]$?  We could type the command 6 times, but
a better option would be to somehow get all the answers at once.  We
can create a list of the $x$ values and get our answers back like this:
\begin{lstlisting}
  x = [ 0, 0.2, 0.4, 0.6, 0.8, 1.0 ];
  y = hump ( x )
\end{lstlisting}
We have just learned that we can create a list, or vector, by enclosing
a sequence of values in square brackets.  Now suppose we wanted to get
a similar table of values between 1 and 2.  We can write
\begin{lstlisting}
  x = x + 1.0;
  y = hump ( x )
\end{lstlisting}
That is, we can do simple arithmetic to all the numbers in a vector, using
the same commands we are familar with for normal arithmetic, like
\begin{lstlisting}
  y = 2 * x
  z = x - sin ( x )
\end{lstlisting}
The other thing to notice is that $[0, 0.2, 0.4, 0.6, 0.8, 1.0]$ is a
{\bf{row vector}}.  It has dimensions $1 \times 6$, 1 row and 6 columns.
If we had wanted to create a column vector, we would separate our list
by semicolons:
\begin{lstlisting}
  w = [ 0; 0.2; 0.4; 0.6; 0.8; 1.0]
\end{lstlisting}
Notice that you can't add the vectors {\tt{x}} and {\tt{w}}, because
they have different shapes.  You can use the transpose operator, an
apostrophe, to flip between the row and column formats.  Look at
\begin{lstlisting}
  v = w'
\end{lstlisting}
\vskip 0.1in
Suppose we wondered about the minimum and maximum values of our {\it{hump(x)}}
function over the interval $[0,2]$.  A good way to estimate this would be to
sample the function at a lot of places.  Instead of creating a very long
{\tt{x}} vector, we can use the {\tt{linspace()}} function:
\begin{lstlisting}
  x = linspace ( 0.0, 2.0, 101 );
  y = hump ( x );
  min ( y )
  max ( y )
  mean ( y )
\end{lstlisting}
Finally, to actually see our function, we can plot the {\tt{x}} and {\tt{y}} values:
\begin{lstlisting}
  plot ( x, y )
  print ( '-dpng', 'hump.png' )  % Saves a copy of your plot.
\end{lstlisting}
There are several ways to make this plot nicer, which we will see in the plotting lab.

\section{Let's make hail!}

Start with 10.  Because it's even, drop it to 5.  Because that's odd, triple it
and add 1 to get 16.  Repeatedly divide by 2 to get 8, 4, 2, 1.  We just did
a hailstone sequence:  start with any integer, get the next number by halving
an even value, or if odd, tripling and adding 1.
\vskip 0.1in
This seems like a task the computer can do.  It would be convenient to have
a function {\tt{hail\_sequence(n)}} which is given our starting value and prints
out the sequence as it is computed.  How can we set this up?  Our idea would
be something like this:
\begin{lstlisting}
function hail_sequence ( n )     % Note: we have NO output

  if n is 1, we are done
  
    if n is even, divide it by 2
    or if n is odd, multiply it by 3 and add 1.
    
  otherwise repeat
\end{lstlisting}
To determine if {\tt{n}} is even, we can call {\tt{mod(n,2)}}, which
returns the remainder when {\tt{n}} is divided by 2.  If this is 0, then
it's an even number.
\vskip 0.1in
We use {\tt{if(condition)}} and {\tt{else}} statements to control what
we do to {\tt{n}}.
\vskip 0.1in
How do we handle the repetition?
The MATLAB {\tt{for}} statement lets us repeat something a fixed number of times.
But we want to repeat something until something happens, which could be any
number of repetitions.  For that, we need the {\tt{while()}} statement, which
has the form:
\begin{lstlisting}
  while ( condition )
    statements to be repeated
  end
\end{lstlisting}
Simple conditions include:
\begin{lstlisting}
  n == 1   n is equal to 1
  n ~= 2   n is NOT equal to 2      % That's the "twiddle" character
  n < 3    n is less than 3
  n >= 4   n is greater than or equal to 4
\end{lstlisting}
So our function looks like this:
\begin{lstlisting}
function hail_sequence ( n )

  n                    % Print the first value
   
  while ( n ~= 1 )
  
    if ( mod ( n, 2 ) == 0 )
      n = n / 2
    else
      n = 3 * n + 1
    end
    
  end
  
  return
end
\end{lstlisting}
\vskip 0.1in
Does {\tt{hail\_sequence()}} work?  Let's try it on $n = 7, 10, 12, 19$.

\section{How long is a hailstone's journey?}

It's interesting that the number of steps varies so much from one starting
point to the next.  Can we find an easy way to record this information?
Suppose we modify {\it{hail\_sequence()}} so that it doesn't print the values
that are computed, but just counts them, and returns that as output?
\begin{lstlisting}
function count = hail_count ( n )

  count = 0;
   
  while ( n ~= 1 )
  
    if ( mod ( n, 2 ) == 0 )
      n = n / 2;
    else
      n = 3 * n + 1;
    end
    
    count = count + 1;
    
  end
  
  return
end
\end{lstlisting}
Now we can use a {\tt{for}} loop to make a table of these values.
\vskip 0.1in
We can even make a plot.  We don't want to connect the successive values,
but just show them as dots.  We use a modified version of the plot command:
\begin{lstlisting}
  x = zeros ( 150, 1 );
  y = zeros ( 150, 1 );
  
  for n = 1 : 150
    x(n) = n;
    y(n) = hail_count ( n )    
  end
  
  plot ( x, y, 'bo' )     %  'bo' means plot data using blue circles.
  
  print ( '-dpng', 'hail_count_plot.png' )  % Save a copy of your plot
\end{lstlisting}

\section{Assignment \#1}

Send me, via email, a copy of your plot {\it{hail\_count\_plot.png}}.  This is due
by Friday evening, August 30th.

\end{document}
