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. to twelve digits needs degree 13 at but degree 23 at , and at the terms would grow to 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 anywhere with a short polynomial.
- The curve is , the error of the degree- Taylor polynomial about 0, on a log scale.
- The error is tiny near 0 and grows rapidly with . Raise and the region where it's below widens, slowly.
- Range reduction keeps , the shaded strip, where degree 13 is already enough.
Predict
The degree-9 Taylor polynomial for about 0 is used at . Its Lagrange bound is . About how accurate is it?
About : . At the same polynomial is good to , and at only to . The bound grows like , which is why libraries reduce the argument first.
Name it
Errors you can bound
For , and the derivatives are bounded nicely:
- and : every derivative is at most 1, so the degree- error is at most (and for 's odd polynomials, );
- on : at most .
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
has period and symmetries that reduce any to with :
On , polynomials of degree 11 and 10 for and reach about .
Worked example
How many terms of 's series give to within , by the Lagrange bound?
Show the solution
- Write the bound. The bound after degree is .
- Try degrees 6 to 9. So degree 9, ten terms.
- Check the actual error. The actual error at degree 6 is , about 40% of the bound.
Answer degree 9, which is ten terms.
Worked example
Evaluate with range reduction.
Show the solution
- Count quarter turns. so .
- Find the remainder.
- Fix the sign. Since :
- See why it pays. A degree-11 polynomial at is accurate to about , while the same polynomial at directly would be off by thousands.
Answer .
Check
What is the Lagrange bound for (odd degree 5) at = 1?
Check
Range reduction subtracts the nearest multiple of from = 7. What is left?
Check
Write taylor_exp(x, n) (the degree- Taylor polynomial of about 0) and exp_bound(x, n) for . Set deg to the smallest whose bound at is below .
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 with tol. Set d1, d2, d3 for = 0.5, and 3, all with tol = .
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 to with = round(x / (math.pi / 2)), then return or by , using degree-11 and degree-10 Taylor polynomials. Set s = sin_reduced(x) for = 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 's degree-9 polynomial, set ratios to the list of actual error divided by the bound at = 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 too: write exp_reduced(x), which sets = round(x / math.log(2)) and (so ), then returns times the degree-12 Taylor polynomial at . Check its relative error against math.exp from 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?
A Taylor polynomial's error goes like : the power of grows with the degree, and only the factorial eventually wins. Far from the center, the terms first grow huge before the factorial takes over, so a large degree is needed, and the cancellation among big terms of alternating sign costs digits in floating point too. Range reduction attacks the base instead of the exponent: bringing down below makes every term small from the start, so a fixed short polynomial is accurate everywhere, and there's no cancellation to lose digits to. Adding terms fights the problem; reducing the argument removes it.
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.