Concept 04: Multivariable Gradients & Hill Climbing

▶ Interactive Demo: Potential Field & Gradient Visualizer

Open the interactive demo below to drag the robot on the 2D contour map and watch the gradient vector \nabla U (uphill) and steering force -\nabla U (downhill toward goal) update live.


1. The Real-World Problem: Hiking in the Fog

Imagine you are hiking blindfolded on a foggy, hilly mountain. Your elevation (height) is given by a 2D terrain surface z = f(x, y):

∇f (Uphill) -∇f (Downhill)
  1. Partial Derivative ∂f/∂x: If you freeze your North-South position and take one step East, how much does elevation change?
  2. Partial Derivative ∂f/∂y: If you freeze your East-West position and take one step North, how much does elevation change?
  3. The Gradient Vector ∇f: If you bundle both slopes into a 2D arrow [∂f/∂x, ∂f/∂y]ᵀ, this vector points in the direction of Steepest Ascent (the fastest way uphill).
  4. Gradient Descent -∇f: To walk downhill to the lowest valley floor as fast as possible, step in the exact opposite direction: -∇f.

2. Solving It in Code (Java & WPILib)

First-Principles Java: Numerical Gradient

// Loss function: Error as a function of shooter angle and flywheel RPM
public static double computeLoss(double angle, double rpm) {
    return Math.pow(angle - 45.0, 2) + 0.01 * Math.pow(rpm - 3500.0, 2);
}

// Numerical gradient estimation
double angle = 40.0;
double rpm = 3200.0;
double eps = 1e-5;

double gradAngle = (computeLoss(angle + eps, rpm) - computeLoss(angle - eps, rpm)) / (2 * eps);
double gradRpm   = (computeLoss(angle, rpm + eps) - computeLoss(angle, rpm - eps)) / (2 * eps);

System.out.printf("Gradient Vector: [dLoss/dAngle = %.2f, dLoss/dRPM = %.4f]%n", gradAngle, gradRpm);

3. Bridge to Machine Learning: Training Deep Neural Networks

In deep learning:


4. Review Checkpoints

Checkpoint 1

Given the loss function f(x, y) = x² + 3·y²:

  1. Find the partial derivatives ∂f/∂x and ∂f/∂y.
  2. Compute the gradient vector ∇f at (2, 1).

Solution:

  1. ∂f/∂x = 2·x, ∂f/∂y = 6·y.
  2. At (2, 1): ∇f = [2(2), 6(1)]ᵀ = [4, 6]ᵀ.

Checkpoint 2

Which direction should a robot or optimizer step to minimize loss as fast as possible?

Solution: In the direction of -∇f (Negative Gradient / Steepest Descent). At point (2, 1), step along [-4, -6]ᵀ.


← Concept 14: Integration & Accumulation
Module 4 Overview
Module 5: Probability →