\lab{Pandas 4: Time Series}{Pandas 4: Time Series}
\objective{Many real world data sets---stock market measurements, ocean tide levels, website traffic, seismograph data, audio signals, fluid simulations, quarterly dividends, and so on are time series, meaning they come with time-based labels.
There is no universal format for such labels, and indexing by time is often difficult with raw data.
Fortunately, pandas has tools for cleaning and analyzing time series.
In this lab, we use pandas to clean and manipulate time-stamped data and introduce some basic tools for time series analysis.}

\begin{info}
This lab will be done using Colab Notebooks.
These notebooks are similar to Jupyter Notebooks but run remotely on Google's servers.
Open a Google Colab notebook by going to your Google Drive account and creating a new Colaboratory file.
If making a Colaboratory file is not an option, download the application Colaboratory onto your Google Drive.
Once opening a new Colab Notebook, upload the file \li{pandas4.ipynb}.
To make the data files accessible, run the following at the top of the lab:
\begin{lstlisting}
>>> from google.colab import files

>>> uploaded = files.upload()
\end{lstlisting}
This will prompt you upload files for this notebook.
For this lab, upload \li{DJIA.csv}, \li{paychecks.csv}, \li{finances.csv}, and \li{website_traffic.csv}.

Once the lab is complete, delete BOTH lines of code used for uploading files (the import statement and the upload statement) and download as a \li{.py} file to your git repository.
Push the newly made \li{pandas4.py} file.
\end{info}

% Time series data is any form of data that comes attached to a timestamp (i.e. Sept 28, 2016 20:32:24) or a period of time (i.e. Q3 2012). Some examples of time series data include:

\section*{Working with Dates and Times} % =====================================

The \li{datetime} module in the standard library provides a few tools for representing and operating on dates and times.
The \li{datetime.datetime} object represents a \emph{time stamp}: a specific time of day on a certain day.
Its constructor accepts a four-digit year, a month (starting at 1 for January), a day, and, optionally, an hour, minute, second, and microsecond.
Each of these arguments must be an integer, with the hour ranging from $0$ to $23$.

\begin{lstlisting}
>>> from datetime import datetime

# Represent November 18th, 1991, at 2:01 PM.
>>> bday = datetime(1991, 11, 18, 14, 1)
>>> print(bday)
1991-11-18 14:01:00

# Find the number of days between 11/18/1991 and 11/9/2017.
>>> dt = datetime(2017, 11, 9) - bday
>>> dt.days
9487
\end{lstlisting}

The \li{datetime.datetime} object has a parser method, \li{strptime()}, that converts a string into a new \li{datetime.datetime} object.
The parser is flexible so the user must specify the format that the dates are in.
For example, if the dates are in the format \li{"Month/Day//Year::Hour"}, specify \li{<<format>>"=\%m/\%d//\%Y::\%H"} to parse the string appropriately.
See Table \ref{table:date_formats} for formatting options.

\begin{table}[H]
\begin{center}
    \begin{tabular}{c|l}
        Pattern & Description \\ \hline
        \li{\%Y} & 4-digit year \\
        \li{\%y} & 2-digit year \\
        \li{\%m} & 1- or 2-digit month \\
        \li{\%d} & 1- or 2-digit day \\
        \li{\%H} & Hour (24-hour) \\
        \li{\%I} & Hour (12-hour) \\
        \li{\%M} & 2-digit minute \\
        \li{\%S} & 2-digit second \\
    \end{tabular}
\end{center}
\caption{Formats recognized by \li{datetime.strptime()}}
\label{table:date_formats}
\end{table}

\begin{lstlisting}
>>> print(datetime.strptime("1991-11-18 / 14:01", "%Y-%m-%d / %H:%M"),
...       datetime.strptime("1/22/1996", "%m/%d/%Y"),
...       datetime.strptime("19-8, 1998", "%d-%m, %Y"), sep='\n')
1991-11-18 14:01:00                 # The date formats are now standardized.
1996-01-22 00:00:00                 # If no hour/minute/seconds data is given,
1998-08-19 00:00:00                 # the default is midnight.
\end{lstlisting}

\subsection*{Converting Dates to an Index} % ----------------------------------

The \li{TimeStamp} class is the pandas equivalent to a \li{datetime.datetime} object.
A pandas index composed of \li{TimeStamp} objects is a \li{DatetimeIndex}, and a \li{Series} or \li{DataFrame} with a \li{DatetimeIndex} is called a \emph{time series}.
The function \li{pd.to_datetime()} converts a collection of dates in a parsable format to a \li{DatetimeIndex}.
The format of the dates is inferred if possible, but it can be specified explicitly with the same syntax as \li{datetime.strptime()}.

\begin{lstlisting}
>>> import pandas as pd

# Convert some dates (as strings) into a DatetimeIndex.
>>> dates = ["2010-1-1", "2010-2-1", "2012-1-1", "2012-1-2"]
>>> pd.to_datetime(dates)
<<DatetimeIndex(['2010-01-01', '2010-02-01', '2012-01-01', '2012-01-02'],
                dtype='datetime64[ns]', freq=None)>>

# Create a time series, specifying the format for the DatetimeIndex.
>>> dates = ["1/1, 2010", "1/2, 2010", "1/1, 2012", "1/2, 2012"]
>>> date_index = pd.to_datetime(dates, format="%m/%d, %Y")
>>> pd.Series([x**2 for x in range(4)], index=date_index)
<<2010-01-01    0
2010-01-02    1
2012-01-01    4
2012-01-02    9
dtype: int64>>
\end{lstlisting}

\begin{comment}
\begin{warn} % Deprecated TimeSeries object.
Prior to version 0.17.0 of \li{pandas}, there was a dedicated \li{TimeSeries} data structure.
This has since been deprecated in favor of the more general \li{Series} and \li{DataFrame} data structures.
Any materials referencing the \li{TimeSeries} can be corrected by replacing the \li{TimeSeries} with a \li{Series} or \li{DataFrame} with a \li{DatetimeIndex} as the index.
\end{warn}
\end{comment}

\begin{problem} % Load, clean, and plot Dow Jones IA data.
The file \texttt{DJIA.csv} contains daily closing values of the Dow Jones Industrial Average from 2006--2016.
Read the data into a \li{Series} or \li{DataFrame} with a \li{DatetimeIndex} as the index.
Drop rows with missing values, cast the \li{"VALUE"} column to floats, then return the updated DataFrame.
\label{prob:timeseries-dowjones}
\end{problem}

\subsection*{Generating Time-based Indices} % ---------------------------------

Some time series datasets come without explicit labels but have instructions for deriving timestamps.
For example, a list of bank account balances might have records from the beginning of every month, or heart rate readings could be recorded by an app every 10 minutes.
Use \li{pd.date\_range()} to generate a \li{DatetimeIndex} where the timestamps are equally spaced.
The function is analogous to \li{np.arange()} and has the following parameters.
\begin{table}[H]
\begin{center}
    \begin{tabular}{r|l}
        Parameter & Description \\ \hline
        \li{start} & Starting date \\
        \li{end} & End date \\
        \li{periods} & Number of dates to include \\
        \li{freq} & Amount of time between consecutive dates \\
        \li{normalize} & Normalizes the start and end times to midnight \\
    \end{tabular}
\end{center}
\caption{Parameters for \li{pd.date_range()}.}
\label{table:date_params}
\end{table}

Exactly three of the parameters \li{start}, \li{end}, \li{periods}, and \li{freq} must be specified to generate a range of dates.
The \li{freq} parameter accepts a variety of string representations, referred to as \emph{offset aliases}.
See Table \ref{table:range_freqs} for a sampling of some of the options.
For a complete list of the options, see \url{http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases}.

\begin{table}[H]
\begin{center}
    \begin{tabular}{r|l}
        Parameter & Description \\ \hline
        \li{"D"} & calendar daily (default) \\
        \li{"B"} & business daily \\
        \li{"H"} & hourly \\
        \li{"T"} & minutely \\
        \li{"S"} & secondly \\
        \li{"MS"} & first day of the month \\
        \li{"BMS"} & first weekday of the month \\
        \li{"W-MON"} & every Monday \\
        \li{"WOM-3FRI"} & every 3rd Friday of the month \\
    \end{tabular}
\end{center}
\caption{Options for the \li{freq} parameter to \li{pd.date_range()}.}
\label{table:range_freqs}
\end{table}

\begin{lstlisting}
# Create a DatetimeIndex for 5 consecutive days starting on September 28, 2016.
>>> pd.date_range(start='9/28/2016 16:00', periods=5)
<<DatetimeIndex(['2016-09-28 16:00:00', '2016-09-29 16:00:00',
               '2016-09-30 16:00:00', '2016-10-01 16:00:00',
               '2016-10-02 16:00:00'],
              dtype='datetime64[ns]', freq='D')>>

# Create a DatetimeIndex with the first weekday of every other month in 2016.
>>> pd.date_range(start='1/1/2016', end='1/1/2017', freq="2BMS" )
<<DatetimeIndex(['2016-01-01', '2016-03-01', '2016-05-02', '2016-07-01',
               '2016-09-01', '2016-11-01'],
              dtype='datetime64[ns]', freq='2BMS')>>

# Create a DatetimeIndex for 10 minute intervals between 4:00 PM and 4:30 PM on September 9, 2016.
>>> pd.date_range(start='9/28/2016 16:00',
            end='9/28/2016 16:30', freq="10T")
<<DatetimeIndex(['2016-09-28 16:00:00', '2016-09-28 16:10:00',
               '2016-09-28 16:20:00', '2016-09-28 16:30:00'],
              dtype='datetime64[ns]', freq='10T')>>

# Create a DatetimeIndex for 2 hour 30 minute intervals between 4:30 PM and 2:30 AM on September 29, 2016.
>>> pd.date_range(start='9/28/2016 16:30', periods=5, freq="2h30min")
<<DatetimeIndex(['2016-09-28 16:30:00', '2016-09-28 19:00:00',
               '2016-09-28 21:30:00', '2016-09-29 00:00:00',
               '2016-09-29 02:30:00'],
              dtype='datetime64[ns]', freq='150T')>>
\end{lstlisting}

\begin{problem} % Use pd.date_range() for something.
The file \texttt{paychecks.csv} contains values of an hourly employee's last 93 paychecks.
Paychecks are given on the first and third Fridays of each month, and the employee started working on March 13, 2008.

Read in the data, using \li{pd.date\_range()} to generate the \li{DatetimeIndex}.
Set this as the new index of the \li{DataFrame} and return the \li{DataFrame}.
\end{problem}

\section*{Periods} % ==========================================================

A pandas \li{Timestamp} object represents a precise moment in time on a given day.
Some data, however, is recorded over a time interval, and it wouldn't make sense to place an exact timestamp on any of the measurements.
For example, a record of the number of steps walked in a day, box office earnings per week, quarterly earnings, and so on.
This kind of data is better represented with the pandas \li{Period} object and the corresponding \li{PeriodIndex}.

The \li{Period} class accepts a \li{value} and a \li{freq}.
The \li{value} parameter indicates the label for a given \li{Period}.
This label is tied to the \textbf{end} of the defined \li{Period}.
The \li{freq} indicates the length of the \li{Period} and in some cases can also indicate the offset of the \li{Period}.
The default value for \li{freq} is "M" for months.
The \li{freq} parameter accepts the majority, but not all, of frequencies listed in Table \ref{table:range_freqs}.

\begin{lstlisting}
# Creates a period for month of Oct, 2016.
>>> p1 = pd.Period("2016-10")
>>> p1.start_time                   # The start and end times of the period
<<Timestamp('2016-10-01 00:00:00')>>    # are recorded as Timestamps.
>>> p1.end_time
<<Timestamp('2016-10-31 23:59:59.999999999')>>

# Represent the annual period ending in December that includes 10/03/2016.
>>> p2 = pd.Period("2016-10-03", freq="A-DEC")
>>> p2.start_time
<<Timestamp('2007-01-01 00:00:00')>>
>>> p2.end_time
<<Timestamp('2007-12-31 23:59:59.999999999')>>

# Get the weekly period ending on a Saturday that includes 10/03/2016.
>>> print(pd.Period("2016-10-03", freq="W-SAT"))
<<2016-10-02/2016-10-08>>
\end{lstlisting}

Like the \li{pd.date\_range()} method, the \li{pd.period\_range()} method is useful for generating a \li{PeriodIndex} for unindexed data.
The syntax is essentially identical to that of \li{pd.date\_range()}.
When using \li{pd.period\_range()}, remember that the \li{freq} parameter marks the end of the period.
After creating a \li{PeriodIndex}, the \li{freq} parameter can be changed via the \li{asfreq()} method.

\begin{lstlisting}
# Represent quarters from 2008 to 2010, with Q4 ending in December.
>>> pd.period_range(start="2008", end="2010-12", freq="Q-DEC")
<<PeriodIndex(['2008Q1', '2008Q2', '2008Q3', '2008Q4', '2009Q1', '2009Q2',
             '2009Q3', '2009Q4', '2010Q1', '2010Q2', '2010Q3', '2010Q4'],
            dtype='period[Q-DEC]', freq='Q-DEC')>>

