$$ % Define colors used throughout LaTeX explanation \definecolor{error}{RGB}{ 255, 0, 0 } \definecolor{taylor}{RGB}{ 0, 0, 255 } \definecolor{estimate}{RGB}{ 160, 80, 0 } \definecolor{normal}{RGB}{ 0, 0, 0 } % i.e. black $$

Taylor Polynomial Approximation

The general case

For a function $f$ defined on some interval of the real number line, and a point $x_0$ internal to the domain of $f$, we can write the identity

$$ f( x ) = \color{taylor} \displaystyle \sum_{ i = 0 }^{ n } \frac{ 1 }{ i \ ! } \cdot \left( \frac{ d }{ d \ t } \right)^{ i } \ \rule[-25px]{2px}{60px}_{ \ t = x_0} f( t ) \cdot ( x - x_0 )^i \color{normal} + \color{error} \frac{ 1 }{ n \ ! } \displaystyle \int_{ t = x_0 }^{x} \left( \frac{ d }{ d \ t } \right)^{ n + 1 } f( t ) \cdot ( x - t )^n \ d \ t $$

provided that all the derivatives and the integral mentioned in it make sense. The sum is called the $ \color{taylor} \text{Taylor polynomial} $ of $f$ at $x_0$, and the integral is the $ \color{error} \text{error term} $ of the approximation.

cos(x)

When used for $f( x ) = \cos( x )$ and $x_0 = 0$, the general $ \color{taylor} \text{Taylor polynomial} $ approximation

turns into
$$ \cos( x ) = \color{taylor} \displaystyle \sum_{ j = 0 }^{ k } \frac{ 1 }{ ( 2j ) \ ! } \cdot x^{2j} \color{normal} + \color{error} \frac{ 1 }{ ( 2k ) \ ! } \displaystyle \int_{ t = 0 }^{x} \left( \frac{ d }{ d \ t } \right)^{ 2k + 1 } \cos( t ) \cdot ( x - t )^{ 2k } \ d \ t $$
for an even $n = 2k$ when denoting $i = 2j$.

The size of the $ \color{error} \text{integral error term} $ can be estimated from above:

$$ \left| \ \color{error} \frac{ 1 }{ n \ ! } \displaystyle \int_{ t = 0 }^{x} \left( \frac{ d }{ d \ t } \right)^{ n + 1 } \cos( t ) \cdot ( x - t )^{ n } \ d \ t \color{normal} \ \right| \le \color{estimate} \frac{ \left| x \right|^{ n + 1 } }{ ( n + 1 ) \ ! } $$

The $ \color{estimate} \text{estimate of the error term} $ (and thus the $ \color{error} \text{error term} $ itself) converges to zero:

$$ \lim_{ n \rightarrow \infty } \color{estimate} \frac{ \left| x \right|^{n + 1} }{ ( n + 1 ) \ ! } \color{normal} = 0 $$

These facts provide the basis for computing $\cos( x )$ with arbitrary guaranteed precision $\varepsilon > 0$, as done in the source code of this HTML page. Our computation uses floating point computer representation of real numbers, and thus suffers from all the usual limitations of floating point arithmetic. This precision can be guaranteed only modulo floating point errors.

To see a demonstration of this computation, press the button below. To start from scratch, refresh the page. (To see how it is done, check the source code of this HTML page.)

Graph of the built-in function $ \definecolor{builtin}{RGB}{ 0, 255, 0 } \color{builtin} \cos( x ) $