Taylor Series: X Terms For Y Digits

How can you determine how many terms in a Taylor series you need to get the first N digits of a number? For instance, if you want to determine the first Y digits of the constant e using the Taylor series e^x = sum(x^n / n!) where n goes from 0 to Z, how can you determine what Z is?

It’s been a while since I’ve done this, but you just have to make sure your remainder is less than your error tolerance.

So, if you want to calculate e^x to Y digits, your remainder term must be less than 5 * 10^(-Y). However, even for e^x, the remainder term seems incalculable, so how can you prove it’s less than this value?

Taylor-with-remainder theorems are generally of the form

f(x) = P[sub]n/sub + g(x)

where P[sub]n/sub is a polynomial of degree n and g(x) is either given as an integral or is bounded down (in magnitude) by an integral of f. In practice, this can also be difficult to calculate exactly, but that doesn’t matter. What you do is find another more easily calculated bound for the magnitude of g(x). Selecting a good one and proving it’s a bound is very specific to a given f(x) and there aren’t general rules. Practice makes perfect.

If I’ve understood the Wikipedia article on Taylor’s Theorem correctly, you get an inequality that looks like this:

10^(-Y) > (1/(5*n!)) * Integral(0,1)[e^t * (1-t)^n dt]

where Y is the number of digits you want and n+1 is the number of Taylor series terms. The formula here is for calculating the number of terms for e itself (e^1).

Using the binomial theorem, you get:

10^(-Y) > (1/5) * Integral(0,1){e^t * Sum(k=0,n)[(1/(k!*(n-k)!)) * (-t)^(n-k)] dt}

Still, I don’t know how to solve an integral of the form t^n * e^t, nor do I know how to estimate the sum for arbitrary values of n.

But that’s exactly what I said: you don’t have to use that integral since you’re just looking for a bound, not a value.

Let’s say that’s the right error term. Now, since you’re integrating over (0,1), you know that 1>|1-t|. This means that (using TeX here)

|\frac{1}{5n!} \int_0^1{e^t (1-t)^n dt}| <
< \frac{1}{5n!} \int_0^1{e^t |1-t|^n dt} <
< \frac{1}{5n!} \int_0^1{e^t dt} = \frac{e-1}{5n!}

You should easily be able to pick a Y from this.

You typically can’t calculate the remainder exactly—if you could, you’d know exactly how far off you were from using the Taylor polynomial and, hence, the exact value. But you can often find an upper bound for the remainder, so that you can say “I’m off by, at most…”

For the example in the OP, the Lagrange form of the remainder [I didn’t know that was what it was called until I read **bitwise’s** link!] would look like
e[sup]z[/sup]/(n+1)! * (1)[sup]n+1[/sup], where z is some number between 0 and 1 (and hence, e[sup]z[/sup] is between 1 and e). If you know that e itself is less than three, then you know the remainder must be less than 3/(n+1)!, and you can work out what n would have to be to make this as small as you want it to be.

Another option, that won’t work for e[sup]1[/sup] but would for e[sup]-1[/sup]: if you plug a negative number into the Taylor series for e[sup]x[/sup], you get an alternating series whose terms decrease to 0 in absolute value. In that case, if you add up the first n terms of the series, the remainder is less than or equal to (in absolute value) the next term along.