# Get every three months form March 2010 to the start of 2011.
>>> p = pd.period_range("2010-03", "2011", freq="3M")
>>> p
<<PeriodIndex(['2010-03', '2010-06', '2010-09', '2010-12'],
            dtype='period[3M]', freq='3M')>>

# Change frequency to be quarterly.
>>> p.asfreq("Q-DEC")
<<PeriodIndex(['2010Q2', '2010Q3', '2010Q4', '2011Q1'],
            dtype='period[Q-DEC]', freq='Q-DEC')>>
\end{lstlisting}

The bounds of a \li{PeriodIndex} object can be shifted by adding or subtracting an integer.
 \li{PeriodIndex} will be shifted by $n$ $\times$ \li{freq}.

\begin{lstlisting}
# Shift index by 1
>>> p _= 1
>>> p
PeriodIndex(['2010Q1', '2010Q2', '2010Q3', '2010Q4'],
            dtype='int64', freq='Q-DEC')
\end{lstlisting}

If for any reason you need to switch from periods to timestamps, pandas provides a very simple method to do so.
The \li{how} parameter can be \li{start} or \li{end} and determines if the timestamp is the beginning or the end of the period.
Similarly, you can switch from timestamps to periods.

\begin{lstlisting}
# Convert to timestamp (last day of each quarter)
>>> p = p.to_timestamp(how='end')
>>> p
DatetimeIndex(['2010-03-31', '2010-06-30', '2010-09-30', '2010-12-31'],
                dtype='datetime64[ns]', freq='Q-DEC')

>>> p.to_period("Q-DEC")
PeriodIndex(['2010Q1', '2010Q2', '2010Q3', '2010Q4'],
            dtype='int64', freq='Q-DEC')

\end{lstlisting}

\begin{problem} %
The file \texttt{finances.csv} contains a list of simulated quarterly earnings and expense totals from a fictional company.
Load the data into a \li{Series} or \li{DataFrame} with a \li{PeriodIndex} with a quarterly frequency.
Let \li{how=end}.
Assume the fiscal year starts at the beginning of September and that the data begins in September 1978.
Return the \li{DataFrame}.
\end{problem}

\section*{Operations on Time Series}

There are certain operations only available to Series and DataFrames that have a \li{DatetimeIndex}. A sampling of this functionality is described throughout the remainder of this lab.

\subsection*{Slicing}

Slicing is much more flexible in pandas for time series. We can slice by year, by month, or even use traditional slicing syntax to select a range of dates.

% TODO: initialize df using pd.date_range().
\begin{lstlisting}
# Select all rows in a given year
>>> df["2010"]
                   0         1
2010-01-01  0.566694  1.093125
2010-02-01 -0.219856  0.852917
2010-03-01  1.511347 -1.324036

# Select all rows in a given month of a given year
>>> df["2012-01"]
                   0         1
2012-01-01  0.212141  0.859555
2012-01-02  1.483123 -0.520873
2012-01-03  1.436843  0.596143

# Select a range of dates using traditional slicing syntax
>>> df["2010-1-2":"2011-12-31"]
                   0         1
2010-02-01 -0.219856  0.852917
2010-03-01  1.511347 -1.324036
2011-01-01  0.300766  0.934895
\end{lstlisting}

\subsection*{Resampling} % ----------------------------------------------------

Some datasets do not have datapoints at a fixed frequency.
For example, a dataset of website traffic has datapoints that occur at irregular intervals.
In situations like these, \emph{resampling} can help provide insight on the data.

The two main forms of resampling are \emph{downsampling}, aggregating data into fewer intervals, and \emph{upsampling}, adding more intervals.

To downsample, use the \li{resample()} method of the \li{Series} or \li{DataFrame}.
This method is similar to \li{groupby()} in that it groups different entries together.
Then aggregation produces a new data set.
The first parameter to \li{resample()} is an offset string from Table \ref{table:range_freqs}: \li{"D"} for daily, \li{"H"} for hourly, and so on.

\begin{lstlisting}
>>> import numpy as np

# Get random data for every day from 2000 to 2010.
>>> dates = pd.date_range(start="2000-1-1", end='2009-12-31', freq='D')
>>> df = pd.Series(np.random.random(len(days)), index=dates)
>>> df
2000-01-01    0.559
2000-01-02    0.874
2000-01-03    0.774
                ...
2009-12-29    0.837
2009-12-30    0.472
2009-12-31    0.211
Freq: D, Length: 3653, dtype: float64

# Group the data by year.
>>> years = df.resample("A")        # 'A' for 'annual'.
>>> years.agg(len)                  # Number of entries per year.
2000-12-31    366.0
2001-12-31    365.0
2002-12-31    365.0
                ...
2007-12-31    365.0
2008-12-31    366.0
2009-12-31    365.0
Freq: A-DEC, dtype: float64

>>> years.mean()                    # Average entry by year.
2000-12-31    0.491
2001-12-31    0.514
2002-12-31    0.484
                ...
2007-12-31    0.508
2008-12-31    0.521
2009-12-31    0.523
Freq: A-DEC, dtype: float64

# Group the data by month.
>>> months = df.resample("M")
>>> len(months.mean())              # 12 months x 10 years = 120 months.
120
\end{lstlisting}

