Skip to main content

MATH 345: Linear Algebra and Optimization

Section 5.2 Directional derivatives and gradient descent

Subsection Directional derivatives

Activity 5.2.1.

Find the system of equations satisfied by the critical points of the function
\begin{equation*} f(x,y,z) = x^2e^{yz} + y\sin(xz) + z\cos(xy)\text{.} \end{equation*}
Solution.
We calculate that
\begin{equation*} f_x(x,y,z)=2xe^{yz}+yz\cos(xz)-yz\sin(xy)\text{,} \end{equation*}
that
\begin{equation*} f_y(x,y,z)=x^2ze^{yz}+\sin(xz)-xz\sin(xy) \end{equation*}
and that
\begin{equation*} f_z(x,y,z)=x^2ye^{yz}+xy\cos(xz)+\cos(xy)\text{.} \end{equation*}
So the critical points of \(f\) are the solutions to the system of equations
\begin{equation*} \begin{cases} 2xe^{yz}+yz\cos(xz)-yz\sin(xy)=0,\\ x^2ze^{yz}+\sin(xz)-xz\sin(xy)=0,\\ x^2ye^{yz}+xy\cos(xz)+\cos(xy)=0. \end{cases} \end{equation*}
Using the calculus we have developed, it is usually straightforward to write the equations that critical points must satisfy. ActivityΒ 5.2.1 also shows the problem: those equations may be nonlinear and difficult or impossible to solve exactly. This is different from the linear systems in Unit 2, where Gaussian elimination gave a systematic exact method.
For such problems, one often uses numerical methods. Gradient descent starts with an initial guess \(\mathbf{x}_0\) and produces iterates
\begin{equation*} \mathbf{x}_{k+1}=\mathbf{x}_k-\alpha \nabla f(\mathbf{x}_k). \end{equation*}
The positive scalar \(\alpha\) is the step size. The minus sign moves in a direction where \(f\) initially decreases. The method does not automatically converge; the function, starting point, and step size matter.
To discuss this further, we must introduce more general kinds of derivatives. Recall that the partial derivatives of a function \(f\) are defined as
\begin{equation*} \frac{\partial f}{\partial x_i}(\mathbf{a}) = \lim_{h \to 0} \frac{f(\mathbf{a} + h \mathbf{e}_i) - f(\mathbf{a})}{h}\text{.} \end{equation*}
By replacing \(\mathbf{e}_i\) by a more general unit vector, we obtain the concept of a directional derivative.

Definition 5.2.1.

Let \(f\) be a scalar-valued function. Then if \(\mathbf{v}\) is a unit vector, the directional derivative of \(f\) in the direction of the vector \(\mathbf{v}\text{,}\) denoted \(D_{\mathbf{v}} f\text{,}\) is defined as
\begin{equation*} D_{\mathbf{v}} f(\mathbf{a}) = \lim_{h \to 0} \frac{f(\mathbf{a} + h \mathbf{v}) - f(\mathbf{a})}{h}\text{,} \end{equation*}
provided the limit exists.
The directional derivative represents the rate of change of the function \(f\) at the point \(\mathbf{a}\) in the direction of the unit vector \(\mathbf{v}\text{.}\) For a function \(f(x,y)\) of two variables, the directional derivative \(D_{\mathbf{v}} f(\mathbf{a})\) is the slope of the tangent line to the curve obtained by intersecting the surface \(z = f(x,y)\) with the plane containing both the line passing through \(\mathbf{a}\) with direction vector \(\mathbf{v}\text{,}\) and the \(z\) axis.

Remark 5.2.2.

Partial derivatives are a special case of directional derivatives, i.e., we have
\begin{equation*} \frac{\partial f}{\partial x_i} = D_{\mathbf{e}_i} f\text{.} \end{equation*}
Despite partial derivatives being a special case of directional derivatives, in most circumstances we can compute directional derivatives using partial derivatives, or equivalently, the gradient.

Why is this true?.

