rat

Rational fraction approximation

Syntax

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

Description

example

````R = rat(X)` returns the rational fraction approximation of `X` to within the default tolerance, `1e-6*norm(X(:),1)`. The approximation is a string containing the truncated continued fractional expansion.```

example

````R = rat(X,tol)` approximates `X` to within the tolerance, `tol`.```

example

``````[N,D] = rat(___)``` returns two arrays, `N` and `D`, such that `N./D` approximates `X`, using any of the above syntaxes.```

Examples

collapse all

Approximate Value of π

Approximate the value of π using a rational representation of the quantity `pi`.

The mathematical quantity π is not a rational number, but the quantity `pi` that approximates it is a rational number since all floating-point numbers are rational.

Find the rational representation of `pi`.

```format rat pi ```
```ans = 355/113 ```

The resulting expression is a string. You also can use `rats(pi)` to get the same answer.

Use `rat` to see the continued fractional expansion of `pi`.

`R = rat(pi)`
```R = 3 + 1/(7 + 1/(16)) ```

The resulting string is an approximation by continued fractional expansion. If you consider the first two terms of the expansion, you get the approximation $3+\frac{1}{7}=\frac{22}{7}$, which only agrees with `pi` to 2 decimals.

However, if you consider all three terms printed by `rat`, you can recover the value `355/113`, which agrees with `pi` to 6 decimals.

$3+\frac{1}{7+\frac{1}{16}}=\frac{355}{113}\text{\hspace{0.17em}}.$

Specify a tolerance for additional accuracy in the approximation.

```R = rat(pi,1e-7) ```
```R = 3 + 1/(7 + 1/(16 + 1/(-294))) ```

The resulting approximation, `104348/33215`, agrees with `pi` to 9 decimals.

Express Array Elements as Ratios

Create a 4-by-4 matrix.

```format short; X = hilb(4)```
```X = 1.0000 0.5000 0.3333 0.2500 0.5000 0.3333 0.2500 0.2000 0.3333 0.2500 0.2000 0.1667 0.2500 0.2000 0.1667 0.1429```

Express the elements of `X` as ratios of small integers using `rat`.

`[N,D] = rat(X)`
```N = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 D = 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7```

The two matrices, `N` and `D`, approximate `X` with `N./D`.

View the elements of `X` as ratios using ```format rat```.

```format rat X```
```X = 1 1/2 1/3 1/4 1/2 1/3 1/4 1/5 1/3 1/4 1/5 1/6 1/4 1/5 1/6 1/7 ```

In this form, it is clear that `N` contains the numerators of each fraction and `D` contains the denominators.

Input Arguments

collapse all

`X` — Input arraynumeric array

Input array, specified as a numeric array of class `single` or `double`.

Data Types: `single` | `double`
Complex Number Support: Yes

`tol` — Tolerancescalar

Tolerance, specified as a scalar. `N` and `D` approximate `X`, such that `N./D - X < tol`. The default tolerance is `1e-6*norm(X(:),1)`.

Output Arguments

collapse all

`R` — Continued fractionstring

Continued fraction, returned as a string. The accuracy of the rational approximation via continued fractions increases with the number of terms.

`N` — Numeratornumeric array

Numerator, returned as a numeric array. `N./D` approximates `X`.

`D` — Denominatornumeric array

Denominator, returned as a numeric array. `N./D` approximates `X`.

collapse all

Algorithms

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}+...+\frac{1}{{D}_{k}}\right)}}\text{\hspace{0.17em}}.$

The Ds 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.