\documentclass{article}
\usepackage{amsmath}
\usepackage[margin=2cm]{geometry}
\usepackage{sagetex}
\usepackage{tikz}
\usepackage{siunitx}
\sisetup{per-mode = symbol}
\usepackage{microtype}

\renewcommand{\d}{\mathrm{d}}
\newcommand{\e}{\mathrm{e}}

\begin{document}

\section*{Brief solutions to the midterm exam}

Here are brief solutions to the midterm exam.

\paragraph{Problem 1}
\label{sec:problem-1}
Everyone did well on this problem. Just take a ``bad'' function and
multiply it by $-1$; then the sum is the zero function everywhere except
maybe at $0$, so the limit exists.

\paragraph{Problem 2}
\label{sec:problem-2}
(a) The area is $A(x) = x \sqrt{25^{2} - x^{2}/4} /2$.

(b) The derivative is $\d A/\d x = \sage{diff(x*sqrt(25^2 - x^2/4)/2, x)}$.

(c) By the chain rule, $\frac{\d A}{\d t} = \frac{\d A}{\d x}\frac{\d
  x}{\d t}$. Since $\d x/\d t = -4$, the area is changing at a rate of 
  \[
  \frac{\d A}{\d x}(20) \cdot (-4) = \sage{diff(x*sqrt(25^2 - x^2/4)/2, x).subs(x=20)*(-4)}\,\si{\centi\meter\squared\per\second}.
  \]

\paragraph{Problem 3}
\label{sec:problem-3}
Start with $u g = f$ and differentiate both sides:
\[
u'g + ug' = f'.
\]
We know what $u$ is, but we are pretending that we don't know what $u'$
is. So solve for $u'$ and replace $u$ with $f/g$:
\[
u' = f' - ug' = \frac{f'}{g} - \frac{fg'}{g^{2}} = \frac{f'g - fg'}{g^{2}}.
\]

\paragraph{Problem 4}
\label{sec:problem-4}
Assume $\varepsilon > 0$ has been given to us. To find an appropriate
$\delta$, we check two cases, since $f$ is defined using two different expressions.

Let's investigate negative $x$ values. Our goal is to have $|f(x) -
0| = |3x^{2}| = 3x^{2} < \varepsilon$. That will happen when $|x| <
\sqrt{\varepsilon/3}$. So let $\delta_{1} = \sqrt{\varepsilon/3}$.

For positive $x$ values, same idea: we want $|\sqrt{x}| < \varepsilon$,
which means $|x| < \varepsilon^{2}$. Let $\delta_{2} = \varepsilon^{2}$.

Now, if we take $\delta$ to be the smaller of $\delta_{1}$ and
$\delta_{2}$, for positive or negative values of $x$ we will have $0 < |
x -0| < \delta$ implying $|f(x) - 0)| < \varepsilon$.

\paragraph{Problem 5}
\label{sec:problem-5}
This problem was difficult for many students, but it's quite simple: the
sine of anything is always between $-1$ and $1$, so 
\[
-(x-\pi/2) \le (x - \pi/2)\sin(\tan(x)) \le (x - \pi/2).
\]
The limits, as $x$ approaches $\pi/2$, of the functions ``on the ends''
are both zero, so by the Sandwich Theorem, the limit of the original
function was zero.

\paragraph{Problem 6}
\label{sec:problem-6}
The graph of the inverse of a function is obtained by reflecting the
graph about the line $y=x$. Only picture B has that property. 

\paragraph{Problem 7}
\label{sec:problem-7}
Drawing the graph of the derivative very accurately is quite difficult.
I graded this problem, and I looked for getting the roots correct and
having the derivative positive (or negative) when the function was
increasing (or decreasing). Soon we'll learn more about the second
derivative, which will make it easier to make such graphs.

Below are the graphs made by the computer. For part (b) there is more
than one answer, since you can move a graph up and down and have the
same derivative.

\begin{sagesilent}
# see the other tex file for some more functions

def b(n, i, t):
    return binomial(n, i) * t^i * (1-t)^(n-i)

