# rat

Rational fraction approximation (continued fraction)

## Description

returns the
rational fraction approximation of `R`

= rat(`X`

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

`___ = rat(___,`

uses additional options specified by one or more `Name,Value`

)`Name,Value`

pair
arguments to approximate `X`

.

## Examples

### Find Rational Approximation of Irrational Number

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

`$$\mathrm{ArationalapproximationofXis}$$`

$$2+\frac{1}{-4+\frac{1}{4+\frac{1}{-4+\frac{1}{4+\frac{1}{-4}}}}}$$

### Approximate Value of π with Different Precision

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$$`

### Approximate Solution of Equation

Solve the equation $$\mathrm{cos}(x)+{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$$`

### Find Rational Approximation of Golden Ratio

Define the golden ratio $$X=(1+\sqrt{5})/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

`X`

— Input

number | vector | matrix | array | symbolic number | symbolic array

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

**Data Types: **`single`

| `double`

| `sym`

**Complex Number Support: **Yes

`tol`

— Tolerance

scalar

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`

`Length`

— Number of coefficients

positive integer

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

.

**Example: **`5`

`Positive`

— Option to return positive coefficients

logical `0`

(`false`

) (default) | logical value

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

`R`

— Continued fraction

character array

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 2*m*+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`

.

`N`

— Numerator

number | vector | matrix | array | symbolic number | symbolic array

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

approximates `X`

.

`D`

— Denominator

number | vector | matrix | array | symbolic number | symbolic array

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`

.

## More About

### Simple Continued Fraction

The `rat`

function approximates each element of
`X`

by a simple continued fraction of the form

$$R=\frac{N}{D}={a}_{1}+\frac{1}{{a}_{2}+\frac{1}{\ddots +\frac{1}{{a}_{k}}}}\text{}$$

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**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)