function printrat(x) % usage: printrat(x) % description: this routine displays each entry of the matrix x in % an approximate rational form, where the approximation is obtained % via continued fractions. If the second form is invoked, the % numerators are stored in the matrix p and denominators in q. % local variables: % j,k,m: index variables tol = 1e-12; max = 40; [nr,nc]=size(x); num = zeros(size(x)); den = zeros(size(x)); p = zeros(1,3); q = zeros(1,3); disp(''); for j = 1:nr for k = 1:nc alpha = x(j,k); a = floor(alpha); p(1) = 0; q(1) = 1; p(2) = 1; q(2) = 0; for m = 1:max p(3) = a*p(2) + p(1); q(3) = a*q(2) + q(1); if (abs(x(j,k)-p(3)/q(3))