# from http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/Bezier/bezier-der.html
def curve(cs, t):
    """
    takes controls points and a t (either specfic value, or symbolic
    var) and returns a point or a parametrized curve (f(t), g(t))
    representing the Bezier curve.
    """
    n = len(cs) - 1
    return sum(b(n, k, t) * cs[k] for k in range(n+1))

def dcurve(cs, t):
    """
    returns the derivative of the curve -- velocity vectors
    """
    n = len(cs) - 1
    return curve([(n-1)*(b-a) for a, b in zip(cs, cs[1:])], t)

def icurve(cs, t):
    """
    Given n+1 control points P_0 to P_n, control points for the
    antiderivative are

    Q_k = sum(P_i for i in range(k))/(n+1),

    with Q_0 = origin. This gives the ``antiderivative'' vectors; it's
    not a parametrization of the antiderivative when the original curve
    is the graph of a function.
    """
    n = len(cs) - 1
    return curve([vector((0,0))] +
                 [sum(cs[:k])/(n+1) for k in range(1, n+2)], t)

import re

def fix(s):
    """
    turns Sage's string for a polynomial into TikZ's notation:

    3*t^2 + 2.1000*t + 1 into
    3*\t*\t + 2.1000*\t + 1
    """
    e = 2
    while '^' in s:
        s = s.replace('t^{0}'.format(e), '*'.join([r'\t'] * e))
        e += 1
    # now take care of t^1: handle either "[0-9]*t" or "+ t".
    # replace "t preceded by not-a-backslash" with
    # "what it was preceded by, then \t".
    return re.sub(r'([^\\])t', r'\1\\t', s)

def tikzderiv(cs, t):
    """
    if you have a Bezier that is the graph of a function, this function
    will return a TikZ command that plots the derivative.
    """
    x = str(curve(cs, t)[0].expand())
    dx, dy = [_.expand() for _ in dcurve(cs, t)]
    m = str(dy/dx)
    return ''.join([r'\draw[domain=0:1, variable=\t, smooth] plot ({',
                    fix(x),
                    '},{',
                    fix(m),
                    '});'])

def multiplederivs(pts, t):
    """
    chews through the points four at a time, returns tikz paths
    
    points = a b c d e f g h i j
             -------
                   -------
                         -------
    """
    n = floor((len(pts) - 1)/3)
    return '\n'.join([tikzderiv(pts[3*k:3*k+4], t) for k in range(n)])

def tikzantideriv(cs, t):
    """
    what it says on the tin
    """
    f, g = [_.expand() for _ in curve(cs, t)]
    u = var('u')
    # integrate() sometimes takes floats and turns them into rationals,
    # typically with huge numerator and denominator that are too big for
    # Tikz/TeX to handle. So make sure we've got floats.
    y = str(sum(RR(c)*t^e for c, e in
                integrate(g(t=u)*diff(f(t=u), u), (u,0,t)).expand().coeffs()))
    return ''.join([r'\draw[domain=0:1, variable=\t, smooth] plot ({',
                    fix(str(f)),
                    '},{',
                    fix(y),
                    '});'])

def multipleantiderivs(pts, t):
    """
    too lazy for proper code factoring

    ...and for going through and making the result continuous. This
    starts each ``subpath'' on the x-axis, so to get a continuous
    antiderivative, you could put everything inside scopes and yshift
    them appropriately. That's what I did manually below.
    """
    n = floor((len(pts) - 1)/3)
    return '\n'.join([tikzantideriv(pts[3*k:3*k+4], t) for k in range(n)])

t = var('t')

vectors = lambda vs: [vector(v) for v in vs]

pts = vectors([(-7.7340,1.3616), (-7.4327,0.9381),
(-7.2643,0.6432), (-7.0711,0.2567), (-4.2944,-5.2967),
(-2.9699,-0.2825), (-1.6099,1.0775), (-1.1389,1.5486), (-0.7901,1.3300),
(-0.2525,1.3300), (0.0918,1.3300), (0.8642,1.3607), (1.1996,1.2038),
(2.2083,0.7317), (2.2738,-0.4072), (2.4938,-0.6271), (3.4113,-1.5447),
(2.9068,-2.7764), (3.7565,-3.2472), (4.6267,-3.7295), (4.2525,-2.6303),
(4.8298,-2.5843), (5.2260,-2.5528), (5.3798,-3.4718), (5.8399,-3.4998),
(6.3132,-3.5285), (6.2424,-2.7320), (6.7238,-2.7422), (7.0406,-2.7488),
(7.1698,-3.4561), (7.7340,-3.4366), (8.7699,-3.4009), (8.3854,-0.2857),
(9.4070,-0.2483)])

\end{sagesilent}

\begin{tikzpicture}[scale=.875, yscale=-1]
  \draw[help lines] (-8,-5) grid (10,5);
  \draw[<->] (-8,0) -- (10,0) ;
  \draw[<->] (0,-5) -- (0,5);

  \path[draw=black,line join=miter,line cap=butt,miter limit=4.00,line
    width=1pt] 
    (-7.7340,1.3616) .. controls (-7.4327,0.9381) and
    (-7.2643,0.6432) .. (-7.0711,0.2567) .. controls (-4.2944,-5.2967) and
    (-2.9699,-0.2825) .. (-1.6099,1.0775) .. controls (-1.1389,1.5486) and
    (-0.7901,1.3300) .. (-0.2525,1.3300) .. controls (0.0918,1.3300) and
    (0.8642,1.3607) .. (1.1996,1.2038) .. controls (2.2083,0.7317) and
    (2.2738,-0.4072) .. (2.4938,-0.6271) .. controls (3.4113,-1.5447) and
    (2.9068,-2.7764) .. (3.7565,-3.2472) .. controls (4.6267,-3.7295) and
    (4.2525,-2.6303) .. (4.8298,-2.5843) .. controls (5.2260,-2.5528) and
    (5.3798,-3.4718) .. (5.8399,-3.4998) .. controls (6.3132,-3.5285) and
    (6.2424,-2.7320) .. (6.7238,-2.7422) .. controls (7.0406,-2.7488) and
    (7.1698,-3.4561) .. (7.7340,-3.4366) .. controls (8.7699,-3.4009) and
    (8.3854,-0.2857) .. (9.4070,-0.2483);

  \sagestr{multiplederivs(pts, t)}

\end{tikzpicture}

\begin{sagesilent}
  pts2 = vectors([ 
  (-6.86090,2.97158),   (-6.43620,.97392),  (-3.69868,-4.37696), 
    (-1.21218,-1.89046),   (-.40612,-1.08440),  (-.01442,.91178), 
    (1.06066,1.22928),   (1.77816,1.44118),  (1.72228,1.31250), 
    (2.29376,1.29459),   (2.86250,1.27676),  (3.00740,1.49569), 
    (3.58604,.98848),   (4.22947,.42447),  (4.33628,-.31735), 
    (4.84873,-.82980),   (5.35560,-1.33666),  (6.40363,-.88337), 
    (8.63680,-.82980)])  
\end{sagesilent}

