# Trick to Apply the Chain Rule FAST - Peeling the Onion

See below for the video version of this post:

In this post, I’m going to teach you how to quickly apply the chain rule multiple times using a technique that I like to call “Peeling the Onion.” Using this technique, you’ll be able to quickly differentiate complicated functions, like the one below.

\begin{align*} &\frac{d}{dx} \left[ \sin^2 \left( x + e^{\tan 3x} \right) \right] \\ &= 2 \sin \left( x + e^{\tan 3x} \right) \cos \left( x + e^{\tan 3x} \right) \left( 1 + e^{\tan 3x} \sec^2 (3x) \cdot 3 \right) \end{align*}

Contrary to what the name suggests, the technique of “Peeling the Onion” will not make you cry. Actually, it’s going to do the opposite – you’re going to be able to do these complicated derivatives without crying, like you would usually do if you had to write out the chain rule over and over and over again.

Before we get into this technique of “Peeling the Onion”, though, let’s recap what the chain rule is, because “Peeling the Onion” is ultimately just a method to speed up the chain rule.

The chain rule says that if we have some expression, say, $(x^2+1)^{10}$, and we want to differentiate that expression, we can make it simpler by lumping up some of these terms into another variable, say, $u$. The full expression simplifies – it’s just $u^{10}$, and that’s what we want to take the derivative of.

\begin{align*} \frac{d}{dx} \left[ ( \underbrace{ x^2+1 }_{u})^{10} \right] &= \frac{d}{dx} \left[ u^{10} \right] \end{align*}

We can do that just like normal using the power rule, but the only catch is that we have to multiply by $\frac{du}{dx}$ afterwards.

\begin{align*} \frac{d}{dx} \left[ ( \underbrace{ x^2+1 }_{u})^{10} \right] &= \frac{d}{dx} \left[ u^{10} \right] \\ &= 10u^9 \, \frac{du}{dx} \end{align*}

But that’s okay, because taking the derivative of $u$ is simpler. The variable $u$ is just $x^2+1$, so we can substitute that back into the expression and simplify.

\begin{align*} \frac{d}{dx} \left[ ( \underbrace{ x^2+1 }_{u})^{10} \right] &= \frac{d}{dx} \left[ u^{10} \right] \\ &= 10u^9 \, \frac{du}{dx} \\ &= 10(x^2+1)^9 \, \frac{d}{dx} \left[ x^2+1 \right] \\ &= 10(x^2+1)^9 \cdot 2x \end{align*}

There we have it, that’s the derivative!

This was a very simple example. If we want to differentiate a more complicated expression, like $\left( \sin(x^2) + 1 \right)^{10}$, then we need to apply the chain rule multiple times. You’ll see how this gets kind of complicated.

We start out the same way by lumping up some terms into $u$, and then it’s just the derivative of $u^{10}$ that we want to find.

\begin{align*} \frac{d}{dx} \left[ ( \underbrace{ \sin(x^2)+1}_{u})^{10} \right] &= \frac{d}{dx} \left[ u^{10} \right] \end{align*}

Just like last time, we apply the power rule, and the catch is that we have to multiply by $\frac{du}{dx}$. Again, we substitute back in for $u$, but this time we still run into some trouble when we try to differentiate $\sin(x^2)+1$.

\begin{align*} \frac{d}{dx} \left[ ( \underbrace{ \sin(x^2)+1 }_{u})^{10} \right] &= \frac{d}{dx} \left[ u^{10} \right] \\ &= 10u^9 \, \frac{du}{dx} \\ &= 10 \left( \sin(x^2)+1 \right)^9 \, \frac{d}{dx} \left[ \sin(x^2)+1 \right] \end{align*}

We need to apply the chain rule again, lump up that $x^2$ into a single variable, say, $w$, and then the derivative simplifies: it’s just the derivative of $\sin(w)+1$, which we can actually compute. Again, the catch is that we have to multiply by $\frac{dw}{dx}$ afterwards.

