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):
xis your coordinate East-West.yis your coordinate North-South.
- Partial Derivative
∂f/∂x: If you freeze your North-South position and take one step East, how much does elevation change? - Partial Derivative
∂f/∂y: If you freeze your East-West position and take one step North, how much does elevation change? - 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). - 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:
- A neural network has an error loss function
Loss(weights). - Training the AI means finding the lowest point on this loss mountain using Gradient Descent:
weights_new = weights_old - learning_rate · ∇Loss - Calculating these partial derivatives layer by layer using the chain rule is called Backpropagation!
4. Review Checkpoints
Checkpoint 1
Given the loss function f(x, y) = x² + 3·y²:
- Find the partial derivatives
∂f/∂xand∂f/∂y. - Compute the gradient vector
∇fat(2, 1).
Solution:
∂f/∂x = 2·x,∂f/∂y = 6·y.- 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]ᵀ.