Taylor polynomial question

For the discussion of math. Duh.

Moderators: gmalivuk, Moderators General, Prelates

hencethus
Posts: 24
Joined: Thu May 31, 2007 4:25 am UTC
Location: Port Orange, FL
Contact:

Taylor polynomial question

OK, this was a homework problem. I've already solved it, but I have a question that i hope someone finds interesting.

Here's the problem:
Taylor's theorem states that if [imath]T_n(x)[/imath] is the [imath]n[/imath]th degree Taylor polynomial of [imath]f(x) = cos(x)[/imath] centered at [imath]a = 0[/imath], then for all [imath]\beta[/imath] there exists an [imath]\alpha[/imath], [imath]0 < \alpha < \beta[/imath], such that $f(\beta) = T_n(\beta) + {f^{n+1}(\alpha)\over(n + 1)!}(\beta)^{n+1}$ How large must [imath]n[/imath] be so that the [imath]n[/imath]th degree Taylor polynomial of [imath]f(x) = cos(x)[/imath] centered at [imath]a = 0[/imath] approximates [imath]cos(x)[/imath] to within [imath]\epsilon = .0001[/imath] for all [imath]x \in [0,10][/imath]?

Here's how I solved it:
We need [imath]|f(10) - T_n(10)| \le .0001[/imath] or [imath]|{f^{n+1}(\alpha)\over(n + 1)!}(10)^{n+1}| \le .0001[/imath], so
$|f^{n+1}(\alpha)| \le 1 \iff {|f^{n+1}(\alpha)|\over(n + 1)!}(10)^{n+1} \le {(10)^{n+1}\over(n + 1)!}$
${(10)^{n+1}\over(n + 1)!} \le {1\over10000} \iff 10^{n+5} \le (n+1)! \iff n \ge 34$
$\therefore n \ge 34 \Rightarrow |f(10) - T_n(10)| \le .0001$

It turns out that that's the answer my professor was looking for, but before I knew that I wanted to check my answer, so I wrote a python script to evaluate the Taylor polynomial:

Code: Select all

from math import factorialprint('Cosine Taylor polynomial evaluator.')degree=input('What degree?')x=input('Evaluate at x=')degree = degree/2sum=0for i in range(0,degree+1):   sum += (float(((-1)**i))/factorial(2*i)) * (x**(2*i))print(sum)

According to my script, if [imath]n \ge 32[/imath] then [imath]|f(10) - T_n(10)| \le .0001[/imath]

Code: Select all

What degree?31Evaluate at x=10-0.83942020518Cosine Taylor polynomial evaluator.What degree?32Evaluate at x=10-0.839040166105Cosine Taylor polynomial evaluator.What degree?33Evaluate at x=10-0.839040166105Cosine Taylor polynomial evaluator.What degree?34Evaluate at x=10-0.83907403768

According to my calculator:
[imath]cos(10) = -0.839071529[/imath]

So here's my question: Where's the discrepancy come from? Is it that my script is dealing with crazy numbers like 1/32! and python isn't precise enough? Or is it because there's some wiggle room in the Taylor remainder when I used [imath]f^{n+1}(\alpha)\le1[/imath] ?

gorcee
Posts: 1501
Joined: Sun Jul 13, 2008 3:14 am UTC

Re: Taylor polynomial question

Specifically, which discrepancy do you mean? It might be more helpful to output your absolute error. It looks to me like your values at n = 34 are within the 0.0001 error tolerance. Isn't this what you expect?

hencethus
Posts: 24
Joined: Thu May 31, 2007 4:25 am UTC
Location: Port Orange, FL
Contact:

Re: Taylor polynomial question

Yeah, I'd expect n = 34 to work. The question is, does n = 32 also work, and my method of solving didn't get me the lowest possible value for n? Or is there something wrong with my script such that n = 32 doesn't really work?

skeptical scientist
closed-minded spiritualist
Posts: 6142
Joined: Tue Nov 28, 2006 6:09 am UTC
Location: San Francisco

Re: Taylor polynomial question

hencethus wrote:Yeah, I'd expect n = 34 to work. The question is, does n = 32 also work, and my method of solving didn't get me the lowest possible value for n?

That's right. By Taylor's theorem, the error from the nth degree polynomial at x is equal to $$\frac{f^{(n+1)}(\alpha)}{(n + 1)!}x^{n+1}$$ for some $$0<\alpha<x$$. Using the fact that we only care about |x|≤10 in this problem, and that all derivatives of cos are bounded by 1, this means that the difference between the actual value and the nth degree Taylor polynomial is no more than 10n+1/(n+1)!. When n≥33,* this quantity is less than .0001, which means that the 33rd degree Taylor approximation is sufficient. But the actual error may be smaller than the 10n+1/(n+1)! bound we obtain from Taylor's theorem, so it may be that a lower degree Taylor approximation would also work.

*I get 33 rather than 34 here; you might have been forgetting that the exponent is n+1 rather than n.
I'm looking forward to the day when the SNES emulator on my computer works by emulating the elementary particles in an actual, physical box with Nintendo stamped on the side.

"With math, all things are possible." —Rebecca Watson

gorcee
Posts: 1501
Joined: Sun Jul 13, 2008 3:14 am UTC

Re: Taylor polynomial question

What SS said. The error bound is an upper bound. You could get under that bound earlier, but if you do, it's generally considered to just be fortuitous.

hencethus
Posts: 24
Joined: Thu May 31, 2007 4:25 am UTC
Location: Port Orange, FL
Contact:

Re: Taylor polynomial question

Ah, thank you. That clears that up.
Also, I don't know where I got 34 from. It's definitely 33. Interestingly, the 33rd degree is the same as the 32nd degree, since every other term has sin(0) in the numerator. So you can infer that if the 33rd degree is sufficient, then so is the 32nd. And n = 32 actually is the lowest value of n for which the error is less than .0001