A638 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A6 · Lesson 38 · Lab: Taylor polynomials in code

How many terms, and how to keep the count small

A Taylor polynomial is only as good as its error bound, and the bound depends brutally on how far from the center you evaluate it. sin⁡x to twelve digits needs degree 13 at x=π/4 but degree 23 at x=3, and at x=100 the terms would grow to 1042 before shrinking. In this lab you'll compute Taylor polynomials, compare their actual errors with the Lagrange bounds, count the terms each accuracy needs, and use range reduction to evaluate sin⁡ anywhere with a short polynomial.

  • The curve is |sin⁡x−Pn(x)|, the error of the degree-n Taylor polynomial about 0, on a log scale.
  • The error is tiny near 0 and grows rapidly with |x|. Raise n and the region where it's below 10−12 widens, slowly.
  • Range reduction keeps |x|≤π/4, the shaded strip, where degree 13 is already enough.

Predict

The degree-9 Taylor polynomial for sin⁡x about 0 is used at x=1. Its Lagrange bound is |x|1111!. About how accurate is it?

Name it

Errors you can bound

For ex, sin⁡x and cos⁡x the derivatives are bounded nicely:

  • sin⁡ and cos⁡: every derivative is at most 1, so the degree-n error is at most |x|n+1(n+1)! (and for sin⁡'s odd polynomials, |x|n+2(n+2)!);
  • ex on [0,x]: at most exxn+1(n+1)!.

Comparing the actual error with the bound shows how pessimistic the bound is: usually within a small factor.

Counting terms

To meet a tolerance, raise the degree until the bound falls below it. Doing this in a loop gives the degree directly, with no guesswork.

Range reduction

sin⁡ has period 2π and symmetries that reduce any x to r=x−kπ2 with |r|≤π4:

sin⁡x={sin⁡rk≡0cos⁡rk≡1−sin⁡rk≡2−cos⁡rk≡3(mod4)

On |r|≤π/4, polynomials of degree 11 and 10 for sin⁡ and cos⁡ reach about 10−13.

Worked example

How many terms of ex's series give e to within 10−6, by the Lagrange bound?

Show the solution
  1. Write the bound. The bound after degree n is e(n+1)!.
  2. Try degrees 6 to 9. n=6:5.4×10−4n=7:6.7×10−5n=8:7.5×10−6n=9:7.5×10−7 So degree 9, ten terms.
  3. Check the actual error. The actual error at degree 6 is 2.3×10−4, about 40% of the bound.

Answer degree 9, which is ten terms.

Worked example

Evaluate sin⁡10 with range reduction.

Show the solution
  1. Count quarter turns. 10π/2=6.37 so k=6.
  2. Find the remainder. r=10−6×π2=0.57522
  3. Fix the sign. Since k≡2(mod4): sin⁡10=−sin⁡r=−0.54402
  4. See why it pays. A degree-11 polynomial at r=0.575 is accurate to about 10−13, while the same polynomial at x=10 directly would be off by thousands.

Answer sin⁡10=−0.54402.

Check

What is the Lagrange bound for sin⁡x≈P5(x) (odd degree 5) at x = 1?

Check

Range reduction subtracts the nearest multiple of π/2 from x = 7. What is left?

Check

Write taylor_exp(x, n) (the degree-n Taylor polynomial of ex about 0) and exp_bound(x, n) =exxn+1(n+1)! for x>0. Set deg to the smallest n whose bound at x=1 is below 10−6.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write sin_degree(x, tol) returning the smallest odd degree n with |x|n+2(n+2)!< tol. Set d1, d2, d3 for x = 0.5, π/4 and 3, all with tol = 10−12.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write sin_reduced(x): reduce x to r=x−kπ2 with k = round(x / (math.pi / 2)), then return ±sin⁡r or ±cos⁡r by kmod4, using degree-11 and degree-10 Taylor polynomials. Set s = sin_reduced(x) for x = 100.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Compare the actual error with the bound. For sin⁡'s degree-9 polynomial, set ratios to the list of actual error divided by the bound |x|1111! at x = 0.5, 1, 2 and 3.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Reduce for ex too: write exp_reduced(x), which sets k = round(x / math.log(2)) and r=x−kln⁡2 (so |r|≤ln⁡22), then returns 2k times the degree-12 Taylor polynomial at r. Check its relative error against math.exp from −20 to 20.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does range reduction cut the work so much more than adding terms would?

Practice

Keep practicing

5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.