[Math] Taylor polynomial max error calculation

calculusreal-analysis

Let
$$\left|R_k(x)\right| \leq M\frac{|x-a|^{k+1}}{(k+1)!}\leq
M\frac{r^{k+1}}{(k+1)!}.$$
Source.

I am trying to calculate the maximum error of an $n$ order Taylor polynomial. First of all $a = 0$.

I try it for the $\cos$ function. It works as excepted. No matter what $x$ or $n$ I enter, the error I get is always smaller than the error $|R_k(x)|$ returns. Now, I try to do the same thing for $e^{-x}$ for $x > 0$. The problem is, this time the error I get is always more than the error $|R_k(x)|$ returns. I use $M = 1$.

I am writing a software application that does that calculations. I strongly believe my software is correct, so there must be some kind of maths issue. Does anyone have any idea what causes my error? What is the difference between $\cos(x)$ and $e^{-x}$ when using the type above?

edit: If I set $M = e$ the max error is always larger than the actual error. Can someone please explain why this works?

edit 2, some numbers:

e^(-3.0), x < 0                 : 0.049787068367863944
Taylor e^(-3.0), n = 3          : -2.0
Max error                       : 3.375
Actual error                    : 2.049787068367864

e^(-3.0), x < 0                 : 0.049787068367863944
Taylor e^(-3.0), n = 6          : 0.36250000000000004
Max error                       : 0.43392857142857144
Actual error                    : 0.3127129316321361

e^(-3.0), x < 0                 : 0.049787068367863944
Taylor e^(-3.0), n = 12         : 0.04999746347402601
Max error                       : 2.5603302947052944E-4
Actual error                    : 2.1039510616206736E-4

e^(-30.0), x < 0                : 9.357622968840175E-14
Taylor e^(-30.0), n = 5         : -172829.0
Max error                       : 1012500.0
Actual error                    : 172829.0

Best Answer

The $M$ term is not arbitrary. If we write the Taylor polynomial of a function $f \in C^{k+1}$ centered in $x_0$ using the Lagrange remainder, we obtain the approximation $$f(x)=f(x_0)+f'(x_0)(x-x_0)+\frac{f''(x_0)}{2!}(x-x_0)^2+\ldots+\frac{f^{(k)}(x_0)}{k!}(x-x_0)^k+\frac{f^{(k+1)}(\xi)}{(k+1)!}(x-x_0)^{(k+1)} \qquad \xi \in \text{Int}(x,x_0)$$

We can obtain an estimate for the remainder if the function $f^{(k+1)}$ is bounded on $\text{Int}(x,x_0)$: $$f^{(k+1)}(\xi)\leq \sup_{x\in \text{Int}(x,x_0)} f^{(k+1)}(x)=M$$

This happens both for $\cos \cdot$ and for $e^{\cdot}$. In fact

  • The derivatives of $\cos\cdot$ are $\pm \sin \cdot, -\cos \cdot$, which are bounded by $1$ on every interval. In this case $M=1$.
  • The derivatives of $e^{\cdot}$ are $e^{\cdot}$; since $e^{\cdot}$ is an increasing function, the function is bounded by $e^{\max\{x,x_0\}}=M$ on every interval of the kind $\text{Int}(x,x_0)$.

In the code you posted, you are considering $x<0$ and the Taylor approximation is centered in $x_0=0$, so $\max\{x,x_0\}=x_0=0$. In this case, you can choose $M=e^0=1$. $e$ works because it is bigger than $1$ and leads to a more inaccurate bound from above.