\documentclass[12pt]{article}
\usepackage[fleqn]{amsmath} %puts eqns to left, not centered
\usepackage{graphicx}
\usepackage{hyperref}
\begin{html}
pre {font-size: 1.2em; background-color: #EEF0F5;}
ul li {list-style-image: url(http://www.math.csi.cuny.edu/static/images/julia.png);}
\end{html}
\begin{document}
\section{Questions to be handed in for project 6:}\newline
To get started, we load \texttt{Plots} so that we can make plots and the \texttt{Roots} package for later usage.\begin{verbatim}
using Plots
gadfly()
using Roots
\end{verbatim}
\rule{\textwidth}{1pt}
\subsubsection{Quick background}\newline
Read about this material here: \href{http://mth229.github.io/derivatives.html}{Approximate derivatives in julia}.\newline
For the impatient, the slope of the tangent line to the graph of $f(x)$ at the point $(c,f(c))$ is given by the following limit:
$$
\lim_{h \rightarrow 0} \frac{f(c + h) - f(c)}{h}.
$$
\newline
The notation for this – when the limit exists – is $f'(c)$, the intuition is that this is the limit of the slope of a sequence of secant lines connecting the points $(c, f(c))$ and $(c+h, f(c+h))$. In general the derivative of a function $f(x)$ is the function $f'(x)$, which returns the slope of the tangent line for each $x$ where it is defined.\subsubsection{Approximate derivatives}\newline
Approximating the slope of the tangent line can be done \href{XXX}{several ways}. The \textit{forward difference quotient} takes a small value of $h$ and uses the values $(f(x+h) - f(x))/h$ as an approximation.\newline
For example to estimate the derivative of $x^x$ at $c=1$ with \texttt{h=1e-6} we could have\begin{verbatim}
f(x) = x^x
c, h = 1, 1e-6
(f(c+h) - f(c))/h
\end{verbatim}
\begin{verbatim}
1.000001000006634\end{verbatim}
\newline
In \texttt{julia} we can write a function that does this, allowing us to pass in any function:\begin{verbatim}
forward(f, c; h=1e-6) = (f(c+h) - f(c))/h
\end{verbatim}
\begin{verbatim}
forward (generic function with 1 method)\end{verbatim}
\newline
We can define an \textit{operator} – something which takes a function and returns a function modifying the above slightly:\begin{verbatim}
Df(f; h=1e-6) = x -> forward(f,x,h=h)
\end{verbatim}
\begin{verbatim}
Df (generic function with 1 method)\end{verbatim}
\subsubsection{Automatic derivatives}\newline
In the \texttt{Roots} package, an operator \texttt{D} (using Euler's notation) is given which uses a numeric approach to compute the derivative. This is more accurate, but conceptually a bit more difficult to understand and does not work for all functions. It is also used like an operator, e.g., \texttt{D(f)} is a function derived from the function \texttt{f}:\begin{verbatim}
using Roots
fp(x) = D(sin)(x)# define a function fp or use D(sin) directly
fp(pi) # finds cos(pi). Also D(sin)(pi)
\end{verbatim}
\begin{verbatim}
-1.0\end{verbatim}
\newline
The usual notation for a deriviative can be defined, though it isn't the default. If we use this command (ignoring the warning):\begin{verbatim}
Base.ctranspose(f::Function) = D(f)
\end{verbatim}
\begin{verbatim}
ctranspose (generic function with 53 methods)\end{verbatim}
\newline
then, we can differentiate using the "usual" notation:\begin{verbatim}
f(x) = sin(x)
f'(pi) # same as D(f)(pi)
f''(pi) # same as D(f,2)(pi)
\end{verbatim}
\begin{verbatim}
-1.2246467991473532e-16\end{verbatim}
\subsubsection{Symbolic derivatives}\newline
The \texttt{D} function gives accurate numeric values for first, second, and even higher-order derivatives. It does not however, return the expression one would get were these computed by hand. The \texttt{diff} function from \texttt{SymPy} will find symbolic derivatives, similar to what is achieved when differentiating "by hand."\newline
The \texttt{diff} function can be called with a function:\begin{verbatim}
using SymPy
f(x) = exp(x) * sin(x)
diff(f)
\end{verbatim}
$$e^{x} \sin{\left (x \right )} + e^{x} \cos{\left (x \right )}$$\newline
It can also be called with a symbolic expression:\begin{verbatim}
x, a = symbols("x, a")
\end{verbatim}
\begin{verbatim}
(x,a)\end{verbatim}
\begin{verbatim}
@vars x a
\end{verbatim}
\begin{verbatim}
diff(x * sin(x))
\end{verbatim}
$$x \cos{\left (x \right )} + \sin{\left (x \right )}$$\newline
If there is another symbol, then a second argument is passed to specify which one is being having its derivative taken:\begin{verbatim}
diff(x*sin(a*x), x)
\end{verbatim}
$$a x \cos{\left (a x \right )} + \sin{\left (a x \right )}$$\subsubsection{Questions}\begin{itemize}\item Calculate the slope of the secant line of $f(x) = 3x^2 + 5$ between $(3,f(3))$ and $(5, f())$.\end{itemize}
\\begin{answer}
type: numeric
reminder: slope of secant line
answer: [23.999, 24.001]
answer_text: [23.999, 24.001]
\\end{answer}
\begin{itemize}\item Verify that the derivative of $f(x) = \sin(x)$ at $\pi/3$ is $1/2$ by finding the following limit using a table:\end{itemize}
$$
\lim_{h \rightarrow 0} \frac{f(\pi/3 + h) - f(\pi/3)}{h}
$$
\newline
(Use \texttt{[hs ys]} to look at your generated data, as was done in the limits project.)
\\begin{answer}
type: longtext
reminder: Verify derivative using a table
answer_text: \verb+[hs map(h->(sin(pi/3+ h)-sin(pi/3))/h, hs)]+
rows: 3
cols: 60
\\end{answer}
\begin{itemize}\item Let $f(x) = 1/x$ and $c=4$. Find the approximate derivative (forward) when \texttt{h=1e-6}.\end{itemize}
\\begin{answer}
type: numeric
reminder: approx forward derivative \verb+(1/x)'(4)+
answer: [-0.06259998438234705, -0.06239998438234705]
answer_text: [-0.063, -0.062]
\\end{answer}
\begin{itemize}\item Let $f(x) = x^x$ and $c=4$. Find the approximate derivative (forward) when \texttt{h=1e-8}.\end{itemize}
\\begin{answer}
type: numeric
reminder: approx forward derivative \verb+(x^x)'(4)+
answer: [610.8913613847911, 610.8913633847911]
answer_text: [610.891, 610.891]
\\end{answer}
\begin{itemize}\item Let $f(x) = (x + 2)/(1 + x^3)$. Plot both $f$ and its approximate derivative on the interval $[0,5]$. Identify the zero of the derivative. What is its value? What is the value of $f(x)$ at this point?\end{itemize}\newline
What commands produce the plot?
\\begin{answer}
type: longtext
reminder: what commands produce a plot of f=(x+2)/(1+x^3) and its derivative over [0,5]?
rows: 3
cols: 60
\\end{answer}
\newline
What is the zero of the derivative on this interval?
\\begin{answer}
type: numeric
reminder: zero of derivative
answer: [0.18436715263814152, 0.5843671526381415]
answer_text: [0.184, 0.584]
\\end{answer}
\newline
What is the value of $f$ at this point:
\\begin{answer}
type: numeric
reminder: value of f(x0)
answer: [2.0562447684254272, 2.4562447684254276]
answer_text: [2.056, 2.456]
\\end{answer}
\begin{itemize}\item Let $f(x) = (x^3 + 5)(x^3 + x + 1)$. The derivative of this function has one real zero. Find it. (You can use \texttt{fzero} with the derivative function after plotting to identify a bracketing interval.)\end{itemize}
\\begin{answer}
type: numeric
reminder: zero of f'(x)
answer: [-1.3642244478879977, -1.3640244478879977]
answer_text: [-1.364, -1.364]
\\end{answer}
\begin{itemize}\item Let $f(x) = \sin(x)$. Following the example on p124 of the Rogawski book we look at a table of values of the forward difference equation at $x=\pi/6$ for various values of $h$. The true derivative is $\cos(\pi/6) = \sqrt{3}/2$.\end{itemize}\newline
Make the following table. \begin{verbatim}
f(x) = sin(x)
c = pi/6
hs = [(1/10)^i for i in 1:12]
ys = [forward(f, c, h=h) for h in hs] - sqrt(3)/2
[hs ys]
\end{verbatim}
\begin{verbatim}
12x2 Array{Any,2}:
0.1 -0.0264218
0.01 -0.00251441
0.001 -0.000250144
0.0001 -2.50014e-5
1.0e-5 -2.50002e-6
1.0e-6 -2.49917e-7
1.0e-7 -2.51525e-8
1.0e-8 -2.39297e-9
1.0e-9 1.42604e-8
1.0e-10 1.80794e-7
1.0e-11 -1.48454e-6
1.0e-12 4.06657e-6\end{verbatim}
\newline
What size \texttt{h} has the closest approximation?
\\begin{answer}
type: radio
reminder: smallest error
values: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12
labels: 1e-1 | 1e-2 | 1e-3 | 1e-4 | 1e-5 | 1e-6 | 1e-7 | 1e-8 | 1e-9 | 1e-10 | 1e-11 | 1e-12
answer: 8
\\end{answer}
\begin{itemize}\item The \texttt{D} operator is easy to use. Here is how we can plot both the sine function and its derivative\end{itemize}\begin{verbatim}
using Plots, Roots# to load plot and D
f(x) = sin(x)
plot([f, D(f)], 0, 2pi) # or plot(f, 0, 2pi); plot!(D(f), 0, 2pi)
\end{verbatim}
\begin{html}
\end{html}
\newline
(If you defined the \texttt{'} notation to work, then it is even easier: \texttt{plot([f,f'], 0, 2pi)}.)\newline
Make a plot of $f(x) = \log(x+1) - x + x^2/2$ and its derivative over the interval $[-3/4, 4]$. The commands are:
\\begin{answer}
type: longtext
reminder: Commands to plot f and its derivative
answer_text: \verb+plot([f,D(f)], -3/4, 4)+
rows: 3
cols: 60
\\end{answer}
\newline
Is the derivative always increasing?
\\begin{answer}
type: radio
reminder: is derivative always increasing?
values: 1 | 2
labels: true | false
answer: 2
\\end{answer}
\begin{itemize}\item The function $f(x) = x^x$ has a derivative for $x > 0$. Use \texttt{fzero} to find a zero of its derivative. What is the value of the zero?\end{itemize}
\\begin{answer}
type: numeric
reminder: zero of derivative of \( x^x \)
answer: [0.36777944117144235, 0.3679794411714423]
answer_text: [0.368, 0.368]
\\end{answer}
\begin{itemize}\item Using the \texttt{diff} function from the \texttt{SymPy} package, identify the proper derivative of $x^x$:\end{itemize}
\\begin{answer}
type: radio
reminder:
values: 1 | 2 | 3 | 4
labels: $x^x$ | $x^(x+1) / (x+1)$ | $x^x*(log(x) + 1)$ | $x*x^(x-1)$
answer: 3
\\end{answer}
\begin{itemize}\item Using the \texttt{diff} function, find the derivative of the inverse tangent, $\tan^{-1}(x)$ (\texttt{atan}). What is the function?\end{itemize}
\\begin{answer}
type: radio
reminder:
values: 2 | 3 | 1
labels: $(-1)\tan^{-2}(x) \cdot (\tan^2(x) + 1)$ | $1/(1+x^2)$ | $(-1)\tan^{-2}(x)$
answer: 2
\\end{answer}
\subsection{Some applications}\begin{itemize}\item Suppose the height of a ball falls according to the formula $h(t) = 300 - 16t^2$. Find the rate of change of height at the instant the ball hits the ground.\end{itemize}
\\begin{answer}
type: numeric
reminder: value of derivative when h is 0
answer: [-138.5641646055102, -138.5639646055102]
answer_text: [-138.564, -138.564]
\\end{answer}
\begin{itemize}\item A formula for blood alcohol level in the body based on time is based on the number of drinks and the time \href{http://en.wikipedia.org/wiki/Blood_alcohol_content}{wikipedia}.\end{itemize}\newline
Suppose a model for the number of drinks consumed per hour is \begin{verbatim}
n(t) = t <= 3 ? 2 * sqrt(3) * sqrt(t) : 6.0
\end{verbatim}
\begin{verbatim}
n (generic function with 1 method)\end{verbatim}
\newline
Then the BAL for a 175 pound male is given by\begin{verbatim}
bal(t) = (0.806 * 1.2 * n(t)) / (0.58 * 175 / 2.2) - 0.017*t
\end{verbatim}
\begin{verbatim}
bal (generic function with 1 method)\end{verbatim}
\newline
From the plot below, describe when the peak blood alcohol level occurs and is the person ever in danger of being above 0.10?\begin{verbatim}
plot(bal, .5,7)
\end{verbatim}
\begin{html}
\end{html}
\\begin{answer}
type: longtext
reminder: Describe when peak BAL occurs, is it ever above 0.10?
answer_text: No, it isn't
rows: 3
cols: 60
\\end{answer}
\begin{itemize}\item Plot the derivative of \texttt{bal} over the time $[0.5, 7]$. Is this function ever positive?\end{itemize}
\\begin{answer}
type: radio
reminder: Is bal' ever positive
values: 2 | 3 | 1
labels: No, it never is | Yes, initially | Yes, after 3
answer: 2
\\end{answer}
\subsubsection{Tangent lines}\newline
The tangent line to the graph of $f(x)$ at $x=c$ is given by $y = f(c) + f'(c)(x-c)$. It is fairly easy to plot both the function and its tangent line – we just need a function to compute the tangent line.\newline
Here we write an operator to return such a function. The operator needs to know both the function name and the value $c$ to find the tangent line at $(c, f(c))$ (notice the \texttt{x->} bit indicating the following returns a function):\begin{verbatim}
tangent(f, c) = x -> f(c) + f'(c)*(x-c) # returns a function
\end{verbatim}
\begin{verbatim}
tangent (generic function with 1 method)\end{verbatim}
\newline
Here we see how to use it:\begin{verbatim}
f(x) = x^2# replace me
plot([f, tangent(f, 1)], 0, 2) # or plot(f, 0, 2); plot!(tangent(f, 1), 0, 2)
\end{verbatim}
\begin{html}
\end{html}
\begin{itemize}\item For the function $f(x) = 1/(x^2 + 1)$ (The witch of Agnesi), graph $f$ over the interval $[-3,3]$ and the tangent line to $f$ at $x=1$.\end{itemize}
\\begin{answer}
type: longtext
reminder: Commands to plot witch of Agnesi over [-3,3] with tangent line at x=1
rows: 3
cols: 60
\\end{answer}
\begin{itemize}\item Let $f(x) = x^3 -2x - 5$. Find the intersection of the tangent line at $x=3$ with the $x$-axis.\end{itemize}
\\begin{answer}
type: numeric
reminder: intersection of tangent line with x axis
answer: [2.3598999999999997, 2.3601]
answer_text: [2.36, 2.36]
\\end{answer}
\begin{itemize}\item Let $f(x)$ be given by the expression below. \end{itemize}\begin{verbatim}
f(x; a=1) = a * log((a + sqrt(a^2 - x^2))/x ) - sqrt(a^2 - x^2)
\end{verbatim}
\begin{verbatim}
f (generic function with 1 method)\end{verbatim}
\newline
The value of \texttt{a} is a parameter, the default value of $a=1$ is fine.\newline
For $x=0.25$ and $x=0.75$ the tangent lines can be drawn with\begin{verbatim}
plot([f, tangent(f, 0.25), tangent(f, 0.75)], 0.01, 0.8)
\end{verbatim}
\begin{html}
\end{html}
\newline
Verify that the length of the tangent line between $(c, f(c))$ and the $y$ axis is the same for $c=0.25$ and $c=0.75$. (For any $c$, the distance formula can be used to find the distance between the point $(c, f(c))$ and $(0, y_0)$ where, $y_0$ is where the tangent line at $c$ crosses the $y$ axis.)
\\begin{answer}
type: longtext
reminder: Verify lengths of two lines are same
answer_text: Write a function to compute length squared: \verb#c^2 + (f(c)-(f(c)+D(f)(c)*(0-c)))^2#
rows: 3
cols: 60
\\end{answer}
\subsubsection{Higher-order derivatives}\newline
Higher-order derivates can be approximated as well. For example, one can use \texttt{D(f,2)} \textit{or}, if defined, \texttt{f''} to approximate the second derivative.\begin{itemize}\item Find the second derivative of $f(x) = \sqrt{x \cdot exp(x)}$ at $c=3$.\end{itemize}
\\begin{answer}
type: numeric
reminder:
answer: [3.0177551230478667, 3.0197551230478665]
answer_text: [3.018, 3.02]
\\end{answer}
\begin{itemize}\item Find the zeros in $[0, 10]$ of the second derivative of the function $f(x) = \sin(2x) + 3\sin(4x)$ using \texttt{fzeros}.\end{itemize}
\\begin{answer}
type: radio
reminder:
values: 3 | 1 | 2
labels: 13 numbers: 0.420534, 1.20943, ..., 9.00424, 9.84531 | 13 numbers: 0.0, 0.806238, ..., 8.61854, 9.42478 | 13 numbers: 0.0, 0.869122, ..., 8.55566, 9.42478
answer: 2
\\end{answer}
\end{document}