Gradient Descent: Stepping Down a Parabola

OptimizationEasy+10 XP+10

Gradient Descent: Stepping Down a Parabola

Every optimiser in machine learning is a variation on one loop: look at the slope, step downhill, repeat. On a 1D parabola you can watch the whole thing happen.

For f(x) = a·x² + b·x + c, the slope is f'(x) = 2a·x + b. Gradient descent repeats:

1x = x - learning_rate * f'(x)

Task: write gradient_descent(a, b, start, learning_rate, steps) that runs exactly steps updates from start and returns the final x, rounded to 4 decimal places.

  • c never appears — shifting a function up or down doesn't change where its minimum sits.
  • steps = 0 returns start unchanged.

Try picturing what happens if learning_rate gets too big. Each step overshoots the bottom by more than it started from, and x spirals outward instead of settling — the same divergence you'd see in a real training run, just visible in one variable.

Walking Downhill, One Step at a Time

Step size stays fixed at α = 0.30 here.
-8-4048
new x = old x − α × slope
5.950 = 7.000 − 0.30 × 3.500
Steps taken: 0 · Loss now: 14.250

The slope tells you which way is uphill, so you subtract it to head downhill. Near the edge the ground is steep and the steps are long. As the ground flattens the slope shrinks, so the steps shrink with it, and the walk eases into the bottom instead of shooting past it.

Examples

a = 1.0
b = -4.0
start = 0.0
learning_rate = 0.1
steps = 50
returns 2.0
a = 2.0
b = 0.0
start = 5.0
learning_rate = 0.1
steps = 10
returns 0.0302

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run