2.5 The derivative of a function¶
Given a function $Y = f (X)$, we now understand the concept of the derivative of $f$ at a point $X_0$.
$$ \left. \frac{df}{dX}\right|_{X_0} = \lim_{\Delta X \to X_0} \frac{f(X_0+ \Delta X) − f(X_0)}{\Delta X} $$
Using this definition, given any point $X_0$, we can assign a number to that point: the value of $\dfrac{df}{dX}$ at $X_0$.
This means that we have defined a new function from $\mathbb{R}$ to $\mathbb{R}$: the function that assigns to a point $X$ the value of $\dfrac{df}{dX}$ at that point $X$.
We call this new function the derivative of $f$ , and we write it as $\dfrac{df}{dX}$
The process of finding $\dfrac{df}{dX}$ given $f$ is called differentiating $f$ or taking the derivative of $f$.
var('X')
f(X) = X^2*(X-2)
dfdx = diff(f,X)
plot([f,dfdx],(X,-1,2.5))
Exercise 2.5.1 (Do it with the textbook)
Exercise 2.5.2 What does “the derivative of $f(x)$ is $7x + 4.5$” mean? Give two answers.
Ans. 1) The rate of change of $f(x)$ with respect to $x$ at each point is $7x+45$. 2) At each point
$$ \Delta f = (7x + 45)\Delta x$$
is the linear approximation to $f(x)$
Given a function $Y = f(X)$, we calculate the derivative function
$$\frac{dY}{dX} \quad \text{or}\quad \frac{df}{dX}$$
by finding
$$\frac{f(X + \Delta X) − f(X)}{\Delta X}$$
and letting $\Delta X$ approach $0$.
Exercise 2.5.3 Find the derivative of the function $f(X) = X^3$ as in the above example. (Recall from algebra that $(a + b)^3= a^3+ 3a^2b + 3ab^2+ b^3$.)
Exercise 2.5.4 Calculate the slope of the tangent line to the graph of $Y = X^3$ at $X = 1$.
Higher Order derivatives¶
Once we have the idea that the derivative is a function that takes a function $f(X)$ and assigns to it the function $\dfrac{df}{dX}$, we can ask: what if we applied this function twice? That is, if the derivative $\dfrac{df}{dX}$ is a function then we can consider the derivative of this function, and the derivative of the derivative is called the second derivative of f with respect to X, and is generally written as
$$\dfrac{d^2f}{dX^2} \quad \text{or}\quad f''(X)$$
displacement $\to$ velocity $\to$ acceleration
Exercise 2.5.5 Find the second derivative of $Y(X) = X^3+ 15X^2+ 3$.
Exercise 2.5.6 The growth rate of cells in a petri dish slows down over time. Is the second derivative of the function giving the number of cells positive or negative?
Derivative of common functions¶
The derivative of a constant function is 0. (rate of change is 0 for a constant function).
$$ \frac{d c}{dX} = 0$$
where $c$ is a number viewed as a costant function.
Exercise 2.5.7 Why does this make sense?
The derivative of the identity function $f(X) =X$ is $1$, that is
$$ \frac{dX}{dX} = 1$$
Exercise Why does this make sense?
Power Functions:
More generally, for any $n \neq 0$,
$$ \frac{dX^n}{dX} = n X^{n-1}$$
Exercise 2.5.8 Differentiate: a) $f(X) = X^5$ b) $f(X) = X^{−3}$ c) $f(X) = X^{17.2}$
Exercise 2.5.9 The maximum life-span, $L$, of a mammalian species increases with average body mass $B$ as roughly $L(B) = B^{0.25}$ . What is the rate of increase of life-span with body mass?
$\sin(X)$ and $\cos(X)$¶
It can be shown that
$$ \frac{d \sin(X)}{dX} = \cos(X) $$
Examine their graphs should convince you that this is indeed the case
plot([sin(x),cos(x)], (x,-pi,pi))
Note that differentiating $\sin(X)$ amongs to shifting its graph to the left by $\pi/2$, i.e.
$$(\sin(X))' = \sin(X+\pi/2) = \cos(X)$$
Thus, $(\cos(X))' = \cos(X + \pi/2) = - \sin(X)$.
And so the higher derivatives of $\sin(X)$ form a 4-cycle
$\sin(X) \to \cos(X) \to -\sin(X) \to -\cos(X) \to \sin(X)$
$e^X$ and $\ln X$¶
The exponential function $X \mapsto e^X$ is special, its derivative with respect to $X$ is the function itself. That is,
$$ \frac{de^X}{dX} = e^X$$
Since $\ln X$ is the inverse function of $e^X$, it can be deduced that
$$ \frac{d \ln X}{dX} = \frac{1}{X}$$
Rules of differentiation¶
Suppose $f$ and $g$ are differentiable then
sum rule $f+g$ is differentiable and $(f+g)' = f' + g'$
In particular, $f$ and $f+c$ where $c$ is any constant have the same derivative
Exercise 2.5.11 A similar rule holds for subtraction. Why?
Exercise 2.5.12 Apply the addition and subtraction rules to calculate the derivative of the function
$$f(X) = X + X^2 − 2X^3 + 2X^4.$$
product rule $fg$ is differentiable moreover, $(fg)' = f'g + fg'$
In particular, $(cf)' = c'f + cf' = 0f + cf' = cf'$ for any constant $c$.
It follows from these rules that $(X^n)' = nX^{n-1}$ for any integer $n$. In fact, something more general is true
Power Rule
$$ (X^r)' = rX^{r-1}$$
for any real number $r$.
Since polynomials are built up from constant and $X$ by taking sum and product, we can differentiate polynomials
e.g
\begin{align*} (3X^5 - 2X^2 + 17)' &= (3X^5)' + (-2X^2)' +(17)' \\ &=3(X^5)' + (-2)(X^2)' + 0 \\ &=15X^4 -4X \end{align*}
The following rule is also a consequence of the sum rule and the product rule
Quotient Rule $f/g$ is differentiable and
$$ \left(\frac{f}{g}\right)' = \frac{f'g - fg'}{g^2}$$
e.g.
\begin{align*} (\tan X)' & = \left(\frac{\sin X}{\cos X}\right)' = \frac{(\sin(X))'\cos(X)- \sin(X)(\cos(X))'}{\cos^2(X)} \\ & = \frac{\cos^2(X) + \sin^2(X)}{\cos^2(X)} = \frac{1}{\cos^2(X)} \\ & = \sec^2(X) \end{align*}
Exercise 2.5.14 Differentiate the following functions:
a) $f(t) = \sin(t)\cos(t)$
b) $h(X) = \dfrac{X^2}{3X+5}$
c) $f(X) = \dfrac{4X}{\sqrt{X}+2}$
d) $g(Y) = (3Y^6)\ln Y$
The Chain Rule¶
A fundamental operation on functions is composition
$$g \circ f(X) = g(f(X))$$
So $g\circ f$ is the function that sends $X$ to $f(X)$ and then to $g(f(X))$
For example, if $f(X) = X^2$ and $g(X) = \sin(X)$, then
$$g\circ f(X) = g(f(X))= g(X^2) = \sin(X^2)$$
plot(sin(x^2),(x,-pi,pi))
Note also that
$$f\circ g(X) = f(\sin(X)) = (\sin(X))^2 = \sin^2(X)$$
plot(sin(x)^2,(x,-pi,pi))
This shows that in general,
$$ f\circ g(X) \neq g \circ f(X)$$
even when both of them make sense.
There is another way of thinking of composition---substitution
e.g. $Y$ is a function of $X$ and $U$ is a function of $Y$, say
$Y = f(X)$ and $U = g(Y)$
Then one can think of $U$ as a function of $X$ by substitution, namely
$$U = g(Y) = g(f(X)) = g\circ f(X)$$
The Chain Rule is a rule that expresses the derivative of a composition in terms of its components and their derivatives.
Chain Rule Suppose $f$ and $g$ are differentiable, then so is $g \circ f$ (whenever the composition make sense). Moreover,
$$(g\circ f)'(x) = g'(f(x))f'(x)$$
Exercise 2.5.15 Write the following expressions of $h(X)$ as a composition of two functions, one outer function $f(Y)$ and one inner function $g(X)$, so that $f (g(X)) = h(X)$. Then, find the derivative of each.
a) $h(X) = (X^3+1)^2$
b) $h(X) = \sqrt{X^5}$
c) $h(X) = e^{X^2+1}$