\begin{tikzpicture}[scale=1]

  \draw [<->] (0,-5) -- (0,5);
  \draw [<->] (-7,0) -- (9,0);
  
  \draw[help lines] (-7,-5) grid (9,5);

  \path[draw=black,line join=miter,line cap=butt,line width=1pt]
    (-6.86090,2.97158) .. controls (-6.43620,.97392) and (-3.69868,-4.37696) ..
    (-1.21218,-1.89046) .. controls (-.40612,-1.08440) and (-.01442,.91178) ..
    (1.06066,1.22928) .. controls (1.77816,1.44118) and (1.72228,1.31250) ..
    (2.29376,1.29459) .. controls (2.86250,1.27676) and (3.00740,1.49569) ..
    (3.58604,.98848) .. controls (4.22947,.42447) and (4.33628,-.31735) ..
    (4.84873,-.82980) .. controls (5.35560,-1.33666) and (6.40363,-.88337) ..
    (8.63680,-.82980);

    \begin{scope}[shift={(0,-.15)}]
    \begin{scope}[shift={(0,-1.62)}]
    \begin{scope}[shift={(0,-1.62)}]
    \begin{scope}[shift={(0,0.25)}]
    \begin{scope}[shift={(0,6.65)}]
        \draw[domain=0:1, variable=\t, smooth] plot
        ({-2.56384000000000*\t*\t*\t + 6.93846000000000*\t*\t +
          1.27410000000000*\t -
          6.86090000000000},{-14.3454538964696*\t*\t*\t*\t*\t*\t +
          46.5330273318300*\t*\t*\t*\t*\t - 19.8110072251084*\t*\t*\t*\t
          - 39.6123611129404*\t*\t*\t + 16.8003607468491*\t*\t +
          3.78609002890173*\t});
    \end{scope}

      \draw[domain=0:1, variable=\t, smooth] plot
      ({1.09774000000000*\t*\t*\t - 1.24308000000000*\t*\t +
        2.41818000000000*\t -
        1.21218000000000},{-1.57459824140687*\t*\t*\t*\t*\t*\t +
        3.77805534242948*\t*\t*\t*\t*\t - 1.96254060890427*\t*\t*\t*\t -
        1.20130337198391*\t*\t*\t + 5.27379033399809*\t*\t -
        4.57147262436629*\t});
    \end{scope}
    \draw[domain=0:1, variable=\t, smooth] plot
    ({1.40074000000000*\t*\t*\t - 2.32014000000000*\t*\t +
      2.15250000000000*\t +
      1.06066000000000},{0.316112005308880*\t*\t*\t*\t*\t*\t -
      1.27759334655717*\t*\t*\t*\t*\t + 2.09601046038355*\t*\t*\t*\t +
      0.00552792991499399*\t*\t*\t - 2.16792960011162*\t*\t +
      2.64602520000000*\t});
    \end{scope}

    \draw[domain=0:1, variable=\t, smooth] plot ({0.857579999999999*\t*\t*\t
  - 1.27152000000000*\t*\t + 1.70622000000000*\t +
  2.29376000000000},{-0.412881886516331*\t*\t*\t*\t*\t*\t +
  0.855211792671163*\t*\t*\t*\t*\t - 0.896701386435071*\t*\t*\t*\t +
  1.55952155112313*\t*\t*\t - 1.69172996265222*\t*\t +
  2.20885538766421*\t});
    \end{scope}

\draw[domain=0:1, variable=\t, smooth] plot ({0.942259999999999*\t*\t*\t
  - 1.60986000000000*\t*\t + 1.93029000000000*\t +
  3.58604000000000},{0.191834715432718*\t*\t*\t*\t*\t*\t -
  0.563778973146181*\t*\t*\t*\t*\t - 0.569881460855418*\t*\t*\t*\t +
  2.40413458976704*\t*\t*\t - 3.22436871191594*\t*\t +
  1.90805306010429*\t});
    \end{scope}

\draw[domain=0:1, variable=\t, smooth] plot ({0.643980000000001*\t*\t*\t
  + 1.62348000000000*\t*\t + 1.52061000000000*\t +
  4.84873000000000},{-0.437864544288621*\t*\t*\t*\t*\t*\t +
  0.229882573784585*\t*\t*\t*\t*\t + 1.08680121223923*\t*\t*\t*\t -
  0.720115061063908*\t*\t*\t - 2.50326828245099*\t*\t -
  1.26180217424128*\t});

\end{tikzpicture}

\paragraph{Problem 8}
\label{sec:problem-8}
This is a straightforward related rates problem:
\[
\frac{\d}{\d t}\left(\frac{1}{R}\right) =
\frac{\d}{\d t}\left(\frac{1}{R_{1}} + \frac{1}{R_{2}}\right),
\]
which is
\[
-\frac{1}{R^{2}}\frac{\d R}{\d t} =
 -\frac{1}{R_{1}^{2}}\frac{\d R_{1}}{\d t} -
  \frac{1}{R_{2}^{2}}\frac{\d R_{2}}{\d t}.
\]
Plugging in the relevant quantities, we get
\[
-\sage{(1/1000 + 1/1500)^(2)}\frac{\d R}{\d t} =
-\frac{1}{1000000}(5)
-\frac{1}{2250000}(-10).
\]
The resistance is changing at $\sage{-(1/1000 + 1/1500)^(-2) * (-5/1000000 +
  10/2250000)}\,\si{\ohm\per\second}$.

