Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Rational fraction approximation

`R = rat(X)`

`R = rat(X,tol)`

```
[N,D] =
rat(___)
```

returns
the rational fraction approximation of `R`

= rat(`X`

)`X`

to
within the default tolerance, `1e-6*norm(X(:),1)`

.
The approximation is a character array containing the truncated continued
fractional expansion.

Even though all floating-point numbers are rational numbers, it is sometimes desirable to approximate them by simple rational numbers, which are fractions whose numerator and denominator are small integers. Rational approximations are generated by truncating continued fraction expansions.

The `rat`

function approximates each element
of `X`

by a continued fraction of the form

$$\frac{N}{D}={D}_{1}+\frac{1}{{D}_{2}+\frac{1}{\left({D}_{3}+\mathrm{...}+\frac{1}{{D}_{k}}\right)}}\text{\hspace{0.17em}}.$$

The *D*s are obtained by repeatedly picking
off the integer part and then taking the reciprocal of the fractional
part. The accuracy of the approximation increases exponentially with
the number of terms and is worst when `X = sqrt(2)`

.
For `X = sqrt(2)`

, the error with `k`

terms
is about `2.68*(.173)^k`

, so each additional term
increases the accuracy by less than one decimal digit. It takes 21
terms to get full floating-point accuracy.