# Implicit Differentiation

The level set $f(x,y) = z$ defines a “contour line” that can be plotted in $x-y$ space. In this section we derive the slope of such a contour line, $dy/dx$. This will turn out to have important economic implications in a wide range of applications.

## The total derivative along a path

Before we think about level sets, let’s think about the more general question of how altitude varies along a path over the surface of a multivariable function. For example, let’s consider the path defined by $\textcolor{#31a354}{y(x) = 4 - 0.4x}$. We can draw that as a green line over the surface of a function, as shown below.

Now think about moving by some amount $\Delta x$ along the path, from some point $\textcolor{#3182bd}{(x,y)}$ to a second point $\textcolor{#d62728}{(x + \Delta x, y + \Delta y)}$. We can *decompose* this overall change into two changes:

- holding $y$ constant, from $\textcolor{#3182bd}{(x,y)}$ to $\textcolor{#e6550d}{(x+\Delta x, y)}$
- holding $x$ constant, from $\textcolor{#e6550d}{(x+\Delta x, y)}$ to $\textcolor{#d62728}{(x + \Delta x, y + \Delta y)}$

If we think about this in terms of partial derivatives, we can approximate the change due to $\Delta x$ as the change in $f(x,y)$ per unit change in $x$ (i.e., the partial derivative with respect to $x$), multiplied by $\Delta x$:

$$\left.\Delta f(x,y)\right|_{\Delta x} \approx {\partial f \over \partial x} \times \Delta x$$

Likewise, the change due to $\Delta y$ as

$$\left.\Delta f(x,y)\right|_{\Delta y} \approx {\partial f \over \partial y} \times \Delta y$$

Therefore the *total change* is the sum of these two changes:

$$\Delta f(x,y) \approx {\partial f \over \partial x} \times \Delta x + {\partial f \over \partial y} \times \Delta y$$

If we divide both sides by $\Delta x$, this becomes

$${\Delta f(x,y) \over \Delta x} \approx {\partial f \over \partial x} + {\partial f \over \partial y} \times {\Delta y \over \Delta x}$$

As $\Delta x \rightarrow 0$ in the limit, $\Delta y/\Delta x$ approaches the derivative of $y$ with respect to $x$, giving us

$$\left.{\partial f \over \partial x}\right|_{y = y(x)} = {\partial f \over \partial x} + {\partial f \over \partial y} \times {dy \over dx}$$

This is really just the chain rule: if $y$ changes when $x$ changes, then the total change in $f$ when $x$ changes is the *direct effect* due to the change in $x$, plus the *indirect effect* of the change in $y$.

## The slope along a level set

The above analysis holds for any path along the surface. We can look in particular at the same analysis for a level set, which is *implicitly defined* by the equation
\(f(x,y) = z\)
where $z$ is some constant.

Taking the derivative of *both sides of this equation* with respect to $x$ gives us
\({\partial f \over \partial x} + {\partial f \over \partial y} \times {dy \over dx} = 0\)
The left-hand side of the equation comes from the analysis above; the right-hand side is zero because $z$ is a constant. Intuitively, along a level set, we know that the *total change is zero*: however much $f(x,y)$ increases as a result of $\Delta x$, it decreases by the same amount as a result of $\Delta y$:

As $\Delta x \rightarrow 0$, the blue line becomes a line tangent to the function at the point $(x, y, z)$. This allows us to solve for the *slope along a level set at a point*, by solving for $dy/dx$:
\(\left.{dy \over dx}\right|_{f(x,y) = z} = - {\partial f/\partial x \over \partial f/\partial y}\)
We can see if we plot level sets and contour maps, that we can use this formula to define the slope of the level set passing through any point $(x, y, f(x,y))$:

Importantly, note that *every point $(x,y)$ defines a level set, and therefore the slope of a level set.* No matter where you drag the blue point in this diagram, it defines a purple curve, and there is a line tangent to that curve at that point. Put another way, we can think of the *level set itself* and the *slope of the level set* as functions of the point $(x,y)$:
\(\begin{aligned}
\text{Level set through }(\hat x, \hat y) &= \{(x,y) | f(x,y) = f(\hat x, \hat y)\}\\
\text{Slope of that level set at }(\hat x, \hat y) &= \left.{dy \over dx}\right|_{f(x,y) = f(\hat x, \hat y)} = -{\partial f(\hat x, \hat y)/\partial x \over \partial f(\hat x, \hat y)/\partial y}
\end{aligned}\)

These expressions will be incredibly important as we evaluate choices economic agents make, so be sure you are fluent in their applications.