2 - Taylor's Series

Taylor's Formula

Let us assume the function $y=f(x)$ has all derivatives up to the $(n+1)$th order, inclusive, in some interval containing the point $x=a$. Let us find a polynomial $y=P_{n}(x)$ of degree $n$, the value of which at $x=a$ is equal to value of the function $f(x)$ at this point, and the values of its derivatives upto the $n$th order at $x=a$ are equal to the values of the corresponding derivatives of the function $f(x)$ at this point:

${P_{n}(a)=f(a),{\space}P_{n}'(a)=f'(a),{\space}P_{n}''(a)=f''(a),\ldots,P_{n}^{(n)}(a)=f^{n}(a)}$

It is natural to expect that in a certain sense, such a polynomial is close to the function $f(x)$.

Let us look for this polynomial in powers of (x-a) with undetermined coefficients:

$P_{n}(x)=C_{0}+C_{1}(x-a)+C_{2}(x-a)^2+C_{3}(x-a)^3+\ldots+C_{n}(x-a)^n$

We define the undetermined coefficients $C_{1},C_{2},\ldots,C_{n}$, so that conditions $(1)$ are satisfied,

Let us find the derivatives of $P_{n}(x)$:

$ P_{n}^{(1)}(x)=C_{1} +2C_{2}(x-a) +3C_{3}(x-a)^2 +\ldots+nC_{n}(x-a)^{n-1}\\ P_{n}^{(2)}(x)={2}C_{2} +{3}\cdot{2}C_{3}(x-a)+{4}\cdot{3}C_{4}(x-a)^2+\ldots+{n}{(n-1)}C_{n}(x-a)^{n-2}\\ \vdots\\ P_{n}^{(n)}(x)= {n}{(n-1)(n-2)\ldots{2}\cdot{1}}C_{n} $

Substituting into the left and right sides of $(2)$ and $(3)$, the value of $a$ in place of $x$ and replacing by $(1)$, $P_{n}(a)$ by $f(a)$, $P_{n}^{(1)}(a)$ by $f_{n}^{(1)}(a)$ etc., we get:

$f^{(n)}(a)={n}{(n-1)(n-2)\ldots{2}\cdot{1}}C_{n}\\ f^{(n-1)}(a)={(n-1)(n-2)\ldots{2}\cdot{1}}C_{n-1}\\ \ldots\\ f^{(3)}(a)={{3}\cdot{2}\cdot{1}}C_{3}\\ f^{(2)}(a)={2}\cdot{1}C_{2}\\ f^{(1)}(a)=C_{1}\\ f(a)=C_{0}$

whence, we find:

$C_{0}=f(a),{\space}C_{1}=f'(a),{\space}C_{2}=\frac{1}{{1}\cdot{2}}f''(a),C_{3}=\frac{1}{{1}\cdot{2}\cdot{3}}f'''(a),\ldots,\frac{1}{{1}\cdot{2}\cdot{3}\ldots{n}}f^{(n)}(a)$

Substituting into $(2)$ the values of $C_{1},C_{2},\ldots,C_{n}$, we get the desired polynomial:

$P_{n}(x)=f(a)+\frac{(x-a)}{1}f'(a)+\frac{(x-a)^2}{2!}f''(a)+\frac{(x-a)^3}{3!}f'''(a)+\ldots+\frac{(x-a)^n}{n!}f^{(n)}(a)$

Designate by $R_{n}(x)$ the difference between the values of the given function $f(x)$ and the constructed polynomial $P_{n}(x)$:

$R_{x}(x)=f(x)-P_{n}(x)$

whence,

$f(x)=f(a)+\frac{(x-a)}{1}f'(a)+\frac{(x-a)^2}{2!}f''(a)+\frac{(x-a)^3}{3!}f'''(a)+\ldots+\frac{(x-a)^n}{n!}f^{(n)}(a)+R_{n}(x)$

$R_{n}(x)$ is called the remainder term. For those values of $x$, for which the remainder $R_{n}(x)$ is small, the polynomial $P_{n}(x)$ is an approximate representation of the function $f(x)$.

Thus, the Taylor's series enables one to represent the function $y=f(x)$ by the polynomial $y=P_{n}(x)$ to an appropriate degree of accuracy equal to the value of the remainder term $R_{n}(x)$.

Our next problem is to evaluate the quantity $R_{n}(x)$ for various values of $x$. Let us write the remainder in the form

$R_n(x)=\frac{(x-a)^{n+1}}{(n+1)!}Q(x)$

where $Q(x)$ is a certain function to be defined, and accordingly re-write $(6)$ as :

$f(x)=f(a)+\frac{(x-a)}{1}f'(a)+\frac{(x-a)^2}{2!}f''(a)+\frac{(x-a)^3}{3!}f'''(a)+\ldots+\frac{(x-a)^n}{n!}f^{(n)}(a)+\frac{(x-a)^{n+1}}{(n+1)!}Q(x)$

For fixed $x$ and $a$, the function $Q(x)$ has a definite value; denote it by $Q$.

Let us define the auxiliary function of $t$, F(t) as ($t$ lies between $a$ and $x$):

$F(t)=f(x)-f(t)-\frac{(x-t)}{1}f'(t)-\frac{(x-t)^2}{2!}f''(t)-\ldots-\frac{(x-t)^{n-1}}{(n-1)!}f^{(n-1)}(t)-\frac{(x-t)^n}{n!}f^{(n)}(t)-\frac{(x-t)^{n+1}}{(n+1)!}Q$

We find the derivative $F'(t)$:

