Previous: Simple File I/O, Up: Basic Input and Output



14.1.4 Rational Approximations

— Function File: s = rat (x, tol)
— Function File: [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
     

Called with two arguments returns the numerator and denominator separately as two matrices.

See also: rats.

— Built-in Function: rats (x, len)

Convert x into a rational approximation represented as a string. You can convert the string 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.

See also: format, rat.