Documentation

rats

Rational output

Syntax

S = rats(X)
S = rats(X,strlen)

Description

example

S= rats(X)returns a character vector containing the rational approximations to the elements ofXusing the default length of13

ratsreturns asterisks for elements that cannot be printed in the allotted space, but which are not negligible compared to the other elements inX

example

S= rats(X,strlen)returns a character vector of lengthstrlen。这一比率nal approximation uses a tolerance that is inversely proportional to the length.

Examples

collapse all

Create a 4-by-4 matrix.

formatshortX = hilb(4)
X =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

View the rational representation of the matrix usingrats。The result is the same as usingformat rat

R = rats(X)
R =4x56 char array' 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 '

Find the rational representation ofpiwith the default character vector length and approximation tolerance. The result is the same as usingformat rat

rats(pi)
ans = ' 355/113 '

Adjust the length of the output, which also adjusts the approximation tolerance.

rats(pi,20)
ans = ' 104348/33215 '

The resulting rational approximation has greater accuracy. As the output length increases, the tolerance decreases.

Adjust the output length again to achieve greater accuracy.

rats(pi,25)
ans = ' 1146408/364913 '

The resulting approximation agrees withpito 10 decimal places.

Input Arguments

collapse all

Input array, specified as a numeric array of classsingleordouble

Data Types:single|double
Complex Number Support:Yes

Length of character vector, specified as a positive integer. The default length is 13, which allows for 6 elements in 78 spaces.

Output Arguments

collapse all

Rational output, returned as a character vector.

Algorithms

ratsobtains rational approximations with[N,D] = rat(X,tol), wheretolismin(10^(-(strlen-1)/2)*norm(X(isfinite(X)),1),.1)。Thus, the tolerance is inversely proportional to the output length,strlen

See Also

|

Introduced before R2006a

Was this topic helpful?