rat
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 as a symbolic number.
X = sqrt(sym(3))
X =
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])
Approximate Value of π with Different Precision
Represent the mathematical quantity as a symbolic constant. The constant is an irrational number.
X = sym(pi)
X =
Use vpa
to show the decimal representation of with 12 significant digits.
Xdec = vpa(X,12)
Xdec =
Find the rational fraction approximation of 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 =
Show the decimal representation of the fractional number . This approximation agrees with to 6 decimal places.
Qdec = vpa(Q,12)
Qdec =
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 =
The resulting approximation, , agrees with to 9 decimal places.
Qdec = vpa(Q,12)
Qdec =
Approximate Solution of Equation
Solve the equation using vpasolve
. The solution is returned in decimal representation.
syms x
sol = vpasolve(cos(x) + x^2 + x == 42)
sol =
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 =
Use str2sym
to turn the continued fraction R
into a single fractional number.
Q = str2sym(R)
Q =
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 =
D =
Find Rational Approximation of Golden Ratio
Define the golden ratio as a symbolic number.
X = (sym(1) + sqrt(5))/ 2
X =
Find the rational approximation of 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, thenR
is returned as a character array with m rows.If
X
is an array of m elements that contains a complex number, thenR
is returned as a character array with 2m+1 rows. The first m rows ofR
represent the continued fraction expansion of the real parts ofX
, followed by' +i* ... '
in the (m+1)-th row, and the last m rows represent the continued fraction expansions of the imaginary parts ofX
.
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 arrayX
contains a symbolic number or the data type ofX
issym
.
More About
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 . The accuracy of the rational approximation increases with the number of terms.
Version History
Introduced in R2020a
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
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)