Lesson 1 of 7
Objectives and gradient descent
Optimization means choosing variables to make a measurable score as small or as large as possible.
Core lesson · about 7 minutes · 3 guided parts
You are blindfolded on a smooth hill and want the valley. Feel the slope under your feet, step downhill, and repeat. The objective is height; your position is the variable.
Your learning route
Build the idea one piece at a time
- Model the goal before solving it
- Follow the negative gradient
- How a controller learns a useful setting instead of guessing
Model the goal before solving it
A solver only sees the score and the allowed choices, so a precise objective is a mathematical version of what success means.
An unconstrained minimization problem is written minₓ f(x). The vector x contains decisions and f maps each decision to one comparable cost.
A useful objective may combine data fit and regularisation: empirical loss measures mistakes, while a penalty discourages overly complex or risky choices.
Scaling matters. Adding quantities with wildly different units or magnitudes can make one term dominate for accidental reasons.
Decision variables must include only quantities the solver is allowed to change; measured data and fixed requirements belong in the objective as inputs, not choices.
A proxy loss should be tested for unintended shortcuts because the optimizer cannot know the human meaning that was omitted from its formula.
Regularisation strength creates a trade-off between fit and preference, so both terms should be scaled and reported rather than silently mixed across incompatible units.
minₓ f(x)Worked example
Write an objective for fitting y≈wx to points (1,2) and (2,4), with L2 penalty λw² and λ=0.1.
- The predictions are w and 2w.
- Squared errors are (w−2)² and (2w−4)².
- Average the errors and add the penalty.
- One valid objective is f(w)=[(w−2)²+(2w−4)²]/2+0.1w².
Answer: min_w [(w−2)²+(2w−4)²]/2+0.1w²
Follow the negative gradient
The gradient points uphill, so subtracting a small multiple gives the locally fastest first-order decrease.
Gradient descent updates xₖ₊₁=xₖ−η∇f(xₖ), where η>0 is the learning rate.
Too small a learning rate wastes steps; too large a rate can bounce across a valley, increase the objective, or diverge.
Stopping rules can use a small gradient norm, small objective improvement, a step budget, or validation performance. Each detects a different kind of 'done'.
The update comes from the local model f(x+Δ)≈f(x)+∇f^TΔ; among equal-length Euclidean steps, choosing Δ opposite the gradient lowers that linear term most.
On a quadratic, the largest curvature limits a stable constant learning rate because a step safe along a flat direction may overshoot along a steep one.
Objective decrease, gradient norm, parameter movement, and validation performance are separate stopping signals and can disagree near plateaus or noisy batches.
xₖ₊₁=xₖ−η∇f(xₖ)Worked example
Take two gradient-descent steps on f(x)=(x−3)² from x₀=0 with η=0.25.
- The derivative is f′(x)=2(x−3).
- At x₀=0, f′=−6, so x₁=0−0.25(−6)=1.5.
- At x₁=1.5, f′=−3, so x₂=1.5−0.25(−3)=2.25.
- The iterates move toward the minimum x=3.
Answer: x₁=1.5 and x₂=2.25
Practical deep dive for this lesson
How a controller learns a useful setting instead of guessing
Optimization begins by turning success into a measurable objective. Gradient descent then uses local slope to improve one setting at a time, while repeated checks make sure the score really means what we want.
Suppose a greenhouse controller chooses heater power x. Too little misses the target temperature, while too much wastes energy and may overshoot.
An objective f(x) combines those consequences into one number for each allowed setting. Smaller values must consistently mean a better decision.
Choosing the variables and score is not clerical work: if energy cost is omitted, the mathematical optimum may simply use maximum heat.
The derivative f′(x) estimates how the score changes for a small positive adjustment. A positive derivative means increasing x raises cost locally, so we should move left.
Gradient descent applies x_new=x−ηf′(x). Subtracting the derivative chooses the downhill direction, while η decides how far to trust the local slope.
A tiny η gives cautious but slow improvement. An oversized η can jump across the valley and land at a worse score or oscillate forever.
Recording f before and after each step makes the process inspectable. If the objective rises repeatedly, reduce the step or reconsider scaling and derivative correctness.
A small derivative is a stopping clue, not proof of success. The point may be a maximum, saddle, flat plateau, or boundary-limited solution.
The controller must also test the chosen setting on new days because weather variation can make an objective fitted to past measurements overconfident.
This same loop—define, differentiate, step, measure, validate—trains regression models, tunes physical designs, and calibrates parameters in simulations.
xₖ₊₁=xₖ−η∇f(xₖ)Worked example
For f(x)=(x−4)²+1, start at x₀=0 and take two gradient-descent steps with η=0.2.
- Differentiate the objective: f′(x)=2(x−4).
- At x₀=0, the derivative is −8, meaning a small increase in x lowers f.
- Update x₁=0−0.2(−8)=1.6.
- Check the score: f(0)=17, while f(1.6)=6.76, so the first step helped.
- At x₁=1.6, the derivative is 2(−2.4)=−4.8.
- Update x₂=1.6−0.2(−4.8)=2.56.
- The new score is f(2.56)=3.0736, again lower and closer to the minimum value 1.
- The remaining distance to x=4 shrinks but is not zero, so more steps or a justified stopping rule are needed.
Answer: x₁=1.6 and x₂=2.56; the objective falls from 17 to 6.76 to 3.0736.
Check your picture
If ∇f(x)=[3,−4], what is the unscaled steepest-descent direction?
Reveal answer
[−3,4].
This lesson versus the whole chapter
Your topic-specific practical example is above.
The final lesson collects five longer chapter-wide workflows for machine learning, LLMs, trading, physics, and everyday decisions.
Formula shelf
Symbols with meaning
xₖ₊₁=xₖ−η∇f(xₖ)Step opposite the local uphill direction.
f(θx+(1−θ)y)≤θf(x)+(1−θ)f(y)The graph lies below every chord.
κ=L/μMeasures how stretched a smooth strongly convex bowl is.
ℒ(x,λ)=f(x)+λᵀg(x)Combines an objective with equality constraints.
λᵢgᵢ(x)=0Only a binding inequality may have a nonzero multiplier.
gB=|B|⁻¹Σᵢ∈B∇ℓᵢA cheap random estimate of the full gradient.
θₜ₊₁=θₜ−ηm̂ₜ/(√v̂ₜ+ε)Adaptive update using first and second gradient moments.
sign(z)max(|z|−λ,0)Proximal map for an L1 penalty that creates exact zeros.
xₖ₊₁=proxηh(xₖ−η∇f(xₖ))Combines a smooth gradient step with an exact nonsmooth subproblem.
f(x+αp)≤f(x)+cα∇fᵀpRequires sufficient objective decrease before accepting a step.
Before moving on
You are ready when you can…
- Translate a real goal into variables, an objective, and constraints.
- Run and diagnose gradient descent with a sensible stopping rule.
- Recognise convex structure and explain conditioning.
- Solve small Lagrange and KKT problems.
- Reason about nonconvex landscapes and stochastic training methods.
- Use subgradients, soft-thresholding, proximal gradient, and coordinate descent on simple nonsmooth objectives.
- Choose and verify step lengths with Armijo backtracking and descent-direction checks.