\begin{align*} \frac{d}{dx} \left[ ( \underbrace{\sin(x^2)+1}_{u})^{10} \right] &= \frac{d}{dx} \left[ u^{10} \right] \\ &= 10u^9 \, \frac{du}{dx} \\ &= 10 \left( \sin( x^2 )+1 \right)^9 \, \frac{d}{dx} \left[ \sin( \underbrace{x^2}_{w})+1 \right] \\ &= 10 \left( \sin( x^2 )+1 \right)^9 \, \frac{d}{dx} \left[ \sin(w)+1 \right] \\ &= 10 \left( \sin( x^2 )+1 \right)^9 \cos(w) \, \frac{dw}{dx} \end{align*}

Thankfully $w$ is pretty simple, it’s just $x^2$, so we substitute that in and simplify.

\begin{align*} \frac{d}{dx} \left[ ( \underbrace{ \sin(x^2)+1}_{u})^{10} \right] &= \frac{d}{dx} \left[ u^{10} \right] \\ &= 10u^9 \, \frac{du}{dx} \\ &= 10 \left( \sin( x^2 )+1 \right)^9 \, \frac{d}{dx} \left[ \sin( \underbrace{x^2}_{w})+1 \right] \\ &= 10 \left( \sin( x^2 )+1 \right)^9 \, \frac{d}{dx} \left[ \sin(w)+1 \right] \\ &= 10 \left( \sin( x^2 )+1 \right)^9 \cos(x^2) \, \frac{d}{dx} \left[ x^2 \right] \\ &= 10 \left( \sin( x^2 )+1 \right)^9 \cos(x^2) \cdot 2x \end{align*}

There, we’ve got it! But only after a fair amount of blood, sweat, and tears. If you thought this was bad, then imagine how painful it would be to differentiate an expression which required, say, 5 iterations of the chain rule. Here we only had 2 iterations, with $u$ and $w$, so imagine if we had to do this for 3 more iterations – that would be more than twice the amount of work we did here.

This is where the technique of “Peeling the Onion” comes to make life a whole lot easier. Let’s look at that same expression, $\left( \sin(x^2)+1 \right)^{10}$, and this time let’s think of it in terms of layers. The power of $10$ is the outermost layer, and then the next layer (the middle layer) will be the $\sin( \, ) + 1$, and then the third layer (the innermost layer) will be the $x^2$.

\begin{align*} \color{red}( \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \color{red}{)^{10}} \end{align*}

When we differentiate this, we start with the outer layer and we differentiate that first.

\begin{align*} \frac{d}{dx} \left[ \color{red}( \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \color{red}{)^{10}} \right] &= \color{red}{10(} \hspace{2cm} \color{red}{)^9} \end{align*}

The rest of the stuff that we fill in, we have to differentiate in the same way.

\begin{align*} \frac{d}{dx} \left[ \color{red}( \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \color{red}{)^{10}} \right] &= \color{red}{10(} \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \color{red}{)^9} \, \frac{d}{dx} \left[ \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \right] \end{align*}

When we evaluate the derivative, again we’re just looking at the outermost layer. But now we’ve already peeled off one layer, so it’s the $\sin( \, )+1$ that we want to differentiate. It’s just $\cos( \, )$.

\begin{align*} \frac{d}{dx} \left[ \color{red}( \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \color{red}{)^{10}} \right] &= \color{red}{10(} \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \color{red}{)^9} \, \frac{d}{dx} \left[ \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \right] \\ &= \color{red}{10(} \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \color{red}{)^9} \color{blue}{\cos( \hspace{.25cm} )} \end{align*}

We fill in the next layer, $x^2$, and that’s what we have to differentiate. It’s straightforward, just $2x$, and there we go – there’s our answer.

\begin{align*} \frac{d}{dx} \left[ {\color{red} ( } {\color{blue} \sin( } {\color{green} x^2} {\color{blue} )+1 } {\color{red} )^{10} ) } \right] &= \color{red}{10(} \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \color{red}{)^9} \, \frac{d}{dx} \left[ \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \right] \\ &= \color{red}{10(} \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \color{red}{)^9} \color{blue}{\cos( \color{green}{x^2} )} \, \frac{d}{dx} \left[ \color{green}{x^2} \right] \\ &= \color{red}{10(} \color{blue}{\sin(} \color{green}{x^2} \color{blue}{)+1} \color{red}{)^9} \color{blue}{\cos( \color{green}{x^2} )} \, \cdot \color{green}{2x} \end{align*}

