23 Derivatives of assembled functions
Chapter 19 used the rules associated with evanescent \(h\), that is, \(\lim_{h\rightarrow 0}\), to confirm our claims about the derivatives of many of the pattern-book functions. We will call these rules h-theory for short. This chapter will use h-theory to find algebraic rules to calculate the derivatives of linear combinations of functions, products of functions, and composition of functions. Remarkably, we can figure out these rules without specifying which functions are being combined. So the rules can be written abstractly using the pronouns \(f()\) and \(g()\). Later, we will apply those rules to specific functions, to show how the rules are used in practical work.
23.1 Derivatives of the basic modeling functions
Opinions vary about whether it is really worthwhile for students to do extensive problem sets involving differentiating arbitrarily complex functions. But to become conversant with calculus, it helps to know at a glance the derivatives of a few, widely used functions. Section 19.3 introduced symbolic derivatives of each of the pattern-book functions. In general, the pattern-book functions have derivatives that are themselves either pattern-book functions or slight modifications of them. For instance, \(\partial_x \pnorm(x) \equiv \dnorm(x)\) and \(\partial_x \ln(x) = \recip(x)\). Most simply of all, \(\partial_x e^x = e^x\).
The basic modeling functions are the same as the pattern-book functions, but with bare \(x\) replaced by \(\line(x)\). In other words, each of the basic modeling functions is a composition of the corresponding pattern-book function with \(\line(x)\). The derivatives of the basic modeling functions are simple variations on the derivatives of the pattern-book functions. It’s easy to learn the rules and well worthwhile committing them to memory.
The “chain rule” provides the underlying logic but takes an especially easy form for the pattern-book functions. We can wait until Section 23.5 to take on the chain rule in general. In the meanwhile, examples will suffice to see how the chain rule plays out in the basic modeling functions.
An example of a basic modeling function is \(g(x) \equiv f(a x + b)\) where \(f()\) is one of the pattern-book functions. Naturally, we could have used any input name, for instance \(g(z) \equiv f(az + b)\) but in the following we will stick to \(x\). And while sometimes we write \(\line(x)\) as \(a x + b\), othertimes we will write an equivalent form: \(a (x - x_0)\) or even \(\frac{2\pi}{P} (x - x_0)\).
We wish to find the derivative of the basic modeling function with respect to its argument, that is, \(\partial_x g(x)\). The answer is simple:
\[\text{Given } g(x) \equiv \!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!{\color{brown}{\underbrace{f}_\text{pattern-book function}}}\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\! (a x + b), \text{ then } \partial_x g(x) = a \!\!\!\!\!\!\!\!{\color{brown}{\underbrace{f'}_{\text{That is, }\partial_x f()}}} \!\!\!\!\!\!\!\!(a x + b) \tag{23.1}\] Or, writing the input scaling \(\line(x)\) in another common form:
\[\text{Given } g(x) \equiv \!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!{\color{brown}{\underbrace{f}_\text{pattern-book function}}}\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\! \left(a (x - x_0)\right), \text{ then } \partial_x g(x) = a \!\!\!\!\!\!\!\!{\color{brown}{\underbrace{f'}_{\text{That is, }\partial_x f()}}} \!\!\!\!\!\!\!\!\left(a (x - x_0)\right) . \tag{23.2}\]
Actually, Math expression 23.1 and Math expression 23.2 apply to any function \(f()\), not just the pattern-book functions. But for now we’re interested in the pattern-book functions.
Some examples illustrate the pattern.
\(g(x)\) | \(\partial_x g(x)\) |
---|---|
\(e^{a x + b}\) | \(a e^{a x + b}\) |
\(\ln(a x + b)\) | \(a / (a x + b)\) |
\(\ln\left(a (x - x_0)\right)\) | \(a / a (x - x_0) = 1/(x-x_0)\) |
\(\sin\left(\frac{2 \pi}{P} (x - x_0)\right)\) | \(\frac{2\pi}{P}\cos\left(\frac{2 \pi}{P} (x - x_0)\right)\) |
\((a x + b)^ n\) | \(a\ n (a x + b)^{n-1}\) |
The pattern-book functions \(\dnorm()\) and \(\pnorm()\) are important exceptions here. We’ve been writing them as \(\dnorm(x, \text{mean}, \text{sd})\) and \(\pnorm(x, \text{mean}, \text{sd})\). This style of notation is important, rather than, say \((x -\text{mean})/\text{sd}\) because \(\dnorm()\) and \(\pnorm()\) use a statistics convention that makes \(\pnorm()\) range from 0 to 1 and, correspondingly, \(\dnorm()\) have unit “area under the curve.”
The consequence is that the simple chain-rule pattern does not apply. Instead, the derivatives are:
- \(\partial_x \pnorm(x, \text{mean}, \text{sd}) = \dnorm(x, \text{mean}, \text{sd})\), as might be expected, but …
- \(\partial_x \dnorm(x, \text{mean}, \text{sd}) = (x - \text{mean}) \dnorm(x, \text{mean}, \text{sd}) / \text{sd}^2\).
23.2 Using the rules
When you encounter a function that you want to differentiate, you first have to examine the function to decide which rule you want to apply. In the following, we will to use the names \(f()\) and \(g()\), but in practice the functions will often be basic modeling functions, for instance \(e^{kx}\) or \(\sin\left(\frac{2\pi}{P}t\right)\), etc.
Step 1: Identify f() and g()
We will write the rules using two function pronouns, \(f()\) and \(g()\), which can stand for any functions whatsoever. It is rare to see the product or the composition written explicitly as \(f(x)g(x)\) of \(f(g(x))\). Instead, you are given something like \(e^x \ln(x)\). The first step in differentiating the product or composition is to identify what are \(f()\) and \(g()\) individually.
In general, \(f()\) and \(g()\) might be complicated functions, themselves involving linear combinations, products, and composition. But to get started, we will practice with cases where they are simple, pattern-book functions.
Step 2: Find f’() and g’()
For differentiating either products or compositions, you will need to identify both \(f()\) and \(g()\) (the first step) and then compute the derivatives \(\partial_x f()\) and \(\partial_x g()\). That is, you will write down four functions.
Step 3: Apply the relevant rule
Recall from Chapter 9 that will will be working with three important forms for creating new functions out of existing functions:
- Linear combinations, e.g. \(a f(x) + bg(x)\)
- Products of functions, e.g. \(f(x) g(x)\)
- Compositions of functions, e.g. \(f\left(g(x)\right)\)
23.3 Differentiating linear combinations
Linear combination is one of the ways in which we make new functions from existing functions. As you recall, linear combination involves scaling functions and then adding the scaled functions as in \(a f(x) + b g(x)\): a linear combination of \(f(x)\) and \(g(x)\). We can easily use \(h\)-theory to show what is the result of differentiating a linear combination of functions. First, let’s figure out what is \(\partial_x\, a f(x)\), Going back to writing \(\partial_x\) as a slope function: \[\partial_x\, a\,f(x) = \frac{a\, f(x + h) - a\,f(x)}{h}\\ \ \\ = a \frac{f(x+h) - f(x)}{h} = a\, \partial_x f(x)\] In other words, if we know the derivative \(\partial_x\, f(x)\), we can easily find the derivative of \(a\, f()\). Notice that even though \(h\) was used in the derivation, it appears nowhere in the result \(\partial_x\, b\,f(x) = b\, \partial_x\, f(x)\). The \(h\) is solvent to get the paint on the wall and evaporates once its job is done.
Now consider the derivative of the sum of two functions, \(f(x)\) and \(g(x)\): \[\begin{eqnarray} \partial_x\, \left(f(x) + g(x)\right) & =\frac{\left(f(x + h) + g(x + h)\right) - \left(f(x) + g(x)\right)}{h} \\ \ \\ &= \frac{\left(f(x+h) -f(x)\right) + \left(g(x+h) - g(x)\right)}{h}\\ \ \\ &= \frac{\left(f(x+h) -f(x)\right)}{h} + \frac{\left(g(x+h) - g(x)\right)}{h}\\ \ \\ &= \partial_x\, f(x) + \partial_x\, g(x) \end{eqnarray}\]
Because of how \(\partial_x\) can be “passed through” a linear combination, mathematicians say that differentiation is a linear operator. Consider this new fact about differentiation as a down payment on what will eventually become a complete theory telling us how to differentiate a product of two functions or the composition of two functions. We will lay out the \(h\)-theory based algebra of this in the next two sections.
We can summarize the h-theory result for linear combinations this way:
The derivative of a linear combination is the linear combination of the derivatives.
That is:
\[\partial_x \left({\large\strut} \color{orange}{a} \color{black}{f(x)} + \color{orange}{b} \color{black}{g(x)}\right) = \color{orange}{a} {\color{brown}{f'(x)}} + \color{orange}{b} \color{brown}{g'(x)}\] as well as
\[\partial_x \left({\large\strut} \color{orange}{a}\, \color{black}{f(x)} + \color{orange}{b}\, \color{black}{g(x)} + \color{orange}{c}\, \color{black}{h(x)} + \cdots\right) = \color{orange}{a}\, {\color{brown}{f'(x)}} + \color{orange}{b}\, {\color{brown}{g'(x)}} + \color{orange}{c}\, {\color{brown}{h'(x)}} + \cdots\]
The derivative of a polynomial is a polynomial of a lower order. We can demonstrate this:
Consider the polynomial \[h(x) = \color{orange}{a}\color{brown}{x^0} + \color{orange}{b} \color{brown}{x^1} + \color{orange}{c} \color{brown}{x^2}\] The derivative is \[\partial_x h(x) = \color{orange}{a}\,\color{brown}{0}\, + \color{orange}{b}\,\color{brown}{1} + \color{orange}{c}\, \color{brown}{2 x} = \color{orange}{b} + \color{brown}{2}\,\color{orange}{c}\,\color{brown}{x}\ .\]
23.4 Product rule for multiplied functions
The question at hand is how to compute the derivative \(\partial_x f(x) g(x)\). Of course, you can always use numerical differentiation. But let’s look at the problem from the point of view of symbolic differentiation. And since \(f(x)\) and \(g(x)\) are just pronoun functions, we will assume you are starting out already knowing the derivatives \(\partial_x f(x)\) and \(\partial_x g(x)\).
This situation arises particularly when \(f(x)\) and \(g(x)\) are pattern-book functions for which you already have memorized \(\partial_x f(x)\) and \(\partial_x g(x)\) or are basic modeling functions whose derivatives you will memorize in Section @ref(basic-derivs).
The purpose of this section is to derive the formula for \(\partial_x f(x) g(x)\) using \(f(x)\), \(g(x)\), \(\partial_x f(x)\) and \(\partial_x g(x)\). This formula is called the product rule. The point of showing a derivation of the product rule is to let you see how the logic of evanescent \(h\) plays a role. In practice, everyone simply memorizes the rule, which has a beautiful, symmetric form:
\[\text{Product rule:}\ \ \ \ \partial_x \left(\strut f(x)g(x)\right) = \left(\strut \partial_x f(x)\right)\, g(x) + f(x)\, \left(\strut\partial_x g(x)\right)\] and is even prettier in Lagrange notation (where \(\partial_x f(x)\) is written \(f'\)): \[ \left(\strut f g\right)' = f' g + g' f\]
As with all derivatives, the product rule is based on slope function (Section 18.3). Symbolic derivatives also invoke evanescent \(h\) (Chapter 19). \[F'(x) \equiv \lim_{h\rightarrow 0} \frac{F(x+h) - F(x)}{h}\]
We also need two other statements about \(h\) and functions:
The derivative \(F'(x)\) is the slope of of \(F()\) at input \(x\). Taking a step of size \(h\) from \(x\) will induce a change of output of \(h F'(x)\), so \[F(x+h) = F(x) + h F'(x)\ .\]
Any result of the form \(h F(x)\), where \(F(x)\) is finite, gives 0. More precisely, \(\lim_{h\rightarrow 0} h F(x) = 0\).
As before, we will put the standard \(\lim_{h\rightarrow 0}\) disclaimer against dividing by \(h\) until there are no such divisions at all, at which point we can safely use the equality \(h = 0\).
Suppose the function \(F(x) \equiv f(x) g(x)\), a product of the two functions \(f(x)\) and \(g(x)\).
\[F'(x) = \partial_x \left(\strut f(x) g(x) \right) \equiv \lim_{h\rightarrow 0}\frac{f(x+h) g(x+h) - f(x) g(x)}{h}\] We will replace \(g(x + h)\) with its equivalent \(g(x) + h g'(x)\) giving
\[= \lim_{h\rightarrow 0} \frac{f(x+h) \left(\strut g(x) + h g'(x) \right) - f(x) g(x)}{h} \] \(g(x)\) appears in both terms in the numerator, once multiplied by \(f(x+h)\) and once by \(f(x)\). Collecting those terms give:
\[=\lim_{h\rightarrow 0}\frac{\left(\strut f(x+ h) - f(x)\right) g(x) + \left(\strut f(x+h) h\, g'(x)\right)}{h}\] This has two bracketed terms added together over a common denominator. Let’s split them into separate terms:
\[=\lim_{h\rightarrow 0}\underbrace{\left(\strut \frac{f(x+h) - f(x)}{h}\right)}_{f'(x)} g(x) + \lim_{h\rightarrow 0}\frac{\left(\strut f(x) + h f'(x)\right)h\,g'(x)}{h}\]
Notice that the second term has an \(h\) both in the numerator and the denominator. \(h\)-theory tells us that \(\lim_{h\rightarrow 0} h/h = 1\).
The first term is \(g(x)\) multiplied by the familiar form for the derivative of \(f(x)\) \[= f'(x) g(x) + \lim_{h\rightarrow 0}\frac{f(x) h g'(x)}{h} + \lim_{h\rightarrow 0}\frac{h f'(x) h g'(x)}{h}\] In each of the last two terms there is an \(h/h\) involved. This is safely set to 1, since the \(\lim_{h\rightarrow 0}\) implies that \(h\) will not be exactly zero. There remain no divisions by \(h\) so we can drop the \(\lim_{h\rightarrow 0}\) in favor of \(h=0\): \[= f'(x) g(x) + f(x) g'(x) + \cancel{h f'(x) g'(x)}\]
\[=f'(x) g(x) + g'(x) f(x)\]
The last step relies on statement (2) above.
Some people find it easier to read the rule in Lagrange shorthand, where \(f\) and \(g\) stand for \(f(x)\) and \(g(x)\) respectivly, and \(f'\) (“f-prime”) and \(g'\) (“g-prime”) stand for \(\partial f()\) and \(\partial g()\).
\[\text{Lagrange shorthand:}\ \ \partial(\color{magenta}f \times \color{brown}g) = (\color{magenta}f \times \color{brown}g)' = \color{magenta}{f'}\color{brown}g + \color{brown}{g'}\color{magenta}f\]
Occasionally, mathematics gives us a situation where being more general produces simplicity.
In the case of function products, the generalization is from products of two functions \(f(x)\cdot g(x)\) to products of more than two functions, e.g. \(u(x) \cdot v(x) \cdot w(x)\).
The chain rule here takes a form that makes the overall structure much clearer:
\[\begin{eqnarray} \partial_x \left(\strut u(x) \cdot v(x) \cdot w(x)\right) = \ \ \ \ \ \ \ \ \ \ \ \ \\ \color{blue}{\partial_x u(x)} \cdot v(x) \cdot w(x)\ + \\ u(x) \cdot \color{blue}{\partial_x v(x)} \cdot w(x)\ + \\ u(x) \cdot v(x) \cdot \color{blue}{\partial_x w(x)}\ \ \ \ \end{eqnarray}\]\end{eqnarray}
In the Lagrange shorthand, the pattern is even more evident: \[\left( u\cdot v\cdot w\right)' = \color{blue}{u'}\cdot v\cdot w\ +\ u\cdot \color{blue}{v'}\cdot w\ +\ u\cdot v\cdot \color{blue}{w}'\]
As an example, consider the derivative of \(x^3\) with respect to \(x\). Obviously, \(x^3 \equiv x \cdot x \cdot x\), a product of three simple functions:
\[\begin{array}{ccc}\partial_x x^3 = \partial_x [x \cdot x \cdot x]\ \ \ & = \\ & [\partial_x x] &x&x& +\\ & x & [\partial_x x] & x& +\\ &x & x &[\partial_x x] \end{array}\]Since \(\partial_x x = 1\) this collapses to \(\partial_x x^3 = 3 x^2\).
23.5 Chain rule for function composition
A function composition, as described in Section 9.2, involves inserting the output of one function (the “interior function”) as the input of the other function (the “exterior function”). As we so often do, we will be using pronouns a lot. A list might help keep things straight:
- There are two functions involved in a composition. Generically, we call them \(f(y)\) and \(g(x)\). In the composition \(f(g(x))\), the exterior function is \(f()\) and the interior function is \(g()\).
- Each of the two functions \(f()\) and \(g()\) has an input. In our examples, we use \(y\) to stand for the input to the exterior function and \(x\) for the input to the interior function.
- As with all rules for differentiation, we will need to compute the derivatives of the functions involved, each with respect to its own input. So these will be \(\partial_y f(y)\) and \(\partial_x g(x)\).
A reason to use different pronouns for the inputs to \(f()\) and \(g()\) is to remind us that the output \(g(x)\) is in general not the same kind of quantity as the input \(x\). In a function composition, the \(f()\) function will take the output \(g(x)\) as input. But since \(g(x)\) is not necessarily the same kind of thing as \(x\), why would we want to use the same name for the input to \(f()\) as we use for the input to \(g()\).
With this distinction between the names of the inputs, we can be even more explicit about the composition, writing \(f(y=g(x))\) instead of \(f(g(x))\). Had we used the pronound \(x\) for the input to \(f()\) but our explicit statement, although technically correct, would be confusing: \(f(x = g(x))\)!
With all these pronouns in mind, here is the chain rule for the derivative \(\partial_x f(g(x))\):
\[\partial_x \left({\large\strut} f\left(\strut{\color{magenta}{g(x)}}\right)\right) =\color{brown}{\partial_y f({\color{magenta}{g(x)}})} \times \color{brown}{\partial_x g(x)}\]
Or, using the Lagrange prime notation, where \('\) stands for the derivative of a function with respect to its input, we have \[\text{Lagrange shorthand:}\ \ \left(\color{black}f({\color{magenta}g)}\right)' = \color{brown}{f'} (\color{magenta}{g}) \times \color{blue}{g}'\]
The chain rule can be used in a clever way to find a formula for \(\partial_x \ln(x)\).
We’ve already seen that the logarithm is the inverse function to the exponential, and vice versa. That is: \[e^{\ln(y)} = y \ \ \ \text{and}\ \ \ \ln(e^y) = x\] Since \(\ln(e^y)\) is the same function as \(y\), the derivative \(\partial_y \ln(e^y) = \partial_y y = 1\).
Let’s differentiate the second form using the chain rule: \[\partial_y \ln(e^y) = \left(\partial_y \ln\right)(e^y)\, e^x = 1\] giving \[\left(\partial_y \ln\right](e^y) = \frac{1}{e^y} = \recip(e^y)\] Whatever the function \(\partial_x \ln()\) might be, it takes its input and produces as output the reciprocal of that input. In other words: \[\partial_x \ln(x) = \frac{1}{x}\ .\]
The derivation of the chain rule relies on two closely related statements which are expressions of the idea that near any value \(x\) a function can be expressed as a linear approximation with the slope equal to the derivative of the function :
- \(g(x + h) = g(x) + h g'(x)\)
- \(f(y + \epsilon) = f(y) + \epsilon f'(y)\), which is the same thing as (1) but uses \(y\) as the argument name and \(\epsilon\) to stand for the small quantity we usually write with an \(h\).
We will now look at \(\partial_x f\left({\large\strut} g(x)\right)\) by writing down the fundamental definition of the derivative. This, of course, involves the disclaimer \(\lim_{h\rightarrow 0}\) until we are sure that there is no division by \(h\) involved.
\[\partial_x \left({\large\strut} f\left(\strut\strut g(x)\right)\right) \equiv \lim_{h\rightarrow 0}\frac{\color{magenta}{f(g(x+h))} - f(g(x))}{h}\]
Let’s examine closely the expression \(\color{magenta}{f\left(\strut g(x+h)\right)}\). Applying rule (1) above turns it into \[\lim_{h\rightarrow 0} f\left(\strut g(x) + \color{blue}{h g'(x)}\right)\] Now apply rule (2) but substituting in \(g(x)\) for \(y\) and \(\color{blue}{h g'(x)}\) for \(\epsilon\), giving
\[\lim_{h\rightarrow 0} \color{magenta}{f\left(\strut g(x+h)\right)} = \lim_{h\rightarrow 0} \color{brown}{\left({\large\strut} f\left(g(x)\right) + \color{blue}{h g'(x)}f'\left(g(x)\right)\right)}\] We will substitute the \(\color{blue}{blue}\) and \(\color{brown}{brown}\) expression for the \(\color{magenta}{magenta}\) expression in \[\partial_x f\left(\strut g(x)\right) \equiv \lim_{h\rightarrow 0}\frac{\color{magenta}{f(g(x+h))} - f(g(x))}{h}\] giving \[\partial_x f\left(\strut g(x)\right) \equiv \lim_{h\rightarrow 0}\frac{\color{brown}{f\left(g(x)\right) + \color{blue}{h g'(x)}f'\left(g(x)\right)} - f\left(g(x)\right)}{h}\] In the denominator, \(f\left(g(x)\right)\) appears twice and cancels itself out. That leaves a single term with an \(h\) in the numerator and an \(h\) in the denominator. Those \(h\)’s cancel out, at the same time obviating the need for \(\lim_{h\rightarrow 0}\) and leaving us with the chain rule: \[\partial_x f\left(\strut g(x)\right) \equiv \lim_{h\rightarrow 0}\frac{\color{brown}{ \color{blue}{h g'(x)} f'\left(g(x)\right)}}{h} = f'\left(g(x)\right)\ g'(x)\]