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.
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
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:
Next find the derivative factors. Since \(f'(x)=2x\), \(g'(u)=3\), and \(q'(v)=\cos v\), their values at the required inputs are
Multiply all three factors:
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:
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:
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:
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
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
Find the gradient of \(q(g(f(x,y)))\) at \((1,2)\). First follow the stages:
The Jacobian matrices at these points are
Multiply in the required order. First,
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:
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
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
uses the wrong input for \(q'\). The function \(q\) receives \(g(f(1))=4\), not \(f(1)=2\). The corrected calculation is
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
For a scalar composition, label \(u=f(x)\), \(v=g(u)\), and \(y=q(v)\).
Use \(f'(x)\), \(g'(f(x))\), and \(q'(g(f(x)))\), or the corresponding Jacobians.
For scalar derivatives multiply the factors; for Jacobians multiply from the outermost matrix on the left to the innermost on the right.
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.
- If \(h(x)=q(g(f(x)))\), what is the correct three-factor formula for \(h'(x)\)?
- For \(f(x)=x+2\), \(g(u)=u^2\), and \(q(v)=3v\), find the derivative of \(q(g(f(x)))\) at \(x=1\).
- 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\)?
- For \(q\circ g\circ f\), which Jacobian is on the left in the derivative-matrix product, and why?
- Give one way to check a derivative obtained from a nested chain rule.