We apply the chain rule. We note that \(D_{\mathbf{v}} f(\mathbf{a})\) is equal to the derivative of the function \(g(t) = f(\mathbf{a} + t \mathbf{v})\) at \(t = 0\) (both derivatives are defined as the same limits). If we write \(\mathbf{c}(t) = \mathbf{a} + t \mathbf{v}\text{,}\) then \(g = f \circ \mathbf{c}\text{.}\) But the chain rule (TheoremΒ 3.6.4) implies that
\begin{equation*} g'(0) = (\nabla f)(\mathbf{c}(0)) \cdot \mathbf{c}'(0) = (\nabla f)(\mathbf{a}) \cdot \mathbf{v}\text{.} \end{equation*}
TheoremΒ 5.2.3 greatly simplifies the computations of directional derivatives.

Activity 5.2.2. Dot product controls direction.

Suppose
\begin{equation*} \nabla f(\mathbf{a})= \begin{bmatrix} 3\\ 4 \end{bmatrix}. \end{equation*}
Compute \(D_{\mathbf{v}}f(\mathbf{a})\) for each unit vector below:
\begin{equation*} \mathbf{v}_1= \begin{bmatrix} 1\\ 0 \end{bmatrix}, \qquad \mathbf{v}_2= \begin{bmatrix} 0\\ 1 \end{bmatrix}, \qquad \mathbf{v}_3= \frac15 \begin{bmatrix} 3\\ 4 \end{bmatrix}, \qquad \mathbf{v}_4= -\frac15 \begin{bmatrix} 3\\ 4 \end{bmatrix}. \end{equation*}
  1. Which listed direction gives the largest directional derivative?
  2. Which listed direction gives the smallest directional derivative?
  3. How is this the Unit 1 dot product appearing again?
Tags. [U5-LO2, U1-LO1 | C+R | Core]
Solution.
Since
\begin{equation*} D_{\mathbf{v}}f(\mathbf{a})=\nabla f(\mathbf{a})\cdot\mathbf{v}, \end{equation*}
we compute
\begin{equation*} D_{\mathbf{v}_1}f(\mathbf{a})=3, \qquad D_{\mathbf{v}_2}f(\mathbf{a})=4. \end{equation*}
Also,
\begin{equation*} D_{\mathbf{v}_3}f(\mathbf{a}) = \begin{bmatrix} 3\\ 4 \end{bmatrix} \cdot \frac15 \begin{bmatrix} 3\\ 4 \end{bmatrix} = \frac{9+16}{5}=5, \end{equation*}
and
\begin{equation*} D_{\mathbf{v}_4}f(\mathbf{a})=-5. \end{equation*}
The largest listed directional derivative occurs in direction \(\mathbf{v}_3\text{,}\) the direction of the gradient. The smallest occurs in direction \(\mathbf{v}_4\text{,}\) the direction opposite the gradient. This is the Unit 1 dot product measuring alignment: larger positive alignment gives larger directional derivative.

Activity 5.2.3.

