# Math 141, Lesson 30: Product and Quotient Rules I

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

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

Reading: MMAC Ch 4.4, pp 407-416, and Ch 4.3, pp 400-401

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

## Objectives

1. Use the derivative at a given input to obtain a linear approximation of the function there.
2. Use basic derivative rules plus the product and quotient rules to find derivatives by hand.
3. In R, use `D()` to find the derivative of a function.

## 1. Linear Approximation

*Objective.* Use the derivative at a given input to obtain a linear approximation of the function there.

**Definition 1** (Linear approximation).

For a function $f(x)$ differentiable at $x = a$, the *linear approximation* of $f(x)$ when $x = a$ is the tangent line to $f(x)$ when $x = a$, given by

$$L(x) = f(a) + f'(a)(x - a).$$

The line through $(4, 2)$ and $(9, 3)$, two points on the graph of $f(x) = \sqrt{x}$, is not the linear approximation of $\sqrt{x}$ when $x = 4$, because the definition requires the tangent line. This line is a secant line with slope $(3 - 2)/(9 - 4) = 0.2$, while the tangent line has slope $f'(4) = \tfrac{1}{2} \cdot 4^{-1/2} = 0.25$ (Example 1).

For inputs near $a$, the value $L(x)$ is close to $f(x)$, because $L$ has the same output $f(a)$ and the same rate of change $f'(a)$ as $f$ at $x = a$. The difference $f(x) - L(x)$ is the *error* of the approximation. Farther from $a$, the graph of $f$ can bend away from the line, and the error can grow.

### Example 1

Find the linear approximation of

$$f(x) = \sqrt{x}$$

at $a = 4$. Estimate $\sqrt{4.1}$ and $\sqrt{16}$ with the linear approximation.

**Solution.**

By the definition of the linear approximation, $L$ is the tangent line to $f$ when $x = 4$. Substitute $a = 4$ into the formula for the linear approximation:

$$L(x) = f(4) + f'(4)(x - 4).$$

Rewrite $\sqrt{x}$ as $x^{1/2}$. The power rule gives $f'(x) = \tfrac{1}{2}x^{-1/2}$. Evaluate $f$ and $f'$ at 4:

$$f(4) = \sqrt{4} = 2, \qquad f'(4) = \tfrac{1}{2} \cdot 4^{-1/2} = \tfrac{1}{2} \cdot \tfrac{1}{2} = 0.25.$$

Substitute both values into $L(x)$:

$$L(x) = 2 + 0.25(x - 4).$$

Estimate $\sqrt{4.1}$ with $L$ at $x = 4.1$:

$$L(4.1) = 2 + 0.25(4.1 - 4) = 2 + 0.25(0.1) = 2.025.$$

For comparison, R computes the true value with `sqrt(4.1)`: $\sqrt{4.1} \approx 2.024846$. The error $f(4.1) - L(4.1) \approx 2.024846 - 2.025 = -0.000154$ is small, because 4.1 is close to $a = 4$.

Estimate $\sqrt{16}$ with $L$ at $x = 16$:

$$L(16) = 2 + 0.25(16 - 4) = 2 + 0.25(12) = 5.$$

The true value is $\sqrt{16} = 4$. The error $f(16) - L(16) = 4 - 5 = -1$ is far larger in size, because 16 is far from $a = 4$. On the graph, the red line $L$ touches the curve at $(4, 2)$ and lies close to the curve for inputs near $x = 4$. At $x = 16$ the line lies above the curve.

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

### Example 2

In R, find the linear approximation of

$$f(x) = x^3 - 5x$$

at $a = 1$. Estimate $f(1.1)$ with the linear approximation, compare the estimate with $f(1.1)$, and plot $f$ with $L$.

**Solution.**

Build the linear approximation $L(x) = f(1) + f'(1)(x - 1)$ as in Example 1, with $f(1)$ and $f'(1)$ from R. Then compare $L(1.1)$ with $f(1.1)$, and plot $f$ with $L$. Used R.