\begin{comment}
\begin{table}[H]
\begin{center}
    \begin{tabular}{r|l}
        Parameter & Description \\ \hline
        \li{rule} & the offset string (see Table \ref{table:range_freqs}) \\
        \li{how} & the data aggregation method (i.e. ``sum'' or ``mean'')\\
        \li{closed} & the boundary of the interval that is closed/inclusive (default ``right") \\
        \li{label} & the boundary of the interval that is assigned to the label (default ``right") \\
    \end{tabular}
\end{center}
\caption{Parameters for \li{DataFrame.resample()}}
\end{table}
\end{comment}

\begin{problem} % Downsample
The file \texttt{website\_traffic.csv} contains records for different visits to a fictitious website.
Read in the data, calculate the duration of each visit in seconds and convert the index to a \li{DatetimeIndex}.
Use downsampling to calculate the number of visits each minute and the number of visits each hour.
Return these DataFrames.

(Hint: \li{minute.agg(func)} returns a DataFrame).
\end{problem}

\section*{Elementary Time Series Analysis} % ==================================

\subsection*{Shifting}

\li{DataFrame} and \li{Series} objects have a \li{shift()} method that allows you to move data up or down relative to the index.
When dealing with time series data, we can also shift the \li{DatetimeIndex} relative to a time offset.

\begin{lstlisting}
>>> df = pd.DataFrame(dict(VALUE=np.random.rand(5)),
                index=pd.date_range("2016-10-7", periods=5, freq='D'))
>>> df
<<               VALUE
2016-10-07  0.127895
2016-10-08  0.811226
2016-10-09  0.656711
2016-10-10  0.351431
2016-10-11  0.608767>>

>>> df.shift(1)
<<               VALUE
2016-10-07       NaN
2016-10-08  0.127895
2016-10-09  0.811226
2016-10-10  0.656711
2016-10-11  0.351431>>

>>> df.shift(-2)
<<               VALUE
2016-10-07  0.656711
2016-10-08  0.351431
2016-10-09  0.608767
2016-10-10       NaN
2016-10-11       NaN>>

>>> df.shift(14, freq="D")
<<               VALUE
2016-10-21  0.127895
2016-10-22  0.811226
2016-10-23  0.656711
2016-10-24  0.351431
2016-10-25  0.608767>>
\end{lstlisting}

Shifting data makes it easy to gather statistics about changes from one timestamp or period to the next.

\begin{lstlisting}
# Find the changes from one period/timestamp to the next
>>> df - df.shift(1)            # Equivalent to df.diff().
               VALUE
2016-10-07       NaN
2016-10-08  0.683331
2016-10-09 -0.154516
2016-10-10 -0.305279
2016-10-11  0.257336
\end{lstlisting}

\begin{problem}
Compute the following information about the DJIA dataset from Problem \ref{prob:timeseries-dowjones}.
\begin{itemize}
    \item The single day with the largest gain.
    \item The single day with the largest loss.
\end{itemize}
Return the index of the day with the largest gain and the day with the largest loss.

(Hint: Call \li{prob1()} to get the DataFrame already cleaned and with DatetimeIndex).
\end{problem}

\subsection*{Rolling Functions and Exponentially-Weighted Moving Functions}

Many time series are inherently noisy.
To analyze general trends in data, we use \emph{rolling functions} and \emph{exponentally-weighted moving (EWM)} functions.
Rolling functions, or \emph{moving window functions}, perform a calculation on a window of data.
There are a few rolling functions that come standard with pandas.

\subsubsection*{Rolling Functions (Moving Window Functions)}

One of the most commonly used rolling functions is the \emph{rolling average}, which takes the average value over a window of data.

\begin{lstlisting}
# Generate a time series using random walk from a uniform distribution.
N = 10000
bias = 0.01
s = np.zeros(N)
s[1:] = np.random.uniform(low=-1, high=1, size=N-1) + bias
s = pd.Series(s.cumsum(),
              index=pd.date_range("2015-10-20", freq='H', periods=N))

# Plot the original data together with a rolling average.
ax1 = plt.subplot(121)
s.plot(color="gray", lw=.3, ax=ax1)
s.rolling(window=200).mean().plot(color='r', lw=1, ax=ax1)
ax1.legend(["Actual", "Rolling"], loc="lower right")
ax1.set_title("Rolling Average")
\end{lstlisting}

The function call \li{s.rolling(window=200)} creates a \li{pd.core.rolling.Window} object that can be aggregated with a function like \li{mean()}, \li{std()}, \li{var()}, \li{<<min>>()}, \li{<<max>>()}, and so on.

\subsubsection*{Exponentially-Weighted Moving (EWM) Functions}

Whereas a moving window function gives equal weight to the whole window, an \emph{exponentially-weighted moving} function gives more weight to the most recent data points.

In the case of a \emph{exponentially-weighted moving average} (EWMA), each data point is calculated as follows.
\[
z_i = \alpha \bar{x}_i + (1 - \alpha)z_{i-1},
\]
where $z_i$ is the value of the EWMA at time $i$, $\bar{x}_i$ is the average for the $i$-th window, and $\alpha$ is the decay factor that controls the importance of previous data points.
Notice that $\alpha=1$ reduces to the rolling average.

More commonly, the decay is expressed as a function of the window size.
In fact, the \li{span} for an EWMA is nearly analogous to \li{window} size for a rolling average.

Notice the syntax for EWM functions is very similar to that of rolling functions.

\begin{figure}[H] % Rolling average vs. EWMA
\captionsetup[subfigure]{justification=centering}
\centering
\begin{subfigure}{.49\textwidth}
    \centering
    \includegraphics[width=\textwidth]{figures/moving_rolling.pdf}
    \caption{}
    \label{fig:pandas-ts-moving-rolling}
\end{subfigure}
%
\begin{subfigure}{.49\textwidth}
    \centering
    \includegraphics[width=\textwidth]{figures/moving_ewma.pdf}
    \caption{}
    \label{fig:pandas-ts-moving-ewma}
\end{subfigure}
\caption{Rolling average and EWMA.}
\end{figure}

\begin{lstlisting}
ax2 = plt.subplot(122)
s.plot(color="gray", lw=.3, ax=ax2)
s.ewm(span=200).mean().plot(color='g', lw=1, ax=ax2)
ax2.legend(["Actual", "EWMA"], loc="lower right")
ax2.set_title("EWMA")
\end{lstlisting}

\begin{problem}
Plot the following from the DJIA dataset with a window or span of 30, 120, and 365.
\begin{itemize}
    \item The original data points.
    \item Rolling average.
    \item Exponential average.
\end{itemize}
Your plots should look like Figure \ref{fig:prob6}.
Return a list of the minimum rolling average value for each window size and a list of the minimum exponential average value for each span size.
\label{prob:rolling}
\end{problem}

\begin{figure}[h]
\includegraphics[width=\textwidth]{figures/prob6.pdf}
\caption{Plots for Problem \ref{prob:rolling}.}
\label{fig:prob6}
\end{figure}


% LaTeX source for ``Python for Informatics: Exploring Information''
% Copyright (c)  2010-  Charles R. Severance, All Rights Reserved

\chapter{Functions}
\label{funcchap}

\section{Function calls}
\label{functionchap}
\index{function call}

In the context of programming, a {\bf function} is a named sequence of
statements that performs a computation.  When you define a function,
you specify the name and the sequence of statements.  Later, you can
``call'' the function by name.
We have already seen one example of a {\bf function call}:

\beforeverb
\begin{verbatim}
>>> type(32)
<type 'int'>
\end{verbatim}
\afterverb
%
The name of the function is {\tt type}.  The expression in parentheses
is called the {\bf argument} of the function.  The argument is
a value or variable that we are passing into the function as input
to the function.
The result, for the {\tt type} function, is the type of the argument.

\index{parentheses!argument in}

It is common to say that a function ``takes'' an argument and ``returns''
a result.  The result is called the {\bf return value}.

\index{argument}
\index{return value}

\section{Built-in functions}

Python provides a number of important built-in functions that
we can use without needing to provide the function definition.
The creators of Python wrote a set of functions
to solve common problems and included them in Python for us to use.

The {\tt max} and {\tt min} functions give us the largest and
smallest values in a list, respectively:

\beforeverb
\begin{verbatim}
>>> max('Hello world')
'w'
>>> min('Hello world')
' '
>>>
\end{verbatim}
\afterverb
%
The {\tt max} function tells us the ``largest character'' in the
string (which turns out to be the letter ``w'') and the {\tt min}
function shows us the smallest character (which turns out to be a
space).

Another very common built-in function is the {\tt len} function
which tells us how many items are in its argument. If the argument
to {\tt len} is a string, it returns the number of characters
in the string.

\beforeverb
\begin{verbatim}
>>> len('Hello world')
11
>>>
\end{verbatim}
\afterverb
%
These functions are not limited to looking at strings. They can operate
on any set of values, as we will see in later chapters.

You should treat the names of built-in functions as reserved words (i.e.,
avoid using ``max'' as a variable name).

\section{Type conversion functions}
\index{conversion!type}
\index{type conversion}

% from Elkner:
% comment on whether these things are _really_ functions?
% use max as an example of a built-in?

% my reply:
% they are on the list of ``built-in functions'' so I am
% willing to call them functions.

Python also provides built-in functions that convert values
from one type to another.  The {\tt int} function takes any value and
converts it to an integer, if it can, or complains otherwise:

\index{int function}
\index{function!int}

\beforeverb
\begin{verbatim}
>>> int('32')
32
>>> int('Hello')
ValueError: invalid literal for int(): Hello
\end{verbatim}
\afterverb
%
{\tt int} can convert floating-point values to integers, but it
doesn't round off; it chops off the fraction part:

\beforeverb
\begin{verbatim}
>>> int(3.99999)
3
>>> int(-2.3)
-2
\end{verbatim}
\afterverb
%
{\tt float} converts integers and strings to floating-point
numbers:

\index{float function}
\index{function!float}

\beforeverb
\begin{verbatim}
>>> float(32)
32.0
>>> float('3.14159')
3.14159
\end{verbatim}
\afterverb
%
Finally, {\tt str} converts its argument to a string:

\index{str function}
\index{function!str}

\beforeverb
\begin{verbatim}
>>> str(32)
'32'
>>> str(3.14159)
'3.14159'
\end{verbatim}
\afterverb
%

\section{Random numbers}

\index{random number}
\index{number, random}
\index{deterministic}
\index{pseudorandom}

Given the same inputs, most computer programs generate the same
outputs every time, so they are said to be {\bf deterministic}.
Determinism is usually a good thing, since we expect the same
calculation to yield the same result.  For some applications, though,
we want the computer to be unpredictable.  Games are an obvious
example, but there are more.

Making a program truly nondeterministic turns out to be not so easy,
but there are ways to make it at least seem nondeterministic.  One of
them is to use {\bf algorithms} that generate {\bf pseudorandom} numbers.
Pseudorandom numbers are not truly random because they are generated
by a deterministic computation, but just by looking at the numbers it
is all but impossible to distinguish them from random.

\index{random module}
\index{module!random}

The {\tt random} module provides functions that generate
pseudorandom numbers (which I will simply call ``random'' from
here on).

\index{random function}
\index{function!random}

The function {\tt random} returns a random float
between 0.0 and 1.0 (including 0.0 but not 1.0).  Each time you
call {\tt random}, you get the next number in a long series.  To see a
sample, run this loop:

\beforeverb
\begin{verbatim}
import random

for i in range(10):
    x = random.random()
    print x
\end{verbatim}
\afterverb
%
This program produces the following list of 10 random numbers
between 0.0 and up to but not including 1.0.

\beforeverb
\begin{verbatim}
0.301927091705
0.513787075867
0.319470430881
0.285145917252
0.839069045123
0.322027080731
0.550722110248
0.366591677812
0.396981483964
0.838116437404
\end{verbatim}
\afterverb
%
\begin{ex}
Run the program on your system and see what numbers you get.
Run the program more than once and see what numbers you get.
\end{ex}

The {\tt random} function is only one of many
functions that handle random numbers.
The function {\tt randint} takes the parameters {\tt low} and
{\tt high}, and returns an integer between {\tt low} and
{\tt high} (including both).

\index{randint function}
\index{function!randint}

\beforeverb
\begin{verbatim}
>>> random.randint(5, 10)
5
>>> random.randint(5, 10)
9
\end{verbatim}
\afterverb
%
To choose an element from a sequence at random, you can use
{\tt choice}:

\index{choice function}
\index{function!choice}

\beforeverb
\begin{verbatim}
>>> t = [1, 2, 3]
>>> random.choice(t)
2
>>> random.choice(t)
3
\end{verbatim}
\afterverb
%
The {\tt random} module also provides functions to generate
random values from continuous distributions including
Gaussian, exponential, gamma, and a few more.

\section{Math functions}
\index{math function}
\index{function, math}
\index{module}
\index{module object}

Python has a {\tt math} module that provides most of the familiar
mathematical functions.
Before we can use the module, we have to import it:

\beforeverb
\begin{verbatim}
>>> import math
\end{verbatim}
\afterverb
%
This statement creates a {\bf module object} named math.  If
you print the module object, you get some information about it:

\beforeverb
\begin{verbatim}
>>> print math
<module 'math' from '/usr/lib/python2.5/lib-dynload/math.so'>
\end{verbatim}
\afterverb
%
The module object contains the functions and variables defined in the
module.  To access one of the functions, you have to specify the name
of the module and the name of the function, separated by a dot (also
known as a period).  This format is called {\bf dot notation}.

\index{dot notation}

\beforeverb
\begin{verbatim}
>>> ratio = signal_power / noise_power
>>> decibels = 10 * math.log10(ratio)

>>> radians = 0.7
>>> height = math.sin(radians)
\end{verbatim}
\afterverb
%
The first example computes the logarithm base 10 of the
signal-to-noise ratio.  The math module also provides a
function called {\tt log} that computes logarithms base {\tt e}.

\index{log function}
\index{function!log}
\index{sine function}
\index{radian}
\index{trigonometric function}
\index{function, trigonometric}

The second example finds the sine of {\tt radians}.  The name of the
variable is a hint that {\tt sin} and the other trigonometric
functions ({\tt cos}, {\tt tan}, etc.)  take arguments in radians. To
convert from degrees to radians, divide by 360 and multiply by $2
\pi$:

\beforeverb
\begin{verbatim}
>>> degrees = 45
>>> radians = degrees / 360.0 * 2 * math.pi
>>> math.sin(radians)
0.707106781187
\end{verbatim}
\afterverb
%
The expression {\tt math.pi} gets the variable {\tt pi} from the math
module.  The value of this variable is an approximation
of $\pi$, accurate to about 15 digits.

\index{pi}

If you know
your trigonometry, you can check the previous result by comparing it to
the square root of two divided by two:

\index{sqrt function}
\index{function!sqrt}

\beforeverb
\begin{verbatim}
>>> math.sqrt(2) / 2.0
0.707106781187
\end{verbatim}
\afterverb
%

\section{Adding new functions}

So far, we have only been using the functions that come with Python,
but it is also possible to add new functions.
A {\bf function definition} specifies the name of a new function and
the sequence of statements that execute when the function is called.
Once we define a function, we can reuse the function over and over
throughout our program.

\index{function}
\index{function definition}
\index{definition!function}

Here is an example:

\beforeverb
\begin{verbatim}
def print_lyrics():
    print "I'm a lumberjack, and I'm okay."
    print 'I sleep all night and I work all day.'
\end{verbatim}
\afterverb
%
{\tt def} is a keyword that indicates that this is a function
definition.  The name of the function is \verb"print_lyrics".  The
rules for function names are the same as for variable names: letters,
numbers and some punctuation marks are legal, but the first character
can't be a number.  You can't use a keyword as the name of a function,
and you should avoid having a variable and a function with the same
name.

\index{def keyword}
\index{keyword!def}
\index{argument}

The empty parentheses after the name indicate that this function
doesn't take any arguments.   Later we will build functions that
take arguments as their inputs.

\index{parentheses!empty}
\index{header}
\index{body}
\index{indentation}
\index{colon}

The first line of the function definition is called the {\bf header};
the rest is called the {\bf body}.  The header has to end with a colon
and the body has to be indented.  By convention, the indentation is
always four spaces.  The body can contain
any number of statements.

The strings in the print statements are enclosed in
quotes.  Single quotes and double quotes do the same thing;
most people use single quotes except in cases like this where
a single quote (which is also an apostrophe) appears in the string.

\index{ellipses}

If you type a function definition in interactive mode, the interpreter
prints ellipses (\emph{...}) to let you know that the definition
isn't complete:

\beforeverb
\begin{verbatim}
>>> def print_lyrics():
...     print "I'm a lumberjack, and I'm okay."
...     print 'I sleep all night and I work all day.'
...
\end{verbatim}
\afterverb
%
To end the function, you have to enter an empty line (this is
not necessary in a script).

Defining a function creates a variable with the same name.

\beforeverb
\begin{verbatim}
>>> print print_lyrics
<function print_lyrics at 0xb7e99e9c>
>>> print type(print_lyrics)
<type 'function'>
\end{verbatim}
\afterverb
%
The value of \verb"print_lyrics" is a {\bf function object}, which
has type \verb"'function'".

\index{function object}
\index{object!function}

The syntax for calling the new function is the same as
for built-in functions:

\beforeverb
\begin{verbatim}
>>> print_lyrics()
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
\end{verbatim}
\afterverb
%
Once you have defined a function, you can use it inside another
function.  For example, to repeat the previous refrain, we could write
a function called \verb"repeat_lyrics":

\beforeverb
\begin{verbatim}
def repeat_lyrics():
    print_lyrics()
    print_lyrics()
\end{verbatim}
\afterverb
%
And then call \verb"repeat_lyrics":

\beforeverb
\begin{verbatim}
>>> repeat_lyrics()
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
\end{verbatim}
\afterverb
%
But that's not really how the song goes.

\section{Definitions and uses}
\index{function definition}

Pulling together the code fragments from the previous section, the
whole program looks like this:

\beforeverb
\begin{verbatim}
def print_lyrics():
    print "I'm a lumberjack, and I'm okay."
    print 'I sleep all night and I work all day.'

def repeat_lyrics():
    print_lyrics()
    print_lyrics()

repeat_lyrics()
\end{verbatim}
\afterverb
%
This program contains two function definitions: \verb"print_lyrics" and
\verb"repeat_lyrics".  Function definitions get executed just like other
statements, but the effect is to create function objects.  The statements
inside the function do not get executed until the function is called, and
the function definition generates no output.

\index{use before def}

As you might expect, you have to create a function before you can
execute it.  In other words, the function definition has to be
executed before the first time it is called.

\begin{ex}
Move the last line of this program
to the top, so the function call appears before the definitions. Run
the program and see what error
message you get.
\end{ex}

\begin{ex}
Move the function call back to the bottom
and move the definition of \verb"print_lyrics" after the definition of
\verb"repeat_lyrics".  What happens when you run this program?
\end{ex}

\section{Flow of execution}
\index{flow of execution}

In order to ensure that a function is defined before its first use,
you have to know the order in which statements are executed, which is
called the {\bf flow of execution}.

Execution always begins at the first statement of the program.
Statements are executed one at a time, in order from top to bottom.

Function \emph{definitions} do not alter the flow of execution of the
program, but remember that statements inside the function are not
executed until the function is called.

A function call is like a detour in the flow of execution. Instead of
going to the next statement, the flow jumps to the body of
the function, executes all the statements there, and then comes back
to pick up where it left off.

That sounds simple enough, until you remember that one function can
call another.  While in the middle of one function, the program might
have to execute the statements in another function. But while
executing that new function, the program might have to execute yet
another function!

Fortunately, Python is good at keeping track of where it is, so each
time a function completes, the program picks up where it left off in
the function that called it.  When it gets to the end of the program,
it terminates.

What's the moral of this sordid tale?  When you read a program, you
don't always want to read from top to bottom.  Sometimes it makes
more sense if you follow the flow of execution.

\section{Parameters and arguments}
\label{parameters}
\index{parameter}
\index{function parameter}
\index{argument}
\index{function argument}

Some of the built-in functions we have seen require arguments.  For
example, when you call {\tt math.sin} you pass a number
as an argument.  Some functions take more than one argument:
{\tt math.pow} takes two, the base and the exponent.

Inside the function, the arguments are assigned to
variables called {\bf parameters}.  Here is an example of a
user-defined function that takes an argument:

\index{parentheses!parameters in}

\beforeverb
\begin{verbatim}
def print_twice(bruce):
    print bruce
    print bruce
\end{verbatim}
\afterverb
%
This function assigns the argument to a parameter
named {\tt bruce}.  When the function is called, it prints the value of
the parameter (whatever it is) twice.

This function works with any value that can be printed.

\beforeverb
\begin{verbatim}
>>> print_twice('Spam')
Spam
Spam
>>> print_twice(17)
17
17
>>> print_twice(math.pi)
3.14159265359
3.14159265359
\end{verbatim}
\afterverb
%
The same rules of composition that apply to built-in functions also
apply to user-defined functions, so we can use any kind of expression
as an argument for \verb"print_twice":

\index{composition}

\beforeverb
\begin{verbatim}
>>> print_twice('Spam '*4)
Spam Spam Spam Spam
Spam Spam Spam Spam
>>> print_twice(math.cos(math.pi))
-1.0
-1.0
\end{verbatim}
\afterverb
%
The argument is evaluated before the function is called, so
in the examples the expressions \verb"'Spam '*4" and
{\tt math.cos(math.pi)} are only evaluated once.

\index{argument}

You can also use a variable as an argument:

\beforeverb
\begin{verbatim}
>>> michael = 'Eric, the half a bee.'
>>> print_twice(michael)
Eric, the half a bee.
Eric, the half a bee.
\end{verbatim}
\afterverb
%
The name of the variable we pass as an argument ({\tt michael}) has
nothing to do with the name of the parameter ({\tt bruce}).  It
doesn't matter what the value was called back home (in the caller);
here in \verb"print_twice", we call everybody {\tt bruce}.

\section{Fruitful functions and void functions}

\index{fruitful function}
\index{void function}
\index{function, fruitful}
\index{function, void}

Some of the functions we are using, such as the math functions, yield
results; for lack of a better name, I call them {\bf fruitful
  functions}.  Other functions, like \verb"print_twice", perform an
action but don't return a value.  They are called {\bf void
  functions}.

When you call a fruitful function, you almost always
want to do something with the result; for example, you might
assign it to a variable or use it as part of an expression:

\beforeverb
\begin{verbatim}
x = math.cos(radians)
golden = (math.sqrt(5) + 1) / 2
\end{verbatim}
\afterverb
%
When you call a function in interactive mode, Python displays
the result:

\beforeverb
\begin{verbatim}
>>> math.sqrt(5)
2.2360679774997898
\end{verbatim}
\afterverb
%
But in a script, if you call a fruitful function and do
not store the result of the function in a variable,
the return value vanishes into the mist!

\beforeverb
\begin{verbatim}
math.sqrt(5)
\end{verbatim}
\afterverb
%
This script computes the square root of 5, but since it doesn't store
the result in a variable or display the result, it is not very useful.

\index{interactive mode}
\index{script mode}

Void functions might display something on the screen or have some
other effect, but they don't have a return value.  If you try to
assign the result to a variable, you get a special value called
{\tt None}.

\index{None special value}
\index{special value!None}

\beforeverb
\begin{verbatim}
>>> result = print_twice('Bing')
Bing
Bing
>>> print result
None
\end{verbatim}
\afterverb
%
The value {\tt None} is not the same as the string \verb"'None'".
It is a special value that has its own type:

\beforeverb
\begin{verbatim}
>>> print type(None)
<type 'NoneType'>
\end{verbatim}
\afterverb
%
To return a result from a function, we use the {\tt return} statement
in our function.  For example, we could make a very
simple function called {\tt addtwo}
that adds two numbers together and returns a result.

\beforeverb
\begin{verbatim}
def addtwo(a, b):
    added = a + b
    return added

x = addtwo(3, 5)
print x
\end{verbatim}
\afterverb
%
When this script executes, the {\tt print} statement will print out ``8''
because the {\tt addtwo} function was called with 3 and 5 as arguments.
Within the function, the parameters {\tt a} and {\tt b} were 3 and 5
respectively. The function computed the sum of the two numbers and placed
it in the local function variable named {\tt added}.
Then it used the {\tt return} statement
to send the computed value back to the calling code
as the function result, which was assigned
to the variable {\tt x} and printed out.

\section{Why functions?}
\index{function, reasons for}

It may not be clear why it is worth the trouble to divide
a program into functions.  There are several reasons:

\begin{itemize}

\item Creating a new function gives you an opportunity to name a group
of statements, which makes your program easier to read, understand,
and debug.

\item Functions can make a program smaller by eliminating repetitive
code.  Later, if you make a change, you only have
to make it in one place.

\item Dividing a long program into functions allows you to debug the
parts one at a time and then assemble them into a working whole.

\item Well-designed functions are often useful for many programs.
Once you write and debug one, you can reuse it.

\end{itemize}

Throughout the rest of the book, often we will use a function definition to
explain a concept.  Part of the skill of creating and using functions is
to have a function properly capture an idea such as ``find the smallest
value in a list of values''.  Later we will show you code that finds
the smallest in a list of values and we will present it to you as a function
named {\tt min} which takes a list of values as its argument and
returns the smallest value in the list.

\section{Debugging}
\label{editor}
\index{debugging}

If you are using a text editor to write your scripts, you might
run into problems with spaces and tabs.  The best way to avoid
these problems is to use spaces exclusively (no tabs).  Most text
editors that know about Python do this by default, but some
don't.

\index{whitespace}

Tabs and spaces are usually invisible, which makes them
hard to debug, so try to find an editor that manages indentation
for you.

Also, don't forget to save your program before you run it.  Some
development environments do this automatically, but some don't.
In that case, the program you are looking at in the text editor
is not the same as the program you are running.

Debugging can take a long time if you keep running the same
incorrect program over and over!

Make sure that the code you are looking at is the code you are running.
If you're not sure, put something like \verb"print 'hello'" at the
beginning of the program and run it again.  If you don't see
\verb"hello", you're not running the right program!

\section{Glossary}

\begin{description}

\item[algorithm:]  A general process for solving a category of
problems.
\index{algorithm}

\item[argument:]  A value provided to a function when the function is called.
This value is assigned to the corresponding parameter in the function.
\index{argument}

\item[body:] The sequence of statements inside a function definition.
\index{body}

\item[composition:] Using an expression as part of a larger expression,
or a statement as part of a larger statement.
\index{composition}

\item[deterministic:] Pertaining to a program that does the same
thing each time it runs, given the same inputs.
\index{deterministic}

\item[dot notation:]  The syntax for calling a function in another
module by specifying the module name followed by a dot (period) and
the function name.
\index{dot notation}

\item[flow of execution:]  The order in which statements are executed during
a program run.
\index{flow of execution}

\item[fruitful function:] A function that returns a value.
\index{fruitful function}

\item[function:] A named sequence of statements that performs some
useful operation.  Functions may or may not take arguments and may or
may not produce a result.
\index{function}

\item[function call:] A statement that executes a function. It
consists of the function name followed by an argument list.
\index{function call}

\item[function definition:]  A statement that creates a new function,
specifying its name, parameters, and the statements it executes.
\index{function definition}

\item[function object:]  A value created by a function definition.
The name of the function is a variable that refers to a function
object.
\index{function definition}

\item[header:] The first line of a function definition.
\index{header}

\item[import statement:] A statement that reads a module file and creates
a module object.
\index{import statement}
\index{statement!import}

\item[module object:] A value created by an {\tt import} statement
that provides access to the data and code defined in a module.
\index{module}

\item[parameter:] A name used inside a function to refer to the value
passed as an argument.
\index{parameter}

\item[pseudorandom:] Pertaining to a sequence of numbers that appear
to be random, but are generated by a deterministic program.
\index{pseudorandom}

\item[return value:]  The result of a function.  If a function call
is used as an expression, the return value is the value of
the expression.
\index{return value}

\item[void function:] A function that does not return a value.
\index{void function}

\end{description}

\section{Exercises}

\begin{ex}
What is the purpose of the "def" keyword in Python?

a) It is slang that means "the following code is really cool"\\
b) It indicates the start of a function\\
c) It indicates that the following indented section of code is to be stored for later\\
d) b and c are both true\\
e) None of the above
\end{ex}

