# Math 141, Lesson 32: Chain Rule I

The lesson page, with interactive tools and R in the browser: https://tangentspace.org/lessons/32/

This lesson is marked "Updates coming": parts of it are still being written.

Reading: MMAC Ch 4.5, pp 425-431

Practice: Optional HW 32 **[Q?? and Q??]**.

## Objectives

1. Given two functions, find the specified composition; recognize that order matters.
2. Given a composition, identify the inside and outside functions.
3. Use the chain rule to find the derivative of a composition by hand.

## 1. Composition of Functions

*Objective.* Given two functions, find the specified composition; recognize that order matters.

**Definition 1** (Composition).

If $f(x)$ and $g(x)$ are functions, then the *composition* of $f$ with $g$ is $f \circ g(x) = f[g(x)]$ when this expression is defined.

**Definition 2** (Inside and outside functions).

In the composition $f \circ g$, the function $g$ is the *inside function*. The function $f$ is the *outside function*.

The inside function is applied first, to the input $x$. The outside function is applied to the output of $g$. The brackets in $f[g(x)]$ mark evaluation: $f[g(x)]$ means $f$ evaluated at $g(x)$, the same as $f(g(x))$.

For $f(x) = \ln(x)$ and $g(x) = -1 - x^2$, the expression $f[g(x)] = \ln(-1 - x^2)$ is undefined at every input, because every output $g(x)$ is at most $-1$ and $\ln$ is defined only for positive inputs. The expression fails the condition in the definition of composition that $f[g(x)]$ be defined.

The product $x^2\sin(x)$ is not the composition of $x^2$ and $\sin(x)$ in either order. Its last step multiplies the outputs of the two functions instead of applying one function to the output of the other. The product has no inside function and no outside function.

To find $f \circ g(x)$, replace every $x$ in the formula for $f$ with the whole formula for $g(x)$, in parentheses.

### Example 1

For

$$f(x) = x^3, \qquad g(x) = x + 1,$$

find $f \circ g(x)$ and $g \circ f(x)$, and evaluate both at $x = -2$.

**Solution.**

By the definition of composition, $f \circ g(x) = f[g(x)]$. For $f \circ g$, replace $x$ in the formula for $f$ with $x + 1$, then evaluate at $x = -2$:

$$f \circ g(x) = f[x + 1] = (x + 1)^3, \qquad f \circ g(-2) = (-2 + 1)^3 = (-1)^3 = -1.$$

For $g \circ f$, replace $x$ in the formula for $g$ with $x^3$, then evaluate at $x = -2$:

$$g \circ f(x) = g[x^3] = x^3 + 1, \qquad g \circ f(-2) = (-2)^3 + 1 = -8 + 1 = -7.$$

In the diagram, the input $-2$ passes through both compositions. In $f \circ g$ the inside function $g$ is applied first and sends $-2$ to $-1$. In $g \circ f$ the inside function $f$ is applied first and sends $-2$ to $-8$.

