Continued Fractions - Maple Help

Home : Support : Online Help : System : Information : Updates : Maple 16 : Continued Fractions

Enhancements to Continued Fractions in Maple 16

A continued fraction is a mathematical expression of the form ${b}_{0}+\frac{{a}_{1}}{{b}_{1}+\frac{{a}_{2}}{{b}_{2}+\frac{{a}_{3}}{{b}_{3}+\frac{{a}_{4}}{{b}_{4}+\cdots }}}}$ approximating either a constant or a function. Continued fractions are well known to provide very good rational approximations, as demonstrated in the following examples.

Example 1: Approximating $\mathrm{π}$ by rational numbers

The following command computes the continued fraction approximation to $\mathrm{π}$ of order $10$.

 > $\mathrm{with}\left(\mathrm{numtheory}\right):$
 > $\mathrm{cfrac}\left(\mathrm{π}\right)$
 ${3}{+}\frac{{1}}{{7}{+}\frac{{1}}{{15}{+}\frac{{1}}{{1}{+}\frac{{1}}{{292}{+}\frac{{1}}{{1}{+}\frac{{1}}{{1}{+}\frac{{1}}{{1}{+}\frac{{1}}{{2}{+}\frac{{1}}{{1}{+}\frac{{1}}{{3}{+}{\mathrm{...}}}}}}}}}}}}$ (1)

In this case, all "numerators" ${a}_{i}$ are $1$, and all "denominators" ${b}_{i}$ are positive. The successive convergents  of this continued fraction are finite continued fractions, namely rational numbers, giving better and better numerical approximations of $\mathrm{π}$. They can be computed by specifying optional arguments:

 > $\mathrm{cfrac}\left(\mathrm{π},10,'c'\right):$
 > $c$
 $\left[{3}{,}\frac{{22}}{{7}}{,}\frac{{333}}{{106}}{,}\frac{{355}}{{113}}{,}\frac{{103993}}{{33102}}{,}\frac{{104348}}{{33215}}{,}\frac{{208341}}{{66317}}{,}\frac{{312689}}{{99532}}{,}\frac{{833719}}{{265381}}{,}\frac{{1146408}}{{364913}}{,}\frac{{4272943}}{{1360120}}{,}{\mathrm{...}}\right]$ (2)
 > $\mathrm{evalf}\left[20\right]\left(c\right)$
 $\left[{3.}{,}{3.1428571428571428571}{,}{3.1415094339622641509}{,}{3.1415929203539823009}{,}{3.1415926530119026041}{,}{3.1415926539214210447}{,}{3.1415926534674367055}{,}{3.1415926536189366234}{,}{3.1415926535810777712}{,}{3.1415926535914039785}{,}{3.1415926535893891715}{,}{\mathrm{...}}\right]$ (3)
 >
 ${\mathrm{abserror}}{:=}\left[{0.1415926535897932385}{,}{-}{0.0012644892673496186}{,}{0.0000832196275290876}{,}{-}{2.667641890624}{}{{10}}^{{-7}}{,}{5.778906344}{}{{10}}^{{-10}}{,}{-}{3.316278062}{}{{10}}^{{-10}}{,}{1.223565330}{}{{10}}^{{-10}}{,}{-}{2.91433849}{}{{10}}^{{-11}}{,}{8.7154673}{}{{10}}^{{-12}}{,}{-}{1.6107400}{}{{10}}^{{-12}}\right]$ (4)

You can see that the well-known rational approximation $\mathrm{π}\approx \frac{22}{7}$ occurs as the second convergent ${c}_{2}$, and it is accurate to $2$ decimal digits after the decimal point.

The best approximations, however, are obtained from the centered continued fraction, which allows "denominators" to be negative as well:

 > $\mathrm{cfrac}\left(\mathrm{π},10,'c',\mathrm{centered}\right)$
 ${3}{+}\frac{{1}}{{7}{+}\frac{{1}}{{16}{-}\frac{{1}}{{294}{-}\frac{{1}}{{3}{-}\frac{{1}}{{4}{-}\frac{{1}}{{5}{-}\frac{{1}}{{15}{+}\frac{{1}}{{3}{-}\frac{{1}}{{2}{+}\frac{{1}}{{2}{+}{\mathrm{...}}}}}}}}}}}}$ (5)

