# Taylor polynomial max error calculation

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

• Formatting tips here. – Em. Jan 10 '16 at 7:55
• @probablyme: thanks – MathsGuy Jan 10 '16 at 7:57
• What is $r$ in your computations? Sure you are only considering $x\in(0,r)$ when you say the error is larger than this? It's strange, $M=1$ should work for $0< x < r$ when $f(x)=e^{-x}$ since $\max_{x\in(0,r)} |f^{(k)}(x)| =\max_{x\in(0,r)} e^{-x} = 1$. – Winther Jan 10 '16 at 8:49
• It seems that you use this estimates (Please explain all the symbols you use every time you ask questions). So you have to choose the constant $M$ appropriately as mentioned there (depending on interval), at least. Perhaps you should care about $\vert \frac{d^{k+1}}{dx^{k+1}}e^{-x}\vert$ in $x \in (-r, 0)$ . – Orat Jan 10 '16 at 8:54
• If you edit the question with some explicit numbers for the errors you get (especially when the error is larger than the constraint) then it's easy for others to check your calculations. That would confirm or rule out a coding error. – Winther Jan 10 '16 at 9:01

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.