Tutorials › Calculus III › Repeated Chain Rules for Nested Compositions

Chain Rule and Implicit Differentiation · Tutorial 357 of 999

Repeated Chain Rules for Nested Compositions

Apply the chain rule through three nested stages, keeping track of the inputs, factors, and Jacobian order at every step.

Intermediate 9 min read

What You'll Learn

  • Write the derivative of a three-stage scalar composition with every factor evaluated at the correct input.
  • Apply the chain rule successively when a vector-valued map lies between scalar functions.
  • Use Jacobian matrices to differentiate a three-stage multivariable composition.
  • Check a nested derivative by simplifying the composition and differentiating directly.
  • Identify and correct errors caused by omitting a factor or evaluating it at the wrong stage.

Why Nested Compositions Need Care

A function may depend on an input through several intermediate stages. To find the rate of change of the final output, the chain rule must account for every stage along the way. The calculation is simple in principle: multiply the derivative factors in sequence. The main challenge is keeping track of which function is evaluated at which input.

For three scalar stages, write the composition as \(q(g(f(x)))\). The input \(x\) first enters \(f\), the output of \(f\) enters \(g\), and the output of \(g\) enters \(q\). Thus the derivative contains three factors, evaluated at three corresponding points.

Key rule: If \(f\), \(g\), and \(q\) are differentiable at the successive inputs involved, then \(q(g(f(x)))\) has derivative \(q'(g(f(x)))g'(f(x))f'(x)\). The outermost derivative is evaluated at the output of the middle function.

The Three-Stage Scalar Chain Rule

To see the rule clearly, name the intermediate quantities. Let \(u=f(x)\), then \(v=g(u)\), and finally \(y=q(v)\). Applying the ordinary chain rule first to the outer stage and then to the inner stages gives

$$ \frac{dy}{dx} = \frac{dy}{dv}\frac{dv}{du}\frac{du}{dx} = q'(v)g'(u)f'(x) = q'(g(f(x)))g'(f(x))f'(x). $$

The derivative of \(q\) is evaluated at \(v=g(f(x))\), the derivative of \(g\) at \(u=f(x)\), and the derivative of \(f\) at \(x\). This order follows the path from the input to the output, while the factors are conventionally written from the outer derivative to the inner derivative.

Worked Example: Three Scalar Functions

Let \(f(x)=x^2+1\), \(g(u)=3u-2\), and \(q(v)=\sin v\). Find the derivative of \(q(g(f(x)))\) at \(x=1\).

First evaluate the intermediate inputs:

$$ f(1)=1^2+1=2,\qquad g(f(1))=g(2)=3(2)-2=4. $$

Next find the derivative factors. Since \(f'(x)=2x\), \(g'(u)=3\), and \(q'(v)=\cos v\), their values at the required inputs are

$$ f'(1)=2,\qquad g'(f(1))=g'(2)=3,\qquad q'(g(f(1)))=q'(4)=\cos 4. $$

Multiply all three factors:

$$ \left.\frac{d}{dx}q(g(f(x)))\right|_{x=1} = (\cos 4)(3)(2) = 6\cos 4. $$

Check by simplifying the composition: \(g(f(x))=3(x^2+1)-2=3x^2+1\), so \(q(g(f(x)))=\sin(3x^2+1)\). Differentiating directly gives \(6x\cos(3x^2+1)\). At \(x=1\), this is \(6\cos 4\), as before.

When an Intermediate Stage Is Vector-Valued

The same idea applies when one stage produces several intermediate quantities. Suppose \(v\) is a scalar input, \(u(v)\) is a vector in \(\mathbb{R}^n\), and \(F\) is a scalar-valued function of that vector. Then the rate of change of \(F(u(v))\) is the gradient of \(F\) at \(u(v)\), dotted with the derivative vector \(u'(v)\). If the scalar input itself is \(v(t)\), apply the scalar chain rule once more:

$$ \frac{d}{dt}F(u(v(t))) = \nabla F(u(v(t)))\mathbin{\cdot}u'(v(t))\,v'(t). $$

Here \(\nabla F\) is the vector of first partial derivatives of \(F\). The dot product combines the changes through all coordinates of \(u\); the final factor \(v'(t)\) accounts for the dependence of the intermediate scalar on \(t\).

Worked Example: A Scalar Input Through a Vector Stage

Let \(v(t)=t^2+1\), \(u(s)=(2s,s-1)\), and \(F(x,y)=xy+y^3\). Find the derivative of \(F(u(v(t)))\) at \(t=1\).

Track the inputs in order:

$$ v(1)=1^2+1=2,\qquad u(2)=(2(2),2-1)=(4,1). $$

The gradient of \(F\) is \(\nabla F(x,y)=(y,x+3y^2)\). Thus \(\nabla F(4,1)=(1,4+3)=(1,7)\). Also \(u'(s)=(2,1)\), so \(u'(2)=(2,1)\), and \(v'(t)=2t\), giving \(v'(1)=2\).

Apply the chain rule and show the dot product explicitly:

$$ \left.\frac{d}{dt}F(u(v(t)))\right|_{t=1} = (1,7)\mathbin{\cdot}(2,1)\,(2) = (1\cdot2+7\cdot1)(2) = 9\cdot2 = 18. $$

Check by composing first. Since \(u(v(t))=(2(t^2+1),t^2)=(2t^2+2,t^2)\), the full function is \(F(u(v(t)))=(2t^2+2)t^2+(t^2)^3=2t^4+2t^2+t^6\). Its derivative is \(8t^3+4t+6t^5\), which at \(t=1\) is \(8+4+6=18\). The two calculations agree.

Three Multivariable Stages and Jacobian Order

For maps between vector spaces, the same repeated rule is expressed with Jacobian matrices. Let \(f:\mathbb{R}^m\to\mathbb{R}^n\), \(g:\mathbb{R}^n\to\mathbb{R}^p\), and \(q:\mathbb{R}^p\to\mathbb{R}^r\). If each map is differentiable at the relevant point, then the derivative matrix of the composition is

$$ D(q\circ g\circ f)(\mathbf{x}) = Dq(g(f(\mathbf{x})))\,Dg(f(\mathbf{x}))\,Df(\mathbf{x}). $$

The dimensions determine the order: the matrix on the right acts first on an input change, then the middle matrix, then the leftmost matrix. For a scalar-valued final map, \(r=1\), so the result is a row vector. This formula is simply the multivariable chain rule applied twice, with each Jacobian evaluated where its own map receives an input.

Worked Example: Multiplying Three Jacobians

Define \(f:\mathbb{R}^2\to\mathbb{R}^3\), \(g:\mathbb{R}^3\to\mathbb{R}^2\), and \(q:\mathbb{R}^2\to\mathbb{R}\) by

$$ f(x,y)=(x+y,xy,x-y),\qquad g(a,b,c)=(a+c,b+ac),\qquad q(r,s)=r^2+3s. $$

Find the gradient of \(q(g(f(x,y)))\) at \((1,2)\). First follow the stages:

$$ f(1,2)=(3,2,-1),\qquad g(3,2,-1)=(2,2+3(-1))=(2,-1). $$

The Jacobian matrices at these points are

$$ Df(1,2)= \begin{bmatrix} 1&1\\ 2&1\\ 1&-1 \end{bmatrix}, \qquad Dg(3,2,-1)= \begin{bmatrix} 1&0&1\\ -1&1&3 \end{bmatrix}, \qquad Dq(2,-1)= \begin{bmatrix} 4&3 \end{bmatrix}. $$

Multiply in the required order. First,

$$ Dg\,Df = \begin{bmatrix} 1&0&1\\ -1&1&3 \end{bmatrix} \begin{bmatrix} 1&1\\ 2&1\\ 1&-1 \end{bmatrix} = \begin{bmatrix} 2&0\\ 4&-3 \end{bmatrix}. $$

For example, the lower-left entry is \((-1)(1)+(1)(2)+(3)(1)=4\), and the lower-right entry is \((-1)(1)+(1)(1)+(3)(-1)=-3\). Now multiply by the outer Jacobian:

$$ Dq(Dg\,Df) = \begin{bmatrix}4&3\end{bmatrix} \begin{bmatrix}2&0\\4&-3\end{bmatrix} = \begin{bmatrix}20&-9\end{bmatrix}. $$

Thus the gradient is \((20,-9)\). Check by simplifying: \(g(f(x,y))=(2x,xy+x^2-y^2)\), so \(q(g(f(x,y)))=4x^2+3xy+3x^2-3y^2=7x^2+3xy-3y^2\). Its partial derivatives are \(14x+3y\) and \(3x-6y\). At \((1,2)\), these are \(14+6=20\) and \(3-12=-9\), confirming the Jacobian calculation.

Why the Repeated Rule Works

The justification is repeated use of the chain rule for two maps. If \(u=f(x)\), then \(g(f(x))=g(u)\), and the chain rule gives \(\frac{d}{dx}g(f(x))=g'(f(x))f'(x)\). Now regard \(g(f(x))\) as the input to \(q\). Applying the chain rule to \(q(g(f(x)))\) gives

$$ \frac{d}{dx}q(g(f(x))) = q'(g(f(x)))\frac{d}{dx}g(f(x)) = q'(g(f(x)))g'(f(x))f'(x). $$

Each equality uses the chain rule at the point where the inner function supplies an input to the outer one. For differentiable maps between finite-dimensional spaces, the same reasoning uses the multivariable chain rule at each stage; the derivative matrices compose by matrix multiplication. This also explains why their order cannot be rearranged in general.

A Common Mistake: Using the Wrong Input

A frequent error is to evaluate the outer derivative at the output of \(f\), rather than at the output of \(g\). In the first example, \(f(1)=2\) but \(g(f(1))=4\). The incorrect calculation

$$ q'(f(1))g'(f(1))f'(1) = (\cos 2)(3)(2) = 6\cos 2 $$

uses the wrong input for \(q'\). The function \(q\) receives \(g(f(1))=4\), not \(f(1)=2\). The corrected calculation is

$$ q'(g(f(1)))g'(f(1))f'(1) = (\cos 4)(3)(2) = 6\cos 4. $$

Another error is to omit a factor, such as writing \(q'(g(f(x)))f'(x)\). That ignores how changes in \(f(x)\) pass through \(g\). A useful check is to list every stage, its input, and its derivative before multiplying.

A Reliable Procedure

1
Write the stages.
For a scalar composition, label \(u=f(x)\), \(v=g(u)\), and \(y=q(v)\).
2
Record each derivative at its own input.
Use \(f'(x)\), \(g'(f(x))\), and \(q'(g(f(x)))\), or the corresponding Jacobians.
3
Multiply in chain order.
For scalar derivatives multiply the factors; for Jacobians multiply from the outermost matrix on the left to the innermost on the right.
4
Check the result.
When practical, simplify the full composition and differentiate it directly to verify the value.

Why This Skill Matters

Nested functions arise whenever a quantity is computed in stages: an input is transformed, that result is used to form another quantity, and a final function reports the output of interest. Repeated chain rules make the dependency of the final result explicit. This is especially useful when the intermediate stages have different types, such as a scalar input producing a vector before entering a scalar-valued function.

The essential habit is not merely to remember a product of derivatives. It is to trace the path of the input, keep each evaluation point attached to its derivative, and respect matrix dimensions when the stages are multivariable. Those checks make a compact chain-rule calculation both interpretable and verifiable.

Check Your Understanding

For each question, identify the inputs to the derivative factors before calculating.

  1. If \(h(x)=q(g(f(x)))\), what is the correct three-factor formula for \(h'(x)\)?
  2. For \(f(x)=x+2\), \(g(u)=u^2\), and \(q(v)=3v\), find the derivative of \(q(g(f(x)))\) at \(x=1\).
  3. In \(F(u(v(t)))\), where \(u\) is vector-valued and \(F\) is scalar-valued, which two operations combine the derivative of \(F\) with the changes in \(u\)?
  4. For \(q\circ g\circ f\), which Jacobian is on the left in the derivative-matrix product, and why?
  5. Give one way to check a derivative obtained from a nested chain rule.