\begin{ex}
What will the following Python program print out?

\beforeverb
\begin{verbatim}
def fred():
   print "Zap"

def jane():
   print "ABC"

jane()
fred()
jane()
\end{verbatim}
\afterverb
%
a) Zap ABC jane fred jane\\
b) Zap ABC Zap\\
c) ABC Zap jane\\
d) ABC Zap ABC\\
e) Zap Zap Zap
\end{ex}

\begin{ex}
Rewrite your pay computation with time-and-a-half for overtime
and create a function called {\tt computepay} which takes
two parameters ({\tt hours} and {\tt rate}).

\begin{verbatim}
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
\end{verbatim}
\end{ex}

\begin{ex}
Rewrite the grade program from the previous chapter
using a function called {\tt computegrade} that takes
a score as its parameter and returns a grade as a string.

\begin{verbatim}
Score   Grade
> 0.9     A
> 0.8     B
> 0.7     C
> 0.6     D
<= 0.6    F

Program Execution:

Enter score: 0.95
A

Enter score: perfect
Bad score

Enter score: 10.0
Bad score

Enter score: 0.75
C

Enter score: 0.5
F
\end{verbatim}

Run the program repeatedly to test the various different values
for input.
\end{ex}


\documentclass[11pt, a4paper, twoside]{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage[margin=0.8in]{geometry}
\usepackage{listings}
\usepackage{hyperref}
\usepackage{float}
\usepackage{fancyhdr}
\usepackage{indentfirst}
\usepackage[inline]{enumitem}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{array}
\usepackage{minted}
\usemintedstyle{vs}
\usepackage[belowskip=0pt,aboveskip=0pt,font=small,labelfont=small]{caption}
\captionsetup{width=\linewidth}

\makeatletter
    \renewcommand{\fps@figure}{!ht}
    \renewcommand{\fps@table}{!ht}
\makeatother

\def \courseNumber {EE2703}
\def \assignmentNumber {9}
\def \myName {Akilesh Kannan}
\def \rollNumber {EE18B122}

\setlength\intextsep{0pt}
\graphicspath{{plots/}}
\setlist[itemize]{noitemsep, topsep=0pt}
\fancyhead[RO,LE]{\courseNumber : Assignment \assignmentNumber}
\fancyhead[LO,RE]{\myName}
\cfoot{\thepage}

\title{\courseNumber : Assignment \assignmentNumber}
\author{\myName\ (\rollNumber)}
\date{\today}

\pagestyle{fancy}

\begin{document}
\maketitle

\section{Introduction}
    In this assignment, we continue our explorations on the DFT of a finite-length sequence with the FFT algorithm, using the \texttt{numpy.fft} module. We shall look at finding the DFTs of non-periodic functions, and problems associated with them, namely the \textit{Gibbs Phenomenon} and how to overcome them, using \textit{windowing}.

\section{Spectrum of $sin(\sqrt{2}t)$}
    Using the method we followed for obtaining the DFT of a periodic signal, we get the following spectrum for $sin(\sqrt{2}t)$ :
    \begin{figure}
        \centering
        \includegraphics[scale=0.5]{Fig0.png}
        \caption{Spectrum of $sin(\sqrt{2}t)$}
        \label{fig:Fig0}
    \end{figure}

    But, this is not what we expected! We expected two peaks, but that is not what we got. This is because, we aren't finding out the DFT of the required function, $sin(\sqrt{2}t)$ :
    \begin{figure}
        \centering
        \includegraphics[scale=0.5]{fig10-2.png}
        \caption{$sin(\sqrt{2}t)$}
        \label{fig:fig10-2}
    \end{figure}

    \begin{figure}
        \centering
        \includegraphics[scale=0.5]{fig10-3.png}
        \caption{The function for which we are calculating DFT}
        \label{fig:fig10-3}
    \end{figure}

    We can see that this is not what we want to calculate the DFT for. The discontinuities in the function has led to the very problematic \textit{Gibb's Phenomenon}. So, we do not observe a sharp peak, but rather a flat one.

    This problem can be fixed by \textbf{windowing}. Windowing is an operation in which we multiply the time-domain function with a suitable window function. In this assignment, we choose the \textit{Hamming Window}, defined as:
    \begin{equation*}
        W_N[n] = \begin{cases}
                0.54 + 0.46\ cos(\frac{2\pi n}{N-1}),\  |n| < N\\
                0,\ otherwise
                \end{cases}
    \end{equation*}

    The result in time domain is that the magnitude of the jump is greatly reduced, thus minimizing the effect of Gibb's phenomenon in the frequency domain:
    \begin{figure}
        \centering
        \includegraphics[scale=0.5]{fig10-5.png}
        \caption{Windowed $sin(\sqrt{2}t)$}
        \label{fig:fig10-5}
    \end{figure}

    \begin{figure}
        \centering
        \includegraphics[scale=0.5]{Fig1.png}
        \caption{Spectrum of windowed $sin(\sqrt{2}t)$}
        \label{fig:Fig1}
    \end{figure}

\section{Spectrum of $cos^3(0.86 t)$}
    We can see the effect of windowing even better for $cos^3(0.86 t)$. We get the following spectra before and after windowing:
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{Fig2.png}
        \caption{Spectrum of $cos^3(0.86 t)$ without windowing}
        \label{fig:Fig2}
    \end{figure}
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{Fig3.png}
        \caption{Spectrum of $cos^3(0.86 t)$ after windowing}
        \label{fig:Fig3}
    \end{figure}

    We can see narrower and sharper peaks at the frequencies that are present in the signal.

\section{Parameter Estimation using DFT}
    We are given a 128-element vector with sampled values of the signal $cos(\omega_ot+\delta)$, where $\omega_o$ and $\delta$ are to be estimated.

    We cannot extract $\omega_o$ from the DFT spectrum because the sampling rate is very low (only 128 points). The peaks will overlap and so we will not be able to extract their values.

    I used an approach as illustrated below:

    \begin{gather*}
        \omega_{o_{est}} = \frac{\sum_{\omega}|Y(j\omega)|^k\cdot \omega}{\sum_{\omega}|Y(j\omega)|^k}\\
        y(t) = Acos(\omega_ot) - Bsin(\omega_ot)\\
        \delta_{est} = \arctan(-\frac{B}{A})
    \end{gather*}

    In the above equations, I used a least squares approach to find A, B.

    I varied the parameter $k$ manually to find out which worked best for the given range of $\omega_o$. I used $k = 1.7$, for estimation in absence of noise, and $k = 2.4$, for estimation in presence of noise, to give the following results (I had set $\omega_o = 1.35$ and $\delta=\frac{\pi}{2}$ in both cases) :
    \vspace{3mm}
    \begin{table}[htbp]
        \centering
        \begin{tabular}{c|c|c|c|c}
            Noise & $\omega_o$ & $\omega_{o_{est}}$ & $\delta$ & $\delta_{est}$\\
            \hline
             0 & 1.35 & 1.453 & 1.571 & 1.571\\
             0.1*\texttt{rand($128$)} & 1.35 & 1.437 & 1.571 & 1.571\\
        \end{tabular}
        \label{tab:results}
    \end{table}
\section{DFT of chirp $cos(16t(1.5 + \frac{t}{2\pi}))$}
    A chirp is a signal in which the frequency increases or decreases with time\footnote{\textit{Source}: \href{https://en.wikipedia.org/wiki/Chirp}{https://en.wikipedia.org/wiki/Chirp}}. We are going to analyse the chirp signal $cos(16t(1.5 + \frac{t}{2\pi}))$. First, we shall plot the signal versus time, to get an idea of how the signal looks like:
    \begin{figure}
        \centering
        \includegraphics[scale=0.5]{Fig6.png}
        \caption{$cos(16t(1.5 + \frac{t}{2\pi}))$}
        \label{fig:Fig6}
    \end{figure}

    We see that the frequency varies from $16\ rad/sec$ to $32\ rad/sec$ as $t$ goes from $-\pi \ sec$ to $\pi\ sec$.

    On finding the DFT of the above signal, we get:
    \begin{figure}
        \centering
        \includegraphics[scale=0.5]{Fig7.png}
        \caption{Spectrum of $cos(16t(1.5 + \frac{t}{2\pi}))$}
        \label{fig:Fig6}
    \end{figure}

    Applying the Hamming Window to the chirp results in the following:
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.45]{Fig8.png}
        \caption{Windowed $cos(16t(1.5 + \frac{t}{2\pi}))$}
        \label{fig:Fig6}
    \end{figure}
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{Fig9.png}
        \caption{Spectrum of windowed $cos(16t(1.5 + \frac{t}{2\pi}))$}
        \label{fig:Fig6}
    \end{figure}

    The variations in the frequency have been smoothed out by the window, and also, we can see that the frequencies are more accurately confined to the range 16-32 $rad/sec$.

\section{Time-frequency plot of $cos(16t(1.5 + \frac{t}{2\pi}))$}
    We shall split the chirp in the time interval $[-\pi,\ \pi]$ into smaller intervals of time, and observe how the frequency of the signal varies with time.

    Initially we had a 1024-length vector with the values of the chirp signal. We shall split it into 64-length vectors, take the DFTs of these localized vectors, and plot a time-frequency surface plot to observe the variation of the frequency with time.

    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{Fig10.png}
        \caption{Spectrum of windowed $cos(16t(1.5 + \frac{t}{2\pi}))$}
        \label{fig:Fig6}
    \end{figure}
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{Figure_10.png}
        \caption{Spectrum of windowed $cos(16t(1.5 + \frac{t}{2\pi}))$}
        \label{fig:Fig6}
    \end{figure}

    Now, we shall do the same, but with a windowed version of the chirp.
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{Fig11.png}
        \caption{Spectrum of windowed $cos(16t(1.5 + \frac{t}{2\pi}))$}
        \label{fig:Fig6}
    \end{figure}
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{Figure_11.png}
        \caption{Spectrum of windowed $cos(16t(1.5 + \frac{t}{2\pi}))$}
        \label{fig:Fig6}
    \end{figure}

    We can see that the change in the magnitude is more gradual in the windowed case.

\section{Conclusion}
    The DFT was obtained using a $2\pi$ periodic extension of the signal, and thus the spectrum was found to be erroneous for a non periodic function.  The spectrum was rectified by the using a windowing technique, by employing the Hamming window.  Given a vector of cosine values in the a time interval, the frequency and phase were estimated from the DFT spectrum, by using the expectation value of the frequency and a parameter tuning for optimum values.  The DFT of a chirped signal was analysed and its time-frequency plot showed the gradual variation of peak frequency of the spectrum with time.
\end{document}


\documentclass[12pt]{article}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage{graphicx}
\usepackage{sectsty}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{array}
\newcommand{\sectionbreak}{\clearpage}

\usepackage{tikz}
	\usetikzlibrary{arrows,shapes,trees}

\allsectionsfont{\centering}

\usepackage{draftwatermark}
	\SetWatermarkText{\copyright wolf-math.com}
	\SetWatermarkScale{4}
	\SetWatermarkLightness{1}

\usepackage[margin=1in, headsep=0pt]{geometry}
\setlength{\parindent}{0cm}
\pagestyle{empty}

\begin{document}

Mr. Wolf  \\ wolf-math.com

\section*{Trig Pre-Requisites}

\subsection*{Goals}

\textbf{SWBAT} calculate and apply the Pythagorean Theorem.\\

\textbf{SWBAT} calculate and apply the distance formula.\\

\textbf{SWBAT} graph circles using $x^2+y^2=r^2$\\

\subsection*{Standards}

\textbf{Expressing Geometric Properties with Equations \hfill	G-GPE}\\

Translate between the geometric description and the equation for a
conic section\\

1.Derive the equation of a circle of given center and radius using the
Pythagorean Theorem; complete the square to find the center and
radius of a circle given by an equation.\\

Use coordinates to prove simple geometric theorems algebraically\\

7.	 Use coordinates to compute perimeters of polygons and areas of
triangles and rectangles, e.g., using the distance formula. \\

\textbf{Geometry \hfill 8.G}\\

Understand and apply the Pythagorean Theorem.\\

6.	 Explain a proof of the Pythagorean Theorem and its converse.\\
7.	 Apply the Pythagorean Theorem to determine unknown side lengths
in right triangles in real-world and mathematical problems in two and
three dimensions.\\
8.	 Apply the Pythagorean Theorem to find the distance between two
points in a coordinate system.\\

\subsection*{Connections}

\textbf{Now} we are learning the equation of a circle, the Pythagorean Theorem and the distance formula, all of which are prerequisites to trigonometry.\\

\textbf{Later} we are going to learn basic trig ratios and how to solve right triangles with those ratios.\\

\let\stdsection\section
\renewcommand\section{\newpage\stdsection}

\section*{The Pythagorean Theorem}

$$a^2+b^2=c^2$$

Given any right triangle, if the lengths of the legs (the shorter sides) are known, but the hypotenuse (the longest side) is unknown, the length of the hypotenuse can be figured out with the Pythagorean Theorem, $a^2+b^2=c^2$, where $a$ and $b$ are the legs, and $c$ is the hypotenuse.\\

\textbf{Example:}

\pagebreak

\section*{The Distance Formula}

An application of the \textit{Pythagorean Theorem} is the \textbf{Distance Formula}. With it the distance between two points on a graph can be calculated.\\

$$d=\sqrt{(x_1-x_2)^{2}+(y_1-y_2)^{2}}$$

\textbf{Example:} 1) Let point a be $(1,1)$, and point b be $(4,5)$. Find the distance between them.\\

\begin{center}
%\includegraphics[scale=.5]{graph1.jpg}\\
\end{center}

With the distance formula the distance can be calculated without graphing.\\

\textbf{Example 2:} Find the distance between $(2,3)$ and $(4,5)$.\\

\vspace{1cm}

\textbf{Example 2:} $(0,0)$ to $(5,5)$.\\

\vspace{1cm}

\textbf{You Try:}

\begin{enumerate}

	\item $(-4,10)$ and $(7,-6)$\\

	\item $(10,2)$ and $(12, 17)$\\

	\item $(-1,-5)$ and $(4,-6)$\\

\end{enumerate}

\section*{Equation of a Circle}

\subsection*{Graphing Circles}

A circle whose center is about the origin can be graphed with the equation $$r^2=x^2+y^2$$ where $r$ is the radius, $x$ is the $x$ coordinate, and $y$ is the $y$ coordinate. \\

\textbf{Example 1:} $x^2+y^2=1$

\begin{center}
\begin{tikzpicture}[scale=1.]

	\draw[help lines] (-1.5,-1.5) grid (1.5,1.5);
	\draw[style=thick] (0,0) circle (1);
	\draw[<->] (-1.5,0) -- (1.5,0);
	\draw[<->] (0,1.5) -- (0,-1.5);
	\draw (0,0) -- (.7,.7);
	\fill[black] (.7,.7) circle (.5ex);
	\fill[black] (0,0) circle (.5ex);

\end{tikzpicture}
\end{center}

\textbf{Example 2:} $x^2+y^2=4$ -- what's the radius?

\begin{center}
\begin{tikzpicture}[scale=.85]

	\draw[help lines] (-2.2,-2.2) grid (2.2,2.2);
	\draw[style=thick] (0,0) circle (2);
	\draw[<->] (-2.3,0) -- (2.3,0);
	\draw[<->] (0,2.3) -- (0,-2.3);
	\draw (0,0) -- (1.414,1.414);
	\fill[black] (1.4,1.4) circle (.5ex);
	\fill[black] (0,0) circle (.5ex);

\end{tikzpicture}
\end{center}

The circle can be moved around. In the following equation the center would be at the point $(h,k)$. Notice that there are minus signs in the parentheses.

$$r^2=(x-h)^2+(y-k)^2$$

\textbf{Example 3:} $9=(x-2)^2+(y+3)^2$

\begin{center}
\begin{tikzpicture}[scale=.75]

	\draw[help lines] (-2,-6) grid (5,1);
	\draw[<->] (-2.1,0) -- (5.2,0);
	\draw[<->] (0,1.2) -- (0, -6.2);
	\draw[style=thick] (2,-3) circle (3);
	\fill[black] (2,-3) circle (1ex);

\end{tikzpicture}
\end{center}

\pagebreak

\subsection*{Writing Equations of Circles}

$$r^2=(x-h)^2+(y-k)^2$$

We can write the equation of any circle given certain information.\\

\textbf{Example 4:} Center at $(-1,4)$, radius $=3$

\vspace{1in}

\textbf{You Try:} Center at $(2,4)$, radius $=6$

\vspace{1in}

\hrulefill

\textbf{Example 5:} Center at $(-5,-2)$ point on circle $(1,1)$\\
\textbf{Hint:} The distance between them is the radius.\\

\vspace{1in}

\textbf{You Try:} Center at $(-5,-4)$ point on circle $(-4,-2)$

\vspace{1in}

\hrulefill

\textbf{Example 6:} What is the equation of this graph?

\begin{tikzpicture}[scale=.65]

	\draw[help lines] (-2,-6) grid (5,1);
	\draw[<->] (-2.1,0) -- (5.2,0);
	\draw[<->] (0,1.2) -- (0, -6.2);
	\draw[style=thick] (2,-3) circle (3);
	\fill[black] (2,-3) circle (1ex);

\end{tikzpicture}

\section*{Review So Far...}

Mr. Wolf \hfill \textbf{NAME:\underline{\hspace{2in}}}\\
Pre-Calculus \hfill \textbf{Date: \underline{\hspace{1in}}}\\

\subsection*{Pythagorean Theorem}

Given the triangle diagram below, determine the unknown distance.

$$a^2+b^2=c^2 \hspace{1in} c=\sqrt{a^2+b^2}$$

\begin{center}
	\begin{tikzpicture}[scale=.8]

		\path[draw, shade] (0,0) --
			(4,0) node[pos=.5,below] {a} --
			(4,3)node[pos=.5,right] {b} --cycle;

			\node (c) at (2,2) {c};

	\end{tikzpicture}
\end{center}

\begin{enumerate}
	\item $a=6$, \hspace{1cm} $b=6$,\hspace{1cm} $c=$\underline{\hspace{1in}}\\

	\item $a=$\underline{\hspace{1in}}, \hspace{1cm}$b=12$,\hspace{1cm} $c=15$\\

	\item $a=8$, \hspace{1cm}$b=6$, \hspace{1cm}$c=$\underline{\hspace{1in}}\\

	\item $a=5$, \hspace{1cm}$b=$\underline{\hspace{1in}}, \hspace{1cm}$c=5\sqrt{2}$\\

	\item $a=10$,\hspace{1cm} $b=\sqrt{21}$,\hspace{1cm} $c=$\underline{\hspace{1in}}
\end{enumerate}

\hrulefill

\subsection*{The Distance Formula}

$$d=\sqrt{(x_{1}-x_{2})^{2}+(y_{1}-y_{2})^{2}}$$

Find the distance between the two points.\\

\begin{enumerate}[resume]
\begin{multicols}{2}

	\item $(0,0),(12,5)$\\

	\item $(-3,-5),(5,10)$\\

	\item $(-10,-10),(10,11)$\\

	\item $(-5,2), (19,-5)$\\

	\item $(4,-2),(-1,8)$\\

\end{multicols}
\end{enumerate}

\pagebreak

\subsection*{Equation of a Circle}

$$r^2=x^2+y^2$$

$$r^2=(x-h)^2+(y-k)^2$$

Write the equation of a circle with the following criteria\\

\begin{enumerate}[resume]

	\item $r=4$, center at the origin\\

	\item $r=9$, center at $(4,9)$\\

	\item $r=1$, center at $(-1,-1)$\\

	\item $r=\sqrt{2}$, center at $(-3,7)$\\

	\item $r=\sqrt{5}$, center at $(5,-1)$\\

	\item center at origin, passes through the point $(9,40)$\\

\end{enumerate}

\hrulefill

Sketch the following equations.\\

\begin{enumerate}[resume]
\begin{multicols}{2}

	\item $x^2+y^2=4$\\

%	\includegraphics[scale=.5]{graph1.jpg}

	\item $(x-1)^2+(y+3)^2=9$\\

%	\includegraphics[scale=.5]{graph1.jpg}

\end{multicols}
\end{enumerate}

\section*{Review}

Mr. Wolf \hfill \textbf{NAME:\underline{\hspace{2in}}}\\
Pre-Calculus \hfill \textbf{Date: \underline{\hspace{1in}}}\\

\subsection*{Pythagorean Theorem}

Given the triangle diagram below, determine the unknown distance. (2 points each)

$$a^2+b^2=c^2 \hspace{1in} c=\sqrt{a^2+b^2} \hspace{1in} a=\sqrt{c^2-b^2}$$

\begin{center}
	\begin{tikzpicture}[scale=.8]

		\path[draw,  inner color=blue ] (0,0)--
			(0,3) node[pos=.5,left] {a} --
			(4,0) --cycle;

			\node (c) at (2,2) {c};
			\node (b) at (2,-.5) {b};

	\end{tikzpicture}
\end{center}

\begin{enumerate}
	\item $a=21$, \hspace{1in} $b=20$,\hspace{1in} $c=$\underline{\hspace{1in}}\\

	\item $a=$\underline{\hspace{1in}}, \hspace{.5cm} $b=40$,\hspace{1in} $c=41$\\

	\item $a=15$, \hspace{1in}$b=8$, \hspace{1in}$c=$\underline{\hspace{1in}}\\

	\item $a=1$, \hspace{1in}$b=$\underline{\hspace{1in}},\hspace{.5cm} $c=\sqrt{2}$\\

	\item $a=1$,\hspace{1in} $b=\sqrt{2}$,\hspace{1in} $c=$\underline{\hspace{1in}}
\end{enumerate}

\hrulefill

\subsection*{The Distance Formula}

$$d=\sqrt{(x_{1}-x_{2})^{2}+(y_{1}-y_{2})^{2}}$$

Find the distance between the two points. (3 points each)\\

\begin{enumerate}[resume]
\begin{multicols}{2}

	\item $(-5,0),(3,15)$\\

	\item $(-13,-7),(7,14)$\\

	\item $(-5,2), (19,-5)$\\

	\item $(5,-2),(0,8)$\\

\end{multicols}
\end{enumerate}

\pagebreak

\subsection*{Equation of a Circle}

$$r^2=x^2+y^2$$

$$r^2=(x-h)^2+(y-k)^2$$

Write the equation of a circle with the following criteria. (2 points each)\\

\begin{enumerate}[resume]

	\item $r=3$, center at the origin\\

	\item $r=5$, center at $(3,-5)$\\

	\item $r=7$, center at $(-2,-9)$\\

	\item $r=\sqrt{5}$, center at $(-2,4)$\\

	\item $r=\sqrt{8}$, center at $(6,-2)$\\

	\item center at $(1,1)$, passes through the point $(15,12)$\\

\end{enumerate}

\hrulefill

Sketch the following equations. (3 points each)\\

\begin{enumerate}[resume]
\begin{multicols}{2}

	\item $x^2+y^2=16$\\

%	\includegraphics[scale=.5]{graph1.jpg}

	\item $(x+2)^2+(y-3)^2=4$\\

%	\includegraphics[scale=.5]{graph1.jpg}

\end{multicols}
\end{enumerate}

\section*{Quiz.}

Mr. Wolf \hfill \textbf{NAME:\underline{\hspace{2in}}}\\
Pre-Calculus \hfill \textbf{Date: \underline{\hspace{1in}}}\\

\subsection*{Pythagorean Theorem}

Given the triangle diagram below, determine the unknown distance. (2 points each)

$$a^2+b^2=c^2 \hspace{1in} c=\sqrt{a^2+b^2} \hspace{1in} a=\sqrt{c^2-b^2} \hspace{1in} b=\sqrt{c^2-a^2}$$

\begin{center}
	\begin{tikzpicture}[scale=.8]

		\path[draw, fill=blue!15 ] (0,0)--
			(0,3) node[pos=.5,left] {a} --
			(4,0) --cycle;

			\node (c) at (2,2) {c};
			\node (b) at (2,-.5) {b};

	\end{tikzpicture}
\end{center}

\begin{enumerate}
	\item $a=10$, \hspace{1in} $b=24$,\hspace{1in} $c=$\underline{\hspace{1in}}\\

	\item $a=$\underline{\hspace{1in}}, \hspace{.5cm} $b=80$,\hspace{1in} $c=82$\\

	\item $a=6$, \hspace{1in}$b=8$, \hspace{1in}$c=$\underline{\hspace{1in}}\\

	\item $a=1$, \hspace{1in}$b=$\underline{\hspace{1in}},\hspace{.5cm} $c=\sqrt{3}$\\

	\item $a=1$,\hspace{1in} $b=1$,\hspace{1in} $c=$\underline{\hspace{1in}}
\end{enumerate}

\hrulefill

\subsection*{The Distance Formula}

$$d=\sqrt{(x_{1}-x_{2})^{2}+(y_{1}-y_{2})^{2}}$$

Find the distance between the two points. (3 points each)\\

\begin{enumerate}[resume]
\begin{multicols}{2}

	\item $(-5,0),(-2,-4)$\\

	\item $(-13,-20),(-4,20)$\\

	\item $(-5,2), (0,14)$\\

	\item $(5,-2),(-3,10)$\\

\end{multicols}
\end{enumerate}

\pagebreak

\subsection*{Equation of a Circle}

$$r^2=x^2+y^2$$

$$r^2=(x-h)^2+(y-k)^2$$

Write the equation of a circle with the following criteria. (2 points each)\\

\begin{enumerate}[resume]

	\item $r=4$, center at the origin\\

	\item $r=6$, center at $(-3,-4)$\\

	\item $r=2$, center at $(0,7)$\\

	\item $r=\sqrt{3}$, center at $(9,7)$\\

	\item $r=\sqrt{7}$, center at $(6,0)$\\

	\item center at $(0,0)$, passes through the point $(33,56)$\\

\end{enumerate}

\hrulefill

Sketch the following equations. (3 points each)\\

\begin{enumerate}[resume]
\begin{multicols}{2}

	\item $x^2+y^2=25$\\

%	\includegraphics[scale=.75]{graph1.jpg}

	\item $(x-1)^2+(y+1)^2=16$\\

%	\includegraphics[scale=.75]{graph1.jpg}

\end{multicols}
\end{enumerate}

\section*{Quiz:}

Mr. Wolf \hfill \textbf{NAME:\underline{\hspace{2in}}}\\
Pre-Calculus \hfill \textbf{Date: \underline{\hspace{1in}}}\\

\subsection*{Pythagorean Theorem}

Given the triangle diagram below, determine the unknown distance. (2 points each)

$$a^2+b^2=c^2 \hspace{1in} c=\sqrt{a^2+b^2} \hspace{1in} a=\sqrt{c^2-b^2} \hspace{1in} b=\sqrt{c^2-a^2}$$

\begin{center}
	\begin{tikzpicture}[scale=.8]

		\path[draw, fill=blue!15 ] (0,0)--
			(0,3) node[pos=.5,left] {a} --
			(4,0) --cycle;

			\node (c) at (2,2) {c};
			\node (b) at (2,-.5) {b};

	\end{tikzpicture}
\end{center}

\begin{enumerate}
	\item $a=5$, \hspace{1in} $b=12$,\hspace{1in} $c=$\underline{\hspace{1in}}\\

	\item $a=$\underline{\hspace{1in}}, \hspace{.5cm} $b=40$,\hspace{1in} $c=41$\\

	\item $a=9$, \hspace{1in}$b=12$, \hspace{1in}$c=$\underline{\hspace{1in}}\\

	\item $a=2$, \hspace{1in}$b=$\underline{\hspace{1in}},\hspace{.5cm} $c=3$\\

	\item $a=2$,\hspace{1in} $b=2$,\hspace{1in} $c=$\underline{\hspace{1in}}
\end{enumerate}

\hrulefill

\subsection*{The Distance Formula}

$$d=\sqrt{(x_{1}-x_{2})^{2}+(y_{1}-y_{2})^{2}}$$

Find the distance between the two points. (3 points each)\\

\begin{enumerate}[resume]
\begin{multicols}{2}

	\item $(-15,-10),(-6,30)$\\

	\item $(7,-2),(-1,10)$\\

	\item $(0,10),(3,6)$\\

	\item $(3,0), (8,12)$\\

\end{multicols}
\end{enumerate}

\pagebreak

\subsection*{Equation of a Circle}

$$r^2=x^2+y^2$$

$$r^2=(x-h)^2+(y-k)^2$$

Write the equation of a circle with the following criteria. (2 points each)\\

\begin{enumerate}[resume]

	\item $r=1$, center at the origin\\

	\item $r=2$, center at $(3,-4)$\\

	\item $r=3$, center at $(-2,12)$\\

	\item $r=\sqrt{5}$, center at $(-10,-10)$\\

	\item $r=\sqrt{11}$, center at $(-1,2)$\\

	\item center at $(-10,-10)$, passes through the point $(23,46)$\\

\end{enumerate}

\hrulefill

Sketch the following equations. (3 points each)\\

\begin{enumerate}[resume]
\begin{multicols}{2}

	\item $x^2+y^2=25$\\

%	\includegraphics[scale=.75]{graph1.jpg}

	\item $(x-1)^2+(y+1)^2=16$\\

%	\includegraphics[scale=.75]{graph1.jpg}

\end{multicols}
\end{enumerate}

\section*{Quiz.:}

Mr. Wolf \hfill \textbf{NAME:\underline{\hspace{2in}}}\\
Pre-Calculus \hfill \textbf{Date: \underline{\hspace{1in}}}\\

\subsection*{Pythagorean Theorem}

Given the triangle diagram below, determine the unknown distance. (2 points each)

$$a^2+b^2=c^2 \hspace{1in} c=\sqrt{a^2+b^2} \hspace{1in} a=\sqrt{c^2-b^2} \hspace{1in} b=\sqrt{c^2-a^2}$$

\begin{center}
	\begin{tikzpicture}[scale=.8]

		\path[draw, fill=blue!15 ] (0,0)--
			(0,3) node[pos=.5,left] {a} --
			(4,0) --cycle;

			\node (c) at (2,2) {c};
			\node (b) at (2,-.5) {b};

	\end{tikzpicture}
\end{center}

\begin{enumerate}

	\item $a=3$,\hspace{1in} $b=3$,\hspace{1in} $c=$\underline{\hspace{1in}}\\

	\item $a=70$, \hspace{1in} $b=24$,\hspace{1in} $c=$\underline{\hspace{1in}}\\

	\item $a=2$, \hspace{1in}$b=$\underline{\hspace{1in}},\hspace{.5cm} $c=3$\\

	\item $a=15$, \hspace{1in}$b=20$, \hspace{1in}$c=$\underline{\hspace{1in}}\\

	\item $a=$\underline{\hspace{1in}}, \hspace{.5cm} $b=80$,\hspace{1in} $c=89$\\
\end{enumerate}

\hrulefill

\subsection*{The Distance Formula}

$$d=\sqrt{(x_{1}-x_{2})^{2}+(y_{1}-y_{2})^{2}}$$

Find the distance between the two points. (3 points each)\\

\begin{enumerate}[resume]
\begin{multicols}{2}

	\item $(0,0), (5,12)$\\

	\item $(-6,1),(-3,-3)$\\

	\item $(-10,-20),(-1,20)$\\

	\item $(5,5),(-3,17)$

\end{multicols}
\end{enumerate}

\pagebreak

\subsection*{Equation of a Circle}

$$r^2=x^2+y^2$$

$$r^2=(x-h)^2+(y-k)^2$$

Write the equation of a circle with the following criteria. (2 points each)\\

\begin{enumerate}[resume]

	\item $r=10$, center at the origin\\

	\item $r=9$, center at $(-8,-7)$\\

	\item $r=6$, center at $(5,-4)$\\

	\item $r=\sqrt{3}$, center at $(2,1)$\\

	\item $r=\sqrt{8}$, center at $(-4,0)$\\

	\item center at $(0,0)$, passes through the point $(11,60)$\\

\end{enumerate}

\hrulefill

Sketch the following equations. (3 points each)\\

\begin{enumerate}[resume]
\begin{multicols}{2}

	\item $x^2+y^2=25$\\

%	\includegraphics[scale=.75]{graph1.jpg}

	\item $(x-1)^2+(y+1)^2=16$\\

%	\includegraphics[scale=.75]{graph1.jpg}

\end{multicols}
\end{enumerate}

\section*{Quiz::}

Mr. Wolf \hfill \textbf{NAME:\underline{\hspace{2in}}}\\
Pre-Calculus \hfill \textbf{Date: \underline{\hspace{1in}}}\\

\subsection*{Pythagorean Theorem}

Given the triangle diagram below, determine the unknown distance. (2 points each)

$$a^2+b^2=c^2 \hspace{1in} c=\sqrt{a^2+b^2} \hspace{1in} a=\sqrt{c^2-b^2} \hspace{1in} b=\sqrt{c^2-a^2}$$

\begin{center}
	\begin{tikzpicture}[scale=.8]

		\path[draw, fill=blue!15 ] (0,0)--
			(0,3) node[pos=.5,left] {a} --
			(4,0) --cycle;

			\node (c) at (2,2) {c};
			\node (b) at (2,-.5) {b};

	\end{tikzpicture}
\end{center}

\begin{enumerate}
	\item $a=10$, \hspace{1in} $b=\sqrt{21}$,\hspace{1in} $c=$\underline{\hspace{1in}}\\

	\item $a=$\underline{\hspace{1in}}, \hspace{.5cm} $b=80$,\hspace{1in} $c=82$\\

	\item $a=30$, \hspace{1in}$b=40$, \hspace{1in}$c=$\underline{\hspace{1in}}\\

	\item $a=1$, \hspace{1in}$b=$\underline{\hspace{1in}},\hspace{.5cm} $c=\sqrt{3}$\\

	\item $a=7$,\hspace{1in} $b=7$,\hspace{1in} $c=$\underline{\hspace{1in}}
\end{enumerate}

\hrulefill

\subsection*{The Distance Formula}

$$d=\sqrt{(x_{1}-x_{2})^{2}+(y_{1}-y_{2})^{2}}$$

Find the distance between the two points. (3 points each)\\

\begin{enumerate}[resume]
\begin{multicols}{2}

	\item $(5,-2),(-3,10)$\\

	\item $(-8,2),(-2,-4)$\\

	\item $(-10,-20),(-1,20)$\\

	\item $(-5,-10), (5,14)$\\

\end{multicols}
\end{enumerate}

\pagebreak

\subsection*{Equation of a Circle}

$$r^2=x^2+y^2$$

$$r^2=(x-h)^2+(y-k)^2$$

Write the equation of a circle with the following criteria. (2 points each)\\

\begin{enumerate}[resume]

	\item $r=5$, center at the origin\\

	\item $r=6$, center at $(-4,-7)$\\

	\item $r=3$, center at $(8,1)$\\

	\item $r=\sqrt{10}$, center at $(11,12)$\\

	\item $r=\sqrt{13}$, center at $(-14,0)$\\

	\item center at $(0,0)$, passes through the point $(55,48)$\\

\end{enumerate}

\hrulefill

Sketch the following equations. (3 points each)\\

\begin{enumerate}[resume]
\begin{multicols}{2}

	\item $x^2+y^2=25$\\

%	\includegraphics[scale=.75]{graph1.jpg}

	\item $(x-1)^2+(y+1)^2=16$\\

%	\includegraphics[scale=.75]{graph1.jpg}

\end{multicols}
\end{enumerate}

\section*{Answer Key}

\begin{multicols}{3}

Quiz 1
\begin{enumerate}

\item 26

\item 18

\item $\sqrt{2}$

\item $\sqrt{2}$

\item 5

\item 41

\item 13

\item $4\sqrt{13}$

\item $4^2=x^2+y^2$

\item $6^2=(x+3)^2+(x+4)^2$

\item $2^2=x^2+(y-7)^2$

\item $3=(x-9)^2+(y-7)^2$

\item $7=(x-6)^2+y^2$

\item $65^2=x^2+y^2$\\

\end{enumerate}

Quiz 2

\begin{enumerate}

\item 13

\item 9

\item 15

\item $\sqrt{5}$

\item $2\sqrt{2}$

\item 41

\item $4\sqrt{13}$

\item 5

\item 13

\item $1=x^2+y^2$

\item $2^2=(x-3)^2+(Y+4)^2$

\item $3^2=(x+2)^2+(y-12)^2$

\item $5=(x+10)^2+(y+10)^2$

\item $11=(x+1)^2+(y-2)^2$

\item $65=(x+10)^2+(y+10)^2$

\end{enumerate}

quiz 3

\begin{enumerate}

\item $3\sqrt{2}$

\item 74

\item $\sqrt{5}$

\item 25

\item 39

\item 13

\item 5

\item 41

\item $4\sqrt{13}$

\item $10^2=x^2+y^2$

\item $9^2=(x+8)^2+(y+7)^2$

\item $6^2=(x-5)^2+(y+4)^2$

\item  $3=(x-2)^2+(x-1)^2$

\item $8=(x+4)^2+y^2$

\item $61^2=X^2+y^2$

\end{enumerate}
\end{multicols}

\hrulefill

Quiz 4

\begin{multicols}{2}
\begin{enumerate}

\item 11

\item 18

\item 50

\item $\sqrt{2}$

\item $7\sqrt{2}$

\item $4\sqrt{13}$

\item $6\sqrt{2}$

\item 41

\item 26

\item $5^2=x^2+y^2$

\item $6^2=(x+4)^2+(y+7)^2$

\item $3^2=(x-8)^2+(y-1)^2$

\item $10=(x-11)^2+(y-12)^2$

\item $13=(x+14)^2+y^2$

\item $73^2=x^2+y^2$

\end{enumerate}
\end{multicols}

\end{document}


\input{PreambleCommon}
\input{../WeekTitles}

\begin{document}
\setfont
\pagestyle{fancy}
\renewcommand{\Week}{3 }
\renewcommand{\WeekTitle}{\WeekTitleThree }

\fancyhead[LE,RO]{Week \Week}  % default, usually only for first page
\fancyfoot{}
\sectionbox{Week \#\Week: \WeekTitle}

\vspace{5mm}
\goals
\begin{itemize}
\item Calculate and interpret the first and second derivatives, as
  well as higher order derivatives.
\item Define and calculate Taylor polynomials.
\item Use MATLAB to graph and compare functions with their Taylor
  polynomial approximations.
\item Find and use critical points for global and local optimization
  problems.
\item Use MATLAB optimizers and equation solvers to identify optimal
  values and critical points.
\end{itemize}

\vspace{5mm}

\newpage
\topic{Second and Higher Derivatives}
\subsection*{Second and Higher Derivatives}

A lot of information about the graph of a function $f$ can be deduced
by the sign of $f'(x)$ and $f''(x)$ (the {\bf second derivative} of
$f(x)$) on an interval $(a,b)$.

{\huge
\vsc
\begin{center}
\begin{tabular}{|l|l|} \hline
\qquad \qquad \qquad \qquad \qquad  \qquad \qquad & \qquad  \qquad \qquad \qquad \qquad  \qquad \qquad \\
\qquad $f'(x) > 0 {\mbox{ on }} (a,b)$  &  \qquad $f$ increasing on $[a,b]$  \\
\qquad \qquad \qquad \qquad \qquad & \qquad \qquad \qquad  \qquad \qquad \\
 \hline
\qquad \qquad \qquad \qquad \qquad & \qquad \qquad \qquad  \qquad \qquad \\
\qquad $f'(x) < 0 {\mbox{ on }} (a,b)$  &  \qquad $f$ decreasing on $[a,b]$  \\
\qquad \qquad \qquad \qquad \qquad & \qquad \qquad \qquad  \qquad \qquad \\
 \hline
\qquad \qquad \qquad \qquad \qquad & \qquad \qquad \qquad  \qquad \qquad \\
\qquad $f''(x) > 0 {\mbox{ on }} (a,b)$  &  \qquad $f$ concave up on $[a,b]$  \\ \qquad \qquad \qquad \qquad \qquad & \qquad \qquad \qquad  \qquad \qquad \\
\hline
\qquad \qquad \qquad \qquad \qquad & \qquad \qquad \qquad  \qquad \qquad \\
\qquad $f''(x) < 0 {\mbox{ on }} (a,b)$  &  \qquad $f$ concave down on $[a,b]$ \qquad  \qquad  \\
\qquad \qquad \qquad \qquad \qquad & \qquad \qquad \qquad  \qquad \qquad \\
\hline
\end{tabular}
\end{center}
}
\setfont

\newpage

\problem Sketch the possible graphs combining different signs of
positive and negative first and second derivatives.

\vfill
\vfill

\problem Sketch graphs where the {\bf first} derivative has a zero value.

\vfill \problem Sketch graphs where the {\bf second} derivative has a
zero value.

\vfill

\newpage

Aside from their graphical interpretation, the second derivative
frequently has an important physical meaning in kinematics problems.

\problem If $x(t) = 4 \sin(2t)$ gives the position of a particle at
time $t$, what is particle's {\bf speed} at $\ds t=\frac{\pi}{6}$?

\vfill

For the same particle, what is its {\bf acceleration} at
$\ds t=\frac{\pi}{6}$?

\vfill

\newpage

While their interpretations are not as immediately obvious, it is also
possible to compute 3rd, 4th, or higher derivatives of function if we
want.

\problem Find the first four derivatives of the function
$$f(x) = 7 e^{-2x} + \ln(x).$$

\newpage
\topic{Taylor Polynomials}
\subsection*{Taylor Polynomials}

One application of higher derivative information is to help us build
{\bf polynomial approximations} to other functions.

Previously we found a formula for linear approximations to functions
$f(x)$ around a point $x=a$:

\vspace{1.5in}

This linear approximation, or tangent line formula, can also be called
the {\bf Taylor polynomial of degree 1 approximating $f(x)$ near
  $x=a$.}

\newpage

\problem Sketch the graph of $\cos(x)$ around $x=0$, and add its
tangent line based at $x=0$.

\includegraphics[width=3in]{graphics/empty_graph_square_12}

The linearization or tangent line is clearly a very
  limited approximation to this function.  What might be a {\em
    slightly} more complex form of function that would work better in
  this case?

\vfill

\newpage

\begin{boxnote}

{\bf Taylor Polynomial of Degree 2}
\vspace{1in}

$$ f(x) \approx ~~~~~f(a) ~~~~~+ f'(a) (x-a) ~~~~~+ \frac{f''(a)}{2} (x-a)^2$$

\vspace{1in}

is a {\em quadratic} approximation to $f(x)$ near $x=a$.
\end{boxnote}

\problem For values of $x$ close to $a$ do you think this quadratic
approximation will be a better or worse approximation than the tangent
line?  Why?

\vfill

\newpage

\problem Find the quadratic Taylor approximation to \\$f(x) = \cos(x)$
  near $x=0$.

\vfill

\newpage

\problem Use MATLAB to draw the graph of $\cos(x)$ around $x=0$, and
add both its 1st and 2nd degree Taylor polynomial approximations for
$x$ near 0.

\newpage
$$ f(x) \approx f(a) + f'(a) (x-a) + \frac{f''(a)}{2} (x-a)^2$$

The form of the coefficients in the Taylor polynomial are carefully
chosen.

\problem What mathematical features will the original $f(x)$ share
with its 2nd degree Taylor approximation at the point $x=a$?

\vfill

\newpage
\topic{Taylor Polynomials of Higher Degree}
\subsection{Taylor Polynomials of Higher Degree}

\problem If we wanted a still-better approximation for a function
$f(x)$ near a specific point $x=a$, how could we generalize our
earlier 1st and 2nd degree Taylor polynomials?

\vfill

\newpage

Below is the general formula for the terms in a Taylor polynomial, up
to degree $n$.  \\
\begin{boxnote} ~\\[1ex]
$$ f(x) \approx ~~f(a) ~~+ f'(a) (x-a) ~~+ \frac{f''(a)}{2} (x-a)^2 + \ldots
+ \frac{f^{(n)}(a)}{n!} (x-a)^n$$    \\[-0.5ex]
\end{boxnote}
\begin{itemize}
\item $f^{(n)}$ means ``the $n$-th derivative of $f$''. \\
\item $n!$ means ``$n$ factorial''
\end{itemize}

\newpage
\subsection*{Higher Degree Taylor Polynomials - Example 1}

Consider the function $f(x) = \sin(x)$.

\problem Find the first five derivatives of $f(x)$, and evaluate them at $x=0$.

\newpage

\problem Write out the Taylor polynomial of degree 5 for
$f(x) = \sin(x)$.

\vfill

\problem Write out the general form of the Taylor polynomial of degree
$n$ for $f(x) = \sin(x)$.

\vfill

\newpage

\problem Use MATLAB to plot the graph of $f(x) = \sin(x)$ and the
Taylor polynomial approximations up to degree 5.

\newpage

MATLAB Demo of even higher degree Taylor approximations to $\sin(x)$.

\newpage

\topic{Higher Degree Taylor Polynomials - Example 2}
\subsection*{Higher Degree Taylor Polynomials - Example 2}

Consider the function $g(x) = x e^{-x}$.

\problem Find the first three derivatives of $g(x)$, and evaluate them
at $x=1$.

\newpage

\problem Write out the Taylor polynomial of degree 3 for \\
$g(x) = x e^{-x}$ centered at $x=1$.

\vfill

\newpage

\problem Use MATLAB to plot the graph of $g(x) = x e^{-x}$ and the
Taylor polynomial approximation from degree 1, 2 and 3.

\newpage
\topic{Critical Points}

\subsection*{Critical Points}

Aside from understanding the shape of functions, derivative
information can help us identify and classify interesting points of a
function, like the highest and lowest values.

\problem Sketch graphs which have high and low points.

\vfill

What do those extreme values have in common?

\newpage

To help identify local extreme values, we introduce the definition of
a critical point.

\begin{boxnote}

  If $f(x)$ is defined on the interval $(a,b)$, then we call a point
  $c$ in the interval a {\bf critical point} if:
\begin{itemize}
	\item $f'(c) = 0$, or
	\item $f'(c)$ does not exist.
\end{itemize}
We will also refer to the point $(c,f(c))$ on the graph of $f(x)$ as a
critical point.  We call the function value $f(c)$ at a critical point
$c$ a {\bf critical value}.

\vsc
\end{boxnote}

\newpage

Technical Notes:
\begin{enumerate}
\item By this definition, $f(c)$ must be {\bf defined} for
  $c$ to be a critical point.

\problem   Sketch $f(x) = 1/x$, and decide whether $x=0$ is a
    critical point.

\vfill
  Sketch $g(x) = |x|$, and decide whether $x=0$ is a
    critical point.

\vfill

\newpage

\item By the definition, if a function is defined on a closed
  interval, the endpoints of interval {\bf cannot} be critical points.

\problem   Sketch the graph of $f(x) = \sqrt{x}$ and decide
    whether $x=0$ is a critical point.

\vfill

\newpage

\problem Sketch the graph of $g(x) = \sqrt[3]{x}$ and decide
  whether $x=0$ is a critical point.

\vfill

\end{enumerate}

\newpage

% \problem Identify all the critical points on the graph below, and
%   characterize any other interesting points by continuity, limits, or
%   other properties.

\problem Identify all the critical points on the graph below.

\includegraphics[width=8in]{graphics/notes_03_crit_pnt_oddities}

\newpage

\topic{Classifying Critical Points}

\section*{Classifying Critical Points}

We will now formalize two ways to determine if a critical point is a
local min, max, or neither. This avoids the need for a sketch of the
graph. \\[2ex]
\newpage
{\bf First Derivative Test}

One indicator of the type of critical points is the sign of the
derivative on each side of the critical point.  This classification
method is called the {\bf{first derivative test}}.

\problem Complete this table:
{\LARGE
\vspace{1mm}
\begin{center}
\begin{tabular}{|l|c|c|p{2.1in}|} \hline
\qquad \qquad \qquad \qquad \qquad &  $f'$ sign left of $c$ & $f'$ sign right of $c$ & Sketch of $f(x)$  \\ \hline
local minimum at $c$ & & &\\[1.0in] \hline
local maximum at $c$ & & &\\[1.0in] \hline
neither local max nor min & & &\\[1.0in] \hline
\end{tabular}
\end{center}
}

%\def\arraystretch{1}
\vspace{1.5mm}

\newpage

\problem Find the critical points of the function
$$f(x) = 2 x^3 - 9 x^2 + 12 x +3.$$ Use the first derivative test to
show whether each critical point is a local maximum or a local
minimum.

\vfill
\vfill

\newpage

\problem Using your answer to the preceding question, determine the
number of real solutions of the equation
$2 x^3 - 9 x^2 + 12 x + 3 = 0$.

\vfill

\newpage
\begin{boxnote}
{\bf Second Derivative Test}

You may also use the Second Derivative Test to determine if a critical
point is a local minimum or maximum.
\begin{itemize}
\item The first derivative test uses the {\bf first} derivative {\bf
    around} the critical point.
\item The second derivative test uses the {\bf second} derivative {\bf at} the critical point.
\end{itemize}
\begin{itemize}
\item If $f'(c)$ = 0 and $f''(c) > 0$ then $f$ has a local minimum at $c$. \\[2ex]
\item If $f'(c)$ = 0 and $f''(c) < 0$ then $f$ has a local maximum at $c$. \\[2ex]
\item If $f'(c)$ = 0 and $f''(c) = 0$ then the test is inconclusive. \\[2ex]
\end{itemize}
\end{boxnote}

\vsc
\newpage
\problem A function $f$ has derivative $f'(x) = \cos(x^2) + 2x - 1$.
  Does it have a local maximum, a local minimum, or neither at its
  critical point $x=0$?

\newpage

\topic{Global vs. Local Optimization}
\section*{Global vs. Local Optimization}

The first and second derivative tests only give us {\em local}
information in most cases.  However, if there are multiple local
maxima or minima, we usually want the {\bf global} max or min.  The
ease of determining when we have found the global max or min of a
function depends strongly on the properties of the question.

\begin{boxnote}

  \subsection*{Local vs Global Extrema}

  A {\bf local max} occurs at $x=c$ when $f(c) > f(x)$ for $x$ values
  near $c$.

  A {\bf global max} occurs at $x=c$ if $f(c) \ge f(x)$ for {\bf all}
  values of $x$ in the domain.

  It is possible to have several global maxima if the function reaches
  its peak value at more than one point.

  Corresponding definitions apply for local and global minima.

\end{boxnote}

\newpage

\problem Give an example of a simple function with multiple global
  maxima.

\vfill

\problem Give an example of a simple function with a single global
  maximum, but no global minimum.

\vfill

\problem Give an example of a simple function with neither a global
  maximum nor a global minimum.

\vfill

\newpage Earlier we worked with the function $$f(x) = 2 x^3 -
  9 x^2 + 12 x +3.$$
\problem If we limit the function to the interval $x \in
  [0, 2.5]$, what are the {\bf global max} and {\bf global minimum}
  values on that interval?

\vfill
\vfill

Confirm your answer using MATLAB to plot the graph of $f(x)$.

\newpage

\topic{Global Extrema on Closed and Open Intervals}

\begin{boxnote}
\subsection*{Global Extrema on Closed Intervals}

A continuous function on a closed interval will {\bf always} have a
global max and a global min value.  These values will occur at either
\begin{itemize}
\item a critical point {\em or}
\item an end point of the interval.
\end{itemize}
To find which value is the global extrema, you can compute the
original function's values at all the critical points and end points,
and select the point with the highest/lowest value of the function.

\end{boxnote}

\newpage
\begin{boxnote}
\subsection*{Global Extrema on Open Intervals}

A function defined on an open interval may or may not have global
maxima or minima.
\vsc

If you are trying to demonstrate that a point is a global max or min,
and you are working with an open interval, including the possible
interval $(-\infty, \infty)$, proving that a particular point is a global
max or min requires a careful argument.  A recommendation is to look
at either:
\begin{itemize}
\item values of $f$ when $x$ approaches the endpoints of the interval,
  or $\pm \infty$, as appropriate; or
\item if there is only one critical point, look at the sign of $f'$ on
  either side of the critical point.
\end{itemize}
With that information, you can often construct an argument about a
particular point being a global max or min.

\end{boxnote}

\newpage

\problem Determine whether the function
$$f(x) = 2 x^3 - 9 x^2 + 12 x +3$$ has a global max and/or min.

\vfill

\newpage
\problem Determine whether the function $f(x) = (x-2)^4$ has a global
  max and/or min.

\vfill

\newpage

\topic{Optimization - Fencing Example}
\section*{Optimization}

An optimization problem is one in which we have to find the maximum or
minimum value of some quantity.  In principle, we already know how to
find the maximum and minimum values of a function if we are given a
formula for the function and the interval on which the maximum or
minimum is sought.  Usually the hard part in an optimization problem
is interpreting the word problem in order to find the formula of the
function to be optimized.

\vsc

\newpage

\problem A site manager wants to build a rectangular security fence to
surround a site.  The manager has 480 meters of wire fencing with
which to build a fence, and one side of the enclosure will be part of
the side an already existing building (so there is no need to put up
fence on that side). What should the dimensions of each side be to
maximize the area enclosed?

\vsc

What is the quantity to be maximized in this example?

\vspace{2cm}

\newpage

\problem What are the variables in this question, and how are they
related?  You may want to draw a picture.

\vfill

Express the quantity to be optimized in terms of the
  variables.  Try to eliminate all but one of the variables.

  \vfill

  What is the domain on which the one remaining variable
    makes sense?

\vspace{2cm}
  \newpage

  \problem Use the techniques learned earlier in the course to
  maximize the enclosed by the fence.  Give reasons explaining why the
  answer you found is the {\bf global} maximum.

\vspace{5cm}

\newpage

\hfill (continued)

\vfill

\problem Confirm your solution using a MATLAB graph of your function
for the enclosed area.  \vspace{1in}

\newpage

\topic{Optimization - Storage Example}
\problem (Storage Container)\\ A rectangular storage container with an
  open top is to have a volume of 10 m$^3$.  The length of its base is
  to be twice its width.  Material for the base costs \$10.00 per
  m$^2$, and material for the sides costs \$6.00 per m$^2$.  Determine
  the cost of the material for the cheapest such container.

~ \hfill \includegraphics[width=1.5in]{graphics/notes_03_storage_container}

\newpage
~ \hfill \includegraphics[width=1.5in]{graphics/notes_03_storage_container}

\newpage
~ \hfill \includegraphics[width=1.5in]{graphics/notes_03_storage_container}

\newpage

\topic{Optimization in MATLAB}
\subsection*{Optimization in MATLAB}

MATLAB has built-in numerical methods tools that can also perform
optimization, given the formula for a function.  However, those tools
have the same limitations as our earlier work with numerical methods
for solving equations.

These {\bf numerical methods} are all {\bf fancy versions of guess and
  check}!  This means numerical solutions are a poor second choice,
compared to by-hand solving:
\begin{itemize}
\item Numerical optimums give no insight into the optimal value
  (existence, patterns).
\item Finding numerical optimums usually requires some amount of trial
  and error by the user.
\end{itemize}

Despite these limitations though, numerical methods are great in that
they can be applied to any function, and don't require any by-hand
solving for critical points.

\newpage

{\bf Defining New MATLAB Functions}

User-written functions are an important tool in MATLAB.  For purposes
of our optimization problems, we will use the simplest implementation,
called {\bf anonymous functions}.

\begin{verbatim}
f = @(x)  x.^2;

x = linspace(-2, 2);
plot(x, f(x));
\end{verbatim}

\newpage

{\bf Optimizing MATLAB functions}

The single-variable optimization function in MATLAB is called \texttt{fminbnd}.

\problem Look at the help menu for \texttt{fminbnd}.
\vfill

\newpage

\problem Use \texttt{fminbnd} to find a minimum of
$f(x) = e^{-x} + x$.

\vfill

Graph $f(x) = e^{-x} + x$ and add a point at the minimum that was
found.
\vfill

\newpage
\problem Use \texttt{fminbnd} to find the global minimum of \\
$g(x) = x \sin(x)$ on the interval $x \in [0, 4\pi]$.

\vfill

Graph $g(x)$ and comment on steps you need to take to find the global
minimum.
\vfill

\newpage

\topic{MATLAB Optimization - Further Examples}
\subsection*{MATLAB Optimization - Further Examples}

You'll notice that command \texttt{fminbnd} does {\bf not} have a
sister function \texttt{fmaxbnd}.  Instead, MATLAB requires users to
turn any maximization problem they have into a minimization problem
themselves. \\[1ex]

\begin{minipage}[t]{0.6\linewidth}

\problem Sketch a graph $y = f(x)$ which has a local maximum.
\vspace{2in}

Now sketch the graph of $y = -f(x)$.

What do you notice about the $x$ locations of the minima on this
function?
\vspace{1in}~
\end{minipage}

\newpage
\problem Use \texttt{fminbnd} to find the global {\bf maximum} of \\
$g(x) = x \sin(x)$ on the interval $x \in [0, 4\pi]$.

\newpage
\problem Use \texttt{fminbnd} to find the length of side fence $x$
that maximizes an enclosed rectangular area, if one side is against
the wall.  The total length of fence is 480 m.

$\displaystyle A(x) = x \frac{(480-x)}{2}$

\vfill

Also find the total area enclosed with the optimal design.  \vspace{1in}

\newpage

{\bf Notation Confusion}

There is an unfortunate coincidence in MATLAB notation that often
catches new users.

\problem Contrast the meaning of the \texttt{y(1.5)} in these two
MATLAB examples.

\begin{minipage}[t]{0.45\linewidth}
\vspace{0pt}
\begin{verbatim}
x = linspace(-2, 2);
y = x.^2;

y(1.5)
\end{verbatim}
\end{minipage} \hfill
\begin{minipage}[t]{0.45\linewidth}
\vspace{0pt}
\begin{verbatim}
x = linspace(-2, 2);
y = @(x)  x.^2;

y(1.5)
\end{verbatim}

\end{minipage}

\end{document}