TExcelTAD
Home › Calculus in Depth › Applications of Taylor Polynomials

Applications of Taylor Polynomials

A Taylor polynomial is a cheap stand-in for a function, and Taylor's inequality says exactly how good it is. This lesson uses that to answer practical questions (how many terms, over what interval, with what error) and then looks inside a math library to see how exp is really computed: range reduction, a short polynomial, and Horner's method.

The basics of approximation error are in Approximation Error; the theory used here (Lagrange remainder, Taylor's inequality) is proved in Taylor and Maclaurin Series.

Three ways to bound the error

The nn-th degree Taylor polynomial of ff at aa is Tn(x)=∑k=0nf(k)(a)k!(x−a)kT_n(x) = \sum_{k=0}^{n} \frac{f^{(k)}(a)}{k!}(x - a)^k, and f(x)=Tn(x)+Rn(x)f(x) = T_n(x) + R_n(x).

Method When it applies Bound on ∣Rn(x)∣\lvert R_n(x) \rvert
Taylor's inequality always, if you can bound f(n+1)f^{(n+1)} on the interval by MM M∣x−a∣n+1/(n+1)!M\lvert x - a \rvert^{n+1}/(n + 1)!
Alternating series bound the series at xx is alternating with decreasing terms the first omitted term
Numerical check as a sanity check, never as a proof max⁡∣f−Tn∣\max \lvert f - T_n \rvert on a fine grid

Worked example. Approximate f(x)=xf(x) = \sqrt{x} by T2T_2 at a=9a = 9 on the interval [8,10][8, 10].

f(x)=x1/2,f′=12x−1/2,f′′=−14x−3/2,f′′′=38x−5/2T2(x)=3+x−96−(x−9)2216f′′′ is positive and decreasing on [8,10], so M=f′′′(8)=38⋅8−5/2≈0.002072∣R2(x)∣≤M⋅∣x−9∣33!≤0.002072⋅16≈3.45×10−4\begin{aligned} f(x) &= x^{1/2}, \quad f' = \tfrac{1}{2}x^{-1/2}, \quad f'' = -\tfrac{1}{4}x^{-3/2}, \quad f''' = \tfrac{3}{8}x^{-5/2} \\ T_2(x) &= 3 + \frac{x - 9}{6} - \frac{(x - 9)^2}{216} \\ &f''' \text{ is positive and decreasing on } [8, 10], \text{ so } M = f'''(8) = \tfrac{3}{8} \cdot 8^{-5/2} \approx 0.002072 \\ \lvert R_2(x) \rvert &\le \frac{M \cdot \lvert x - 9 \rvert^3}{3!} \le \frac{0.002072 \cdot 1}{6} \approx 3.45 \times 10^{-4} \end{aligned}
python · try it yourself
import numpy as np

xs = np.linspace(8, 10, 2001)
T2 = 3 + (xs - 9) / 6 - (xs - 9) ** 2 / 216
err = np.abs(np.sqrt(xs) - T2)
M = 3 / 8 * 8 ** (-2.5)
print(f"Taylor bound : {M / 6:.3e}")
print(f"actual max   : {float(err.max()):.3e} at x = {float(xs[err.argmax()]):.1f}")
Taylor bound : 3.453e-04
actual max   : 2.766e-04 at x = 8.0

The real worst error is about 80% of the bound, at x=8x = 8, the end of the interval where f′′′f''' is largest. On the other side (xx near 10) the error is much smaller than the bound. Bounds are deliberately pessimistic: they replace f(n+1)(z)f^{(n+1)}(z) by its worst value.

How many terms?

To hit a tolerance ε\varepsilon on ∣x−a∣≤d\lvert x - a \rvert \le d, find the smallest nn with M⋅dn+1/(n+1)!<εM \cdot d^{n+1}/(n + 1)! < \varepsilon. For sin⁡x\sin x on [−π/4,π/4][-\pi/4, \pi/4] (M=1M = 1):

python · try it yourself
import math

d, eps = math.pi / 4, 1e-10
n = 0
while d ** (n + 1) / math.factorial(n + 1) >= eps:
    n += 1
print(f"degree needed: {n}, bound {d ** (n + 1) / math.factorial(n + 1):.2e}")

x = d
T = sum((-1) ** k * x ** (2 * k + 1) / math.factorial(2 * k + 1) for k in range((n + 1) // 2))
print(f"T_{n}(π/4) = {T:.15f}\nsin(π/4)  = {math.sin(x):.15f}")
degree needed: 12, bound 6.95e-12
T_12(π/4) = 0.707106781179619
sin(π/4)  = 0.707106781186547

The loop asks for degree 12, but sin has no x12x^{12} term, so T12=T11T_{12} = T_{11}: the six odd-power terms x,x3,…,x11x, x^3, \ldots, x^{11} already meet the tolerance, with the error bound of R12R_{12}. Library authors exploit tricks like this to shave terms.

A physics application: the drop of the horizon

A point on the Earth's surface at arc distance ss from you lies below your tangent line by h(s)=R(1−cos⁡(s/R))h(s) = R(1 - \cos(s/R)), where R≈6371 kmR \approx 6371\ \text{km}. Using cos⁡u≈1−u2/2+u4/24\cos u \approx 1 - u^2/2 + u^4/24:

h(s)=R(1−cos⁡sR)≈s22R−s424R3h(s) = R\left(1 - \cos\frac{s}{R}\right) \approx \frac{s^2}{2R} - \frac{s^4}{24R^3}

The first term gives the surveyor's rule: about 7.8 cm at 1 km, 7.8 m at 10 km. The second term is s4/(24R3)s^4/(24R^3); since cos is alternating in its series, the error of the one-term rule is at most that, which for s=10 kms = 10\ \text{km} is about 1.6×10−6 m1.6 \times 10^{-6}\ \text{m} (under two micrometres). One Taylor term is all an engineer needs here, and the next term proves it.

In software: how libm computes exp

A math library cannot use the raw Maclaurin series of exe^x: for x=700x = 700 it would need hundreds of terms and would overflow on the way. Instead it combines three ideas.

  1. Range reduction. Write x=k⋅ln⁡2+rx = k \cdot \ln 2 + r with k=round⁡(x/ln⁡2)k = \operatorname{round}(x/\ln 2), so ∣r∣≤ln⁡2/2≈0.347\lvert r \rvert \le \ln 2/2 \approx 0.347. Then ex=2k⋅ere^x = 2^k \cdot e^r. Multiplying by 2k2^k is exact in binary floating point (it only changes the exponent bits: math.ldexp).
  2. A short polynomial on a tiny interval. On ∣r∣≤0.347\lvert r \rvert \le 0.347, Taylor's inequality with M=e0.347M = e^{0.347} gives ∣R11(r)∣≤1.42⋅0.34712/12!≈8.9×10−15\lvert R_{11}(r) \rvert \le 1.42 \cdot 0.347^{12}/12! \approx 8.9 \times 10^{-15} and ∣R12(r)∣≈2.4×10−16\lvert R_{12}(r) \rvert \approx 2.4 \times 10^{-16}, which is about one rounding step of a double near 1. So degree 12 is enough. Real libraries use a minimax polynomial instead (fitted to minimise the maximum error over the interval, Remez algorithm), which reaches the same accuracy with a lower degree. Taylor's error is tiny at r=0r = 0 and largest at the ends; minimax spreads it evenly.
  3. Horner's method. Evaluate c0+r(c1+r(c2+⋯+r⋅cn))c_0 + r(c_1 + r(c_2 + \cdots + r \cdot c_n)) with nn multiplications and nn additions, no powers, and good rounding behaviour.
python · try it yourself
import math

LN2 = math.log(2)
C = [1 / math.factorial(k) for k in range(13)]    # Taylor coefficients of e^r

def horner(coeffs, r):
    acc = 0.0
    for c in reversed(coeffs):
        acc = acc * r + c
    return acc

def my_exp(x):
    k = round(x / LN2)
    r = x - k * LN2                                # |r| <= ln2/2
    return math.ldexp(horner(C, r), k)             # 2^k · e^r

for x in (-20.5, -1.0, 0.1, 1.0, 10.0, 300.0, 700.0):
    ours, ref = my_exp(x), math.exp(x)
    print(f"x={x:7.1f}  my_exp={ours:.16e}  rel.err={abs(ours - ref) / ref:.1e}")
x=  -20.5  my_exp=1.2501528663867415e-09  rel.err=8.3e-16
x=   -1.0  my_exp=3.6787944117144233e-01  rel.err=0.0e+00
x=    0.1  my_exp=1.1051709180756477e+00  rel.err=0.0e+00
x=    1.0  my_exp=2.7182818284590455e+00  rel.err=1.6e-16
x=   10.0  my_exp=2.2026465794806729e+04  rel.err=5.0e-16
x=  300.0  my_exp=1.9424263952412339e+130  rel.err=1.1e-14
x=  700.0  my_exp=1.0142320547350550e+304  rel.err=5.0e-14

For moderate xx the relative errors are at the level of double rounding (the spacing of doubles near 1 is about 2.2×10−162.2 \times 10^{-16}). For x=300x = 300 and 700 they grow to about 10−1410^{-14}. The polynomial is not to blame: the stored ln⁡2\ln 2 is off by about 10−1710^{-17}, k⋅ln⁡2k \cdot \ln 2 multiplies that by k≈1000k \approx 1000, and r=x−k⋅ln⁡2r = x - k \cdot \ln 2 subtracts two nearly equal numbers near 700 whose own rounding is about 10−1310^{-13}. The resulting absolute error in rr becomes a relative error of the same size in ere^r. A production libm fixes exactly this: it splits ln⁡2\ln 2 into a high part with trailing zero bits (so k⋅ln⁡2hik \cdot \ln 2_{\text{hi}} is exact) and a tiny low part, so that rr is computed without that cancellation error. It also uses a table lookup for finer reduction (ex=2k⋅2j/32⋅er′e^x = 2^k \cdot 2^{j/32} \cdot e^{r'}) and a degree-5 or 6 minimax polynomial on the smaller interval. The same pattern of reduce, approximate with a polynomial, reconstruct is used for sin/cos (reduce modulo π/2\pi/2), log (split off the exponent bits) and in GPU shader math.

Tip: NumPy's np.polynomial.Chebyshev.interpolate gives a near-minimax polynomial in one line; compare its maximum error with the Taylor polynomial of the same degree in Exercise 4.

Exercises

Exercise 1: A bound by hand

Use Taylor's inequality to bound the error of T3T_3 for exe^x at a=0a = 0 on [0,0.5][0, 0.5].

Answer

∣R3(x)∣≤e0.5⋅0.54/4!≈0.00429\lvert R_3(x) \rvert \le e^{0.5} \cdot 0.5^4/4! \approx 0.00429.

Solution
f(4)(x)=ex≤e0.5≈1.6487on [0,0.5]∣R3(x)∣≤1.6487⋅0.5424=1.6487⋅0.062524≈0.00429\begin{aligned} f^{(4)}(x) &= e^x \le e^{0.5} \approx 1.6487 \quad \text{on } [0, 0.5] \\ \lvert R_3(x) \rvert &\le \frac{1.6487 \cdot 0.5^4}{24} = \frac{1.6487 \cdot 0.0625}{24} \approx 0.00429 \end{aligned}

The actual error at x=0.5x = 0.5 is e0.5−(1+0.5+0.125+0.0208333)≈0.00289e^{0.5} - (1 + 0.5 + 0.125 + 0.0208333) \approx 0.00289, below the bound.

Exercise 2: Alternating bound

How large can ∣x∣\lvert x \rvert be if sin⁡x≈x−x3/6\sin x \approx x - x^3/6 must be accurate to 10−510^{-5}?

Answer

∣x∣≤(1.2×10−3)1/5≈0.2605\lvert x \rvert \le (1.2 \times 10^{-3})^{1/5} \approx 0.2605 (about 14.9∘14.9^\circ).

Solution

For ∣x∣<1\lvert x \rvert < 1 the sin series is alternating with decreasing terms, so the error is at most the next term ∣x∣5/120\lvert x \rvert^5/120:

∣x∣5120≤10−5  ⟺  ∣x∣5≤1.2×10−3  ⟺  ∣x∣≤0.2605\frac{\lvert x \rvert^5}{120} \le 10^{-5} \iff \lvert x \rvert^5 \le 1.2 \times 10^{-3} \iff \lvert x \rvert \le 0.2605
python · try it yourself
import math

x = (1.2e-3) ** 0.2
print(f"x = {x:.4f} rad = {math.degrees(x):.1f}°, error = {abs(math.sin(x) - (x - x**3 / 6)):.2e}")
x = 0.2605 rad = 14.9°, error = 9.98e-06

Exercise 3: Horner by hand

Evaluate p(x)=2x4−3x3+x−5p(x) = 2x^4 - 3x^3 + x - 5 at x=3x = 3 with Horner's scheme. How many multiplications does it use, compared with the naive method?

Answer

p(3)=79p(3) = 79. Horner uses 4 multiplications; computing each term separately uses 8.

Solution
text
check: 2·81 − 3·27 + 3 − 5 = 162 − 81 + 3 − 5 = 79
coefficients: 2, −3, 0, 1, −5
2
2·3 − 3 = 3
3·3 + 0 = 9
9·3 + 1 = 28
28·3 − 5 = 79

Naively, 2⋅x⋅x⋅x⋅x2 \cdot x \cdot x \cdot x \cdot x needs 4 multiplications, 3⋅x⋅x⋅x3 \cdot x \cdot x \cdot x needs 3 and 1⋅x1 \cdot x needs 1: 8 in total. For degree nn, Horner needs nn, the naive way about n2/2n^2/2.

Exercise 4: Taylor versus near-minimax

On [−ln⁡2/2,ln⁡2/2][-\ln 2/2, \ln 2/2], compare the maximum relative error of the degree-6 Taylor polynomial of ere^r with a degree-6 Chebyshev interpolant (np.polynomial.Chebyshev.interpolate).

Answer

Taylor: about 1.6×10−71.6 \times 10^{-7}; Chebyshev: about 2.5×10−92.5 \times 10^{-9}, roughly 64 times more accurate at the same degree.

Solution

Taylor concentrates its accuracy at r=0r = 0; the Chebyshev interpolant spreads it across the interval, which is close to the minimax ideal.

python · try it yourself
import math
import numpy as np
from numpy.polynomial import Chebyshev

h = math.log(2) / 2
r = np.linspace(-h, h, 5001)
taylor = sum(r**k / math.factorial(k) for k in range(7))
cheb = Chebyshev.interpolate(np.exp, 6, domain=[-h, h])
t_err = float(np.max(np.abs(taylor - np.exp(r)) / np.exp(r)))
c_err = float(np.max(np.abs(cheb(r) - np.exp(r)) / np.exp(r)))
print(f"Taylor deg 6   : {t_err:.2e}")
print(f"Chebyshev deg 6: {c_err:.2e}  ({t_err / c_err:.0f}x better)")
Taylor deg 6   : 1.61e-07
Chebyshev deg 6: 2.53e-09  (64x better)

Test yourself

Why does a math library reduce xx to r=x−k⋅ln⁡2r = x - k \cdot \ln 2 before evaluating a polynomial for exe^x?

  1. Because ln⁡2\ln 2 is rational
  2. So the polynomial only has to be accurate on a tiny interval, needing few terms
  3. To avoid using multiplication
  4. Because exe^x is periodic

Taylor's inequality gives ∣R4(x)∣≤0.01\lvert R_4(x) \rvert \le 0.01 on an interval. The true maximum error is 0.004. Is that a contradiction?

  1. Yes, the bound must be exact
  2. Yes, the true error must be larger
  3. No: the bound uses the worst-case value of f(5)f^{(5)}, so it is usually pessimistic
  4. No: Taylor's inequality only works at the centre

What does a minimax polynomial minimise?

  1. The maximum error over the whole interval
  2. The error at the centre
  3. The number of coefficients
  4. The sum of the coefficients