# Math 141, Lesson 33: Chain Rule II

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

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

Reading: MMAC Ch 4.5, pp 432-437

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

## Objectives

1. Use basic rules, the product/quotient rules, and the chain rule to find derivatives by hand.
2. Using R, obtain and plot the derivative of a function.

## 1. Combining Derivative Rules

*Objective.* Use basic rules, the product/quotient rules, and the chain rule to find derivatives by hand.

Finding a derivative by hand combines the basic rules ([Lesson 29 sections 2](https://tangentspace.org/lessons/29/#section-2) to 4), the product and quotient rules ([Lesson 30 section 2](https://tangentspace.org/lessons/30/#section-2)), and the chain rule ([Lesson 32 section 3](https://tangentspace.org/lessons/32/#section-3)). The extended exponential rule, $\frac{d}{dx}[e^{mx + b}] = me^{mx + b}$ for constants $m$ and $b$, is one of the basic rules ([Lesson 29 section 3](https://tangentspace.org/lessons/29/#section-3)).

**Rule 1** (Chain rule for three functions).

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

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

In the composition $f(g(h(x)))$, the *middle function* is the function $g$, applied after the inside function $h$ and before the outside function $f$.

*Choosing the rules.*

1. Rewrite each root or reciprocal as a power ([Lesson 29 section 2](https://tangentspace.org/lessons/29/#section-2)).
2. Find the last operation performed when the function is evaluated at a number.
3. When the last operation is a sum or a difference, differentiate term by term.
4. When the last operation multiplies a function by a constant, keep the constant and differentiate the function (the constant multiple rule, a basic rule).
5. When the last operation multiplies or divides two functions of $x$, apply the product rule or the quotient rule.
6. When the last operation applies one function to the output of another, name the outside function and the inside function, then apply the chain rule.
7. Repeat steps 2 to 6 on each term, factor, and inside function until only basic rules remain.

### Example 1

For each function, determine which rules apply in finding its derivative. The choices are the basic rules only, the product or quotient rule with the basic rules, the chain rule with the basic rules, and the product or quotient rule and the chain rule with the basic rules.

$$(3\cos(x) - e^x)^5, \qquad (2\sin(x) + 5)(x^3 - 4), \qquad \ln(x) - \sin(x) + 4e^x,$$

$$e^{x^2\cos(3x)}, \qquad -3\sqrt{1 + x^2}, \qquad \cos(\ln(x^2 + 4)).$$

**Solution.**

- For $(3\cos(x) - e^x)^5$: the chain rule with the basic rules, because the function is a composition, a power of the difference $3\cos(x) - e^x$.
- For $(2\sin(x) + 5)(x^3 - 4)$: the product or quotient rule with the basic rules, because the function is a product of two factors, and each factor is differentiated by the basic rules.
- For $\ln(x) - \sin(x) + 4e^x$: the basic rules only, because every term is a basic modeling function of $x$ or a constant times a basic modeling function.
- For $e^{x^2\cos(3x)}$: the product or quotient rule and the chain rule with the basic rules, because the outside function $e^x$ takes the product $x^2\cos(3x)$ as input, and the factor $\cos(3x)$ is a composition.
- For $-3\sqrt{1 + x^2}$: the chain rule with the basic rules, because the function is the constant $-3$ times the composition $\sqrt{1 + x^2}$.
- For $\cos(\ln(x^2 + 4))$: the chain rule with the basic rules, because the function is a composition of three functions: the outside function $\cos(x)$, the middle function $\ln(x)$, and the inside function $x^2 + 4$.

### Example 2

Find the derivative:

$$p(x) = (3\cos(x) - e^x)^5.$$

**Solution.**

The inside function is $g(x) = 3\cos(x) - e^x$, with $g'(x) = -3\sin(x) - e^x$. The outside function is $f(x) = x^5$, with $f'(x) = 5x^4$. The chain rule, $p'(x) = f'(g(x))\cdot g'(x)$, then gives

$$p'(x) = 5(3\cos(x) - e^x)^4(-3\sin(x) - e^x).$$

### Example 3

Find the derivative:

$$s(x) = (2\sin(x) + 5)(x^3 - 4).$$

**Solution.**

The derivatives of the two factors are

$$\frac{d}{dx}[2\sin(x) + 5] = 2\cos(x), \qquad \frac{d}{dx}[x^3 - 4] = 3x^2.$$

The product rule ([Lesson 30 section 2](https://tangentspace.org/lessons/30/#section-2)) is

$$\frac{d}{dx}\left[f(x) \cdot g(x)\right] = g(x) \cdot f'(x) + f(x) \cdot g'(x).$$

With the first factor $2\sin(x) + 5$ in place of $f(x)$ and the second factor $x^3 - 4$ in place of $g(x)$,

$$s'(x) = (x^3 - 4)(2\cos(x)) + (2\sin(x) + 5)(3x^2).$$

### Example 4

Find the derivative:

$$y = 7\sin(4x)\,e^{-2x}.$$

**Solution.**

The function is the product of $7\sin(4x)$ and $e^{-2x}$. Differentiate each factor, then apply the product rule. The first factor is differentiated by the chain rule, with inside function $4x$ and outside function $7\sin(x)$. The second factor is differentiated by the extended exponential rule:

$$\frac{d}{dx}[7\sin(4x)] = 7\cos(4x)\cdot 4 = 28\cos(4x), \qquad \frac{d}{dx}[e^{-2x}] = -2e^{-2x}.$$

Substitute both factors and their derivatives into the product rule, and factor out $14e^{-2x}$:

$$y' = 28\cos(4x)\,e^{-2x} + 7\sin(4x)\big({-}2e^{-2x}\big)
= 28\cos(4x)\,e^{-2x} - 14\sin(4x)\,e^{-2x} = 14e^{-2x}\big(2\cos(4x) - \sin(4x)\big).$$

### Example 5

Find the derivative:

$$q(x) = \frac{\cos(x^2)}{e^{2x}}.$$

**Solution.**

The function is a quotient. Differentiate the numerator and the denominator, then apply the quotient rule. The numerator is differentiated by the chain rule, with inside function $x^2$ and outside function $\cos(x)$. The denominator is differentiated by the extended exponential rule:

$$\frac{d}{dx}[\cos(x^2)] = -\sin(x^2)\cdot 2x = -2x\sin(x^2), \qquad \frac{d}{dx}[e^{2x}] = 2e^{2x}.$$

The quotient rule ([Lesson 30 section 2](https://tangentspace.org/lessons/30/#section-2)) is

$$\frac{d}{dx}\left[\frac{f(x)}{g(x)}\right] = \frac{g(x) \cdot f'(x) - f(x) \cdot g'(x)}{[g(x)]^2}.$$

Substitute the numerator $\cos(x^2)$ for $f(x)$ and the denominator $e^{2x}$ for $g(x)$, then cancel the factor $e^{2x}$ common to every term:

$$q'(x) = \frac{e^{2x}\big({-}2x\sin(x^2)\big) - \cos(x^2)\cdot 2e^{2x}}{(e^{2x})^2}
= \frac{-2x\sin(x^2) - 2\cos(x^2)}{e^{2x}}
= -\frac{2x\sin(x^2) + 2\cos(x^2)}{e^{2x}}.$$

### Example 6

Find the derivative:

$$r(x) = -6\sqrt[3]{x^2 - 4x + 9}.$$

**Solution.**

Rewrite the cube root as a power:

$$r(x) = -6(x^2 - 4x + 9)^{1/3}.$$

The inside function is $g(x) = x^2 - 4x + 9$, with $g'(x) = 2x - 4$. The outside function is $f(x) = -6x^{1/3}$, with

$$f'(x) = -6 \cdot \frac{1}{3}x^{-2/3} = -2x^{-2/3}.$$

The constant $-6$ stays in the outside function, as in section 2 of the [Lesson 32](https://tangentspace.org/lessons/32/) Notes. Substitute into the chain rule, distribute $-2$, and move the negative power to the denominator:

$$r'(x) = -2(x^2 - 4x + 9)^{-2/3}(2x - 4) = \frac{-4x + 8}{(x^2 - 4x + 9)^{2/3}}.$$

### Example 7

Find the derivative:

$$k(x) = \cos\big((3\ln(x) + 2)^4\big).$$

**Solution.**

The inside function is $h(x) = 3\ln(x) + 2$, with $h'(x) = \frac{3}{x}$. The middle function is $g(x) = x^4$, with $g'(x) = 4x^3$. The outside function is $f(x) = \cos(x)$, with $f'(x) = -\sin(x)$. In the diagram, the arrows run from $x$ through $h$, $g$, and $f$ in turn. Under each function is its factor in the derivative.

![Figure](https://tangentspace.org/lessons/fig/74b237222d6b.svg)

By the chain rule for three functions, $k'(x)$ is the product of the three factors:

$$k'(x) = f'(g(h(x)))\cdot g'(h(x))\cdot h'(x),$$

which gives

$$k'(x) = -\sin\big((3\ln(x) + 2)^4\big)\cdot 4(3\ln(x) + 2)^3\cdot\frac{3}{x}
= -\frac{12}{x}(3\ln(x) + 2)^3\sin\big((3\ln(x) + 2)^4\big).$$

### Example 8

Find the derivative:

$$w(x) = e^{x^2\cos(3x)}.$$

**Solution.**

The inside function is the product $g(x) = x^2\cos(3x)$. Find $g'$ by the product rule, then substitute $g'$ into the chain rule for $e^{g(x)}$. The factor $x^2$ has derivative $2x$. The factor $\cos(3x)$ is itself a composition, with derivative $-\sin(3x)\cdot 3 = -3\sin(3x)$ by the chain rule. By the product rule,

$$g'(x) = 2x\cos(3x) + x^2\big({-}3\sin(3x)\big) = 2x\cos(3x) - 3x^2\sin(3x).$$

The outside function is $f(x) = e^x$, with $f'(x) = e^x$. Substitute into the chain rule:

$$w'(x) = e^{x^2\cos(3x)}\big(2x\cos(3x) - 3x^2\sin(3x)\big).$$

## 2. Derivatives in R

*Objective.* Using R, obtain and plot the derivative of a function.

The commands `D()` and `plotFun()` work as in section 2 of the [Lesson 31](https://tangentspace.org/lessons/31/) Notes. In a model whose input is $M$, the formula in `D()` ends in `~ M`, the same input variable as in `makeFun()`.

To check a hand derivative from section 1, evaluate it and the `D()` result ([Lesson 30 section 3](https://tangentspace.org/lessons/30/#section-3)) at two inputs. Matching values support the hand derivative.

### Example 9

The average maximum temperature, in degrees Fahrenheit, in central Kentucky at the beginning of each month $M$ since January 2006 (month 0) is modeled by

$$\text{Temp}(M) = 23.3\sin\!\left[\frac{2\pi}{11.98}(M - 3.16)\right] + 65.4.$$

Evaluate $\text{Temp}(28)$ and $\text{Temp}'(28)$ in R, and interpret each value.

**Solution.**

Define Temp and differentiate it with `D()` to get `dTemp`, R’s name for $\text{Temp}'$. Evaluate both at month 28, and evaluate Temp at months 27 and 29. Used R.

```r
Temp = makeFun(23.3*sin(2*pi/11.98*(M - 3.16)) + 65.4 ~ M)
dTemp = D(Temp(M) ~ M)
Temp(28)
#> [1] 75.77605
dTemp(28)
#> [1] 10.94161
Temp(27)
#> [1] 63.93454
Temp(29)
#> [1] 84.82823
```

Month 28 is May 2008, since month 24 is January 2008. At the beginning of May 2008, the model gives an average maximum temperature in central Kentucky of about 75.776 degrees Fahrenheit: $\text{Temp}(28) \approx 75.776$.

At month 28, the model’s average maximum temperature is increasing at about 10.942 degrees Fahrenheit per month: $\text{Temp}'(28) \approx 10.942$.

As a check, the rate lies between the model’s changes over the months before and after month 28:

$$\text{Temp}(28) - \text{Temp}(27) \approx 11.842 > 10.942 > 9.052 \approx \text{Temp}(29) - \text{Temp}(28).$$

It should, because $\text{Temp}'$ falls toward zero on the approach to the next peak.

### Example 10

In R, plot the function

$$\text{Temp}(M) = 23.3\sin\!\left[\frac{2\pi}{11.98}(M - 3.16)\right] + 65.4$$

and its derivative $\text{Temp}'$ on $0 \le M \le 60$.

**Solution.**

Define Temp and differentiate it with `D()` to get `dTemp`, R’s name for $\text{Temp}'$. Plot each on $0 \le M \le 60$. Used R.

| `Temp = makeFun(23.3*sin(2*pi/11.98*(M - 3.16))` |
| --- |
| ` + 65.4 ~ M)` |
| `dTemp = D(Temp(M) ~ M)` |
| `plotFun(Temp(M) ~ M, xlim = range(0, 60))` |
| `plotFun(dTemp(M) ~ M, xlim = range(0, 60))` |

The derivative $\text{Temp}'$ is zero at each peak and trough of Temp, because the tangent line to the graph of Temp is horizontal at each peak and trough. Sine reaches its maximum, 1, when its input is $\pi/2$. The first peak of Temp with $M \ge 0$ comes where

$$\frac{2\pi}{11.98}(M - 3.16) = \frac{\pi}{2}.$$

Multiply both sides by $\frac{11.98}{2\pi}$, then add 3.16:

$$M - 3.16 = \frac{11.98}{4}, \qquad M = 3.16 + 2.995 = 6.155.$$

The input $\pi/2 - 2\pi$ gives $M = 6.155 - 11.98 < 0$. The peak value of Temp is $23.3(1) + 65.4 = 88.7$. Each of R’s two plots appears here with a dashed line and a marked point added. The dashed line marks $M = 6.155$ on both graphs: Temp is at its peak, and $\text{Temp}'$ is zero. The derivative $\text{Temp}'$ is positive just to the left of the line, where Temp rises, and negative just to the right, where Temp falls.

![Figure](https://tangentspace.org/lessons/fig/22c342c97352.svg)

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

### Example 11

The quotient rule and the chain rule give the hand derivative $q'$ of the function $q$:

$$q(x) = \frac{\cos(x^2)}{e^{2x}}, \qquad q'(x) = -\frac{2x\sin(x^2) + 2\cos(x^2)}{e^{2x}}.$$

Check this derivative against `D()` at $x = 0$ and at $x = 1$.

**Solution.**

Evaluate the hand derivative at each input, using $\sin(1) \approx 0.841471$, $\cos(1) \approx 0.540302$, and $e^2 \approx 7.389056$, with sine and cosine in radians.

$$\begin{align*}
q'(0) &= -\frac{2(0)\sin(0^2) + 2\cos(0^2)}{e^{2(0)}} = -\frac{0 + 2}{1} = -2,\\
q'(1) &= -\frac{2(1)\sin(1^2) + 2\cos(1^2)}{e^{2(1)}} \approx -\frac{2(0.841471) + 2(0.540302)}{7.389056}
= -\frac{2.763546}{7.389056} \approx -0.374.
\end{align*}$$

Define $q$ and differentiate it with `D()` to get `dq`, R’s name for $q'$. Evaluate `dq` at the same inputs. Used R.

```r
q = makeFun(cos(x^2) / exp(2*x) ~ x)
dq = D(q(x) ~ x)
dq(0)
#> [1] -2
dq(1)
#> [1] -0.3740054
```

At $x = 0$, R gives the hand value $-2$ exactly. At $x = 1$, R’s value rounds to the hand value $-0.374$. The agreement at both inputs supports the hand derivative.