Once you practice this “Peeling the Onion” technique a couple times, you’ll get to the stage where you can do it all in one step: first differentiate the outer layer, and then multiply by the derivative of the next layer, and then multipy by the derivative of the layer after that, and so on. It’s like we’re peeling an onion one layer at a time.

Now that we see how “Peeling the Onion” works on a 3-layer onion, let’s try something crazy. Remember that expression at the very beginning of the post, that had an extroardinarily long derivative? Let’s differentiate it. We’ll do this by “Peeling the Onion,” and it’s just going to take a single step of work.

\begin{align*} \sin^2 \left( x + e^{\tan 3x} \right) \end{align*}

This expression is a little sneaky, because $\sin^2$ actually means take the square after you’ve taken the $\sin$. So the outermost layer is really the squared.

\begin{align*} \color{red}{( \hspace{3cm} )^2} \end{align*}

Then after that is the $\sin( \, )$, and after that is the $x + e^\square$, and the next layer is in the power of $e$, the $\tan( \, )$, and the final layer (the innermost layer) is the $3x$ inside of the $\tan( \, )$.

\begin{align*} \color{red}{(} \color{blue}{\sin(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{red}{)^2} \end{align*}

Let’s go ahead and peel this onion! The first step is to peel the outer layer, the squared. For that, we just use the power rule.

\begin{align*} \frac{d}{dx} &\left[ \color{red}{(} \color{blue}{\sin(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{red}{)^2} \right] \\ &= \color{red}{2(} \hspace{3cm} \color{red}{)} \end{align*}

We fill in the inner layers and then peel the next layer, which is the $\sin( \, )$.

\begin{align*} \frac{d}{dx} &\left[ \color{red}{(} \color{blue}{\sin(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{red}{)^2} \right] \\ &= \color{red}{2(} \color{blue}{\sin(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{red}{)} \color{blue}{\cos( \hspace{2cm} )} \end{align*}

We continue on filling in and peeling the next layer, which is $x + e^\square$, and because the next layer is entirely in the power of $e$, it’s only the $e$ that gets multiplied by the derivative. So we’re going to multiply by something inside of the parentheses.

\begin{align*} \frac{d}{dx} &\left[ \color{red}{(} \color{blue}{\sin(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{red}{)^2} \right] \\ &= \color{red}{2(} \color{blue}{\sin(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{red}{)} \color{blue}{\cos(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{green}{(1+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{red}{\cdot} \hspace{2cm} \color{green}{)} \end{align*}

The thing that we multiply by, will be the result of peeling the next layer. The next layer is the $\tan( \, )$, and the derivative of $\tan( \, )$ is $\sec^2( \, )$.

\begin{align*} \frac{d}{dx} &\left[ \color{red}{(} \color{blue}{\sin(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{red}{)^2} \right] \\ &= \color{red}{2(} \color{blue}{\sin(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{red}{)} \color{blue}{\cos(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{green}{(1+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{red}{\cdot \sec^2(} \hspace{.5cm} \color{red}{)} \hspace{.5cm} \color{green}{)} \end{align*}

Inside of that is the $3x$, which is our final layer, and the derivative of $3x$ is just $3$.

\begin{align*} \frac{d}{dx} &\left[ \color{red}{(} \color{blue}{\sin(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{red}{)^2} \right] \\ &= \color{red}{2(} \color{blue}{\sin(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{red}{)} \color{blue}{\cos(} \color{green}{x+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{blue}{)} \color{green}{(1+e}^{ \color{red}{\tan(} \color{blue}{3x} \color{red}{)} } \color{red}{\cdot \sec^2(} \color{blue}{3x} \color{red}{)} \color{blue}{\cdot 3} \color{green}{)} \end{align*}

There we go, we’ve got it! That’s the derivative.

Tags: