If the rate is a constant ``60`` miles/hour, then in one hour the distance traveled is ``60`` miles.
Of course, the odometer isn't just incrementing once per hour, it is incrementing once every ``1/10``th of a mile. How much time does that take? Well, we would need to solve $1/10=60 \cdot t$ which means $t=1/600$ hours, better known as once every ``6`` seconds.
Using some mathematical notation, would give $x(t) = v\cdot t$, where
$x$ is position at time $t$, $v$ is the *constant* velocity and $t$ the time
traveled in hours. A simple graph of the first three hours of travel would show:
```julia; hold=true;
position(t) = 60 * t
plot(position, 0, 3)
```
Oh no, we hit traffic. In the next ``30`` minutes we only traveled
``15`` miles. We were so busy looking out for traffic, the speedometer was
not checked. What would the average speed have been? Though in the ``30``
minutes of stop-and-go traffic, the displayed speed may have varied, the *average speed*
would simply be the change in distance over the change in time, or
$\Delta x / \Delta t$. That is
```julia
15/(1/2)
```
Now suppose that after $6$ hours of travel the GPS in the car gives us a readout of distance traveled
as a function of time. The graph looks like this:
```julia; hold=true; echo=false
function position(t)
t <= 3 ? 60*t :
t <= 3.5 ? position(3) + 30*(t-3) :
t <= 4 ? position(3.5) + 75 * (t-3.5) :
t <= 4.5 ? position(4) : position(4.5) + 60*(t-4.5)
end
plot(position, 0, 6)
```
We can see with some effort that the slope is steady for the first three hours, is slightly less between $3$ and
$3.5$ hours, then is a bit steeper for the next half hour. After that, it is flat for the
about half an hour, then the slope continues on with same value as in the first
``3`` hours. What does that say about our speed during our trip?
Based on the graph, what was the average speed over the first three hours? Well, we traveled ``180`` miles, and took ``3`` hours:
```julia
180/3
```
What about the next half hour? Squinting shows the amount traveled was ``15`` miles (``195 - 180``) and it took ``1/2`` an hour:
```julia
15/(1/2)
```
And the half hour after that? The average speed is found from the distance traveled, ``37.5`` miles, divided by the time, ``1/2`` hour:
```julia
37.5 / (1/2)
```
Okay, so there was some speeding involved.
The next half hour the car did not move. What was the average speed? Well the change in position was ``0``, but the time was ``1/2`` hour, so the average was ``0``.
Perhaps a graph of the speed is a bit more clear. We can do this based on the above:
```julia
function speed(t)
0 < t <= 3 ? 60 :
t <= 3.5 ? 30 :
t <= 4 ? 75 :
t <= 4.5 ? 0 : 60
end
plot(speed, 0, 6)
```
The jumps, as discussed before, are artifacts of the graphing
algorithm. What is interesting, is we could have derived the graph of
`speed` from that of `x` by just finding the slopes of the line
segments, and we could have derived the graph of `x` from that of
`speed`, just using the simple formula relating distance, rate, and
We see it increments by $2$. The acceleration is the rate of change of
speed. We see the rate of change of speed is constant, as the speed
increments by ``2`` each time unit.
Based on this - and given Galileo's insight - it appears the
acceleration for a falling body subject to gravity will be
**constant** and the position as a function of time will be quadratic.
## The slope of the secant line
In the above examples, we see that the average speed is computed using
the slope formula. This can be generalized for any univariate function
$f(x)$:
> The average rate of change between $a$ and $b$ is $(f(b) - f(a)) /
> (b - a)$. It is typical to express this as $\Delta y/ \Delta x$,
> where $\Delta$ means "change".
Geometrically, this is the slope of the line connecting the points
$(a, f(a))$ and $(b, f(b))$. This line is called a
[secant](http://en.wikipedia.org/wiki/Secant_line) line, which is just
a line intersecting two specified points on a curve.
Rather than parameterize this problem using $a$ and $b$, we let $c$ and $c+h$ represent the two values for $x$, then the secant-line-slope formula becomes
```math
m = \frac{f(c+h) - f(c)}{h}.
```
## The slope of the tangent line
The slope of the secant line represents the average rate of change
over a given period, $h$. What if this rate is so variable, that it
makes sense to take smaller and smaller periods $h$? In fact, what if
The slope of each secant line represents the *average* rate of change between $c$ and $c+h$. As $h$ goes towards $0$, we recover the slope of the tangent line, which represents the *instantatneous* rate of change.
"""
n = 5
anim = @animate for i=0:n
secant_line_tangent_line_graph(i)
end
imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
ImageFile(imgfile, caption)
```
The graphic suggests that the slopes of the secant line converge to
the slope of a "tangent" line. That is, for a given $c$, this
limit exists:
```math
\lim_{h \rightarrow 0} \frac{f(c+h) - f(c)}{h}.
```
We will define the tangent line at $(c, f(c))$ to be the line through
the point with the slope from the limit above - provided that limit
exists. Informally, the tangent line is the line through the point
The limit above is identical, only it uses $x$ instead of $c$ to
emphasize that we are thinking of a function now, and not just a value
at a point.
The derivative is related to a function, but at times it is more convenient to write only the expression defining the rule of the function. In that case, we use this notation for the derivative ``[\text{expression}]'``.
### Some basic derivatives
- **The power rule**. What is the derivative of the monomial $f(x) = x^n$? We need to look
at $(x+h)^n - x^n$ for positive, integer-value $n$. Let's look at a case, $n=5$
```julia
@syms x::real h::real
n = 5
ex = expand((x+h)^n - x^n)
```
All terms have an `h` in them, so we cancel it out:
```julia
cancel(ex/h, h)
```
We see the lone term `5x^4` without an $h$, so as we let $h$ go to $0$, this will be the limit. That is, $f'(x) = 5x^4$.
For integer-valued, positive, $n$, the binomial theorem gives an
and take a limit. If the answer isn't clear, we can let `SymPy` do this work:
```julia
limit((sin(x+h) - sin(x))/ h, h => 0)
```
From the formula ``[\sin(x)]' = \cos(x)`` we can easily get the *slope* of the tangent line to ``f(x) = \sin(x)`` at ``x=0`` by simply evaluating ``\cos(0) = 1``.
- Let's see what the derivative of $\ln(x) = \log(x)$ is (using base ``e`` for ``\log`` unless otherwise indicated). We have
for derivatives. Some are historical, some just add
flexibility. We use the prime notation of Lagrange: ``f'(x)``, ``u'`` and ``[\text{expr}]'``,
where the first emphasizes that the derivative is a function with a
value at ``x``, the second emphasizes the derivative operates on
functions, the last emphasizes that we are taking the derivative of
some expression.
There are many other notations:
- The Leibniz notation uses the infinitesimals: ``dy/dx`` to relate to
``\Delta y/\Delta x``. This notation is very common, and especially
useful when more than one variable is involved. `SymPy` uses
Leibniz notation in some of its output, expressing somethings such
as:
```math
f'(x) = \frac{d}{d\xi}(f(\xi)) \big|_{\xi=x}.
```
The notation - ``\big|`` - on the right-hand side separates the tasks of finding the
derivative and evaluating the derivative at a specific value.
- Euler used `D` for the operator `D(f)`. This was initially used by
[Argobast](http://jeff560.tripod.com/calculus.html). The notation `D(f)(c)` would be needed to evaluate the derivative at a point.
- Newton used a "dot" above the variable, ``\dot{x}(t)``, which is still widely used in physics to indicate a derivative in time. This inidicates take the derivative and then plug in ``t``.
- The notation ``[expr]'(c)`` or ``[expr]'\big|_{x=c}``would similarly mean, take the derivative of the expression and **then** evaluate at ``c``.
## Rules of derivatives
We could proceed in a similar manner -- using limits to find other
derivatives, but let's not. If we have a function $f(x) = x^5
\sin(x)$, it would be nice to leverage our previous work on the
derivatives of $f(x) =x^5$ and $g(x) = \sin(x)$, rather than derive an
answer from scratch.
As with limits and continuity, it proves very useful to consider rules
that make the process of finding derivatives of combinations of
functions a matter of combining derivatives of the individual functions in some manner.
We already have one such rule:
### Power rule
We have seen for integer ``n \geq 0`` the formula:
```math
[x^n]' = n x^{n-1}.
```
This will be shown true for all real exponents.
### Sum rule
Let's consider $k(x) = a\cdot f(x) + b\cdot g(x)$, what is its derivative? That is, in terms of $f$, $g$ and their derivatives, can we express $k'(x)$?
Compute the derivative of ``f(x) = (x-1) \cdot (x-2)``.
This can be done using the product rule *or* by expanding the polynomial and using the power and sum rule. As this polynomial is easy to expand, we do both and compare:
Find the derivative of $f(x) = (x-1)(x-2)(x-3)(x-4)(x-5)$.
We could expand this, as above, but without computer assistance the potential for error is high. Instead we will use the product rule on the product of ``5`` terms.
Let's first treat the case of $3$ products:
```math
[u\cdot v\cdot w]' =[ u \cdot (vw)]' = u' (vw) + u [vw]' = u'(vw) + u[v' w + v w'] =
There are $n$ terms, each where one of the $f_i$s have a derivative. Were we to multiply top and bottom by $f_i$, we would get each term looks like: $f \cdot f_i'/f_i$.
With this, we can proceed. Each term $x-i$ has derivative $1$, so the answer to $f'(x)$, with $f$ as above, is $f'(x) = f(x)/(x-1) + f(x)/(x-2) + f(x)/(x-3) + f(x)/(x-4) + f(x)/(x-5)$, that is:
Finally, the derivative of a composition of functions can be computed
using pieces of each function. This gives a rule called the *chain
rule*. Before deriving, let's give a slight motivation.
Consider the output of a factory for some widget. It depends on two steps:
an initial manufacturing step and a finishing step. The number of
employees is important in how much is initially manufactured. Suppose
$x$ is the number of employees and $g(x)$ is the amount initially
manufactured. Adding more employees increases the amount made by the
made-up rule $g(x) = \sqrt{x}$. The finishing step depends on how much
is made by the employees. If $y$ is the amount made, then $f(y)$ is
the number of widgets finished. Suppose for some reason that $f(y) =
y^2.$
How many widgets are made as a function of employees? The composition
$u(x) = f(g(x))$ would provide that. Changes in the initial manufacturing step lead to changes in how much is initially made; changes in the initial amount made leads to changes in the finished products. Each change contributes to the overall change.
What is the effect of adding employees on the rate of output of widgets?
In this specific case we know the answer, as $(f \circ g)(x) = x$, so
the answer is just the rate is $1$.
In general, we want to express $\Delta f / \Delta x$ in a form so that we can take a limit.
But what do we know? We know $\Delta g / \Delta x$ and $\Delta f/\Delta y$. Using $y=g(x)$, this suggests that we might have luck with the right side of this equation:
We use ``\square`` for the argument of `f'` to emphasize that ``g(x)`` is the needed value, not just ``x``:
```math
\begin{align*}
[(\sqrt{x})^2]' &= [f(g(x)]'\\
&= f'(g(x)) \cdot g'(x) \\
&= 2(\sqrt{x}) \cdot \frac{1}{2}x^{-1/2}\\
&= \frac{2\sqrt{x}}{2\sqrt{x}}\\
&=1
\end{align*}
```
This is the same as the derivative of $x$ found by first evaluating the composition. For this problem, the chain rule is not necessary, but typically it is a needed rule to fully differentiate a function.
##### Examples
Find the derivative of ``f(x) = \sqrt{1 - x^2}``. We identify the composition of ``\sqrt{x}`` and ``(1-x^2)``. We set the functions and their derivatives into a pattern to emphasize the pieces in the chain-rule formula:
Find the derivative of ``\log(2 + \sin(x))``. This is a composition ``\log(x)`` -- with derivative ``1/x`` and ``2 + \sin(x)`` -- with derivative ``\cos(x)``. We get ``(1/\sin(x)) \cos(x)``.
In general,
```math
[\log(f(x))]' \frac{f'(x)}{f(x)}.
```
----
Find the derivative of ``e^{f(x)}``. The inner function has derivative ``f'(x)``, the outer function has derivative ``e^x`` (the same as the outer function itself). We get for a derivative
```math
[e^{f(x)}]' = e^{f(x)} \cdot f'(x).
```
This is a useful rule to remember for expressions involving exponentials.
----
Find the derivative of ``\sin(x)\cos(2x)`` at ``x=\pi``.
A function is *differentiable* at $a$ if the following limit exists $\lim_{h \rightarrow 0}(f(a+h)-f(a))/h$. Reexpressing this as: $f(a+h) - f(a) - f'(a)h = \epsilon_f(h) h$ where as $h\rightarrow 0$, $\epsilon_f(h) \rightarrow 0$. Then, we have:
where $\epsilon(h)$ combines the above terms which go to zero as $h\rightarrow 0$ into one. This is
the alternative definition of the derivative, showing $(f\circ g)'(a) = f'(g(a)) g'(a)$ when $g$ is differentiable at $a$ and $f$ is differentiable at $g(a)$.
The chain rule name could also be simply the "composition rule," as that is the operation the rule works for. However, in practice, there are usually *multiple* compositions, and the "chain" rule is used to chain together the different pieces. To get a sense, consider a triple composition ``u(v(w(x())))``. This will have derivative:
```math
\begin{align*}
[u(v(w(x)))]' &= u'(v(w(x))) \cdot [v(w(x))]' \\
&= u'(v(w(x))) \cdot v'(w(x)) \cdot w'(x)
\end{align*}
```
The answer can be viewed as a repeated peeling off of the outer
function, a view with immediate application to many compositions. To
see that in action with an expression, consider this derivative
The general product rule: For any $n$ - not just integer values - we can re-express $x^n$ using $e$: $x^n = e^{n \log(x)}$. Now the chain rule can be applied:
```math
[x^n]' = [e^{n\log(x)}]' = e^{n\log(x)} \cdot (n \frac{1}{x}) = n x^n \cdot \frac{1}{x} = n x^{n-1}.
```
----
Find the derivative of $f(x) = x^3 (1-x)^2$ using either the power rule or the sum rule.
The power rule expresses $f=u\cdot v$. With $u(x)=x^3$ and $v(x)=(1-x)^2$ we get:
Suppose we knew that $\log(x)$ had derivative of $1/x$, but didn't know the derivative of $e^x$. From their inverse relation, we have: $x=\log(e^x)$, so taking derivatives of both sides would yield:
```math
1 = (\frac{1}{e^x}) \cdot [e^x]'.
```
Or solving, $[e^x]' = e^x$. This is a general strategy to find the
derivative of an *inverse* function.
The graph of an inverse function is related to the graph of the function through the symmetry ``y=x``.
For example, the graph of ``e^x`` and ``\log(x)`` have this symmetry, emphasized below:
```julia;hold=true;echo=false;
f(x) = exp(x)
f′(x) = exp(x)
f⁻¹(x) = log(x) # using Unicode typed with "f^\-^\1"
The point ``(1, e)`` on the graph of ``e^x`` matches the point ``(e, 1)`` on the graph of the inverse function, ``\log(x)``. The slope of the tangent line at ``x=1`` to ``e^x`` is given by ``e`` as well. What is the slope of the tangent line to ``\log(x)`` at ``x=e``?
As seen, the value can be computed, but how?
Finding the derivative of the inverse function can be achieved from the chain rule using the identify ``f^{-1}(f(x)) = x`` for all ``x`` in the domain of ``f``.
The chain rule applied to both sides, yields:
```math
1 = [f^{-1}]'(f(x)) \cdot f'(x)
```
Solving, we see that ``[f^{-1}]'(f(x)) = 1/f'(x)``. To emphasize the evaluation of the derivative of the inverse function at ``f(x)`` we might write:
So the reciprocal of the slope of the tangent line of ``f`` at the mirror image point. In the above, we see if the slope of the tangent line at ``(1,e)`` to ``f`` is ``e``, then the slope of the tangent line to ``f^{-1}(x)`` at ``(e,1)`` would be ``1/e``.
#### Rules of derivatives and some sample functions
This table summarizes the rules of derivatives that allow derivatives
of more complicated expressions to be computed with the derivatives of
The derivative of a function is an operator, it takes a function and
returns a new, derived, function. We could repeat this
operation. The result is called a higher-order derivative. The
Lagrange notation uses additional "primes" to indicate how many. So
$f''(x)$ is the second derivative and $f'''(x)$ the third. For even
higher orders, sometimes the notation is $f^{(n)}(x)$ to indicate an
$n$th derivative.
##### Examples
Find the first ``3`` derivatives of ``f(x) = ax^3 + bx^2 + cx + d``.
Differentiating a polynomial is done with the sum rule, here we repeat three times:
```math
\begin{align}
f(x) &= ax^3 + bx^2 + cx + d\\
f'(x) &= 3ax^2 + 2bx + c \\
f''(x) &= 3\cdot 2 a x + 2b \\
f'''(x) &= 6a
\end{align}
```
We can see, the fourth derivative -- and all higher order ones -- would be identically ``0``. This is part of a general phenomenon: an ``n``th degree polynomial has only ``n`` non-zero derivatives.
----
Find the first ``5`` derivatives of ``\sin(x)``.
```math
\begin{align}
f(x) &= \sin(x) \\
f'(x) &= \cos(x) \\
f''(x) &= -\sin(x) \\
f'''(x) &= -\cos(x) \\
f^{(4)} &= \sin(x) \\
f^{(5)} &= \cos(x)
\end{align}
```
We see the derivatives repeat themselves. (We also see alternative notation for higher order derivatives.)
Having to iterate the use of `diff` is cumbersome. An alternate notation is either specifying the variable twice: `diff(ex, x, x)` or using a number after the variable: `diff(ex, x, 2)`:
```julia
diff(exp(-x^2), x, x) |> simplify
```
Higher-order derivatives can become involved when the product or quotient rules becomes involved.
## Questions
###### Question
The derivative at $c$ is the slope of the tangent line at $x=c$. Answer the following based on this graph:
```julia
fn = x -> -x*exp(x)*sin(pi*x)
plot(fn, 0, 2)
```
At which of these points $c= 1/2, 1, 3/2$ is the derivative negative?
Consider the function $f$ and its transformation $g(x) = a + f(x)$
(shift up by $a$). Do $f$ and $g$ have the same derivative?
```julia; hold=true; echo=false
yesnoq("yes")
```
Consider the function $f$ and its transformation $g(x) = f(x - a)$
(shift right by $a$). Do $f$ and $g$ have the same derivative?
```julia; hold=true; echo=false
yesnoq("no")
```
Consider the function $f$ and its transformation $g(x) = f(x - a)$
(shift right by $a$). Is $f'$ at $x$ equal to $g'$ at $x-a$?
```julia; hold=true; echo=false
yesnoq("yes")
```
Consider the function $f$ and its transformation $g(x) = c f(x)$, $c >
1$. Do $f$ and $g$ have the same derivative?
```julia; hold=true; echo=false
yesnoq("no")
```
Consider the function $f$ and its transformation $g(x) = f(x/c)$, $c >
1$. Do $f$ and $g$ have the same derivative?
```julia; hold=true; echo=false
yesnoq("no")
```
Which of the following is true?
```julia; hold=true; echo=false
choices = [
L"If the graphs of $f$ and $g$ are translations up and down, the tangent line at corresponding points is unchanged.",
L"If the graphs of $f$ and $g$ are rescalings of each other through $g(x)=f(x/c)$, $c > 1$. Then the tangent line for corresponding points is the same.",
L"If the graphs of $f$ and $g$ are rescalings of each other through $g(x)=cf(x)$, $c > 1$. Then the tangent line for corresponding points is the same."
The rate of change of volume with respect to height is $3h$. The rate of change of height with respect to time is $2t$. At at $t=3$ the height is $h=14$ what is the rate of change of volume with respect to time when $t=3$?
```julia; hold=true; echo=false
## dv/dt = dv/dh * dh/dt = 3h * 2t
h = 14; t=3
val = (3*h) * (2*t)
numericq(val)
```
###### Question
Which equation below is $f(x) = \sin(k\cdot x)$ a solution of ($k > 1$)?
Their are ``6`` trig functions. The derivatives of ``\sin(x)`` and ``\cos(x)`` should be memorized. The others can be derived if not memorized using the quotient rule or chain rule.
What is ``[\tan(x)]'``? (Use ``\tan(x) = \sin(x)/\cos(x)``.)
```julia; echo=false
trig_choices = [
"``\\sec^2(x)``",
"``\\sec(x)\\tan(x)``",
"``-\\csc^2(x)``",
"``-\\csc(x)\\cot(x)``"
]
radioq(trig_choices, 1)
```
What is ``[\cot(x)]'``? (Use ``\tan(x) = \cos(x)/\sin(x)``.)
```julia; echo=false
radioq(trig_choices, 3)
```
What is ``[\sec(x)]'``? (Use ``\sec(x) = 1/\cos(x)``.)
```julia; echo=false
radioq(trig_choices, 2)
```
What is ``[\csc(x)]'``? (Use ``\csc(x) = 1/\sin(x)``.)
The right graph is of ``g(x) = \exp(x)`` at ``x=1``, the left graph of ``f(x) = \sin(x)`` *rotated* ``90`` degrees counter-clockwise. Chasing the arrows shows graphically how ``f(g(1))`` can be computed. The nearby values ``f(g(1+h))`` are -- using the tangent line of ``g`` at ``x-1`` -- approximated by ``f(g(1) + g'(1)\cdot h)``, as shown in the graph segment on the left.
Assuming the approximation gets better for ``h`` close to ``0``, as it visually does, the derivative at ``1`` for ``f(g(x))`` should be given by this limit: