Skip to main content

Taylor polynomials

The tangent line matches a curve’s height and slope; a polynomial of degree n can match its first n derivatives too. Derive the coefficients f^(k)(a)/k!, build e^x, sin x and cos x term by term, and see the line and the parabola that gradient descent and Newton’s method put in place of a loss.
M04.8.3L1 Core25 minIn review

Before this lesson

M04.8.2 Does the sum settle? Convergence tests M04.2.2 The tangent line and linear approximation M04.3.2 Exponentials, sine and cosine M04.4.4 Gradient descent and Newton's method in one dimension

After this lesson you can

  • Derive the coefficients f^(k)(a)/k! of the polynomial that matches a function’s value and first n derivatives at a point.
  • Build the Taylor polynomials of e^x, sin x and cos x at 0 term by term, and recognise the geometric series as those of 1/(1 - x).
  • Explain a gradient step as trusting T_1 and a Newton step as jumping to the bottom of T_2, and show that T_2 of the running loss is the loss itself.
  • Evaluate Taylor polynomials from scratch and with numpy.polynomial.Polynomial, and measure their errors.

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 arithmetic a processor does fastest is adding and multiplying, and a polynomial needs nothing more. Yet numpy.exp(0.5) returns 1.64872127…, a value of \({e^x}\). If a polynomial can stand in for \({e^x}\) near 0, which one is it, and how good is it? Asked of a loss near the current weight, the same question is behind every training step: gradient descent treats the loss as a straight line, and Newton’s method treats it as a parabola.

From the last lesson

1. For which \(p\) does \({\sum 1/k^p}\) converge?

Show answer

Exactly for \({p > 1}\).

2. What does the ratio test say when the ratios tend to \({\rho = 1}\)?

Show answer

Nothing: some such series converge, such as \({\sum 1/k^2}\), and some diverge, such as \({\sum 1/k}\).

Intuition

The tangent line of M04.2.2 is the best straight line near a point: it has the curve’s height and slope there. A parabola can match the bend as well, a cubic the way the bend changes, and so on. Each extra term lets the polynomial follow the curve a little further from the point.

Two graphs. Left, titled e to the x equals 1 plus x plus x squared over 2 plus x cubed over 6: the thick blue curve e to the x for x from minus 2.6 to 2.1, with an orange dot at the centre 0, height 1. Three vermillion polynomials pass through the dot: a dotted straight line T 1, a dashed parabola T 2, and a solid cubic T 3. Near the dot all four agree; further out the line leaves the curve first, the parabola next, and the cubic stays close longest. Right, titled sin x equals x minus x cubed over 6 plus x to the fifth over 120 minus and so on: the thick blue sine curve for x from 0 to 5.5, with an orange dot at the centre 0 and vermillion polynomials T 1, T 3, T 5 and T 7. Each leaves the sine curve further out than the one before: the line T 1 near x equals 1, T 3 near 2.5, T 5 near 3.5 and T 7 near 4.2.
M04.8.3-F1

Taylor polynomials building up at the centre 0. Each one matches one more derivative than the last, and follows the curve further out.

These polynomials are the Taylor polynomials of the function about the point, which is called the centre \(a\). The one of degree \(n\) is written \({T_n(x)}\).

Build it up

Step 1. Degree 0: match the value. \({T_0(x) = f(a)}\), a flat line at the right height.

