## The Chain Rule of Differentiation

The chain rule is a powerful and useful derivation technique that allows the derivation of functions that would not be straightforward or possible with the only the previously discussed rules at our disposal. The rule takes advantage of the "compositeness" of a function. For example, consider the function:

$$f(x) = \sin{4x}$$

This function can be broken into a composite function $f \circ g$ by observing that:

$$y = f(u) = \sin{u} \space \text{and} \space u = g(x) = 4x$$

Therefore we can reinterpret the original function as:

$$F(x) = f(g(x)), \space \text{or} \space F = f \circ g$$

The chain rule gives us the ability the find the derivatives of $f$ and $g$ using the tools we previously discussed. We can state the chain rule more precisely as:

Assuming $g$ is differentiable at $x$ and the derivative of $f(g(x))$ exists, then we can state the composite function $F = f \circ g$ as $F(x) = f(g(x))$ and $F^\prime$ is given by:

$$F^\prime (x) = f^\prime (g(x)) \circ g^\prime(x)$$

In Leibniz notation, assuming $y = f(u)$ and $u = g(x)$ have derivatives, the chain rule can be expressed as:

$$\frac{dy}{dx} = \frac{dy}{du} \space \frac{du}{dx}$$

With the chain rule, we can now find the derivative of the function $f(x) = \sin{4x}$.

$$u = 4x, \qquad y = \sin{u}$$
$$\frac{du}{dx} = 4, \qquad \frac{dy}{du} = \cos{u}$$
$$\frac{dy}{dx} = 4 \cos{4x}$$

We can check our answer using SymPy

In [12]:
from sympy import symbols, diff, sin, cos, sqrt, simplify, init_printing

In [4]:
init_printing()
x = symbols('x')

In [6]:
diff(sin(4 * x))

Out[6]:
$$4 \cos{\left (4 x \right )}$$

## Examples¶

These and the previous example are taken from James Stewart's Calculus Early Transcendentals (Section 3.4, pp. 203)

Example 1: Find the derivative of $f(x) = (1 - x^2)^{10}$

$$u = 1 - x^2, \qquad y = u^{10}$$
$$\frac{dy}{du} = 10u^9, \qquad \frac{du}{dx} = -2x$$
$$\frac{dy}{dx} = 10(1 - x^2)^9 * - 2x = -20x (1 - x^2)^9$$
In [7]:
diff((1 - x ** 2) ** 10)

Out[7]:
$$- 20 x \left(- x^{2} + 1\right)^{9}$$

Example 2: Find the derivative of $f(x) = e^{\sqrt{x}}$

$$y = e^{\sqrt{u}}, \qquad u = \sqrt{x}$$
$$\frac{dy}{du} = e^u, \qquad \frac{du}{dx} = \frac{1}{2}x^{-\frac{1}{2}} = \frac{1}{2\sqrt{x}}$$
$$\frac{dy}{dx} = e^{\sqrt{x}} * \frac{1}{2\sqrt{x}} = \frac{e^{\sqrt{x}}}{2\sqrt{x}}$$

Import the constant e from the mpmath library for SymPy to calculate the derivative.

In [14]:
from mpmath import e

In [13]:
diff(e ** sqrt(x))

Out[13]:
$$\frac{0.5}{\sqrt{x}} 2.71828182845905^{\sqrt{x}}$$

Example 3: Find the derivative of $f(x) = \sqrt[4]{1 + 2x + x^3}$

$$u = 1 + 2x + x^3, \qquad y = u^\frac{1}{4}$$
$$\frac{du}{dx} = 3x^2 + 2, \qquad \frac{dy}{du} = \frac{1}{4} u^{-\frac{3}{4}} = \frac{3}{4\sqrt{u}}$$
$$\frac{dy}{dx} = \frac{3x + 2}{4 (1 + 2x + x^3)^\frac{3}{4}}$$
In [16]:
diff((1 + 2 * x + x ** 3) ** (1/4))

Out[16]:
$$\frac{0.75 x^{2} + 0.5}{\left(x^{3} + 2 x + 1\right)^{0.75}}$$

Example 4: Find the derivative of $f(x) = \frac{1}{(t^4 + 1)^3}$

$$u = t^4 + 1, \qquad y = \frac{1}{u^3} = u^{-3}$$
$$\frac{du}{dt} = 4t^3, \qquad \frac{dy}{du} = u^{-3} = -3u^{-4} = -\frac{3}{u^4}$$
$$\frac{dy}{dx} = 4t^3 * -\frac{3}{(t^4 + 1)^4} = -\frac{12t^3}{(t^4 + 1)^4}$$
In [17]:
diff(1 / (x ** 4 + 1) ** 3)

Out[17]:
$$- \frac{12 x^{3}}{\left(x^{4} + 1\right)^{4}}$$

Example 5: Find the derivative of $f(x) = \cos{(a^3 + x^3)}$

$$u = a^3 + x^3, \qquad y = \cos{u}$$
$$\frac{du}{dx} = 3x^2, \qquad \frac{dy}{du} = -\sin{u}$$
$$\frac{dy}{dx} = -3x^2 \sin{a^3 + x^3}$$
In [19]:
a = symbols('a') # define a variable that we'll treat as constant


Because there is more than variable, must specify which we're interested in for SymPy to compute the derivative.

In [22]:
diff(cos(a ** 3 + x ** 3), x)

Out[22]:
$$- 3 x^{2} \sin{\left (a^{3} + x^{3} \right )}$$