Foundations of numerical computing · Lecture 3

Taylor Series and Order of Accuracy

Almost every formula in this course comes from a Taylor series cut off after a few terms. The first neglected term tells us the error, and "O(h²)" tells us how fast it shrinks.

Md Mahbub E Noor · Assistant Professor
CSE-3107 Numerical Methods · B.Sc. 3rd Year 1st Semester
Department of CSE, University of Barishal

Today's goals

  • Write the Taylor series of a function and its remainder term
  • Approximate values and bound the truncation error using the remainder
  • Explain big-O notation and the order of accuracy of a method
  • Derive the forward and central difference formulas and their O(h)O(h), O(h2)O(h^2) errors
  • See the Taylor series as the engine of later methods: Newton (L9), difference operators (L15), ODE solvers (L21)
Last time

Round-off: doubles keep about 16 significant digits (ε≈2.2×10−16\varepsilon \approx 2.2\times10^{-16}).


Today

The other source of error, truncation. The Taylor series lets us predict it before computing anything, and compare methods by their order.

Foundations of numerical computing · you are hereBuilds on: L1
Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
2 / 15
Start here

The idea in one picture

x−ππsin x1 term: x2 terms: x − x³/63 terms: x − x³/6 + x⁵/120near the centre:excellent
The true curve sin x: hard to compute directly.One term: a straight line. Good only very close to 0.Two terms: the curve is followed further out.Three terms: almost exact from −π/2 to π/2.More terms = wider good zone. The part we cut off is the truncation error.
In plain words
  • Near one point, any smooth curve looks like a simple polynomial
  • More terms: the polynomial follows the curve further away
  • What we leave out is an error we can estimate in advance
New words today
Taylor series
a function written as a sum of powers of (x − a)
truncation
stopping after a finite number of terms
remainder
the part that was cut off
order
how fast the error shrinks when the step shrinks
Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
3 / 15

The Taylor series

About the point af(x)=f(a)+f′(a)(x−a)+f′′(a)2!(x−a)2+⋯+f(n)(a)n!(x−a)n+Rn(x)\begin{aligned} f(x) = {} & f(a) + f'(a)(x-a) + \frac{f''(a)}{2!}(x-a)^2 \\ & + \cdots + \frac{f^{(n)}(a)}{n!}(x-a)^n + R_n(x) \end{aligned}
Lagrange remainder (the error of stopping at degree n)Rn(x)=f(n+1)(ξ)(n+1)!(x−a)n+1,ξ between a and xR_n(x) = \frac{f^{(n+1)}(\xi)}{(n+1)!}(x-a)^{n+1}, \qquad \xi \text{ between } a \text{ and } x
  • a=0a = 0 gives the Maclaurin series
  • The polynomial matches ff, f′f', f′′f'', … at aa
  • We never know ξ\xi, but we can bound ∣f(n+1)∣|f^{(n+1)}| on the interval, which bounds the error
  • Close to aa, (x−a)n+1(x-a)^{n+1} is tiny: the approximation is excellent
Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
4 / 15

Series worth knowing by heart

exe^{x}

all xx

1+x+x22!+x33!+⋯1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \cdots

sin⁡x\sin x

all xx (radians)

x−x33!+x55!−⋯x - \frac{x^3}{3!} + \frac{x^5}{5!} - \cdots

cos⁡x\cos x

all xx (radians)

1−x22!+x44!−⋯1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \cdots

ln⁡(1+x)\ln(1+x)

−1<x≤1-1 < x \le 1 only

x−x22+x33−⋯x - \frac{x^2}{2} + \frac{x^3}{3} - \cdots

11−x\dfrac{1}{1-x}

∣x∣<1|x| < 1 only

1+x+x2+x3+⋯1 + x + x^2 + x^3 + \cdots

1+x\sqrt{1+x}

∣x∣≤1|x| \le 1

1+x2−x28+⋯1 + \frac{x}{2} - \frac{x^2}{8} + \cdots
Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
5 / 15

Explore: adding terms

  • Increase the degree: the polynomial hugs ff over a wider range
  • The error at a point is close to the first neglected term
  • Choose ln⁡(1+x)\ln(1+x) and go past x=1x = 1: more terms make it worse
  • Choose sin⁡x\sin x: odd degrees only add new terms
Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
6 / 15

Problem 1: e0.5e^{0.5} with an error bound