The ability to obtain the convergents for a centered continued fraction with an optional argument was added in Maple 16.

 > $c$
 $\left[{3}{,}\frac{{22}}{{7}}{,}\frac{{355}}{{113}}{,}\frac{{104348}}{{33215}}{,}\frac{{312689}}{{99532}}{,}\frac{{1146408}}{{364913}}{,}\frac{{5419351}}{{1725033}}{,}\frac{{80143857}}{{25510582}}{,}\frac{{245850922}}{{78256779}}{,}\frac{{411557987}}{{131002976}}{,}\frac{{1068966896}}{{340262731}}{,}{\mathrm{...}}\right]$ (6)
 > $\mathrm{evalf}\left[20\right]\left(c\right)$
 $\left[{3.}{,}{3.1428571428571428571}{,}{3.1415929203539823009}{,}{3.1415926539214210447}{,}{3.1415926536189366234}{,}{3.1415926535914039785}{,}{3.1415926535898153832}{,}{3.1415926535897926594}{,}{3.1415926535897931603}{,}{3.1415926535897932578}{,}{3.1415926535897932354}{,}{\mathrm{...}}\right]$ (7)
 >
 ${\mathrm{abserror}}{:=}\left[{0.1415926535897932385}{,}{-}{0.0012644892673496186}{,}{-}{2.667641890624}{}{{10}}^{{-7}}{,}{-}{3.316278062}{}{{10}}^{{-10}}{,}{-}{2.91433849}{}{{10}}^{{-11}}{,}{-}{1.6107400}{}{{10}}^{{-12}}{,}{-}{2.21447}{}{{10}}^{{-14}}{,}{5.791}{}{{10}}^{{-16}}{,}{7.82}{}{{10}}^{{-17}}{,}{-}{1.93}{}{{10}}^{{-17}}\right]$ (8)
 >

Example 2: Approximating $\mathrm{tan}\left(x\right)$ by a rational function

In this example, a continued fraction approximation of order $10$ to the tangent function around $x=0$ is computed:

 > $\mathrm{cfrac}\left(\mathrm{tan}\left(x\right)\right)$
 $\frac{{x}}{{1}{-}\frac{{{x}}^{{2}}}{{3}{-}\frac{{{x}}^{{2}}}{{5}{-}\frac{{{x}}^{{2}}}{{7}{-}\frac{{{x}}^{{2}}}{{9}{-}\frac{{{x}}^{{2}}}{{11}{-}\frac{{{x}}^{{2}}}{{13}{-}\frac{{{x}}^{{2}}}{{15}{-}\frac{{{x}}^{{2}}}{{17}{-}\frac{{{x}}^{{2}}}{{19}{+}{\mathrm{...}}}}}}}}}}}}$ (9)

