Homework Assignment

HW 30 — Gradient Descent User-Defined Function

📘 Related: Lesson 30 🛠 MATLAB required

📖 Background

Gradient descent is the foundation of virtually all modern machine learning algorithms, from logistic regression to deep neural networks. Before applying it to complex models in later lessons, you will implement it as a clean, reusable MATLAB UDF.

The update rule at each iteration is:

\[ x^{k+1} = x^k - \gamma\, \nabla f(x^k) \]

where \(\gamma\) is the step size and \(\nabla f(x^k)\) is the gradient evaluated at the current point. The algorithm stops when the change in position is smaller than a threshold \(\epsilon\):

\[ \| x^{k+1} - x^k \|_2 \leq \epsilon \]

In this assignment, you will use MATLAB's symbolic toolbox to compute the gradient analytically, convert it to an anonymous function, and then pass it to your GD UDF.

Preflight

For each of the following functions, use MATLAB's symbolic toolbox to define the variables, write an anonymous function for \(f\), compute the symbolic gradient, convert it to an anonymous function using matlabFunction, and evaluate both \(f\) and \(\nabla f\) at the origin. The final output values should be of type double, not symbolic.

  1. \(f_{2D}(x_1, x_2) = 2x_1 + x_2 + 2x_1 x_2 + x_1^2 + 7\)
  2. \(f_{3D}(x_1, x_2, x_3) = 3x_1 + 2x_2 + x_3 + 2x_1 x_2 + x_1 x_3 + 2x_2 x_3 + x_2^2/4 + x_3^2/4 - 2\)
Note: The gradient is a vector, not a scalar. For the 2D function, \(\nabla f\) is a \(2 \times 1\) column vector; for the 3D function, it is \(3 \times 1\).

Main Tasks

Part 1 — Gradient Descent UDF

Write a MATLAB UDF called gradient_descent with the following interface:

The UDF should use the stopping criterion \(\|x^{k+1} - x^k\|_2 \leq \epsilon\).

Part 2 — Apply to a 2D Function

Apply your gradient_descent UDF to minimize the following function:

\[ f(x_1, x_2) = 4x_1^2 - 16x_1 + x_2^2 - 6x_2 + 25 \]

Use the following parameters:

Report the minimum location and function value. Verify your answer analytically by setting \(\nabla f = 0\) and solving by hand.

Part 3 — Contour Plot

Generate a contour plot of \(f(x_1, x_2)\) over the range \(x_1, x_2 \in [-2,\; 8]\). Mark the minimum found by your GD UDF with an × symbol. Include axis labels and a title.

💡 Hints