Let \(f(x,y) = x^2 y + y^2\text{.}\)
(a)
Compute the directional derivative \(D_{\mathbf{v}} f(1,2)\text{,}\) where \(\mathbf{v} = (1/\sqrt{2}, 1/\sqrt{2})\text{,}\) and \(f(x,y) = x^2y + y^2\text{.}\)
Solution.
We begin by computing the gradient of \(f\) at \((1,2)\text{.}\) We have
\begin{equation*} f_x(x,y) = 2xy \quad\text{and}\quad f_y(x,y) = x^2 + 2y\text{.} \end{equation*}
So \(\nabla f(x,y) = (2xy, x^2 + 2y)\text{,}\) and in particular
\begin{equation*} \nabla f(1,2) = \begin{bmatrix} 2(1)(2) \\ (1)^2 + 2 (2) \end{bmatrix} = \begin{bmatrix} 4 \\ 5 \end{bmatrix}\text{.} \end{equation*}
So
\begin{equation*} D_{\mathbf{v}} f(1,2) = \nabla f(1,2) \cdot \mathbf{v} = \begin{bmatrix} 4 \\ 5 \end{bmatrix} \cdot \begin{bmatrix} 1/\sqrt{2} \\ 1/\sqrt{2} \end{bmatrix} = 9 / \sqrt{2}\text{.} \end{equation*}
(b)
Find the directional derivative of \(f\) at the point \((-1,3)\) in the direction of the vector \((-1,\sqrt{3})\text{.}\) That is, compute \(D_{\mathbf{v}} f(-1,3)\text{,}\) where \(\mathbf{v}\) is the unit vector in the direction of \((-1,\sqrt{3})\text{.}\)
Solution.
The vector
\begin{equation*} \begin{bmatrix} -1\\ \sqrt{3} \end{bmatrix} \end{equation*}
has norm
\begin{equation*} \sqrt{(-1)^2+(\sqrt{3})^2}=2\text{.} \end{equation*}
So the unit vector in this direction is
\begin{equation*} \mathbf{v} = \frac12 \begin{bmatrix} -1\\ \sqrt{3} \end{bmatrix} = \begin{bmatrix} -1/2\\ \sqrt{3}/2 \end{bmatrix}\text{.} \end{equation*}
For
\begin{equation*} f(x,y)=x^2y+y^2, \end{equation*}
we have
\begin{equation*} \nabla f(x,y)= \begin{bmatrix} 2xy\\ x^2+2y \end{bmatrix}. \end{equation*}
Thus
\begin{equation*} \nabla f(-1,3)= \begin{bmatrix} -6\\ 7 \end{bmatrix}. \end{equation*}
Therefore
\begin{equation*} D_{\mathbf{v}}f(-1,3) = \nabla f(-1,3)\cdot \mathbf{v} = \begin{bmatrix} -6\\ 7 \end{bmatrix} \cdot \begin{bmatrix} -1/2\\ \sqrt{3}/2 \end{bmatrix} = 3+\frac{7\sqrt{3}}{2} = \frac{6+7\sqrt{3}}{2}\text{.} \end{equation*}

Activity 5.2.4.

Use the gradient of the function \(f(x,y) = x^3y\) to find the directional derivative of \(f\) at \((2,1)\) in the direction of the unit vector which we placed at \((2,1)\) points in the direction of the point \((3,5)\text{.}\)
Solution.
The unit vector pointing in the direction from \((2,1)\) to \((3,5)\) is the unit vector pointing in the same direction as the vector \((3-2,5-1) = (1,4)\text{.}\) This vector has magnitude \(\sqrt{1^2 + 4^2} = \sqrt{17}\text{,}\) so the unit vector pointing in this direction is equal to
\begin{equation*} \mathbf{v} = \begin{bmatrix} 1/\sqrt{17}\\ 4/\sqrt{17} \end{bmatrix}\text{.} \end{equation*}
Now
\begin{equation*} \nabla f(x,y) = \begin{bmatrix} 3x^2 y\\ x^3 \end{bmatrix}\text{,} \end{equation*}
and in particular,
\begin{equation*} \nabla f(2,1) = \begin{bmatrix} 3(2)^2 (1)\\ (2)^3 \end{bmatrix} = \begin{bmatrix} 12\\ 8 \end{bmatrix}\text{.} \end{equation*}
So
\begin{equation*} D_{\mathbf{v}} f(2,1) = \nabla f(2,1) \cdot \mathbf{v} = \begin{bmatrix} 12\\ 8 \end{bmatrix} \cdot \begin{bmatrix} 1/\sqrt{17}\\ 4/\sqrt{17} \end{bmatrix} = \frac{12 + 32}{\sqrt{17}} = \frac{44}{\sqrt{17}}\text{.} \end{equation*}

Activity 5.2.5.

Let \(f(x,y)=x^2e^y\text{.}\) Find the directional derivative of \(f\) at \((2,0)\) in the direction pointing from \((2,0)\) to \((3,0)\text{.}\)
Solution.
The unit vector is \((1,0) = \mathbf{e}_1\text{,}\) and
\begin{equation*} D_{\mathbf{e}_1} f(x,y) = f_x(x,y) = 2x e^y\text{.} \end{equation*}
So in particular
\begin{equation*} D_{\mathbf{e}_1} f(2,0) = 2(2) e^{(0)} = 4\text{.} \end{equation*}

Why is this true?.