In this example, all the "numerators" are powers of $x$ (up to sign), and the "denominators" exhibit an obvious pattern. Again, the convergents  form approximations of $\mathrm{tan}\left(x\right)$ of higher and higher orders. In this case, we obtain the convergents by calling cfrac twice, since applying cfrac to a continued fraction returns the rational function (or rational number) it represents:

 > $\mathrm{cfrac}\left(\mathrm{tan}\left(x\right),1\right)$
 $\frac{{x}}{{1}{+}{\mathrm{...}}}$ (10)
 > ${c}_{1}≔\mathrm{cfrac}\left(\right)$
 ${{c}}_{{1}}{:=}{x}$ (11)
 > $\mathrm{cfrac}\left(\mathrm{tan}\left(x\right),2\right)$
 $\frac{{x}}{{1}{-}\frac{{{x}}^{{2}}}{{3}{+}{\mathrm{...}}}}$ (12)
 > ${c}_{2}≔\mathrm{cfrac}\left(\right)$
 ${{c}}_{{2}}{:=}{-}\frac{{3}{}{x}}{{-}{3}{+}{{x}}^{{2}}}$ (13)
 > ${c}_{3}≔\mathrm{cfrac}\left(\mathrm{cfrac}\left(\mathrm{tan}\left(x\right),3\right)\right)$
 ${{c}}_{{3}}{:=}\frac{{1}}{{3}}{}\frac{{x}{}\left({-}{15}{+}{{x}}^{{2}}\right)}{{-}{5}{+}{2}{}{{x}}^{{2}}}$ (14)
 > ${c}_{4}≔\mathrm{cfrac}\left(\mathrm{cfrac}\left(\mathrm{tan}\left(x\right),4\right)\right)$
 ${{c}}_{{4}}{:=}{-}\frac{{5}{}{x}{}\left({-}{21}{+}{2}{}{{x}}^{{2}}\right)}{{105}{-}{45}{}{{x}}^{{2}}{+}{{x}}^{{4}}}$ (15)
 > ${c}_{5}≔\mathrm{cfrac}\left(\mathrm{cfrac}\left(\mathrm{tan}\left(x\right),5\right)\right)$
 ${{c}}_{{5}}{:=}\frac{{1}}{{15}}{}\frac{{x}{}\left({945}{-}{105}{}{{x}}^{{2}}{+}{{x}}^{{4}}\right)}{{63}{-}{28}{}{{x}}^{{2}}{+}{{x}}^{{4}}}$ (16)
 >
 ${\mathrm{O}}\left({{x}}^{{3}}\right)$
 ${\mathrm{O}}\left({{x}}^{{5}}\right)$
 ${\mathrm{O}}\left({{x}}^{{7}}\right)$
 ${\mathrm{O}}\left({{x}}^{{9}}\right)$
 ${\mathrm{O}}\left({{x}}^{{11}}\right)$ (17)

In fact, the convergents provide better approximations to then tangent function around $x=0$ than the Taylor expansion of the same order. Below, this is illustrated for ${c}_{5}$ and the Taylor expansion $s$ to $\mathrm{tan}\left(x\right)$ with the same error term, $\mathrm{O}\left({x}^{11}\right)$, by plotting the two differences $\mathrm{tan}\left(x\right)-s$ and $\mathrm{tan}\left(x\right)-{c}_{5}$:

 > $\mathrm{series}\left(\mathrm{tan}\left(x\right),x,11\right)$
 ${x}{+}\frac{{1}}{{3}}{}{{x}}^{{3}}{+}\frac{{2}}{{15}}{}{{x}}^{{5}}{+}\frac{{17}}{{315}}{}{{x}}^{{7}}{+}\frac{{62}}{{2835}}{}{{x}}^{{9}}{+}{\mathrm{O}}\left({{x}}^{{11}}\right)$ (18)
 > $s≔\mathrm{convert}\left(,\mathrm{polynom}\right)$
 ${s}{:=}{x}{+}\frac{{1}}{{3}}{}{{x}}^{{3}}{+}\frac{{2}}{{15}}{}{{x}}^{{5}}{+}\frac{{17}}{{315}}{}{{x}}^{{7}}{+}\frac{{62}}{{2835}}{}{{x}}^{{9}}$ (19)
 >

For example, the absolute errors for the two approximations at $x=1$ are:

 > $\mathrm{evalf}\left(\genfrac{}{}{0}{}{\mathrm{tan}\left(x\right)-s}{\phantom{x=a}}|\genfrac{}{}{0}{}{\phantom{\mathrm{f\left(x\right)}}}{x=1}\right)$
 ${0.014903316}$ (20)
 > $\mathrm{evalf}\left(\genfrac{}{}{0}{}{\mathrm{tan}\left(x\right)-{c}_{5}}{\phantom{x=a}}|\genfrac{}{}{0}{}{\phantom{\mathrm{f\left(x\right)}}}{x=1}\right)$
 ${3.18}{}{{10}}^{{-7}}$ (21)