```r
f = makeFun(x^3 - 5*x ~ x)
df = D(f(x) ~ x)
f(1)
#> [1] -4
df(1)
#> [1] -2
L = makeFun(f(1) + df(1)*(x - 1) ~ x)
L(1.1)
#> [1] -4.2
f(1.1)
#> [1] -4.169
plotFun(f(x) ~ x, xlim = range(-3, 3))
plotFun(L(x) ~ x, add = TRUE, col = "red")
```

The code stores the derivative $f'$ under the name `df`. R prints $f(1) = -4$ and $f'(1) = -2$. The linear approximation is therefore $L(x) = -4 - 2(x - 1)$. The command that defines `L` is the formula $L(x) = f(1) + f'(1)(x - 1)$ written in R. The error of the estimate is $f(1.1) - L(1.1) = -4.169 - (-4.2) = 0.031$. In the plot, the red line $L$ touches the black curve $f$ at $(1, f(1)) = (1, -4)$ with slope $f'(1) = -2$.

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

### Example 3

For the function

$$g(x) = \frac{x^2\sin(x)}{x + 1},$$

find $g(2)$ and $g'(2)$ in R. Write the linear approximation of $g$ at $a = 2$, and estimate $g(1.9)$ with it.

**Solution.**

Compute $g(2)$ from `g` and $g'(2)$ from the derivative that `D()` returns, build $L$ as in Example 2, and evaluate $L(1.9)$. Used R.

```r
g = makeFun(x^2*sin(x)/(x + 1) ~ x)
dg = D(g(x) ~ x)
g(2)
#> [1] 1.212397
dg(2)
#> [1] 0.2534019
L = makeFun(g(2) + dg(2)*(x - 2) ~ x)
L(1.9)
#> [1] 1.187056
```

To three decimals, $g(2) \approx 1.212$ and $g'(2) \approx 0.253$. The linear approximation is

$$L(x) = g(2) + g'(2)(x - 2) \approx 1.212 + 0.253(x - 2),$$

and the estimate is $g(1.9) \approx L(1.9) \approx 1.187$.

## 2. Product and Quotient Rules

*Objective.* Use basic derivative rules plus the product and quotient rules to find derivatives by hand.

To differentiate a product of two functions, use the product rule below. For $x^2 \cdot x^3$, combining the factors into one power first gives

$$\dfrac{d}{dx}\left[x^2 \cdot x^3\right] = \dfrac{d}{dx}\left[x^5\right] = 5x^4,$$

and the product rule gives the same derivative without combining:

$$\dfrac{d}{dx}\left[x^2 \cdot x^3\right] = x^3 \cdot 2x + x^2 \cdot 3x^2 = 2x^4 + 3x^4 = 5x^4.$$

**Rule 2** (Product rule).

Let $f(x)$ and $g(x)$ be differentiable functions. Then

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

**Rule 3** (Quotient rule).

Let $f(x)$ and $g(x)$ be differentiable functions. When $g(x) \neq 0$, the following holds:

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

*The rules in words.* The derivative of a product is the sum of two terms, each the derivative of one factor times the other factor. The derivative of a quotient is the denominator times the derivative of the numerator, minus the numerator times the derivative of the denominator, all divided by the square of the denominator.

*Order in the quotient rule.* In the quotient rule, the numerator is a difference. Reversing its two terms gives the negative of the derivative.

*Squared sine notation.* The expression $\sin^2(x)$ means $(\sin(x))^2$.

### Example 4

Differentiate

$$f(x) = 3x^2e^x.$$

**Solution.**

By the product rule, the derivative of a product of two differentiable factors $u(x)$ and $v(x)$ is

$$\dfrac{d}{dx}\left[u(x) \cdot v(x)\right] = v(x) \cdot u'(x) + u(x) \cdot v'(x).$$

Here the factors are $u(x) = 3x^2$, with derivative $6x$, and $v(x) = e^x$, with derivative $e^x$. Substitute them into the product rule:

$$f'(x) = e^x \cdot 6x + 3x^2 \cdot e^x = 6xe^x + 3x^2e^x.$$

### Example 5

Differentiate

$$w(t) = \sqrt{t}\,\sin(t).$$

**Solution.**