Problem 1Approximate e0.5e^{0.5} using the first four terms of the Maclaurin series, and bound the error.
1
ex≈1+x+x22+x36e^{x} \approx 1 + x + \dfrac{x^2}{2} + \dfrac{x^3}{6}, so e0.5≈1+0.5+0.125+0.0208333=1.6458333e^{0.5} \approx 1 + 0.5 + 0.125 + 0.0208333 = 1.6458333
2
Remainder: R3=eξ4!(0.5)4R_3 = \dfrac{e^{\xi}}{4!}(0.5)^4 with 0<ξ<0.50 < \xi < 0.5
3
Bound: eξ<e0.5<2e^{\xi} < e^{0.5} < 2, so ∣R3∣<2×0.062524=0.0052|R_3| < \dfrac{2 \times 0.0625}{24} = 0.0052 (sharper: e0.5(0.5)4/24=0.0043e^{0.5}(0.5)^4/24 = 0.0043)
4
Check: true e0.5=1.6487213e^{0.5} = 1.6487213, actual error =0.0028879= 0.0028879, inside the bound ✓
e0.5≈1.6458e^{0.5} \approx 1.6458 with error less than 0.0050.005. The bound is honest (never too small) but a little pessimistic.
Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
7 / 15

How many terms do we need?

Problem 2How many terms of ex=∑xk/k!e^{x} = \sum x^k/k! are needed to compute ee (that is, x=1x = 1) with error less than 10−610^{-6}?
1
Stopping after degree nn: ∣Rn∣=eξ(n+1)!<3(n+1)!|R_n| = \dfrac{e^{\xi}}{(n+1)!} < \dfrac{3}{(n+1)!} since eξ<e<3e^{\xi} < e < 3
2
Need 3(n+1)!<10−6\dfrac{3}{(n+1)!} < 10^{-6}, i.e. (n+1)!>3×106(n+1)! > 3\times10^{6}
3
9!=362,8809! = 362{,}880 (too small), 10!=3,628,80010! = 3{,}628{,}800 ✓, so n+1=10n + 1 = 10, n=9n = 9
4
Degree 9 means the terms k=0,1,…,9k = 0, 1, \dots, 9: 10 terms. Check: ∑k=091/k!=2.7182815\sum_{k=0}^{9} 1/k! = 2.7182815, error 3.0×10−73.0\times10^{-7} ✓
10 terms. This is how library designers decide the length of a polynomial: from the remainder bound, before writing any code.
Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
8 / 15

Step form and big-O notation

Taylor series with step hf(x+h)=f(x)+hf′(x)+h22f′′(x)+h36f′′′(x)+⋯f(x+h) = f(x) + h f'(x) + \frac{h^2}{2} f''(x) + \frac{h^3}{6} f'''(x) + \cdots
Big-OE(h)=O(hp)  ⟺  ∣E(h)∣≤C hp for small hE(h) = O(h^p) \iff |E(h)| \le C\,h^p \text{ for small } h
Order of accuracyE(h/2)≈E(h)/2p⇒p≈log⁡2E(h)E(h/2)E(h/2) \approx E(h)/2^{p} \qquad \Rightarrow \qquad p \approx \log_2 \frac{E(h)}{E(h/2)}
  • O(h)O(h): halve hh → error halves (first order)
  • O(h2)O(h^2): halve hh → error ÷ 4 (second order)
  • O(h4)O(h^4): halve hh → error ÷ 16 (RK4, Simpson)
  • Higher order = far fewer steps for the same accuracy
Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
9 / 15

Deriving the difference formulas

Problem 3From the Taylor series, derive the forward and central difference approximations of f′(x)f'(x) and their truncation errors.
1
Forward: f(x+h)=f(x)+hf′(x)+h22f′′(ξ)f(x+h) = f(x) + hf'(x) + \tfrac{h^2}{2}f''(\xi) ⟹ f′(x)=f(x+h)−f(x)h−h2f′′(ξ)⏟O(h)f'(x) = \dfrac{f(x+h) - f(x)}{h} - \underbrace{\tfrac{h}{2}f''(\xi)}_{O(h)}
2
Write both f(x±h)=f(x)±hf′(x)+h22f′′(x)±h36f′′′(x)+⋯f(x \pm h) = f(x) \pm hf'(x) + \tfrac{h^2}{2}f''(x) \pm \tfrac{h^3}{6}f'''(x) + \cdots
3
Subtract: f(x+h)−f(x−h)=2hf′(x)+h33f′′′(x)+⋯f(x+h) - f(x-h) = 2hf'(x) + \tfrac{h^3}{3}f'''(x) + \cdots (the ff and f′′f'' terms cancel)
4
Divide by 2h2h: f′(x)=f(x+h)−f(x−h)2h−h26f′′′(x)⏟O(h2)+⋯f'(x) = \dfrac{f(x+h) - f(x-h)}{2h} - \underbrace{\tfrac{h^2}{6}f'''(x)}_{O(h^2)} + \cdots
Forward difference: error O(h)O(h). Central difference: error O(h2)O(h^2), for one extra function evaluation per point… and a much better answer.
Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
10 / 15