The ability to compute a continued fraction at an expansion point other than $0$ was added in Maple 16:

 > $\mathrm{cfrac}\left(\mathrm{tan}\left(x\right),x=\mathrm{π}\right)$
 $\frac{{x}{-}{\mathrm{π}}}{{1}{-}\frac{{\left({x}{-}{\mathrm{π}}\right)}^{{2}}}{{3}{-}\frac{{\left({x}{-}{\mathrm{π}}\right)}^{{2}}}{{5}{-}\frac{{\left({x}{-}{\mathrm{π}}\right)}^{{2}}}{{7}{-}\frac{{\left({x}{-}{\mathrm{π}}\right)}^{{2}}}{{9}{+}{\mathrm{...}}}}}}}$ (22)
 >

Periodic continued fractions

Any rational number has a finite, and therefore periodic, continued fraction. It is a classical fact that more generally a real number has a periodic continued fraction expansion if and only if it is either rational or a real algebraic number of degree $2$, i.e., a number of the form $a+b\sqrt{d}$, where $a$ and $b$ are rational numbers and $d$ is a positive integer. For example, the periodic continued fraction expansion for $\sqrt{7}$ is:

 > $\mathrm{cfrac}\left(\sqrt{7},\mathrm{periodic}\right)$
 ${2}{+}\frac{{1}}{{1}{+}\frac{{1}}{{1}{+}\frac{{1}}{{1}{+}\frac{{1}}{{4}{+}\frac{{1}}{{1}{+}\frac{{1}}{{1}{+}\frac{{1}}{{1}{+}\frac{{1}}{{4}{+}{\mathrm{...}}}}}}}}}}$ (23)

Maple's data structure for periodic continued fractions has two components: a list of integers representing the preperiod, and another list of positive integers denoting the repeating period. This representation can be requested using an optional argument:

 > $\mathrm{cfrac}\left(\sqrt{7},\mathrm{periodic},\mathrm{quotients}\right)$
 $\left[\left[{2}\right]{,}\left[{1}{,}{1}{,}{1}{,}{4}\right]\right]$ (24)

As of Maple 16, applying the $\mathrm{cfrac}$ command to such a list will return the real number represented by that continued fraction. For example, the following command finds the quadratic irrational number whose continued fraction expansion is an infinite repetition of the period $1+\frac{1}{2+\frac{1}{3+\frac{1}{4+\frac{1}{5}}}}$:

 > $\mathrm{cfrac}\left(\left[\left[\right],\left[1,2,3,4,5\right]\right]\right)$
 $\frac{{1}}{{314}}{}\sqrt{{65029}}{+}\frac{{195}}{{314}}$ (25)
 > $\mathrm{cfrac}\left(,\mathrm{periodic}\right)$
 ${1}{+}\frac{{1}}{{2}{+}\frac{{1}}{{3}{+}\frac{{1}}{{4}{+}\frac{{1}}{{5}{+}\frac{{1}}{{1}{+}\frac{{1}}{{2}{+}\frac{{1}}{{3}{+}\frac{{1}}{{4}{+}\frac{{1}}{{5}{+}{\mathrm{...}}}}}}}}}}}$ (26)
 > $\mathrm{cfrac}\left(,20\right)$
 ${1}{+}\frac{{1}}{{2}{+}\frac{{1}}{{3}{+}\frac{{1}}{{4}{+}\frac{{1}}{{5}{+}\frac{{1}}{{1}{+}\frac{{1}}{{2}{+}\frac{{1}}{{3}{+}\frac{{1}}{{4}{+}\frac{{1}}{{5}{+}\frac{{1}}{{1}{+}\frac{{1}}{{2}{+}\frac{{1}}{{3}{+}\frac{{1}}{{4}{+}\frac{{1}}{{5}{+}\frac{{1}}{{1}{+}\frac{{1}}{{2}{+}\frac{{1}}{{3}{+}\frac{{1}}{{4}{+}\frac{{1}}{{5}{+}\frac{{1}}{{1}{+}{\mathrm{...}}}}}}}}}}}}}}}}}}}}}}$ (27)
 >