EasyOptimization

Implement Gradient Descent for a 1D Quadratic

Optimization

Easy

Problem

Implement vanilla gradient descent to minimize the one-dimensional quadratic

f(x) = ax^2 + bx + c

Starting from x_0, determine the gradient at the current value of x and apply the following update exactly steps times:

x ← x - mathrm{lr} , mathrm{gradient}

Return the final value of x as a Python float. Deriving the gradient of the quadratic is part of the task.

Theory

In machine learning, optimization means finding the set of parameters that makes a model perform as well as possible. We measure performance with a loss function (also called a cost function or objective function). The loss tells you how wrong your model is:

The goal is to find the parameter values that minimize the loss function. For a simple 1D quadratic:

f(x) = ax^2 + bx + c

the "parameter" is just x, and we want to find the value of x that makes f(x) as small as possible.

When a > 0, this is a parabola opening upward, with a single minimum. You could find it analytically: set the derivative to zero and solve. The minimum is at x^* = -\frac{b}{2a}.

But in real machine learning:

That iterative method is gradient descent.


The Derivative Points Uphill

The derivative of a function at a point tells you two things:

For our quadratic:

f'(x) = 2ax + b

The key insight: the negative of the derivative always points toward decreasing values. If the slope is positive, the negative slope is negative (go left). If the slope is negative, the negative slope is positive (go right). Either way, following the negative derivative moves you downhill.


The Gradient Descent Update Rule

Gradient descent uses this insight repeatedly. At each step:

  1. Compute the derivative at the current position
  2. Move a small step in the negative derivative direction
  3. Repeat

The update rule:

x_{t+1} = x_t - \eta \cdot f'(x_t)


Walking Through an Example

Let f(x) = x^2 - 4x + 5, so a = 1, b = -4, c = 5.

Starting at x_0 = 0 with learning rate \eta = 0.1:

Step 1:

Step 2:

Step 3:

Step 10:

Step 20:

Notice the pattern:


The Learning Rate: The Most Important Hyperparameter

The learning rate \eta controls how big each step is. It is the single most important number you choose when running gradient descent.

Too large (\eta too big):

Too small (\eta too tiny):

Just right:

In practice (with complex, non-quadratic loss functions), finding the "just right" learning rate requires experimentation. Common starting values are 0.01, 0.001, or 0.0001.


Convergence: How Fast Does It Get There?

For a convex quadratic, gradient descent converges exponentially (also called linear convergence):

For a = 1 and \eta = 0.1:

This exponential decay on a log scale looks like a straight line going down, which is why it is called "linear convergence."


From 1D Quadratic to Real Machine Learning

This 1D quadratic is the training ground for understanding optimization. Every concept here scales up:

But the core operation never changes: compute the gradient, take a step in the opposite direction.

Examples

Example 1

Input
a = 1.0, b = -4.0, c = 3.0, x0 = 0.0, lr = 0.1, steps = 50
Output
1.999971
Explanation
Repeated updates move x toward the minimum without jumping directly to it.

Example 2

Input
a = 2.0, b = 8.0, c = 0.0, x0 = 10.0, lr = 0.05, steps = 200
Output
-2.0

Hints

  1. Use a loop that performs exactly steps parameter updates.
  2. Compute the gradient at the current x before applying the learning-rate update.

Requirements

Constraints

Starter Code

def gradient_descent_quadratic(a: float, b: float, c: float, x0: float, lr: float, steps: int) -> float:
    """
    Returns the final scalar x after the requested iterations.
    """
    # Write code here
    pass

Test Cases

CaseMatches
Basicpublic
Variant 1public