We have already verified the theorem when \(\nabla f(\mathbf{a}) = \mathbf{0}\) in TheoremΒ 5.2.3. When \(\nabla f(\mathbf{a}) \neq \mathbf{0}\text{,}\) TheoremΒ 5.2.3 and the Cauchy-Schwarz Inequality TheoremΒ 4.1.2 says that
\begin{equation*} |D_{\mathbf{u}}(\mathbf{a})| = |\nabla f(\mathbf{a})\cdot \mathbf{u}| \leq \left\|\nabla f(\mathbf{a})\right\|\|\mathbf{u}\| = \left\|\nabla f(\mathbf{a})\right\|\text{,} \end{equation*}
where the inequality is an equality precisely when \(\mathbf{u}\) either points in the direction of \(\nabla f(\mathbf{a})\text{,}\) or points in the opposite of this direction. But when \(\mathbf{u}\) points in the direction of \(\nabla f(\mathbf{a})\text{,}\) i.e., when \(\mathbf{u} = \nabla f(\mathbf{a}) / \| \nabla f(\mathbf{a}) \|\text{,}\) we have
\begin{align*} D_{\mathbf{u}} f(\mathbf{a}) \amp = \nabla f(\mathbf{a}) \cdot \frac{\nabla f(\mathbf{a})}{\|\nabla f(\mathbf{a})\|}\\ \amp = \frac{\nabla f(\mathbf{a}) \cdot \nabla f(\mathbf{a})}{\|\nabla f(\mathbf{a}) \|} = \frac{\|\nabla f(\mathbf{a})\|^2}{\|\nabla f(\mathbf{a}) \|} = \|\nabla f(\mathbf{a}) \|\text{.} \end{align*}
Conversely, one can check that when \(\mathbf{u} = - \nabla f(\mathbf{a}) / \| \nabla f(\mathbf{a}) \|\text{,}\) then
\begin{equation*} D_{\mathbf{u}} f(\mathbf{a}) = - \| \nabla f(\mathbf{a}) \|\text{.} \end{equation*}
So we see that these two vectors are precisely the maximizer and minimizer of the directional derivatives.
Gradient directions at a point on a surface.
A bowl-shaped surface is shown above the \(xy\)-plane with a point \(P_0(x_0,y_0,z_0)\) on the side of the surface. On the plane below, arrows based at the corresponding point show \(\nabla f\) for most rapid increase, \(-\nabla f\) for most rapid decrease, and perpendicular directions for zero change in \(f\text{.}\) Dashed vertical guide lines connect the point on the surface to its location in the domain.
Figure 5.2.5. The gradient indicates the maximum and minimum values of the directional derivative at a point. Figure 4.41 from Edwin β€œJed” Herman and Gilbert Strang, Calculus Volume 3, OpenStax, Β© 2018 Rice University, licensed under CC BY-NC-SA 4.0; source: OpenStax Figure 4.41.

Activity 5.2.6.

Find the direction for which the directional derivative of \(f(x,y) = 3x^2 - 4xy + 2y^2\) at \((-2,3)\) is a maximum. What is the maximum value of the directional derivative?
Solution.
First, we calculate that
\begin{equation*} \nabla f(x,y) = \begin{bmatrix} 6x - 4y\\ -4x + 4y \end{bmatrix}\text{.} \end{equation*}
So
\begin{equation*} \nabla f(-2,3) = \begin{bmatrix} 6(-2) - 4(3)\\ -4(-2) + 4(3) \end{bmatrix} = \begin{bmatrix} -24\\ 20 \end{bmatrix}\text{.} \end{equation*}
TheoremΒ 5.2.4 tells us that the directional derivative \(D_{\mathbf{v}} f\) is maximized when \(\mathbf{v}\) is the unit vector pointing in the direction of \((-24,20)\text{.}\) Since \((-24,20)\) has magnitude \(\sqrt{(-24)^2 + (20)^2} = \sqrt{976}\text{,}\) we see the direction which maximizes the directional derivative is
\begin{equation*} \mathbf{v} = \begin{bmatrix} -24/\sqrt{976}\\ 20/\sqrt{976} \end{bmatrix}\text{,} \end{equation*}
and the value of the directional derivative in this direction is equal to the magnitude of the gradient, which is
\begin{equation*} \sqrt{976}\text{.} \end{equation*}