The two factors are $\sqrt{t} = t^{1/2}$, with derivative $\tfrac{1}{2}t^{-1/2}$ by the power rule, and $\sin(t)$, with derivative $\cos(t)$. By the product rule,

$$w'(t) = \sin(t) \cdot \tfrac{1}{2}t^{-1/2} + t^{1/2}\cos(t)
= \frac{\sin(t)}{2\sqrt{t}} + \sqrt{t}\cos(t)$$

for every $t$ with $t > 0$, where both factors are differentiable.

### Example 6

Differentiate

$$f(x) = \frac{\sin(x)}{x^2}.$$

**Solution.**

By the quotient rule, the derivative of a quotient of a differentiable $u(x)$ by a differentiable $v(x)$, at each input $x$ with $v(x) \neq 0$, is

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

Here the numerator is $u(x) = \sin(x)$, with derivative $\cos(x)$, and the denominator is $v(x) = x^2$, with derivative $2x$. Substitute them into the quotient rule, then divide the numerator and the denominator by $x$:

$$f'(x) = \frac{x^2\cos(x) - \sin(x) \cdot 2x}{(x^2)^2} = \frac{x^2\cos(x) - 2x\sin(x)}{x^4}
= \frac{x\cos(x) - 2\sin(x)}{x^3}$$

for every $x$ with $x \neq 0$.

### Example 7

Differentiate

$$r(x) = \frac{x^2}{x + 1}.$$

**Solution.**

The numerator is $x^2$, with derivative $2x$, and the denominator is $x + 1$, with derivative 1. By the quotient rule,

$$r'(x) = \frac{(x + 1) \cdot 2x - x^2 \cdot 1}{(x + 1)^2} = \frac{2x^2 + 2x - x^2}{(x + 1)^2}
= \frac{x^2 + 2x}{(x + 1)^2}$$

for every $x$ with $x \neq -1$.

### Example 8

Differentiate

$$z(x) = \frac{\cos(x)}{\sin(x)}.$$

**Solution.**

The numerator is $\cos(x)$, with derivative $-\sin(x)$, and the denominator is $\sin(x)$, with derivative $\cos(x)$. By the quotient rule,

$$z'(x) = \frac{\sin(x)(-\sin(x)) - \cos(x)\cos(x)}{\sin^2(x)}
= \frac{-\left(\sin^2(x) + \cos^2(x)\right)}{\sin^2(x)} = -\frac{1}{\sin^2(x)}$$

for every $x$ with $\sin(x) \neq 0$, because $\sin^2(x) + \cos^2(x) = 1$ for every input $x$.

### Example 9

Differentiate

$$y(x) = \frac{x^2\cos(x)}{\ln(x)}.$$

**Solution.**

The numerator $x^2\cos(x)$ is a product. By the product rule, its derivative is

$$\dfrac{d}{dx}\left[x^2\cos(x)\right] = \cos(x) \cdot 2x + x^2 \cdot (-\sin(x)) = 2x\cos(x) - x^2\sin(x).$$

The denominator is $\ln(x)$, with derivative $\frac{1}{x}$. By the quotient rule,

$$y'(x) = \frac{\ln(x)\left(2x\cos(x) - x^2\sin(x)\right) - x^2\cos(x) \cdot \frac{1}{x}}{[\ln(x)]^2}
= \frac{\ln(x)\left(2x\cos(x) - x^2\sin(x)\right) - x\cos(x)}{[\ln(x)]^2}$$

for every $x$ with $x > 0$ and $x \neq 1$.

### Example 10

