## Continued fractions vs Power series

The Taylor series of the exponential function, $e^x = \sum_{n=0}^\infty \frac{x^n}{n!} = 1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \frac{x^4}{24} + \frac{x^5}{120} + \cdots$
provides reasonable approximations to the function near zero: for example, with $T_3(x) = 1 + x + \frac{x^2}{2} + \frac{x^3}{6}$ we get

The quality of approximation not being spectacular, one can try to improve it by using rational functions instead of polynomials. In view of the identity $e^x = \frac{e^{x/2}}{e^{-x/2}} \approx \frac{T_3(x/2)}{T_3(-x/2)}$
one can get ${e^x\approx p(x)/p(-x)}$ with $p(x) = 1 + \frac{x}{2} + \frac{x^2}{8} + \frac{x^3}{48}$. The improvement is substantial for negative ${x}$:

Having rational approximation of the form ${e^x\approx p(x)/p(-x)}$ makes perfect sense, because such approximants obey the same functional equation ${f(x)f(-x)=1}$ as the exponential function itself. We cannot hope to satisfy other functional equations like ${f(x+1)=e f(x)}$ by functions simpler than ${\exp}$.

However, the polynomial ${T_n(x/2)}$ is not optimal for approximation ${e^x \approx p(x)/p(-x)}$ except for ${n=0, 1}$. For degree ${3}$, the optimal choice is $p(x) = 1 + \frac{x}{2} + \frac{x^2}{10} + \frac{x^3}{120}$. In the same plot window as above, the graph of ${p(x)/p(-x)}$ is indistinguishable from ${e^x}$.

This is a Padé approximant to the exponential function. One way to obtain such approximants is to replace the Taylor series with continued fraction, using long division: $e^x = 1 + \dfrac{x}{1 + \dfrac{-x/2}{1 + \dfrac{x/6}{1 + \dfrac{-x/6}{1 + \dfrac{x/10}{1 + \dfrac{-x/10}{1 + \cdots }}}}}}$

Terminating the expansion after an even number of ${x}$ apprearances gives a Padé approximant of the above form.

This can be compared to replacing the decimal expansion of number ${e}$: $e = 2 + \frac{7}{10} + \frac{1}{10^2} + \frac{8}{10^3} + \frac{2}{10^4}+\cdots$

with the continued fraction expansion $e = 2 + \dfrac{1}{1 + \dfrac{1}{2 + \dfrac{1}{1 + \dfrac{1}{1 + \dfrac{1}{4 + \dfrac{1}{1 + \cdots }}}}}}$

which, besides greater accuracy, has a regular pattern: 121 141 161 181 …

The numerators ${p}$ of the (diagonal) Padé approximants to the exponential function happen to have a closed form: $p(x) = \sum_{k=0}^n \frac{\binom{n}{k}}{\binom{2n}{k}} \frac{x^k}{k!}$

which shows that for every fixed ${k}$, the coefficient of ${x^k}$ converges to ${2^{-k}/k!}$ as ${n\to\infty }$. The latter is precisely the Taylor coefficient of ${e^{x/2}}$.

In practice, a recurrence relation is probably the easiest way to get these numerators: begin with ${p_0(x)=1}$ and ${p_1(x) = 1+x/2}$, and after that use $p_{n+1}(x) = p_n(x) + \frac{x^2}{16n^2 - 4} p_{n-1}(x)$. This relation can be derived from the recurrence relations for the convergents ${A_n/B_n}$ of a generalized continued fraction $\mathop{\raisebox{-5pt}{\huge K}}_{n=1}^\infty \frac{a_n}{b_n}$. Namely, ${A_n = b_nA_{n-1}+a_nA_{n-2}}$ and ${B_n = b_nB_{n-1}+a_nB_{n-2}}$. Only the first relation is actually needed here.

Using the recurrence relation, we get $p_2(x) = p_1(x) + \frac{x^2}{12}p_0(x) = 1 + \frac{x}{2} + \frac{x^{2}}{12}$ $p_3(x) = p_2(x) + \frac{x^2}{60}p_1(x) = 1 + \frac{x}{2} + \frac{x^{2}}{10} + \frac{x^{3}}{120}$ $p_4(x) = p_3(x) + \frac{x^2}{140}p_2(x) = 1 + \frac{x}{2} + \frac{3 x^{2}}{28} + \frac{x^{3}}{84} + \frac{x^{4}}{1680}$

(so, not all coefficients have numerator 1…) $p_5(x) = p_4(x) + \frac{x^2}{252}p_3(x) = 1 + \frac{x}{2} + \frac{x^{2}}{9} + \frac{x^{3}}{72} + \frac{x^{4}}{1008} + \frac{x^{5}}{30240}$

The quality of approximation to ${e^x}$ is best seen on logarithmic scale: i.e., how close is ${\log(p(x)/p(-x))}$ to ${x}$? Here is this comparison using ${p_5}$.

For comparison, the Taylor polynomial of fifth degree, also on logarithmic scale: ${\log(T_5(x))}$ where $T_5(x) = 1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \frac{x^4}{24} + \frac{x^5}{120}$.