In the previous Section we talked about derivatives of a function that takes in a single input, at a single point. We then saw how to quickly whip up a way to compute numerical derivatives across entire input spaces. This approach - called Numerical Differentiation - while simple to build it was unfortunately generally unstable and at potentially innacurate depending on the input function.
In this Section we explore how we can derive formulae for the derivatives of generic functions constructed from elementary functions/operations, which has far-reaching and very positive consequences for the effective automated computing of derivatives.
Every mathematical function - whether or not it begins with an equation - can be expressed as a table of input/output values. For example, the function $g(w) = w^2$ can be expressed as an infinitely long table like this
input: $w$ | output: $g(w)=w^2$ |
---|---|
$0$ | $0$ |
$-0.4$ | $0.16$ |
$3$ | $9$ |
$-1$ | $1$ |
$\vdots$ | $\vdots$ |
Here the input/output pairs - of which there are infinitely many - are not ordered in any particular way. On the contrary - as we discuss in our material about the basics of mathematical functions - it is not often the case that a mathematical function that begins as a table of values can be expressed as an equation.
The derivative $\frac{\mathrm{d}}{\mathrm{d}w}g(w)$ of a given function - like $g(w) = w^2$ when evaluated at every point is certainly an example of a table of values. For example we could write it like this
input: $w$ | output: $\frac{\mathrm{d}}{\mathrm{d}w}g(w)$ |
---|---|
$0$ | $0$ |
$-0.4$ | $-0.8$ |
$3$ | $6$ |
$-1$ | $-2$ |
$\vdots$ | $\vdots$ |
where once again the input/output pairs - of which there are infinitely many - are not written in any particular order. So - in general - the derivative is certainly a mathematical function which can be expressed as a table of values. But can we express the derivative as an equation?
Well we could first try plotting the table of values for some examples to see if we could not visually identify an equation associated to a table - what will a derivative function look like if we plot it?
In the next Python cell we compute take function of one input - here
\begin{equation} g(w) = w^2 \end{equation}and compute its derivative over a coarse sample of points on the interval [-2.5,2.5] and use a slider mechanism to animate this computation. In the left panel we show the original function along with both a point shown in red and its corresponding tangent line in green with slope given by the derivative . As you slide from left to right the point at which the derivative is taken moves smoothly across the input inverval. In the right panel we simultenously plot the value of the derivative in green , along with every previous derivative value computed up to that point.
# what function should we play with? Defined in the next line, along with our fixed point where we show tangency.
g = lambda w: w**2
# create an instance of the visualizer with this function
st = calclib.function_derivative_joint_visualizer.visualizer(g = g)
# run the visualizer for our chosen input function and initial point
st.draw_it(num_frames = 250)
Here the plotted derivative looks like a line! In fact - pushing the slider all the way to the right - from the picture it looks like we can be more specific: the line cross the origin, so the vertical intercept must be zero, and at $w = 2.5$ (the end of the plot) the output value looks to be around $g(2.5) = 5$, so the slope of this line appears to be around $\frac{g(2.5) - g(0)}{2.5 - 0} = \frac{5}{2.5} = 2$. So - at least over the region of input plotted - the equation of the derivative appears to be
$$ \frac{\mathrm{d}}{\mathrm{d}w}g(w) = 2w$$Lets try the same experiment with another function - the sinusoid
\begin{equation} g(w) = \text{sin}(w) \end{equation}# what function should we play with? Defined in the next line, along with our fixed point where we show tangency.
g = lambda w: np.sin(w)
# create an instance of the visualizer with this function
st = calclib.function_derivative_joint_visualizer.visualizer(g = g)
# run the visualizer for our chosen input function and initial point
st.draw_it(num_frames = 250)
All right - push the slider all the way to the right and lets examine the derivative. Well first off one thing we can say is that the derivative looks pretty wavy - like a sine or cosine function, and of the same frequency as the original function itself. Since its value at $w = 0$ is maximal we could say that some sort of cosine equation might work. Notice too that the magnitude of the derivative - its vertical range - has not changed.
Putting together these pieces we could loosely guess that the equation for the derivative over this range of input is the cosine function
\begin{equation} \frac{\mathrm{d}}{\mathrm{d}w}g(w) = \text{cos}(w) \end{equation}Ok - one more. Lets try the experiment with a crazier looking function
\begin{equation} g(w) = \text{sin}(w^3) \end{equation}# what function should we play with? Defined in the next line, along with our fixed point where we show tangency.
g = lambda w: np.sin(w**3)
# create an instance of the visualizer with this function
st = calclib.function_derivative_joint_visualizer.visualizer(g = g)
# run the visualizer for our chosen input function and initial point
st.draw_it(num_frames = 400)