![Figure](https://tangentspace.org/lessons/fig/1e4e6c50cd2e.svg)

*Order of composition.* The compositions $f \circ g$ and $g \circ f$ are different functions when their outputs differ at some input, as at $x = -2$ in Example 1. The two can agree at one input and still be different functions. At $x = 0$ in Example 1, both compositions equal 1:

$$f \circ g(0) = (0 + 1)^3 = 1, \qquad g \circ f(0) = 0^3 + 1 = 1.$$

For some pairs the two orders give the same function. For $p(x) = x + 2$ and $q(x) = x + 5$,

$$p \circ q(x) = (x + 5) + 2 = x + 7, \qquad q \circ p(x) = (x + 2) + 5 = x + 7.$$

### Example 2

For

$$f(x) = x^2, \qquad g(x) = \sin(x),$$

find $f \circ g(x)$ and $g \circ f(x)$.

**Solution.**

Replacing $x$ in the formula for each outside function with the formula for the inside function gives

$$f \circ g(x) = f[\sin(x)] = (\sin(x))^2, \qquad g \circ f(x) = g[x^2] = \sin(x^2).$$

The two compositions are different functions, because the graph of $(\sin(x))^2$ stays between 0 and 1 while the graph of $\sin(x^2)$ runs from $-1$ to 1. At $x = 2$, for example, $(\sin(2))^2 \approx 0.827$ and $\sin(2^2) = \sin(4) \approx -0.757$.

![Figure](https://tangentspace.org/lessons/fig/16439abaf7c0.svg)

### Example 3

For

$$f(x) = \cos(3x + 1), \qquad g(x) = e^x - x,$$

find $f \circ g(x)$, $g \circ f(x)$, and $f(x)g(x)$.

**Solution.**

For $f \circ g$, replace each $x$ in $f$ with $e^x - x$ and distribute the 3:

$$f \circ g(x) = \cos\bigl(3(e^x - x) + 1\bigr) = \cos(3e^x - 3x + 1).$$

For $g \circ f$, replace each $x$ in $g$ with $\cos(3x + 1)$:

$$g \circ f(x) = e^{\cos(3x + 1)} - \cos(3x + 1).$$

For the product, multiply the two outputs instead of applying one function to the other:

$$f(x)g(x) = \cos(3x + 1)(e^x - x).$$

## 2. Inside and Outside Functions

*Objective.* Given a composition, identify the inside and outside functions.

*Decomposing a composition.*

1. Find the last function applied in computing the composition. Pass over a constant added, subtracted, multiplied, or divided at the very end, and keep that constant in the outside function. A constant divided by a power counts as a power in a denominator.
2. Find the row of the table for that function, and read the inside function $g(x)$ from it.
3. Replace $g(x)$ with $x$ in the composition to get the outside function $f(x)$.

| Outside function | Where $g(x)$ sits | Composition and $g(x)$ |
| --- | --- | --- |
| Trigonometric or logarithmic | in the parentheses after the function name | $\cos(5x - 2)$, $g(x) = 5x - 2$ |
| Exponential | in the exponent | $2^{\sin(x)}$, $g(x) = \sin(x)$ |
| Power or root, including a power in a denominator | in the base of the power or under the root | $\sqrt{4 + x^3}$, $g(x) = 4 + x^3$ |

In the table’s rows, step 3 gives the outside functions $\cos(x)$, $2^x$, and $\sqrt{x}$.

### Example 4

Identify the inside function $g(x)$ and the outside function $f(x)$ of each composition:

$$(x^2 - 3)^5, \qquad e^{x^2 + 3x + 1}, \qquad \sin(x^2) + 1.$$

**Solution.**

By the definition of inside and outside functions (section 1), the inside function $g$ is applied to $x$ first, and the outside function $f$ is applied to the output of $g$.

- In $(x^2 - 3)^5$, the last function applied is a power, and by the power row $g(x)$ is the base. The inside function is $g(x) = x^2 - 3$ and the outside function is $f(x) = x^5$.
- In $e^{x^2 + 3x + 1}$, the last function applied is an exponential, and by the exponential row $g(x)$ is the exponent. The inside function is $g(x) = x^2 + 3x + 1$ and the outside function is $f(x) = e^x$.
- In $\sin(x^2) + 1$, the last function applied before adding 1 is a sine, and by the trigonometric row $g(x)$ is in the parentheses. The inside function is $g(x) = x^2$ and the outside function is $f(x) = \sin(x) + 1$.

In the last item, the choice $g(x) = \sin(x^2)$ with $f(x) = x + 1$ is also correct, because $f[g(x)] = \sin(x^2) + 1$. With that choice, $g$ is itself a composition, and the chain rule of section 3 is applied a second time to find $g'(x)$.

*Products, quotients, sums, and differences.* Let the last step of a function combine the outputs of two functions of $x$, neither of them a constant. The function is a product when the last step multiplies, a quotient when it divides, and a sum or difference when it adds or subtracts. Differentiate a product with the product rule and a quotient with the quotient rule ([Lesson 30](https://tangentspace.org/lessons/30/)), and a sum or difference term by term. A factor or a term that is itself a composition is differentiated with the chain rule of section 3, as the term $\cos(x^2)$ is in $x^3 + \cos(x^2)$.

### Example 5

For each function, identify the inside and outside functions, or state that the function is a product, a quotient, or a sum:

$$(x^2 + 3)\cos(x), \qquad 5e^{x^3 - x}, \qquad \frac{1}{(x^2 + 1)^3}, \qquad e^x + x^3.$$

**Solution.**

- The function $(x^2 + 3)\cos(x)$ is a product, because its last step multiplies the outputs of two functions of $x$. Differentiate it with the product rule.
- In $5e^{x^3 - x}$, the inside function is $g(x) = x^3 - x$ and the outside function is $f(x) = 5e^x$. The constant 5, multiplied at the last step, stays in the outside function.
- The function $\dfrac{1}{(x^2 + 1)^3}$ is the constant 1 divided by a power, which counts as a power in a denominator, the last row of the table. Its inside function is $g(x) = x^2 + 1$ and its outside function is $f(x) = \dfrac{1}{x^3} = x^{-3}$.
- The function $e^x + x^3$ is a sum, because its last step adds the outputs of two functions of $x$. Differentiate it term by term.

## 3. The Chain Rule

*Objective.* Use the chain rule to find the derivative of a composition by hand.

**Rule 3** (Chain rule).

Let $f(x)$ and $g(x)$ be differentiable functions. When the compositions are defined,

$$\frac{d}{dx}\bigl[f(g(x))\bigr] = f'[g(x)] \cdot g'(x).$$

*The chain rule in words.* The derivative of a composition is the derivative of the outside function $f$, evaluated at the inside function $g(x)$, times the derivative of the inside function.

*Applying the chain rule.*

1. Name the inside function $g(x)$ and the outside function $f(x)$.
2. Differentiate each one: $g'(x)$ and $f'(x)$.
3. Evaluate $f'$ at the inside function: $f'[g(x)]$.
4. Multiply $f'[g(x)]$ by $g'(x)$.

Each row of the table is the chain rule applied to one basic outside function. In the table, $n$ is a constant and $a$ is a constant with $a > 0$. The power row is true at each $x$ where $g$ is differentiable and $[g(x)]^{n-1}$ is defined, and the $\ln$ row at each $x$ where $g$ is differentiable and $g(x) > 0$.

| Outside function | Composition | Derivative |
| --- | --- | --- |
| $x^n$ | $[g(x)]^n$ | $n[g(x)]^{n-1}\cdot g'(x)$ |
| $e^x$ | $e^{g(x)}$ | $e^{g(x)}\cdot g'(x)$ |
| $a^x$ | $a^{g(x)}$ | $\ln(a)\,a^{g(x)}\cdot g'(x)$ |
| $\ln(x)$ | $\ln(g(x))$ | $\dfrac{1}{g(x)}\cdot g'(x)$ |
| $\sin(x)$ | $\sin(g(x))$ | $\cos(g(x))\cdot g'(x)$ |
| $\cos(x)$ | $\cos(g(x))$ | $-\sin(g(x))\cdot g'(x)$ |

*The two factors of the chain rule.* The derivative of $\sin(x^2)$ is $\cos(x^2)\cdot 2x$. The inside function $x^2$ stays unchanged in the cosine, and the derivative $2x$ of the inside function multiplies the cosine as the factor $g'(x)$.

### Example 6

Find the derivative by hand:

$$h(x) = (x^2 - 3)^5.$$

**Solution.**

The function $h$ is the composition $f \circ g$ of the functions named below. By the chain rule, $h'(x) = f'[g(x)] \cdot g'(x)$. Name the inside and outside functions and their derivatives:

$$g(x) = x^2 - 3, \quad g'(x) = 2x, \qquad f(x) = x^5, \quad f'(x) = 5x^4.$$

Evaluate $f'$ at the inside function:

$$f'[g(x)] = 5(x^2 - 3)^4.$$

Multiply $f'[g(x)]$ by $g'(x)$:

$$h'(x) = 5(x^2 - 3)^4 \cdot 2x = 10x(x^2 - 3)^4.$$

### Example 7

Find the derivative by hand:

$$h(x) = \ln(18 - x^3).$$

**Solution.**

The inside function is $g(x) = 18 - x^3$, with $g'(x) = -3x^2$. The outside function is $f(x) = \ln(x)$, with $f'(x) = \frac{1}{x}$. Substitute into the chain rule:

$$h'(x) = \frac{1}{18 - x^3}\cdot(-3x^2) = \frac{-3x^2}{18 - x^3}.$$

### Example 8

Find the derivative by hand:

$$h(x) = 4e^{x^2 - 9x}.$$

**Solution.**

The inside function is $g(x) = x^2 - 9x$, with $g'(x) = 2x - 9$. The outside function is $f(x) = 4e^x$, with $f'(x) = 4e^x$. Substitute into the chain rule and distribute the 4:

$$h'(x) = 4e^{x^2 - 9x}\cdot(2x - 9) = (8x - 36)e^{x^2 - 9x}.$$

### Example 9

Find the derivative by hand:

$$h(x) = 3^{7x + \cos(x)}.$$

**Solution.**

The inside function is $g(x) = 7x + \cos(x)$, with $g'(x) = 7 - \sin(x)$. The outside function is $f(x) = 3^x$, with $f'(x) = \ln(3)\,3^x$. Substitute into the chain rule:

$$h'(x) = \ln(3)\,3^{7x + \cos(x)}\bigl(7 - \sin(x)\bigr).$$

### Example 10

Find the derivative by hand:

$$h(x) = \cos\Bigl(2\ln(x) - \frac{1}{x}\Bigr).$$

**Solution.**

Rewrite $\frac{1}{x}$ as $x^{-1}$. The inside function is $g(x) = 2\ln(x) - x^{-1}$. Differentiate the first term by the rule for $\ln(x)$ and the second term by the power rule:

$$g'(x) = \frac{2}{x} + x^{-2} = \frac{2}{x} + \frac{1}{x^2}.$$

The outside function is $f(x) = \cos(x)$, with $f'(x) = -\sin(x)$. Substitute into the chain rule:

$$h'(x) = -\sin\Bigl(2\ln(x) - \frac{1}{x}\Bigr)\Bigl(\frac{2}{x} + \frac{1}{x^2}\Bigr).$$

### Example 11

Find the slope of the tangent line to the graph of

$$h(x) = \sqrt{x^2 + 9}$$

at $x = 4$.

**Solution.**

The slope of the tangent line at $x = 4$ is $h'(4)$. Find $h'(x)$ by the chain rule, then evaluate it at $x = 4$. Rewrite the root as a power, $h(x) = (x^2 + 9)^{1/2}$. The inside function is $g(x) = x^2 + 9$, with $g'(x) = 2x$. The outside function is $f(x) = x^{1/2}$, with $f'(x) = \tfrac{1}{2}x^{-1/2}$ by the power rule. Substitute into the chain rule:

$$h'(x) = \tfrac{1}{2}(x^2 + 9)^{-1/2}\cdot 2x = \frac{x}{\sqrt{x^2 + 9}}.$$

Evaluate the derivative at $x = 4$:

$$h'(4) = \frac{4}{\sqrt{4^2 + 9}} = \frac{4}{\sqrt{25}} = \frac{4}{5} = 0.8.$$

The tangent line passes through the point $\bigl(4, h(4)\bigr) = \bigl(4, \sqrt{4^2 + 9}\bigr)  
= (4, 5)$ on the graph, and it rises 0.8 units for each unit of $x$.

![Figure](https://tangentspace.org/lessons/fig/39fec58fd530.svg)

*Checking in R.* To check a hand derivative, evaluate it and the derivative function that `D()` returns ([Lesson 30 section 3](https://tangentspace.org/lessons/30/#section-3)) at the same input. Compare values instead of formulas, because the formula that `D()` prints can look different from the hand derivative. A different value shows an error in the hand work or the R entry. Equal values support the hand derivative, and a check at a second input makes a chance agreement of two different functions unlikely. For Example 6, the hand derivative gives

$$h'(2) = 10(2)\bigl(2^2 - 3\bigr)^4 = 20(1)^4 = 20, \qquad h'(1) = 10(1)\bigl(1^2 - 3\bigr)^4 = 10(16) = 160,$$

and `D()` gives the same values:

```r
> dh = D((x^2 - 3)^5 ~ x)
> dh(2)
[1] 20
> dh(1)
[1] 160
```