$F'(t)=-f'(t)+f'(t)-\frac{x-t}{1}f''(t)+\frac{2(x-t)}{2!}f''(t)-\frac{(x-t)^2}{2!}f'''(t)+\ldots\\ -\frac{(x-t)^{n-1}}{(n-1)!}f^{(n)}(t)+\frac{(x-t)^{n-1}}{(n-1)!}f^{(n)}(t)-\frac{(x-t)^n}{n!}f^{(n+1)}(t)+\frac{(x-t)^n}{n!}Q$

Upon cancelling the terms, we have :

$F'(t)=-\frac{(x-t)^n}{n!}f^{(n+1)}(t)+\frac{(x-t)^n}{n!}Q$

Thus, the function $F(t)$ has a derivative at all points $t$ lying near the point with abscissa $a$ (${a}\le{t}\le{x}$. It will be noted further that,

$F(x)=0,{\space}F(a)=0$

Therefore, the Rolle's theorem is applicable to the function $F(t)$ and consequently, there exists a value $t=\xi$ lying between $a$ and $x$ such that $F'(\xi)=0$. Whence, we get,

$F'(\xi)=-\frac{(x-\xi)^n}{n!}f^{(n+1)}(t)+\frac{(x-\xi)^n}{n!}Q=0$

and from this,

$$Q=f^{(n+1)}(\xi)$$

Substituting this in the expression for the remainder term,

$$R_{n}(x)=\frac{(x-a)^{n+1}}{(n+1)!}f^{(n+1)}(\xi)$$

If in the Taylor's formula, we put $a=0$, we will have:

$\displaystyle{f(x)=f(0)+\frac{x}{1}f'(0)+\frac{x^2}{2!}f''(0)+\ldots+\frac{x^{n}}{n!}f^{(n)}(0)+R_{n}(x)}$

Expansion of the functions $e^x$, $\sin{x}$ and $\cos{x}$ in a Taylor's series

Expansion of the function $f(x)=e^{x}$.

Finding the successive derivatives of $f(x)$, we have:

$f(x)=e^{x},{\space}f(0)=1$

$f'(x)=e^{x},{\space}f'(0)=1$

$f''(x)=e^{x},{\space}f''(0)=1$

$\ldots$

$f^{(n)}(x)=e^{x},{\space}f^{(n)}(0)=1$

Substituting the expressions obtained into the formula, we get:

$\displaystyle{e^{x}=1+\frac{x}{1!}+\frac{x^2}{2!}+\frac{x^3}{3!}+\ldots+\frac{x^{n}}{n!}}+R_{n}(x)$


In [7]:
# Taylor's series approximation of e^x

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0,8,0.1)

plt.plot(x,np.exp(x),'b-')
plt.plot(x,1 + x + (x**2)/2 + (x**3)/6 + (x**4)/24 + (x**5)/120 + (x**6)/720 + (x**7)/5040 + (x**8)/40320,'r--')
plt.show()


Expansion of the function $\sin{x}$

We find the successive derivatives of the function $f(x)=\sin{x}$.

$f(x)=\sin{x},{\space}f(0)=0$

$f'(x)=\cos{x}=\sin\left({x+\frac{\pi}{2}}\right),{\space}f'(0)=1$

$f''(x)=-\sin{x}=\sin\left({x+2\frac{\pi}{2}}\right),{\space}f''(0)=0$

$f'''(x)=-\cos{x}=\sin\left({x+3\frac{\pi}{2}}\right),{\space}f'''(0)=-1$

$f^{(4)}(x)=\sin{x}=\sin\left({x+4\frac{\pi}{2}}\right),{\space}f^{(4)}(0)=0$

$\ldots$

$f^{(n)}(x)=\sin\left({x+n\frac{\pi}{2}}\right),{\space}f^{(n)}(0)=\sin\left(n\frac{\pi}{2}\right)$

Substituting the expressions obtained into the formula :

$\sin{x}=\displaystyle{x-\frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!}+\ldots+R_{n}(x)}$


In [20]:
# Taylor's series approximation of sin(x)

import numpy as np
import matplotlib.pyplot as plt
import math

x = np.arange(0,2*np.pi,0.1)

plt.plot(x,np.sin(x),'b-')
plt.plot(x,x - (x**3)/math.factorial(3) + (x**5)/math.factorial(5) - (x**7)/math.factorial(7) + (x**9)/math.factorial(9) - (x**11)/math.factorial(11) + (x**13)/math.factorial(13),'r--')
plt.show()


Expansion of the function $\cos{x}$

We find the successive derivatives of the function $f(x)=\cos{x}$.

$f(x)=\cos{x},{\space}f(0)=1$

$f'(x)=-\sin{x},{\space}f'(0)=0$

$f''(x)=-\cos{x},{\space}f''(0)=-1$

$f'''(x)=\sin{x},{\space}f'''(0)=0$

$f^{(4)}(x)=\cos{x},{\space}f^{(4)}(0)=1$

$\ldots$

Substituting the expressions obtained into the formula :

$\displaystyle{\cos{x}=\displaystyle{1-\frac{x^2}{2!}+\frac{x^4}{4!}-\frac{x^6}{6!}+\ldots+\frac{x^n}{n!}\cos\left(n\frac{\pi}{2}\right)+R_{n}(x)}}$


In [21]:
# Taylor's series approximation of cos(x)

import numpy as np
import matplotlib.pyplot as plt
import math

x = np.arange(0,2*np.pi,0.1)

plt.plot(x,np.cos(x),'b-')
plt.plot(x,1 - (x**2)/math.factorial(2) + (x**4)/math.factorial(4) - (x**6)/math.factorial(6) + (x**8)/math.factorial(8) - (x**10)/math.factorial(10) + (x**12)/math.factorial(12),'r--')
plt.show()