Step 2. Degree 1: match the slope too. \({T_1(x) = f(a) + f'(a)(x - a)}\), the tangent line.

Step 3. Degree 2: match the bend. Add a term \({c_2 (x - a)^2}\). At \({x = a}\) both \({(x - a)^2}\) and its slope \({2(x - a)}\) are 0, so the value and slope still match. Its second derivative is \({2c_2}\), and that of \({T_1}\) is 0, so

\[ \begin{aligned} T_2''(a) &= 2c_2 = f''(a) , \\ \text{so}\quad c_2 &= \frac{f''(a)}{2} . \end{aligned} \]

Step 4. Degree 3. Add \({c_3 (x - a)^3}\). At \(a\) it and its first two derivatives, \({3(x - a)^2}\) and \({6(x - a)}\), are 0, so nothing already matched is disturbed. Its third derivative is \({6c_3 = 3!\,c_3}\), so \({c_3 = f'''(a)/3!}\).

A four by four table. Its rows are the powers 1, x minus a, x minus a squared and x minus a cubed, written in vermillion. Its columns, in green, are the value, the slope, the second and the third derivative at x equals a. Every cell is 0 except the diagonal, highlighted in yellow: 0 factorial equals 1, 1 factorial equals 1, 2 factorial equals 2, and 3 factorial equals 6. Title: each power controls one derivative at a, and gives it k factorial.
M04.8.3-F2

At \({x = a}\) the power \({(x - a)^k}\) has exactly one derivative that is not zero: the \(k\)-th, which is \({k!}\). So the coefficient of \({(x - a)^k}\) controls the \(k\)-th derivative alone.

The pattern is the whole idea. Each power touches one derivative at \(a\), so the coefficients can be chosen one at a time, and the \(k\)-th must be \({f^{(k)}(a)/k!}\).

The formal statement

NoteDefinition: the Taylor polynomial

If \(f\) has \(n\) derivatives at \(a\), its Taylor polynomial of degree \(n\) about \(a\) is

\[ \begin{aligned} T_n(x) &= \sum_{k=0}^{n} \frac{f^{(k)}(a)}{k!}\,(x - a)^k \\ &= f(a) + f'(a)(x - a) \\ &\quad + \frac{f''(a)}{2!}(x - a)^2 + \cdots \\ &\quad + \frac{f^{(n)}(a)}{n!}(x - a)^n . \end{aligned} \]

It is the only polynomial of degree at most \(n\) whose value and first \(n\) derivatives at \(a\) equal those of \(f\).

Proof. Any polynomial of degree at most \(n\) can be written in powers of \({x - a}\): put \({x = (x - a) + a}\) into each power of \(x\) and multiply out with the binomial theorem (M01.8). So take \({p(x) = \sum_{k=0}^n c_k (x - a)^k}\). Differentiating \({(x - a)^k}\) \(j\) times gives

\[ \begin{aligned} &k(k - 1)\cdots(k - j + 1)\,(x - a)^{k-j} \end{aligned} \]

when \({j \le k}\), and 0 when \({j > k}\). At \({x = a}\) the factor \({(x - a)^{k-j}}\) is 0 unless \({j = k}\), when it is 1 and the product in front is \({k!}\). So \({p^{(j)}(a) = j!\,c_j}\), and matching \({p^{(j)}(a) = f^{(j)}(a)}\) forces \({c_j = f^{(j)}(a)/j!}\) for every \(j\). That choice does match, so there is exactly one such polynomial. One consequence: a polynomial of degree at most \(n\) is its own \({T_n}\), about any centre, because it matches itself.

Four functions at 0. Every derivative of \({e^x}\) is \({e^x}\), which is 1 at 0. The derivatives of \({\sin x}\) cycle through \({\cos x}\), \({-\sin x}\), \({-\cos x}\) and \({\sin x}\), with values 1, 0, \({-1}\), 0 at 0; those of \({\cos x}\) take the values 0, \({-1}\), 0, 1. For \({1/(1 - x)}\), the chain rule (M04.3.4) applied \(k\) times gives \({k!/(1 - x)^{k+1}}\), which is \({k!}\) at 0. So

\[ \begin{aligned} e^x &\approx 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} , \\ \sin x &\approx x - \frac{x^3}{3!} + \frac{x^5}{5!} , \\ \cos x &\approx 1 - \frac{x^2}{2!} + \frac{x^4}{4!} , \\ \frac{1}{1 - x} &\approx 1 + x + x^2 + x^3 . \end{aligned} \]

The sine has only odd powers, so its \({T_3}\) and \({T_4}\) are the same polynomial; the cosine has only even powers. The last line is the geometric series of M04.8.1: its Taylor polynomials are its partial sums, and its error is exactly \({x^{n+1}/(1 - x)}\), the gap of M04.8.1.

Two Taylor polynomials that every optimiser uses. About the current weight \({w_t}\) of a loss \(L\),

\[ \begin{aligned} \cmodel{T_1(w)} &= L(w_t) \\ &\quad + \cgrad{L'(w_t)}\,(w - w_t) , \\ \cmodel{T_2(w)} &= T_1(w) \\ &\quad + \tfrac12 L''(w_t)\,(w - w_t)^2 . \end{aligned} \]

A gradient step moves a short way down the line \({T_1}\), trusting it near \({w_t}\). Newton’s step (M04.4.4) jumps to the lowest point of the parabola \({T_2}\). For the running loss about \({w_t = 1}\): \({L(1) = 2.5}\), \({L'(1) = -8.5}\) and \({L'' = 15}\), so

\[ \begin{aligned} T_2(w) &= 2.5 - 8.5(w - 1) \\ &\quad + 7.5(w - 1)^2 \\ &= 2.5 - 8.5w + 8.5 \\ &\quad + 7.5w^2 - 15w + 7.5 \\ &= 7.5w^2 - 23.5w + 18.5 , \end{aligned} \]

which is \(L\) itself, as the consequence above promised. That is why Newton’s method lands on \({47/30}\) in one step.

Two graphs of the purple running loss L of w for w from 0.55 to 2.65, each with a star at its lowest point near 1.57 and an orange dot at w equals 1, height 2.5. Left, titled a gradient step trusts T 1: a dashed vermillion straight line, labelled T 1: the tangent line, touches the curve at the dot; a green arrow below the axis runs from 1 to 1.425, labelled step 0.425, and a hollow dot marks the loss at 1.425. Right, titled Newton jumps to the bottom of T 2: a dashed vermillion parabola, labelled T 2 equals L exactly, lies right on top of the loss curve, and a vermillion arrow below the axis runs from 1 to 47 over 30, under the star, labelled jump to 47 over 30.
M04.8.3-F4

Two Taylor polynomials of the running loss at \({w = 1}\). A gradient step with \({\eta = 0.05}\) moves 0.425 along \({T_1}\); Newton’s step jumps to the bottom of \({T_2}\), which is the loss itself.

The line promises more than it delivers: along \({T_1}\) the step to 1.425 lowers the loss by \({8.5 \times 0.425 \approx 3.61}\), but the loss falls only to 0.24, by 2.26. The difference, \({7.5 \times 0.425^2 \approx 1.35}\), is exactly the bend term that \({T_2}\) adds.

Worked example

Estimate \({e^{0.5}}\) with \({T_0}\) to \({T_4}\) about 0. Each new term is \({0.5^k/k!}\), which is the term before times \({0.5/k}\):

\[ \begin{aligned} T_4(0.5) &= 1 + 0.5 + 0.125 \\ &\quad + 0.0208333 + 0.0026042 \\ &= 1.6484375 . \end{aligned} \]

Left: a table with columns k, new term and total T k of 0.5. Row 0: 1 and 1. Row 1: 0.5 and 1.5. Row 2: 0.125 and 1.625. Row 3: 0.0208333 and 1.6458333. Row 4: 0.0026042 and 1.6484375, highlighted in yellow. Underneath: exact, e to the 0.5 equals 1.6487213 and so on. Right, titled error of T k of 0.5: purple dots of the error on a logarithmic scale from 1 down to about 10 to the minus 8, for degrees 0 to 8, falling along a curve that bends downwards, labelled shrinks faster and faster.
M04.8.3-F3

The worked example as a table: one new term per row and the running total beside it. On the right, the error after each degree falls faster and faster, because each new term is a smaller fraction of the last.

Against \({e^{0.5} = 1.6487213}\), the errors of \({T_0}\) to \({T_4}\) are 0.649, 0.149, 0.0237, 0.00289 and 0.000284. Each error is a little more than the next term, \({0.5^5/5! \approx 0.00026}\) for the last: the terms that were left out add up to not much more than the first of them.

The same way, \({T_3(0.5) = 0.5 - 0.125/6}\), which is 0.4791667, estimates \({\sin 0.5 = 0.4794255}\) with an error of 0.00026; and \({T_4(0.5) = 1 - 0.125 + 0.0026042}\), which is 0.8776042, estimates \({\cos 0.5 = 0.8775826}\) with an error of 0.00002.

Code

From scratch: the coefficients \({f^{(k)}(a)/k!}\), and Horner’s rule, which evaluates a polynomial with one multiplication and one addition per term.

import math

def taylor_coefficients(derivatives):
    """Coefficients f^(k)(a) / k! from the list f(a), f'(a), f''(a), ..."""
    return [d / math.factorial(k) for k, d in enumerate(derivatives)]

def evaluate(coefficients, a, x):
    """c_0 + c_1 (x - a) + ... + c_n (x - a)^n, by Horner's rule."""
    total = 0.0
    for c in reversed(coefficients):
        total = total * (x - a) + c
    return total

exp_c = taylor_coefficients([1] * 13)                     # every derivative of e^x is 1 at 0
sin_c = taylor_coefficients([0, 1, 0, -1, 0, 1])          # sin, cos, -sin, -cos, ... at 0
cos_c = taylor_coefficients([1, 0, -1, 0, 1])
for n in range(5):
    print(f"T_{n}(0.5) = {evaluate(exp_c[:n + 1], 0, 0.5):.7f}")
print("e^0.5 =", math.exp(0.5), "  T_12(0.5) =", evaluate(exp_c, 0, 0.5))
print("sin: T_4 =", round(evaluate(sin_c[:5], 0, 0.5), 7), " T_5 =", round(evaluate(sin_c, 0, 0.5), 7),
      " sin 0.5 =", round(math.sin(0.5), 7))
print("cos: T_4 =", round(evaluate(cos_c, 0, 0.5), 7), " cos 0.5 =", round(math.cos(0.5), 7))
T_0(0.5) = 1.0000000
T_1(0.5) = 1.5000000
T_2(0.5) = 1.6250000
T_3(0.5) = 1.6458333
T_4(0.5) = 1.6484375
e^0.5 = 1.6487212707001282   T_12(0.5) = 1.6487212707001078
sin: T_4 = 0.4791667  T_5 = 0.4794271  sin 0.5 = 0.4794255
cos: T_4 = 0.8776042  cos 0.5 = 0.8775826

The library version is numpy.polynomial.Polynomial, which stores the coefficients from the lowest power up and can be called like a function and differentiated. Here it rebuilds the running loss from its Taylor polynomial at \({w = 1}\):

import numpy as np
from numpy.polynomial import Polynomial

T4 = Polynomial(exp_c[:5])                                # 1 + x + x^2/2 + x^3/6 + x^4/24
print("T_4(0.5) with Polynomial:", T4(0.5))

L = Polynomial([18.5, -23.5, 7.5])                        # the running loss 18.5 - 23.5 w + 7.5 w^2
d0, d1, d2 = L(1.0), L.deriv()(1.0), L.deriv(2)(1.0)      # L(1), L'(1), L''(1)
s = Polynomial([-1.0, 1.0])                               # the power (w - 1)
T2 = d0 + d1 * s + d2 / 2 * s**2
print("L(1), L'(1), L''(1):", d0, d1, d2)
print("T_2 about 1, multiplied out:", T2.coef)
print("Newton's step from 1:", 1 - d1 / d2, "   47/30 =", 47 / 30)
w1 = 1 - 0.05 * d1
print("drop promised by T_1:", round(-d1 * (w1 - 1), 4), "  actual drop:", round(L(1.0) - L(w1), 4))
T_4(0.5) with Polynomial: 1.6484375
L(1), L'(1), L''(1): 2.5 -8.5 15.0
T_2 about 1, multiplied out: [ 18.5 -23.5   7.5]
Newton's step from 1: 1.5666666666666667    47/30 = 1.5666666666666667
drop promised by T_1: 3.6125   actual drop: 2.2578

Explore

M04.8-X1

Taylor builder

Choose a function, then step the degree up from 0 and watch each polynomial hug the curve further out; drag the probe to read the error anywhere. For \({\ln(1 + x)}\) and \({1/(1 - x)}\), put the probe beyond 1 and raise the degree: there, more terms end up making things worse.

Two graphs of Taylor polynomials about 0. Left: the blue curve e to the x with the vermillion polynomials T 1, a line, T 2, a parabola, and T 3, a cubic, which hug the curve over a wider and wider stretch around 0. Right: the blue sine curve with T 1, T 3, T 5 and T 7, each following the curve further from 0 than the one before.

Pitfalls

Two graphs of the blue sine curve for x from 0 to 5.3, each with a dashed vermillion Taylor polynomial, an orange dot at its centre and a purple bar or dot at x equals 4 for the error. Left, marked with a cross and titled T 5 about 0, used at 4: the polynomial follows the sine curve up to about x equals 2.5 and then shoots upward; at x equals 4 a long purple bar from the curve at minus 0.76 up to the polynomial at 1.87 is labelled error 2.62. Right, marked with a tick and titled T 3 about pi, used at 4: the polynomial centred at the dot on the curve at pi follows the sine curve closely from 2 to 4.5, and at x equals 4 the error, labelled error 0.004, is too small to see.
M04.8.3-F5

Wrong beside right. \({T_5}\) about 0 is excellent near 0 and useless at 4. A polynomial of lower degree, \({T_3}\) about \({\pi}\), is within 0.004 of \({\sin 4}\), because its centre is close.

“A higher degree is always better, wherever it is used.”

A Taylor polynomial is built from what happens at its centre. Far away, a higher degree can even be worse: at \({x = 4}\), \({T_1}\) of \({\sin x}\) about 0 misses by 4.76 and \({T_3}\) by 5.91. To use a Taylor polynomial at a point, centre it near that point.

“The coefficients are the derivatives.”

They are the derivatives divided by \({k!}\). Without the factorials, \({1 + x + x^2 + x^3}\) is the \({T_3}\) of \({1/(1 - x)}\), a different function: at 0.5 it gives 1.875, against \({e^{0.5} \approx 1.6487}\).

“A Taylor polynomial is always taken at 0.”

Any centre where the derivatives exist will do, and the best centre is near where the polynomial will be used. In training it is the current weight. Some functions have no Taylor polynomial at 0 at all: \({\ln x}\) is not even defined there, but about 1 it gives \({(x - 1) - (x - 1)^2/2 + \cdots}\).

Check yourself

Recall

1. What does \({T_n}\) about \(a\) share with \(f\)?

Show answer

Its value and its first \(n\) derivatives at \(a\).

2. What is the coefficient of \({(x - a)^k}\) in \({T_n}\), and where does the \({k!}\) come from?

Show answer

\({f^{(k)}(a)/k!}\). The \(k\)-th derivative of \({(x - a)^k}\) is \({k!}\), so the coefficient must be divided by it.

3. Write \({T_4}\) of \({\cos x}\) about 0.

Show answer

\({1 - x^2/2 + x^4/24}\).

4. Why is \({T_4 = T_3}\) for \({\sin x}\) about 0?

Show answer

The coefficient of \({x^4}\) is \({\sin(0)/4! = 0}\): the sine has only odd powers.

5. Which Taylor polynomial does a gradient step trust, and which one does Newton’s method jump to the bottom of?

Show answer

A gradient step trusts the line \({T_1}\); Newton’s method jumps to the lowest point of the parabola \({T_2}\).

Exercises

1. ★ Find \({T_2}\) of \({\sqrt{x}}\) about 4, use it to estimate \({\sqrt{4.1}}\), and compare with the tangent-line estimate 2.025 of M04.2.2.

Show solution

\({f(4) = 2}\); \({f'(x) = 1/(2\sqrt{x})}\), so \({f'(4) = 1/4}\); \({f''(x) = -1/(4x^{3/2})}\), so \({f''(4) = -1/32}\). Then

\[ \begin{aligned} T_2(4.1) &= 2 + \tfrac14(0.1) - \tfrac{1}{64}(0.1)^2 \\ &= 2.025 - 0.00015625 \\ &= 2.02484375 . \end{aligned} \]

The true value is 2.0248457, so the error falls from about 0.00015 for \({T_1}\) to about 0.000002.

2. ★★ Find \({T_3}\) of \({\ln(1 + x)}\) about 0 and use it to estimate \({\ln 1.1}\).

Show solution

The derivatives are \({1/(1 + x)}\), \({-1/(1 + x)^2}\) and \({2/(1 + x)^3}\), with values 1, \({-1}\) and 2 at 0, and \({\ln 1 = 0}\). Dividing by \({1!}\), \({2!}\) and \({3!}\): \({T_3(x) = x - x^2/2 + x^3/3}\). So \({\ln 1.1 \approx 0.1 - 0.005 + 0.000333}\), which is 0.0953333, against the true 0.0953102.

3. ★★★ Show that a polynomial of degree at most \(n\) equals its own \({T_n}\) about every centre, and use it to write \({x^3}\) in powers of \({x - 1}\).

Show solution

A polynomial \(p\) of degree at most \(n\) has the same value and first \(n\) derivatives at \(a\) as itself, and by the result \({T_n}\) is the only polynomial of degree at most \(n\) with that property, so \({T_n = p}\). For \({p(x) = x^3}\) about 1: \({p(1) = 1}\), \({p'(1) = 3}\), \({p''(1) = 6}\) and \({p'''(1) = 6}\), so

\[ \begin{aligned} x^3 &= 1 + 3(x - 1) \\ &\quad + \tfrac{6}{2}(x - 1)^2 + \tfrac{6}{6}(x - 1)^3 \\ &= 1 + 3(x - 1) \\ &\quad + 3(x - 1)^2 + (x - 1)^3 , \end{aligned} \]

which is the binomial theorem applied to \({\big((x - 1) + 1\big)^3}\).

Summary card

Match the value and the first \(n\) derivatives at the centre, and the coefficients must be \({f^{(k)}(a)/k!}\).

The anchor figure again: e to the x with T 1, T 2 and T 3, and sin x with T 1, T 3, T 5 and T 7, each polynomial following its curve further from 0 than the one before.
M04.8.3-F1

Taylor polynomials building up.

  • \({T_n(x)}\) adds up \({f^{(k)}(a)(x - a)^k / k!}\) for \({k = 0, \dots, n}\).
  • At 0: \({e^x \approx 1 + x + x^2/2! + \cdots}\), \({\sin x \approx x - x^3/3! + \cdots}\), \({\cos x \approx 1 - x^2/2! + \cdots}\), \({1/(1 - x) \approx 1 + x + x^2 + \cdots}\).
  • A polynomial of degree at most \(n\) is its own \({T_n}\).
  • In training: a gradient step trusts \({T_1}\); Newton jumps to the bottom of \({T_2}\).

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, Section 5.1.1: the Taylor polynomial \({T_n}\) (Definition 5.3, equation 5.7) and series (Definition 5.4), the remark that a Taylor polynomial of degree \(n\) is exact for a polynomial of degree at most \(n\) (Example 5.3 shows it for \({x^4}\)), and the polynomials of \({\sin x + \cos x}\) in Example 5.4 and Figure 5.4.
  • Gilbert Strang and Edwin Herman, Calculus Volume 2 (OpenStax), Section 6.3: the definition of the Taylor polynomial, and the Maclaurin polynomials of \({e^x}\), \({\sin x}\) and \({\cos x}\) in Example 6.12, where \({p_{2m+1} = p_{2m+2}}\) for the sine.
  • Matthew Boelkins, Active Calculus, Section 8.2: Taylor polynomials defined as the polynomials that match the value and first \(n\) derivatives at the centre (Definitions 8.2.3 and 8.2.17), and the coefficients \({f^{(k)}(a)/k!}\) found in the results that follow each definition.
  • NumPy developers, numpy.polynomial.Polynomial (NumPy 2.5 reference): coefficients in order of increasing degree, deriv, and evaluation by calling the object.

Next. M04.8.4 Taylor series and the size of the error puts a guaranteed bound on the error, lets the degree grow without end, and finds where the result still equals the function.

This lesson unlocks

M04.8.4 Taylor series and the size of the error M05.5 Hessians, curvature and second-order Taylor (planned)

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