Rational fraction approximation (continued fraction)
Declare the irrational number as a symbolic number.
X = sqrt(sym(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
displayFormula(["'A rational approximation of X is'"; R])
Represent the mathematical quantity as a symbolic constant. The constant is an irrational number.
X = sym(pi)
vpa to show the decimal representation of with 12 significant digits.
Xdec = vpa(X,12)
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))'
str2sym to turn the character vector into a single fractional number.
Q = str2sym(R)
Show the decimal representation of the fractional number . This approximation agrees with to 6 decimal places.
Qdec = vpa(Q,12)
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)
The resulting approximation, , agrees with to 9 decimal places.
Qdec = vpa(Q,12)
Solve the equation using
vpasolve. The solution is returned in decimal representation.
syms x sol = vpasolve(cos(x) + x^2 + x == 42)
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))
str2sym to turns the continued fraction
R into a single fractional number.
Q = str2sym(R)
You can also return the numerator and denominator of the rational approximation by specifying two output arguments for the
[N,D] = rat(sol)
Define the golden ratio as a symbolic number.
X = (sym(1) + sqrt(5))/ 2
Find the rational approximation of within a tolerance of
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
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, specified as a number, vector, matrix, array, symbolic number, or symbolic array.
Complex Number Support: Yes
Tolerance, specified as a scalar.
X, such that
N./D - X < tol. The
default tolerance is
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
'Length'— Number of coefficients
Number of coefficients or terms of the continued fraction, specified as a positive
integer. Specifying this option overrides the tolerance argument
'Positive'— Option to return positive coefficients
false) (default) | logical value
Option to return positive coefficients, specified as a logical value (boolean). If
rat returns a regular
continued fraction expansion with all positive integers in the denominator.
R— Continued fraction
Continued fraction, returned as a character array.
X is an array of m elements and all elements are real numbers, then
R is returned as a character array with m rows.
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
... ' in the (m+1)-th row, and the last m rows represent the continued fraction expansions of the
imaginary parts of
Numerator, returned as a number, vector, matrix, array, symbolic number, or symbolic
Denominator, returned as a number, vector, matrix, array, symbolic number, or
You can only specify the
Name,Value arguments, such as
'Length',5,'Positive',true, if the array
contains a symbolic number or the data type of
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.