rat

Rational fraction approximation (continued fraction)

Syntax

``R = rat(X)``
``R = rat(X,tol)``
``[N,D] = rat(___)``
``___ = rat(___,Name,Value)``

Description

example

````R = rat(X)` returns the rational fraction approximation of `X` to within the default tolerance, `1.e-6*norm(X(:),1)`. The approximation is a character array containing the simple continued fraction with finite terms.```

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`. You can use this output syntax with any of the previous input syntaxes.```

example

````___ = rat(___,Name,Value)` uses additional options specified by one or more `Name,Value` pair arguments to approximate `X`.```

Examples

collapse all

Declare the irrational number $\sqrt{3}$ as a symbolic number.

`X = sqrt(sym(3))`
`X = $\sqrt{3}$`

Find the rational fraction approximation (truncated continued fraction) of this number. The resulting expression is a character vector.

`R = rat(X)`
```R = '2 + 1/(-4 + 1/(4 + 1/(-4 + 1/(4 + 1/(-4)))))' ```

Display the symbolic formula from the character vector `R`.

`displayFormula(["'A rational approximation of X is'"; R])`
`$2+\frac{1}{-4+\frac{1}{4+\frac{1}{-4+\frac{1}{4+\frac{1}{-4}}}}}$`

Represent the mathematical quantity $\pi$ as a symbolic constant. The constant $\pi$ is an irrational number.

`X = sym(pi)`
`X = $\pi$`

Use `vpa` to show the decimal representation of $\pi$ with 12 significant digits.

`Xdec = vpa(X,12)`
`Xdec = $3.14159265359$`

Find the rational fraction approximation of $\pi$ using the `rat` function with default tolerance. The resulting expression is a character vector.

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

Use `str2sym` to turn the character vector into a single fractional number.

`Q = str2sym(R)`
```Q =  $\frac{355}{113}$```

Show the decimal representation of the fractional number $355/113$. This approximation agrees with $\pi$ to 6 decimal places.

`Qdec = vpa(Q,12)`
`Qdec = $3.14159292035$`

You can specify a tolerance for additional accuracy in the approximation.

`R = rat(sym(pi),1e-8)`
```R = '3 + 1/(7 + 1/(16 + 1/(-294)))' ```
`Q = str2sym(R)`
```Q =  $\frac{104348}{33215}$```

The resulting approximation, $104348/33215$, agrees with $\pi$ to 9 decimal places.

`Qdec = vpa(Q,12)`
`Qdec = $3.14159265392$`

Solve the equation $\mathrm{cos}\left(x\right)+{x}^{2}+x=42$ using `vpasolve`. The solution is returned in decimal representation.

```syms x sol = vpasolve(cos(x) + x^2 + x == 42)```
`sol = $5.9274875551262136192212919837749$`

Approximate the solution as a continued fraction.

`R = rat(sol)`
```R = '6 + 1/(-14 + 1/(5 + 1/(-5)))' ```

To extract the coefficients in the denominator of the continued fraction, you can use the `regexp` function and convert them to a character array.

`S = char(regexp(R,'(-*\d+','match'))`
```S = 3x4 char array '(-14' '(5 ' '(-5 ' ```

Return the result as a symbolic array.

`coeffs = sym(S(:,2:end))`
```coeffs =  $\left(\begin{array}{c}-14\\ 5\\ -5\end{array}\right)$```

Use `str2sym` to turn the continued fraction `R` into a single fractional number.

`Q = str2sym(R)`
```Q =  $\frac{1962}{331}$```

You can also return the numerator and denominator of the rational approximation by specifying two output arguments for the `rat` function.

`[N,D] = rat(sol)`
`N = $1962$`
`D = $331$`

Define the golden ratio $X=\left(1+\sqrt{5}\right)/2$ as a symbolic number.

`X = (sym(1) + sqrt(5))/ 2`
```X =  $\frac{\sqrt{5}}{2}+\frac{1}{2}$```

Find the rational approximation of $X$ within a tolerance of `1e-4`.

`R = rat(X,1e-4)`
```R = '2 + 1/(-3 + 1/(3 + 1/(-3 + 1/(3 + 1/(-3)))))' ```

To return the rational approximation with 10 coefficients, set the `'Length'` option to `10`. This option ignores the specified tolerance in the approximation.

`R10 = rat(X,1e-4,'Length',10)`
```R10 = '2 + 1/(-3 + 1/(3 + 1/(-3 + 1/(3 + 1/(-3 + 1/(3 + 1/(-3 + 1/(3 + 1/(-3)))))))))' ```

To return the rational approximation with all positive coefficients, set the `'Positive'` option to `true`.

`Rpos = rat(X,1e-4,'Positive',true)`
```Rpos = '1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1))))))))))' ```

Input Arguments

collapse all

Input, specified as a number, vector, matrix, array, symbolic number, or symbolic array.

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

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)`.

Name-Value Arguments

Specify optional pairs of arguments as `Name1=Value1,...,NameN=ValueN`, where `Name` is the argument name and `Value` is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose `Name` in quotes.

Example: `'Length',5,'Positive',true`

Number of coefficients or terms of the continued fraction, specified as a positive integer. Specifying this option overrides the tolerance argument `tol`.

Example: `5`

Option to return positive coefficients, specified as a logical value (boolean). If you specify `true`, then `rat` returns a regular continued fraction expansion with all positive integers in the denominator.

Example: `true`

Output Arguments

collapse all

Continued fraction, returned as a character array.

• If `X` is an array of m elements and all elements are real numbers, then `R` is returned as a character array with m rows.

• If `X` is an array of m elements that contains a complex number, then `R` is returned as a character array with 2m+1 rows. The first m rows of `R` represent the continued fraction expansion of the real parts of `X`, followed by ```' +i* ... '``` in the (m+1)-th row, and the last m rows represent the continued fraction expansions of the imaginary parts of `X`.

Numerator, returned as a number, vector, matrix, array, symbolic number, or symbolic array. `N./D` approximates `X`.

Denominator, returned as a number, vector, matrix, array, symbolic number, or symbolic array. `N./D` approximates `X`.

Limitations

• You can only specify the `Name,Value` arguments, such as `'Length',5,'Positive',true`, if the array `X` contains a symbolic number or the data type of `X` is `sym`.

collapse all

Simple Continued Fraction

The `rat` function approximates each element of `X` by a simple continued fraction of the form

with a finite number of integer terms ${a}_{1},{a}_{2},\dots ,{a}_{k}$. The accuracy of the rational approximation increases with the number of terms.

Version History

Introduced in R2020a