Rational fraction approximation
collapse all in page
Syntax
R = rat(X)
R = rat(X,tol)
[N,D] =rat(___)
Description
example
R = rat(X)
returnsthe rational fraction approximation of X
towithin the default tolerance, 1e-6*norm(X(:),1)
.The approximation is a character array containing the truncated continuedfractional expansion.
example
R = rat(X,tol)
approximates X
towithin 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 π
Open Live Script
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
.
ans = 355/113
The resulting expression is a character vector. 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 result is an approximation by continued fractional expansion. If you consider the first two terms of the expansion, you get the approximation , 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.
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
Open Live Script
Create a 4-by-4 matrix.
format short;X = hilb(4)
X = 4×4 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 = 4×4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
D = 4×4 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 rational
.
format rationalX
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 array
numeric array
Input array, specified as a numeric array of class single
or double
.
Data Types: single
| double
Complex Number Support: Yes
tol
— Tolerance
scalar
Tolerance, specified as a scalar. N
and D
approximate X
, such that abs(N./D - X) <= tol
. The default tolerance is 1e-6*norm(X(:),1)
.
Output Arguments
collapse all
R
— Continued fraction
character array
Continued fraction, returned as a character array with m
rows,where m
is the number of elements in X
.The accuracy of the rational approximation via continued fractionsincreases with the number of terms.
N
— Numerator
numeric array
Numerator, returned as a numeric array. N./D
approximates X
.
D
— Denominator
numeric array
Denominator, returned as a numeric array. N./D
approximates X
.
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 fractionexpansions.
The rat
function approximates each elementof X
by a continued fraction of the form
The Ds are obtained by repeatedly pickingoff the integer part and then taking the reciprocal of the fractionalpart. The accuracy of the approximation increases exponentially withthe number of terms and is worst when X = sqrt(2)
.For X = sqrt(2)
, the error with k
termsis about 2.68*(.173)^k
, so each additional termincreases the accuracy by less than one decimal digit. It takes 21terms to get full floating-point accuracy.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
For code generation, only the two output syntax is supported.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006a
See Also
rats | format
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:
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
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office