Skip to main content

Gradient descent and Newton’s method in one dimension

When f’(x) = 0 cannot be solved by hand, search for the minimum. Derive gradient descent from the tangent line and Newton’s method from the tangent line of the slope, run both on x squared plus e to the minus x, compare them with SciPy, and see how the start decides which valley you reach.
M04.4.4L1 Core25 minIn review

Before this lesson

M04.4.2 The second derivative and convexity M04.4.3 Optimisation problems solved exactly

After this lesson you can

  • Derive the gradient-descent step from the linear approximation, and explain why a small enough step lowers the function.
  • Derive Newton’s step by applying the tangent-line approximation to the derivative, and show that it reaches the minimum of the running loss in one step.
  • Carry out both methods by hand and in Python, and compare how fast they close in on the minimum.
  • Explain why the starting point decides which minimum a search reaches, and why Newton’s method can head for a maximum.

In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.

The question

The function \({f(x) = x^2 + e^{-x}}\) is a smooth bowl: its second derivative \({2 + e^{-x}}\) is always positive, so it is convex. Its lowest point is where \({f'(x) = 2x - e^{-x} = 0}\), that is where \({2x = e^{-x}}\). No rearranging isolates \(x\), because it sits both outside and inside the exponential. Yet a computer finds the bottom to fifteen digits in a few steps. How? The answer is the method that trains almost every model in machine learning.

From the last lesson

1. Which points are the candidates in the closed-interval method?

Show answer

The critical points inside the interval, and the two ends.

2. Which number \(c\) minimises \({\sum (y_i - c)^2}\)?

Show answer

The mean, \({\bar{y}}\): the derivative \({-2(\sum y_i - nc)}\) is zero there, and \({S'' = 2n > 0}\).

Intuition

Stand on the curve at a point you can compute, \({x = 0}\). The tangent there has slope \({f'(0) = 0 - e^0 = -1}\): the ground falls to the right. So step right. Two questions remain: how far, and can the shape of the curve tell us more than a straight line can?

Two panels, each showing the blue bowl f of x equals x squared plus e to the minus x for x from about minus 0.6 to 1.2, with an orange dot at the start, x equals 0, and a star at the lowest point near x equals 0.35. Left, titled Gradient descent: follow the slope: a green tangent line through the start, labelled tangent, slope minus 1, and a green arrow along the bottom from 0 to 0.25, labelled step 0.25, with a hollow orange dot on the curve above 0.25. Right, titled Newton: jump to its bottom: a dashed vermillion parabola that hugs the curve near the start, labelled parabola with the same slope and bend, and a vermillion arrow from 0 to one third, labelled jump 1/3, ending under the bottom of the parabola, right beside the star.
M04.4.4-F1

Two ways downhill from \({x = 0}\). Gradient descent steps against the slope, a distance proportional to it. Newton’s method fits the parabola with the same height, slope and bend, and jumps to its bottom, which lands almost on the star.

Gradient descent steps against the slope, and the steeper the ground, the longer the step. Newton’s method also uses the curvature: it fits the parabola that matches \(f\) in height, slope and bend at the current point, and jumps straight to the bottom of that parabola.

Build it up

Step 1. The local picture. For a small change \(s\), the linear approximation (M04.2.2) says

\[ f(x + s) \approx f(x) + f'(x)\,s . \]

Step 2. Step against the slope. Choose \({s = -\eta f'(x)}\), with a small number \({\eta > 0}\) called the learning rate. Then

\[ \begin{aligned} &f\big(x - \eta f'(x)\big) \\ &\quad \approx f(x) - \eta f'(x)^2 . \end{aligned} \]

The change \({-\eta f'(x)^2}\) is negative whenever \({f'(x) \neq 0}\): a small step against the slope goes downhill, whichever way the slope points. Repeating it is gradient descent.

Step 3. Newton: aim for a flat point. We want a point where \({f' = 0}\). Apply the same linear approximation to the function \({f'}\), whose slope is \({f''}\):

\[ f'(x + s) \approx f'(x) + f''(x)\,s . \]

The right-hand side is zero when \({s = -f'(x)/f''(x)}\). That is Newton’s step: follow the tangent line of \({f'}\) to where it crosses zero.

A graph of the sky-blue slope curve f prime of x equals 2 x minus e to the minus x, rising from about minus 2.6 at x equals minus 0.5 to 1.6 at x equals 1, and crossing zero at a ringed point labelled f prime equals 0 at x star approximately 0.352. An orange dot marks the start at x equals 0, height minus 1. A green straight line, labelled tangent of f prime, slope f double prime of 0 equals 3, runs through the start and crosses zero at a vermillion dot at one third, labelled crosses zero at 1/3, just left of the true zero.
M04.4.4-F2

Newton’s step seen on the graph of the slope. The tangent line of \({f'}\) at the start crosses zero at \({1/3}\), close to where \({f'}\) itself crosses zero, the bottom of \(f\).

Step 4. The same step, from the parabola. The parabola with the height, slope and bend of \(f\) at \(x\) is \({q(s)} = {f(x) + f'(x)s + \tfrac{1}{2}f''(x)s^2}\). Its slope \({f'(x) + f''(x)s}\) is zero at the same \(s\). When \({f''(x) > 0}\) that is the bottom of a cup; when \({f''(x) < 0}\), the top of a cap. This parabola is a Taylor polynomial, met properly in M04.8.3.

The formal statement

NoteDefinitions: gradient descent and Newton’s method

Start at \({x_0}\). Gradient descent with learning rate \({\eta > 0}\) repeats

\[ x_{t+1} = x_t - \eta\, f'(x_t) . \]

Newton’s method repeats, wherever \({f''(x_t) \neq 0}\),

\[ x_{t+1} = x_t - \frac{f'(x_t)}{f''(x_t)} . \]

Both stand still exactly where \({f' = 0}\): at a critical point. With small enough steps, gradient descent at least moves downhill on the way there; Newton’s step heads for the top of a cap wherever \({f'' < 0}\).

Small steps go downhill. If \({f'(x) \neq 0}\), then \({f(x - \eta f'(x)) < f(x)}\) for every small enough \({\eta > 0}\) (proved in the block below). How small is small enough depends on the curvature. On the running loss, with \({L'(w) = 15w - 23.5}\), a learning rate of 0.05 takes \({w = 0}\) to 1.175, 1.469 and 1.542, closing in on \({47/30 \approx 1.567}\) from one side. With 0.12 the first step lands at 2.82, past the bottom. Lesson ML01.5.3 works out exactly which learning rates converge.

Newton on a parabola takes one step. The running loss has \({L''(w) = 15}\), so from any \(w\)

\[ \begin{aligned} &w - \frac{15w - 23.5}{15} \\ &\quad = w - w + \frac{23.5}{15} = \frac{47}{30} . \end{aligned} \]

Every start lands on the minimum at once, because the parabola Newton fits to \(L\) is \(L\) itself.

Go deeper: proof that a small enough step goes downhill

Write the error of the linear approximation as

\[ \begin{aligned} E(s) &= f(x + s) - f(x) \\ &\quad - f'(x)\,s . \end{aligned} \]

By the definition of the derivative, the ratio \({r = E(s)/s}\) tends to 0 as \({s \to 0}\). Put \({s = -\eta f'(x)}\). Then \({f'(x)s = -\eta f'(x)^2}\) and \({E(s)} = {rs} = {-\eta f'(x)\,r}\), so the change \({\Delta} = {f(x + s) - f(x)}\) is

\[ \begin{aligned} \Delta &= f'(x)\,s + E(s) \\ &= -\eta f'(x)\big(f'(x) + r\big) . \end{aligned} \]

As \({\eta \to 0}\), \(s\) and \(r\) shrink to 0, so \({f'(x)}\) times the bracket tends to \({f'(x)^2 > 0}\). Once it is positive, \(\Delta\) is \({-\eta}\) times a positive number: negative.

Worked example

Take steps of both methods on \({f(x) = x^2 + e^{-x}}\) from \({x_0 = 0}\), with \({\eta = 0.25}\) for gradient descent. We need \({f'(x) = 2x - e^{-x}}\) and \({f''(x) = 2 + e^{-x}}\).

Gradient descent. \({f'(0) = -1}\), so \({x_1} = {0 - 0.25 \times (-1)} = {0.25}\). Then \({f'(0.25)} = {0.5 - e^{-0.25}} = {0.5 - 0.7788} = {-0.2788}\), so

\[ \begin{aligned} x_2 &= 0.25 + 0.25 \times 0.2788 \\ &= 0.3197 . \end{aligned} \]

Newton. \({f'(0) = -1}\) and \({f''(0) = 2 + 1 = 3}\), so \({x_1 = 0 + 1/3 \approx 0.3333}\). Then \({f'(1/3)} = {0.6667 - 0.7165} = {-0.0499}\) and \({f''(1/3) = 2.7165}\), so

\[ \begin{aligned} x_2 &= 0.3333 + \frac{0.0499}{2.7165} \\ &= 0.3517 . \end{aligned} \]

Left: a table of steps t from 0 to 3. Gradient descent, in green boxes: 0, 0.25, 0.3197, 0.3414. Newton, in vermillion boxes: 0, 0.3333, 0.351689, 0.3517337110. Below: the bottom, x star equals 0.3517337112 and so on. Right, titled Distance to the bottom: the distance from each step to x star on a logarithmic scale from 1 down to 10 to the minus 10. The green gradient-descent line falls steadily, labelled times 0.32 each step; the vermillion Newton line plunges from 0.35 to about 2.6 times 10 to the minus 10 by step 3, labelled about squared each step.
M04.4.4-F3

The first steps of both methods. Gradient descent shrinks the distance to the bottom by a steady factor; Newton’s method roughly squares it.

The bottom is at \({x^* \approx 0.351734}\), where \({f(x^*) \approx 0.8272}\). The distance of gradient descent from it runs 0.35, 0.10, 0.032, 0.010, cut each time by a factor that settles near 0.32. Newton’s runs 0.35, 0.018, 0.000044 and then about \({2.6 \times 10^{-10}}\): roughly squared at each step, so the number of correct digits roughly doubles. The speed has a price. Each Newton step needs the second derivative, which for a model with a million parameters becomes a table of a million times a million numbers (M08.5). That is why deep learning mostly uses gradient descent.

Code

From scratch, each method is a loop of one line:

import math

def f(x):
    return x**2 + math.exp(-x)

def f_slope(x):
    return 2 * x - math.exp(-x)

def f_bend(x):
    return 2 + math.exp(-x)

def gradient_descent(slope, x, eta, steps):
    path = [x]
    for _ in range(steps):
        x = x - eta * slope(x)
        path.append(x)
    return path

def newton(slope, bend, x, steps):
    path = [x]
    for _ in range(steps):
        x = x - slope(x) / bend(x)
        path.append(x)
    return path

gd = gradient_descent(f_slope, 0.0, 0.25, 30)
nt = newton(f_slope, f_bend, 0.0, 4)
print("gradient descent:", [round(x, 4) for x in gd[:4]], "  after 30 steps:", round(gd[-1], 10))
print("Newton:          ", [round(x, 10) for x in nt])
gradient descent: [0.0, 0.25, 0.3197, 0.3414]   after 30 steps: 0.3517337112
Newton:           [0.0, 0.3333333333, 0.3516893316, 0.351733711, 0.3517337112]

The library call, scipy.optimize.minimize_scalar, uses Brent’s method by default and needs only \(f\) itself:

from scipy.optimize import minimize_scalar

result = minimize_scalar(f)
print(f"minimize_scalar: x = {result.x:.7f}, f(x) = {result.fun:.7f}")
minimize_scalar: x = 0.3517337, f(x) = 0.8271840

On the running loss Newton needs one step from anywhere. On the double well the start decides the valley, and Newton from 0.3 runs to the peak:

loss_slope = lambda w: 15 * w - 23.5
print("Newton on the running loss, one step from w = 0, 3, -1.7:",
      [newton(loss_slope, lambda w: 15.0, w0, 1)[1] for w0 in (0, 3, -1.7)])

well = lambda x: x**4 - 2 * x**2
well_slope = lambda x: 4 * x**3 - 4 * x
well_bend = lambda x: 12 * x**2 - 4
for x0 in (-0.3, 0.3, 1.5):
    print(f"gradient descent (eta = 0.1) from {x0:4}: ends at {gradient_descent(well_slope, x0, 0.1, 200)[-1]:.4f}")
print("Newton from 0.3:", [f"{x:.2e}" for x in newton(well_slope, well_bend, 0.3, 3)])
print("minimize_scalar on the double well:", round(minimize_scalar(well).x, 4))
print("minimize_scalar on the running cubic:", round(minimize_scalar(lambda x: x**3 - 3 * x).x, 4))
Newton on the running loss, one step from w = 0, 3, -1.7: [1.5666666666666667, 1.5666666666666667, 1.5666666666666667]
gradient descent (eta = 0.1) from -0.3: ends at -1.0000
gradient descent (eta = 0.1) from  0.3: ends at 1.0000
gradient descent (eta = 0.1) from  1.5: ends at 1.0000
Newton from 0.3: ['3.00e-01', '-7.40e-02', '8.23e-04', '-1.12e-09']
minimize_scalar on the double well: 1.0
minimize_scalar on the running cubic: 1.0

Explore

M04.4-X1

Descent explorer

Choose a curve, then drag the ball or move it with the slider, and take gradient or Newton steps. Try the double well from 0.3 with both methods, and the running loss with a learning rate above 0.07, where every step overshoots.

A graph of the blue double well f of x equals x to the fourth minus 2 x squared, with stars at its two valleys at x equals minus 1 and 1 and a ring at the peak at 0. Two runs of gradient descent with learning rate 0.1 are drawn as green arrows from step to step: one starts at an orange dot at x equals minus 0.3 and walks down to the left valley; the other starts at x equals 1.5, overshoots to 0.75 on its first step and settles in the right valley.

Pitfalls

Two panels, each showing the blue double well with stars at its valleys at x equals minus 1 and 1, a ring at the peak at 0, and an orange start dot at x equals 0.3. Left, marked with a cross and titled Newton from 0.3: a dashed vermillion upside-down parabola touches the curve at the start, and vermillion arrows jump to minus 0.07 and then onto the peak; the note says Newton ends on the peak. Right, marked with a tick and titled Gradient descent from 0.3: green arrows step down the right-hand slope into the valley at 1, labelled steps of 0.1 f prime, downhill.
M04.4.4-F4

Wrong beside right, from the same start. Where \({f'' < 0}\), Newton’s parabola opens downward and its step heads for the peak; gradient descent walks down into a valley.

“Newton’s method finds the minimum.”

It finds a point where \({f' = 0}\), of any kind. On the double well from 0.3, where \({f''(0.3) = -2.92}\), its steps go to \({-0.074}\), then 0.0008, then the peak at 0. Check that \({f'' > 0}\) where it stops, or use a method that only moves downhill.

“Gradient descent finds the lowest point.”

It finds the valley that the start leads to. On the double well, with learning rate 0.1, starts between \({-1.5}\) and 0 end at \({-1}\) and starts between 0 and 1.5 end at 1. The library is no different: on the running cubic minimize_scalar reports \({x = 1}\) with value \({-2}\), a local minimum of a function that has no lowest point at all.

“A bigger learning rate is always faster.”

A step that is too big jumps past the bottom: on the running loss, 0.12 takes \({w = 0}\) to 2.82. Beyond a limit, worked out in ML01.5.3, the jumps grow and the run blows up.

Check yourself

Recall

1. Write the gradient-descent update and name \(\eta\).

Show answer

\({x_{t+1} = x_t - \eta f'(x_t)}\). \(\eta\) is the learning rate.

2. Why does a small step against the slope go downhill?

Show answer

By the linear approximation the change is about \({-\eta f'(x)^2}\), which is negative whenever \({f'(x) \neq 0}\).

3. Write Newton’s update, and say where it comes from.

Show answer

\({x_{t+1}} = {x_t - f'(x_t)/f''(x_t)}\): the point where the tangent line of \({f'}\) crosses zero, or equally the bottom of the parabola with the same height, slope and bend.

4. Why does Newton’s method reach the minimum of the running loss in one step?

Show answer

\({L'}\) is a straight line, so its tangent line is \({L'}\) itself, and following it to zero lands exactly on \({47/30}\).

5. Why can Newton’s method from 0.3 end at the peak of the double well?

Show answer

There \({f''(0.3) < 0}\), so its parabola opens downward and the step goes to the top. Newton’s method looks for \({f' = 0}\), not for a minimum.

Exercises

1. ★ From \({w = 1}\) on the running loss, take one gradient-descent step with \({\eta = 0.05}\) and one Newton step. Compare both with \({47/30 \approx 1.5667}\).

Show solution

\({L'(1)} = {15 - 23.5} = {-8.5}\). Gradient descent: \({w} = {1 - 0.05 \times (-8.5)} = {1.425}\), still 0.142 short. Newton: \({w} = {1 - (-8.5)/15} = {1 + 0.5667} = {1.5667}\), the minimum exactly.

2. ★★ For \({f(x) = x - \ln x}\) with \({x > 0}\), show that Newton’s step is \({x \mapsto 2x - x^2}\) and that the distance to the minimum at 1 is squared at every step. What happens from \({x_0 = 2}\)?

Show solution

\({f'(x) = 1 - 1/x}\) and \({f''(x) = 1/x^2}\), so the step is \({(1 - 1/x)\,x^2 = x^2 - x}\) and the new point is \({x - (x^2 - x) = 2x - x^2}\). Then \({1 - (2x - x^2)} = {1 - 2x + x^2} = {(1 - x)^2}\): the new distance is the old one squared. From 0.5 the distances are 0.5, 0.25, 0.0625, 0.0039. From 2 the distance 1 squares to 1 again, and indeed \({2 \times 2 - 4 = 0}\): the step lands on \({x = 0}\), where \({\ln x}\) is not defined. Newton’s method can jump out of the domain.

3. ★★★ Show that one Newton step on any parabola \({f(x) = ax^2 + bx + c}\) with \({a > 0}\) lands on its lowest point \({-b/(2a)}\) from every start, and that gradient descent with \({\eta = 1/(2a)}\) does the same. Why can no single learning rate do this for every parabola?

Show solution

\({f'(x) = 2ax + b}\) and \({f''(x) = 2a}\), so Newton gives \({x - (2ax + b)/(2a)} = {-b/(2a)}\), the lowest point found by completing the square (M01.3.3). Gradient descent gives \({x - \eta(2ax + b)}\), which is the same expression when \({\eta = 1/(2a)}\). So Newton’s method is gradient descent with the learning rate \({1/f''}\), chosen afresh from the curvature at every step. A fixed \(\eta\) matches only one curvature: on a parabola with a different \(a\) it falls short or jumps past the bottom.

Summary card

When \({f' = 0}\) has no formula, search: step against the slope, or jump to the bottom of the fitted parabola.

The anchor figure again: from the start at x equals 0 on the bowl x squared plus e to the minus x, gradient descent steps along the tangent's downhill direction, and Newton's method jumps to the bottom of a fitted parabola, landing next to the star.
M04.4.4-F1

Two ways downhill.

  • Gradient descent: \({x_{t+1} = x_t - \eta f'(x_t)}\); a small step lowers \(f\) by about \({\eta f'(x_t)^2}\).
  • Newton: \({x_{t+1}} = {x_t - f'(x_t)/f''(x_t)}\), the zero of the tangent line of \({f'}\); one step on a parabola.
  • Newton’s error roughly squares at each step; gradient descent’s shrinks by a steady factor.
  • Both stop at any flat point. The start decides which, and Newton can head for a peak.

Sources and next steps

Sources. Each was opened and checked against this lesson on 12 November 2026.

  • Marc Peter Deisenroth, A. Aldo Faisal and Cheng Soon Ong, Mathematics for Machine Learning, Chapter 7. The introduction works a one-dimensional polynomial with two minima and a maximum (equations 7.1 to 7.3) and shows that the starting point decides which minimum the negative gradient leads to; Section 7.1 gives the gradient-descent update (7.6), which lowers the function for a small enough step size (7.5).
  • Stephen Boyd and Lieven Vandenberghe, Convex Optimization, Section 9.5.1: the Newton step minimises the second-order approximation (9.28), lands on the exact minimiser when the function is quadratic, and in one dimension is the zero of the linear approximation of \({f'}\) (Figure 9.18).
  • Gilbert Strang and Edwin Herman, Calculus Volume 1 (OpenStax), Section 4.9: Newton’s method for solving an equation (Equation 4.8), built from the tangent line, and the ways it fails. This lesson applies it to the equation \({f'(x) = 0}\).
  • David Jerison, MIT OpenCourseWare 18.01SC, Session 33, “Newton’s Method”: linear approximation used to make better and better guesses, and the cases where it does not work.
  • SciPy developers, scipy.optimize.minimize_scalar (SciPy 1.18 reference): Brent’s method by default, the Bounded method when bounds are given.

Next. This completes the module. M04.10, partial derivatives, may be read straight after it, and ML01.5.2 The gradient and the update rule takes gradient descent to a line with two parameters. The course continues with M04.5, integrals as accumulated area.

This lesson unlocks

M04.5 Integrals as accumulated area M04.10 Partial derivatives, a first look ML01.5 Gradient descent from scratch

Your progress is saved in this browser only. No account, no tracking.
Back to top