\documentclass[11pt,letterpaper]{article}
%\usepackage[framed, autolinebreaks,useliterate]{mcode}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{mcode}
\usepackage[colorlinks=true]{hyperref}
\usepackage[left=3.00cm, right=3.00cm, top=3.00cm, bottom=3.00cm]{geometry}
\usepackage{framed}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}
\usepackage{thmtools}
\newtheorem{problem}{Problem}
\date{}
\author{}
\title{{Illustrating Theorems from Calculus}}
\begin{document}
	\maketitle
%In this activity, you will illustrate the Extreme Value Theorem, Intermediate Value Theorem, and Mean Value Theorem using MATLAB.  These illustrations will be similar to the ones we drew in class.
\section*{Instructions}
\begin{enumerate}
    \item Open the template file: username\_illustrating\_calc.mlx.  Save this template into your chosen MATLAB folder for this course and replace `username' with your username.
    \item Complete the four problems outlined in this document.  You are welcome and encouraged to discuss these with your classmates and instructor!
    \item Suppress all numerical output by terminating commands with \mcode{;}.  The only displayed results should be the relevant plots and tables.
    \item Any mathematical notation in your discussion must be typeset with the equation editor.
    \item When finished, run and save your .mlx file first, then export it as a pdf.
\end{enumerate}
\subsection*{Useful Code to Get Started}
\begin{framed}
\noindent \textbf{Creating Vector Variables}
\begin{lstlisting}
% Rows and Columns
row = [1 2 3]; % row vector
col = [1; 2; 3]; % column vector
% Vectors with Incremental Elements
% Option 1: vector starting at 0, ending at 10, elements in increments of 0.5
X = 0:0.5:10; 
N = numel(X); % N = number of elements in X
% Option 2: vector starting at 10, ending at 50, with N equally spaced elements
Y = linspace(10,50,N);
\end{lstlisting}
\end{framed}
\begin{framed}
\noindent \textbf{Basic Plotting}
\begin{lstlisting}
figure; % create new figure
plot(X,Y) % X and Y must be vectors of same size
xlabel 'x', ylabel 'y' % add axis labels
title('Example of a Plot') % add a title
\end{lstlisting}
\end{framed}
\newpage
\section{Plotting Functions}
\begin{framed}
\noindent\textbf{Anonymous Functions}
\begin{itemize}
    \item a function that stores a single executable line of code producing one array of output.
    \item   basic syntax: \mcode{myFunc = @(input) (single executable line of code)} 
\end{itemize}
  \textbf{Example:} Let's do several calculations with the function $g(x) = x\sqrt{1+\ln(x)}$. 
   \begin{itemize}
       \item Create a function handle \mcode{>> g = @(x) x.*sqrt(1 + log(x));}
       \item Use \mcode{g(input)} to evaluate $g$ on \mcode{input} values (e.g. \mcode{g(1)} or \mcode{g([1,2])}).
       \item \textbf{Notice element-wise multiplication.}\\ The function acts \textit{element-wise} on vector inputs.
       \item Check $g([1,2]) = [g(1),g(2)]$ in the command line.
    \end{itemize} 
    \end{framed}
\subsection*{Exercise 1}
Consider the function $f(x) = e^{-x}+2x^2\sin(x)$ on the interval $[-2,3]$. 
\begin{enumerate}
    \item Create an anonymous function handle for $f$.
    \begin{itemize}
        \item Make sure you use \textit{element-wise multiplication} in your function formula.
        \item Also, remember the exponential function is \mcode{exp} in MATLAB.
    \end{itemize}
   \item Create a variable called \mcode{X1} of $100$ equally spaced points between $-2$ and $3$ using the command \mcode{linspace}. 
    \item Create a variable called \mcode{Y1} of $f$ evaluated at each point in \mcode{X1}. 
    \item Check your workspace to see that \mcode{X1} and \mcode{Y1} are of the same size.
    \item Plot \mcode{(X1,Y1)} using command \mcode{plot}.  
    \begin{itemize}
        \item Make your plot \textcolor{blue}{blue}.
        \item Do not use markers (you want a smooth-looking plot).
        \item Use command \mcode{axis} to set the axis bounds $[-2,3]$ for $x$ and $[-2,10]$ for $y$.
        \item Include axis labels and title your plot ``Plot of f(x)."
    \end{itemize}  
    \item Discussion: Explain where in the formula for $f$ you must use element-wise arithmetic and why.
\end{enumerate}

\section{Illustrating the Extreme Value Theorem}
In this section, you will create an illustration for the Extreme Value Theorem in MATLAB using the same function $f(x) = e^{-x} + 2x^2\sin(x)$ on the same interval $[-2,3]$.  We will do this by investigating the MATLAB commands \mcode{max} and \mcode{min}, as well as a \mcode{table}.

\begin{framed}
\noindent\textbf{Maximum (\mcode{max}) and Minimum (\mcode{min}) in MATLAB}
\begin{itemize}
    \item Commands: \mcode{max} and \mcode{min}
    \item These functions return the maximum/minimum value(s) of an array.  
    \item Basic syntax: \mcode{M = max(A)}.
    \item When $A$ is a vector, it returns the maximum/minimum value of $A$.
    \item \mcode{[M,I] = max(A)} gives you the\textit{ both} the maximal value of the vector $A$ in the variable \mcode{M} \textit{and} tells you its index, \mcode{I}, so that \mcode{A(I) = M} (and similar for \mcode{min}).
\end{itemize}
\end{framed}
\begin{framed}
\noindent \textbf{Making a table with \mcode{table}}
\begin{itemize}
    \item Basic syntax (for a table with 2 columns):
     \begin{lstlisting}
    table(Vector1,Vector2,'RowNames',{'Row1Name','Row2Name'}, ...
        'VariableNames',{'Col1Name','Col2Name'})
    \end{lstlisting}
    \item \mcode{Vector1} and \mcode{Vector2} should be \textit{column} vectors.
    \item The entries in \mcode{'RowNames'} and \mcode{'VariableNames'} must equal the number of rows and columns respectively.
\end{itemize}
\end{framed}
\subsection*{Exercise 2}
\begin{enumerate}
    \item Copy and paste your code from Exercise 1 into this section in your live script.  \\ Change the title of the plot to ``Illustration of the Extreme Value Theorem."
    \item Use the \mcode{max} command to find the max value (output \mcode{M}) of \mcode{Y1} and its index (output \mcode{I}).  \\Repeat for the minimum value (using \mcode{min}), but use the output variables \mcode{[L,J]}.
    \item Add the max and min points in our plot. 
    \begin{itemize}
        \item Use the command \mcode{hold on} and execute another plot command to plot \\ the max point, $(\mcode{X1(I)},\mcode{M})$ and min point, $(\mcode{X1(J)},\mcode{L})$.
        \item Use an open circle as a marker and the color \textcolor{red}{red}.
    \end{itemize} 
    \item Look up the command \mcode{text} in your command line. \\
    Use the \mcode{text} command to label the max and min points on your plot as ``max" and ``min."
    \item Recreate this table displaying the max and min values and locations using the command \mcode{table}.  Make sure the ``Location" is the $x$-value of the extremal point (not the index value). 
    \begin{center}
        \includegraphics[width = 2in]{maxmin_table.png}
    \end{center}
    To make this table, first you need to first make two vectors, \mcode{MaxVector} and \mcode{MinVector}, that store the $x$ and $y$ values of the extrema.
    \item Discussion:  Write a paragraph explaining how your plot illustrates the Extreme Value Theorem as stated in the live script.  
\end{enumerate}
\section{Illustrating the Intermediate Value Theorem}
Next, you will illustrate the Intermediate Value Theorem.  We will use the same function and variables as we did in the previous section to create a new illustration.
\subsection*{Exercise 3}
\begin{enumerate}
    \item Copy and paste your code from Exercise 1 into this section in your live script.  \\Change the title of the plot to ``Illustration of the Intermediate Value Theorem."
    \item Add the following MATLAB commands to your code. 
    \begin{lstlisting}
    V = L + rand(1,1)*(M-L);
    fMinusV = @(x) f(x)-V;
    c1 = fzero(fMinusV,0);
    hold on;
    yline(V,'-r','y=V')
    plot(c1,V,'ro')
    \end{lstlisting}
    \item Run this section several times and take note of what happens.
    \item Add annotations to the above commands using \mcode{\%} explaining what each line is doing.  \\Remember, you can use \mcode{>> help commandName} to quickly look up new commands.
    \item Discussion: Write a paragraph sentences explaining how running this section over and over illustrates the Intermediate Value Theorem as stated in this section of your live script.
\end{enumerate}

\section{Illustrating the Mean Value Theorem}
In this final exercise, you will create an illustration for the Mean Value Theorem, similar to what you did for the other Value theorems in lab 0. We will use the same function as in the previous sections.
\subsection*{Exercise 4}
\begin{enumerate}
	\item Copy and paste your code from Exercise 1 into this section.  Change the title of the plot to ``Illustration of the Mean Value Theorem."
	\item Calculate the slope of the secant line connecting $(-2,f(-2))$ to $(3,f(3))$.  Save this calculation in the variable \mcode{m}. 
	\item Create an anonymous function handle for the equation of the secant line.  Call this function $S$ (you should use \mcode{m} in your formula).
	\item Find $f'(x)$ on paper.  Make an anonymous function handle for $f'(x)$ (call it \mcode{df}). \\ Remember to use element-wise arithmetic!
	\item Add the following MATLAB commands to your code.
	\begin{lstlisting}
		dfMinusm = @(x) df(x)-m;
		x0 = 0;
		c2 = fzero(dfMinusm,x0);
	\end{lstlisting}
	\item Run the code and add annotations to each line of the above code explaining what the code does.
	\item Create an anonymous function handle for the tangent line at point \mcode{c2}. Call the function \mcode{L}. 
	\item Add the secant line and tangent line to your plot.  
	\begin{itemize}
		\item Use a solid \textcolor{red}{red} line for the secant line and a dashed \textcolor{red}{red} line for the tangent line.
		\item Include a Legend for your plot by using the \mcode{legend} command (use \mcode{>> help legend} to see how to use this command).
	\end{itemize} 
	\item Discussion: Write a paragraph explaining how this section illustrates the Mean Value Theorem as stated in this section of the live script.
\end{enumerate}


%\section{Illustrating the Integral Mean Value Theorem}
%\begin{enumerate}
 %   \item Recreate the plot in step 4 of the first section.  This time, make the title ``Illustration of the Integral Value Theorem."
  %  \item An antiderivative of $f(x)$ is $F(x)=-e^{-x}-2x^2\cos(x)+4x\sin(x) + 4\cos(x)$.  Use this to calculate $\int_{-2}^{3}f(x)\, dx$ and store your calculation in the variable \mcode{IntValue}.
  %  \item Use the Integral Mean Value Theorem formula to find the value of $f(c)$. Store this value in the variable \mcode{fc3}. 
  %  \item Re-examine how we found \mcode{c1} and \mcode{c2} in the previous sections by using the command \mcode{fzero}.  Use this approach to solve for $c$ in the Integral Mean Value theorem formula. Store your $c$ value in the variable \mcode{c3}. 
  %  \item Verify that \mcode{f(c3)}=\mcode{fc3}.  Add the point \mcode{(c3,f(c3)} in your plot.  Use an x-marker and the color \textcolor{red}{red}. Remember to use the command \mcode{hold on}.
  %  \item Add the following MATLAB commands to your code.  
  %  \begin{lstlisting}
  %  patch([X1 X1(end) X1(1)],[Y1 0 0],'blue')
  %  alpha(0.2)
  %  patch([-2 3 3 -2],[0 0 fc fc],'r')
  %  alpha(0.2)
  %  \end{lstlisting}
  %  The first \mcode{patch} command above shade the region representing $\int_{-2}^3f(x)\, dx$.  The second \mcode{patch} command above shades the region representing the rectangle with base $[-2,3]$ and height $[0,f(c)]$.  The \mcode{alpha} command makes the shading transparent so you can see where they overlap.
  %  \item Discussion:  Explain how this image illustrate the Integral Mean Value Theorem.  In particular, what does the blue region represent?  the red region? the red x?
%\end{enumerate}
\end{document}