Subsection Gradient descent

Remark 5.2.6.

We now return to the gradient descent algorithm, a method for trying to approximate the local minima of a function \(f\text{.}\) The algorithm works as follows:
  1. Start at an initial point \(\mathbf{x}_0\text{.}\)
  2. Compute the gradient \(\nabla f(\mathbf{x}_0)\text{.}\)
  3. Move in the direction of the negative gradient: set \(\mathbf{x}_1 = \mathbf{x}_0 - \alpha_0 \nabla f(\mathbf{x}_0)\text{,}\) where \(\alpha_0 > 0\) is a step size (or learning rate) parameter.
  4. Repeat steps 2-3 with step sizes \(\alpha_1, \alpha_2, \dots\) to successively define points \(\mathbf{x}_2 = \mathbf{x}_1 - \alpha_1 \nabla f(\mathbf{x}_1)\text{,}\) \(\mathbf{x}_3 = \mathbf{x}_2 - \alpha_2 \nabla f(\mathbf{x}_2)\text{,}\) and so on, with the hope that these points converge to a local minimum.
Intuitively, gradient descent works because each successive value \(\mathbf{x}_k\) is obtained by shifting the previous value \(\mathbf{x}_{k-1}\) in the direction in which the function \(f\) decreases the fastest. The algorithm is likely to find local minima of a function \(f\text{,}\) although a rigorous analysis of this algorithm is quite involved.

Activity 5.2.7. Reading one gradient descent update.

Assume grad_f(x) computes \(\nabla f(\mathbf{x})\text{.}\) Consider the line of code
x = x - alpha * grad_f(x)
  1. What mathematical update rule does this line represent?
  2. Which quantity is the learning rate?
  3. Why must grad_f(x) have the same shape as x?
  4. What would change if the minus sign were a plus sign?
Tags. [U5-LO3 | C+T | Core]
Solution.
The line represents the update
\begin{equation*} \mathbf{x}_{k+1}=\mathbf{x}_k-\alpha\nabla f(\mathbf{x}_k). \end{equation*}
The learning rate is alpha. The vector grad_f(x) must have the same shape as x, because the update subtracts one vector from another. If the minus sign were a plus sign, the update would move in the direction of steepest increase rather than steepest decrease.

Activity 5.2.8.

Find an approximate minimum of the function \(f : \R^2 \to \R\) defined by
\begin{equation*} f(x, y) = x^2 - 3xy + 3y^2 + 5y + 2x \end{equation*}
using gradient descent. Use the starting point \(\mathbf{x}_0 = (0,0)\text{,}\) a constant step size of \(0.1\text{,}\) and stop after computing \(\mathbf{x}_3\text{.}\) You may use a calculator to perform these computations.
Solution.
First, we calculate the gradient:
\begin{equation*} \nabla f(x, y) = \begin{bmatrix} 2x - 3y + 2 \\ -3x + 6y + 5 \end{bmatrix} \end{equation*}
Starting at \(\mathbf{x}_0 = (0,0)\text{,}\) we successively compute that
\begin{equation*} \nabla f(0,0) = \begin{bmatrix} 2 \\ 5 \end{bmatrix}\text{,} \end{equation*}
so
\begin{equation*} \mathbf{x}_1 = \begin{bmatrix}0\\0\end{bmatrix} - 0.1\begin{bmatrix} 2 \\ 5 \end{bmatrix} = \begin{bmatrix} -0.2 \\ -0.5 \end{bmatrix}\text{.} \end{equation*}
We then compute that
\begin{equation*} \nabla f(-0.2,-0.5) = \begin{bmatrix} -0.4-(-1.5)+2 \\ -3(-0.2)+6(-0.5)+5 \end{bmatrix} = \begin{bmatrix} 3.1 \\ 2.6 \end{bmatrix}\text{,} \end{equation*}
so
\begin{equation*} \mathbf{x}_2 = \begin{bmatrix} -0.2 \\ -0.5 \end{bmatrix} - 0.1\begin{bmatrix} 3.1 \\ 2.6 \end{bmatrix} = \begin{bmatrix} -0.51 \\ -0.76 \end{bmatrix}\text{.} \end{equation*}
Finally, we compute that
\begin{equation*} \nabla f(-0.51,-0.76) = \begin{bmatrix} 2(-0.51) - 3(-0.76) + 2 \\ -3(-0.51) + 6(-0.76) + 5 \end{bmatrix} = \begin{bmatrix} 3.26 \\ 1.97 \end{bmatrix}\text{,} \end{equation*}
so
\begin{equation*} \mathbf{x}_3 = \begin{bmatrix} -0.51 \\ -0.76 \end{bmatrix} - 0.1\begin{bmatrix} 3.26 \\ 1.97 \end{bmatrix} = \begin{bmatrix} -0.836 \\ -0.957 \end{bmatrix}\text{.} \end{equation*}
We note that for this function, we can precisely compute that \(f\) has a critical point at \((-9, -16/3)\text{,}\) which by the second derivative test is a local minimum. So \(\mathbf{x}_3\) is not a good approximation to the local minimum. But if we continued to iterate this algorithm, we would find that the points \(\mathbf{x}_k\) for, say, \(k \geq 1000\text{,}\) are very close to the true minimum of the function.