\paragraph{Problem 9}
\label{sec:problem-9}
The derivatives below are automatically computed, so they may look a
little different from your hand-calculated answers.

(a) $\sage{diff(-7*x^4 + sqrt(3)*x^3 - cos(ln(x)) ,x)}$

(b) $\sage{diff(1/(2-x)    ,x)}$

(c) $\sage{diff(x*csc(x-2)    ,x)}$

(d) $\sage{diff(2*exp(sqrt(x) +x)    ,x)}$

(e) $\sage{diff(exp(exp(2*x+1) + 3)    ,x)}$

(f) $\sage{diff(x^e ,x)}$

(g) $\sage{diff(tan(sin(x))^2    ,x)}$

(h) $\sage{diff(sqrt(1+x+sqrt(2+x+sqrt(3+x)))    ,x)}$

\paragraph{Problem 10}
\label{sec:problem-10}
(a) Start with the definition for $f^{\dag}$ and add $0 = f(x) - f(x)$:
\begin{align}
f^{\dag}(x) &= \lim_{h \to 0} \frac{f(x+h) - f(x) + f(x) - f(x-h)}{h}\\
    &= \lim_{h \to 0} \frac{f(x+h) - f(x)}{h} + \frac{f(x) - f(x-h)}{h}\\
    &= \lim_{h \to 0} \frac{f(x+h) - f(x)}{h} + \lim_{h\to 0}\frac{f(x)
      - f(x-h)}{h}\\
    &= f'(x) + f'(x) = 2f'(x).
\end{align}
The second limit in (3) is the ordinary derivative; just substitute $-h$
for $h$. To go from (2) to (3) requires that both limits in (3) exist
(compare with Problem 1!), which they do.

(b) The absolute value function at zero works here: if $x=0$, the
numerator in the limit for $f^{\dag}$ is always zero, so $f^{\dag}(0) =
0$, but the ordinary derivative doesn't exist.

Other possibilities: more generally, any even function whose derivative
doesn't exist at zero will work here. Also, since the definition of
$f^{\dag}(x)$ does not actually involve the value of $f(x)$, you can
take any differentiable function, change the value at $x$ so that it's
discontinuous, but $f^{\dag}$ will still exist and still be twice the
value of the derivative of the original function.

\paragraph{Problem 11}
\label{sec:problem-11}
This is another problem that many students struggled with. But it's a
difficult problem, so it's okay if you had some trouble.

(a) Here's one way to think about this problem: if $f$ was continuous,
then we know that $g \circ f$ is continuous. But here, we just know that
the limit of $f$ exists. So if $g \circ f$ can fail to be continuous, it
must do so because $f(a)$ doesn't equal $\displaystyle\lim_{x \to a}f$.
So let's make up a simple example like that: let $a=0$, and try $f(x) =
0$, except that $f(0) = 1$, and $g(x) = x + 2$.

Then $(g \circ f)(0) = g(1) = 3$. But for any other $x$, $(g \circ f)(x)
= g(0) = 2$. So it is \emph{not} necessarily true that the composition
is continuous.

(b) Same idea here. In fact, use the same $f$ and $g$, with $b=-2$. Then
$(f \circ g)(-2) = f(0) = 1$, but for any other value of $x$ we get
zero. Same conclusion here.

\begin{sagesilent}
p(t) = 4*sin(t-t^2)
\end{sagesilent}

\paragraph{Problem 12}
\label{sec:problem-12}
(a) Acceleration is the second derivative: it's
$\sage{diff(p(t), t, t)}$.

(b) The tangent line at $t=1$ is $y - \sage{p(1)} = \sage{diff(p(t),
  t).subs(t=1)}(t-1)$. The velocity is the slope of the tangent line:
it's $\sage{diff(p(t), t).subs(t=1)}\,\si{\meter\per\second}$.

\end{document}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 