Checking the order: f(x)=ln⁡xf(x) = \ln x at x=1x = 1 (exact f′(1)=1f'(1) = 1)

hhforwarderrorcentralerrorcentral error ratio
0.10.9531018−4.69e−21.00335353.35e−3—
0.050.9758033−2.42e−21.00083468.35e−44.01
0.0250.9877045−1.23e−21.00020842.08e−44.00
0.01250.9938016−6.20e−31.00005215.21e−54.00

Forward errors halve when h halves (order 1). Central errors fall by 4 (order 2). Theory: central error ≈ h26f′′′(1)=h26⋅2\tfrac{h^2}{6}f'''(1) = \tfrac{h^2}{6}\cdot 2 = 0.00333 for h=0.1h = 0.1 ✓

Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
11 / 15
Practice

Practice problem

Problem 4"Starting from the Taylor series expansion of f(x)f(x) about a point xx, derive the central-difference approximation of f′(x)f'(x) and show that its truncation error is O(h2)O(h^2). Using this formula with h=0.1h = 0.1, estimate f′(1)f'(1) for f(x)=ln⁡xf(x) = \ln x. Compare your result with the exact derivative and compute the actual error, commenting on whether it is consistent with the theoretical O(h2)O(h^2) order."
How to write a complete answer
  • Derivation as shown earlier: expand f(x±h)f(x\pm h), subtract, divide by 2h2h, name the leading error term −h26f′′′(x)-\tfrac{h^2}{6}f'''(x)
  • Numbers: ln⁡1.1−ln⁡0.90.2=0.0953102+0.10536050.2=1.0033535\dfrac{\ln 1.1 - \ln 0.9}{0.2} = \dfrac{0.0953102 + 0.1053605}{0.2} = 1.0033535
  • Exact f′(1)=1f'(1) = 1; actual error =0.0033535= 0.0033535
  • Theory: h26∣f′′′(1)∣=0.016×2=0.0033333\tfrac{h^2}{6}|f'''(1)| = \tfrac{0.01}{6}\times 2 = 0.0033333: matches, so consistent with O(h2)O(h^2)
  • Add: halving hh to 0.05 gives error 8.35×10−48.35\times10^{-4}, a factor 4 smaller
Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
12 / 15
Beyond the classroom

Where this shows up in CSE and machine learning

⬇️

Gradient descent is first-order Taylor

L(w+Δ)≈L(w)+∇LTΔL(w + \Delta) \approx L(w) + \nabla L^{T}\Delta. The decrease is largest for Δ=−η∇L\Delta = -\eta\nabla L: that is the update rule (Lecture 25).

🎯

Newton's method is second-order Taylor

Adding 12ΔTHΔ\tfrac12\Delta^T H \Delta (the Hessian) and minimising gives Newton's step: the basis of fast optimisers.

🧮

Activation functions on chips

tanh⁡\tanh, exe^x and GELU are computed with short polynomials whose length comes from remainder bounds.

🔁

Backpropagation checks

Gradient checking compares backprop with L(w+h)−L(w−h)2h\frac{L(w+h)-L(w-h)}{2h}, the central difference, with h≈10−5h \approx 10^{-5}: the best step size, explained in Lecture 4.

Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
13 / 15

Checkpoint

Checkpoint · think, then discuss with your neighbourA method gives error 8×10−48\times10^{-4} with step hh and 5×10−55\times10^{-5} with step h/2h/2. What is its order?
A1
B2
C3
D4
D. The ratio is 8×10−4/5×10−5=16=248\times10^{-4}/5\times10^{-5} = 16 = 2^4, so p=log⁡216=4p = \log_2 16 = 4.
Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
14 / 15

Summary

Key points
  • Taylor: f(x)=∑f(k)(a)k!(x−a)k+Rnf(x) = \sum \frac{f^{(k)}(a)}{k!}(x-a)^k + R_n, with Rn=f(n+1)(ξ)(n+1)!(x−a)n+1R_n = \frac{f^{(n+1)}(\xi)}{(n+1)!}(x-a)^{n+1}
  • Bound ∣f(n+1)∣|f^{(n+1)}| to bound the truncation error; choose nn from the bound
  • O(hp)O(h^p): halving hh divides the error by 2p2^p; measure pp from two step sizes
  • Forward difference O(h)O(h); central difference O(h2)O(h^2)
  • Every method later in the course (Newton, difference formulas, Simpson, Runge–Kutta) is a truncated Taylor series
Homework
  1. Approximate sin⁡(π/4)\sin(\pi/4) with three non-zero terms of the Maclaurin series and bound the error.
  2. How many terms of the series for cos⁡x\cos x give cos⁡1\cos 1 with error below 10−810^{-8}?
  3. Derive the backward difference f′(x)≈[f(x)−f(x−h)]/hf'(x) \approx [f(x) - f(x-h)]/h and its error term.
  4. Lab: run the order program for f(x)=exf(x) = e^x at x=0x = 0. Confirm the orders 1 and 2.
Next lecture

Errors and their propagation. Measuring errors, how round-off and truncation errors combine and grow through arithmetic, catastrophic cancellation, and the best step size hh.

Md Mahbub E Noor
Lecture 3: Taylor Series and Order of Accuracy
University of Barishal
15 / 15