Next: , Previous: , Up: Arithmetic   [Contents][Index]


17.7 Rational Approximations

: s = rat (x, tol)
: [n, d] = rat (x, tol)

Find a rational approximation to x within the tolerance defined by tol using a continued fraction expansion.

For example:

rat (pi) = 3 + 1/(7 + 1/16) = 355/113
rat (e) = 3 + 1/(-4 + 1/(2 + 1/(5 + 1/(-2 + 1/(-7)))))
        = 1457/536

When called with two output arguments return the numerator and denominator separately as two matrices.

See also: rats.

: rats (x, len)

Convert x into a rational approximation represented as a string.

The string can be converted back into a matrix as follows:

r = rats (hilb (4));
x = str2num (r)

The optional second argument defines the maximum length of the string representing the elements of x. By default len is 9.

If the length of the smallest possible rational approximation exceeds len, an asterisk (*) padded with spaces will be returned instead.

See also: format, rat.