Determine which of these functions are differentiated with the product rule, which with the quotient rule, and which with only the basic derivative rules of [Lesson 29](https://tangentspace.org/lessons/29/):

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

**Solution.**

- Differentiate $x^3e^x$ and $(x^2 + 1)\ln(x)$ with the product rule, because each is a product of two non-constant factors, and no algebra rewrites it as a linear combination of power functions and basic modeling functions ([Lesson 29](https://tangentspace.org/lessons/29/)).
- Differentiate $\frac{e^x}{x}$ with the quotient rule, because its denominator is not constant, and no algebra rewrites the quotient as a linear combination of power functions and basic modeling functions.
- Differentiate $4\sin(x)$ with only the basic derivative rules, because the function is the constant 4 times $\sin(x)$. Its derivative is $4\cos(x)$.
- Differentiate $\frac{x^2 + 1}{3}$ with only the basic derivative rules, because the function is the constant $\frac{1}{3}$ times $x^2 + 1$. Its derivative is $\frac{2x}{3}$.
- Differentiate $\frac{x^5}{x^2}$ with only the basic derivative rules, because the function simplifies to $x^3$. Its derivative is $3x^2$. The simplified form $x^3$ equals $\frac{x^5}{x^2}$ only for $x \neq 0$, the domain of $\frac{x^5}{x^2}$. The derivative $3x^2$ applies on that domain.

## 3. Derivatives in R

*Objective.* In R, use `D()` to find the derivative of a function.

The command `D()` differentiates products and quotients with the same syntax as in [Lesson 29](https://tangentspace.org/lessons/29/), `D(f(x) ~ x)`.

*Checking a hand derivative.* The derivative R prints can look different from the hand derivative and still be equal to it. To check, rewrite the printed form by algebra until it matches the hand derivative. To spot-check, evaluate both derivatives at one input: different outputs show that the two derivatives differ, and equal outputs show agreement at that input only.

*Parentheses in a quotient.* Type a quotient with parentheses around a numerator or a denominator that has more than one term, as in `(3*x - 2)/(x^2 + 5)`. Without them, `3*x - 2/(x^2 + 5)` divides only the 2, and R gives a wrong value with no error.

*Products in R.* Type every product with `*`. R rejects `3x^2`, typed in place of `3*x^2`, with an error:

```r
> 3x^2
Error: unexpected symbol in "3x"
```

### Example 11

Find the derivative of

$$f(x) = 3x^2e^x$$

in R. Check it against the hand derivative $6xe^x + 3x^2e^x$.

**Solution.**

Define $f$, differentiate it with `D()`, and print the derivative. Used R.

```r
f = makeFun(3*x^2*exp(x) ~ x)
df = D(f(x) ~ x)
df
#> function (x)
#> 3 * (x^2 * exp(x) + 2 * x * exp(x))
```

R prints the derivative as a function of $x$, with its formula on the second line. The printed form $3(x^2e^x + 2xe^x) = 3x^2e^x + 6xe^x$ matches the hand derivative. As a spot check at $x = 1$, the hand derivative is

$$6(1)e^1 + 3(1)^2e^1 = 6e + 3e = 9e.$$

Evaluate the R derivative and $9e$. Used R.

```r
df(1)
#> [1] 24.46454
9*exp(1)
#> [1] 24.46454
```

The two outputs are equal at $x = 1$, consistent with the algebraic match.

### Example 12

Find the derivative of

$$r(x) = \frac{x^2}{x + 1}$$

in R. Check it against the hand derivative $(x^2 + 2x)/(x + 1)^2$.

**Solution.**

Define $r$ with parentheses around the denominator, differentiate it with `D()`, and print the derivative. Used R.

```r
r = makeFun(x^2/(x + 1) ~ x)
dr = D(r(x) ~ x)
dr
#> function (x)
#> {
#> .e1 <- 1 + x
#> x * (2 - x/.e1)/.e1
#> }
```

The line `.e1 <- 1 + x` stores the denominator $x + 1$ under the name `.e1`, and the last line is the derivative written with that name:

$$\frac{x\left(2 - \dfrac{x}{x + 1}\right)}{x + 1}
= \frac{x \cdot \dfrac{2(x + 1) - x}{x + 1}}{x + 1}
= \frac{x(x + 2)}{(x + 1)^2} = \frac{x^2 + 2x}{(x + 1)^2}.$$

The printed form matches the hand derivative. As a spot check at $x = 1$, the hand derivative is

$$\frac{1^2 + 2(1)}{(1 + 1)^2} = \frac{3}{4} = 0.75.$$

Evaluate the R derivative at 1. Used R.

```r
dr(1)
#> [1] 0.75
```

The R value and the hand value are equal at $x = 1$, consistent with the algebraic match.