Remark 5.2.7.

This algorithm can be rather cumbersome to compute by hand, but if the gradient of a function is easily determined, can be easily done to a large number of iterations on a computer.
Surface plot showing the first gradient descent steps from the origin.
The figure shows a side view of the surface \(z=f(x,y)\) for \(f(x,y)=x^2-3xy+3y^2+5y+2x\) over a small region near the origin. Four red points labeled \(\mathbf{a}_0\text{,}\) \(\mathbf{a}_1\text{,}\) \(\mathbf{a}_2\text{,}\) and \(\mathbf{a}_3\) are connected by red line segments on the surface, showing the first three gradient descent steps from \((0,0)\text{.}\)
Figure 5.2.8. Initial steps of gradient descent for \(f(x,y)=x^2-3xy+3y^2+5y+2x\) with starting point \(\mathbf{a}_0 = (0, 0)\text{.}\) The point \(\mathbf{a}_n\) is determined by the rule \(\mathbf{a}_n = \mathbf{a}_{n-1} - (0.1) \nabla f(\mathbf{a}_{n-1})\text{.}\) Adapted from: Stanford’s MATH 51 textbook.

Activity 5.2.9.

Calculate \(D_{\mathbf{u}}f(1, -2, 3)\) in the direction of \(\mathbf{v} = -\mathbf{i} + 2\mathbf{j} + 2\mathbf{k}\) for the function
\begin{equation*} f(x,y,z) = 5x^2 - 2xy + y^2 - 4yz + z^2 + 3xz. \end{equation*}
Solution.
We compute that
\begin{align*} \|\mathbf{v}\| \amp = \sqrt{(-1)^2 + (2)^2 + (2)^2} = \sqrt{9} = 3 \end{align*}
So the unit vector in the direction of \(\mathbf{v}\) is the vector
\begin{align*} \mathbf{u} \amp = \frac{\mathbf{v}}{\|\mathbf{v}\|} = -\frac{1}{3}\mathbf{i} + \frac{2}{3}\mathbf{j} + \frac{2}{3}\mathbf{k} \end{align*}
Using the formula for the directional derivative, we find that
\begin{align*} D_{\mathbf{u}}f(x,y,z) \amp = \nabla f(x,y,z) \cdot \mathbf{u}\\ \amp = \begin{bmatrix}10x - 2y\\ -2x + 2y - 4z\\ -4y + 2z + 3x\end{bmatrix} \cdot \mathbf{u}\\ \amp = - \frac{10x - 2y}{3} + 2 \frac{-2x + 2y - 4z}{3} + 2 \frac{-4y + 2z + 3x}{3}\text{.} \end{align*}
At the point \((1, -2, 3)\text{,}\) we evaluate that
\begin{equation*} D_{\mathbf{u}}f(1,-2,3) = (23)\left(-\frac{1}{3}\right) + (-18)\left(\frac{2}{3}\right) + (17)\left(\frac{2}{3}\right) = \frac{-25}{3} \end{equation*}