{"id":4867,"date":"2019-05-13T12:00:03","date_gmt":"2019-05-13T17:00:03","guid":{"rendered":"https:\/\/blogs.mathworks.com\/cleve\/?p=4867"},"modified":"2019-05-16T17:48:16","modified_gmt":"2019-05-16T22:48:16","slug":"continued-fractions-and-function-rat","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/cleve\/2019\/05\/13\/continued-fractions-and-function-rat\/","title":{"rendered":"Continued Fractions and Function &#8220;rat&#8221;"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>Let me tell you about MATLAB's controversial function <tt>rat<\/tt>.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#7161eefc-160e-448d-b3e8-387ca200442e\">Classic <tt>rat<\/tt><\/a><\/li><li><a href=\"#c511a1b9-cbde-44ab-a797-1d247242fb32\">An example<\/a><\/li><li><a href=\"#15c5c040-c269-430a-9bc1-d98897efdef3\">Don't try to hide it<\/a><\/li><li><a href=\"#1c90de06-b70e-4ec6-b9ff-3c40a92173b8\">The controversial algorithm<\/a><\/li><li><a href=\"#c49542f6-f4e9-4ff2-aa17-81a92a7bb8c9\">pi<\/a><\/li><li><a href=\"#e573bf01-d66c-4be1-97bc-07403ccab1af\">Latex<\/a><\/li><li><a href=\"#fa827dc1-b57f-4047-b464-2e9da72d5992\">ratp<\/a><\/li><li><a href=\"#bacc2a0b-42f1-420e-aaca-a2c421865a04\">phi<\/a><\/li><li><a href=\"#894597b5-ee09-4be7-9bfb-4c89ad35d439\">DIY<\/a><\/li><\/ul><\/div><h4>Classic <tt>rat<\/tt><a name=\"7161eefc-160e-448d-b3e8-387ca200442e\"><\/a><\/h4><p>The function <tt>rat<\/tt> is older than MathWorks.  It was one of the 73 functions in my original Fortran MATLAB.  Here is the help entry from 40 years ago.<\/p><pre class=\"language-matlab\">&lt;&gt;\r\nhelp <span class=\"string\">rat<\/span>\r\n<\/pre><pre>  RAT   An experimental  function  which  attempts  to  remove  the\r\n        roundoff   error  from  results  that  should  be  \"simple\"\r\n        rational numbers.\r\n        RAT(X) approximates each  element  of   X  by  a  continued\r\n        fraction of the form<\/pre><pre>                  a\/b = d1 + 1\/(d2 + 1\/(d3 + ... + 1\/dk))<\/pre><pre>        with k &lt;= len, integer di and abs(di) &lt;= max .  The default\r\n        values of the parameters are len = 5 and max = 100.\r\n        RAT(len,max) changes the default values.  Increasing either\r\n        len or max increases the number of possible fractions.\r\n        &lt;A,B&gt; = RAT(X) produces integer matrices A and B so that<\/pre><pre>                  A .\/ B  =  RAT(X)<\/pre><pre>        Some examples:<\/pre><pre>              long\r\n              T = hilb(6), X = inv(T)\r\n              &lt;A,B&gt; = rat(X)\r\n              H = A .\/ B, S = inv(H)<\/pre><pre>              short e\r\n              d = 1:8,  e = ones(d),  A = abs(d'*e - e'*d)\r\n              X = inv(A)\r\n              rat(X)\r\n              display(ans)<\/pre><h4>An example<a name=\"c511a1b9-cbde-44ab-a797-1d247242fb32\"><\/a><\/h4><p>Let's try that second example with modern MATLAB and <tt>format rat<\/tt>.<\/p><pre class=\"codeinput\">  format <span class=\"string\">short<\/span>\r\n  d = 1:6\r\n  e = ones(size(d)),\r\n  A = abs(d'*e - e'*d)\r\n\r\n  format <span class=\"string\">short<\/span> <span class=\"string\">e<\/span>\r\n  X = inv(A)\r\n  format <span class=\"string\">rat<\/span>\r\n  X\r\n<\/pre><pre class=\"codeoutput\">\r\nd =\r\n\r\n     1     2     3     4     5     6\r\n\r\n\r\ne =\r\n\r\n     1     1     1     1     1     1\r\n\r\n\r\nA =\r\n\r\n     0     1     2     3     4     5\r\n     1     0     1     2     3     4\r\n     2     1     0     1     2     3\r\n     3     2     1     0     1     2\r\n     4     3     2     1     0     1\r\n     5     4     3     2     1     0\r\n\r\n\r\nX =\r\n\r\n  -4.0000e-01   5.0000e-01   8.3267e-17  -3.3307e-17  -9.9920e-17   1.0000e-01\r\n   5.0000e-01  -1.0000e+00   5.0000e-01   3.6978e-33   8.8818e-17  -5.5511e-17\r\n   8.3267e-17   5.0000e-01  -1.0000e+00   5.0000e-01   1.4063e-16  -9.2519e-17\r\n  -3.3307e-17   3.6978e-33   5.0000e-01  -1.0000e+00   5.0000e-01   5.5511e-17\r\n  -9.9920e-17   8.8818e-17   1.4063e-16   5.0000e-01  -1.0000e+00   5.0000e-01\r\n   1.0000e-01  -5.5511e-17  -9.2519e-17   5.5511e-17   5.0000e-01  -4.0000e-01\r\n\r\n\r\nX =\r\n\r\n  Columns 1 through 5\r\n\r\n      -2\/5            1\/2            *              *              *       \r\n       1\/2           -1              1\/2            *              *       \r\n       *              1\/2           -1              1\/2            *       \r\n       *              *              1\/2           -1              1\/2     \r\n       *              *              *              1\/2           -1       \r\n       1\/10           *              *              *              1\/2     \r\n\r\n  Column 6\r\n\r\n       1\/10    \r\n       *       \r\n       *       \r\n       *       \r\n       1\/2     \r\n      -2\/5     \r\n\r\n<\/pre><p>Which version of <tt>X<\/tt> do you prefer?  The first, printed with a floating point format so the roundoff errors are readily apparent. Or the second, printed with a rational format that seeks to disguise the roundoff.  For this example, almost everybody would vote for the rational format.<\/p><h4>Don't try to hide it<a name=\"15c5c040-c269-430a-9bc1-d98897efdef3\"><\/a><\/h4><p>But I have learned that it is better to explain floating point arithmetic than it is to try to hide it.  Besides, most important MATLAB calculations involve larger matrices, and eigenvalues, differential equations and signal processing.  They don't have such tidy results.<\/p><p>So, we don't have much use for <tt>rat<\/tt> and <tt>format rat<\/tt> today, but they're still there and, as I said, they're moderately controversial.<\/p><h4>The controversial algorithm<a name=\"1c90de06-b70e-4ec6-b9ff-3c40a92173b8\"><\/a><\/h4><p>The core of <tt>rat<\/tt> generates continued fractions by repeatedly subtracting off the integer part and taking the reciprocal of what is left.  If there is nothing left to reciprocate, the process terminates because the input is a rational number.  If the input is irrational, the process does not terminate.<\/p><p>The controversary comes from what is meant by integer part.  To generate proper continued fractions, integer part should be <tt>floor<\/tt>. That always leaves a positive fraction to reciprocate.  But 40 years ago, I got clever and used <tt>round<\/tt> instead of <tt>floor<\/tt>.  That means it may take fewer terms to obtain a specified accuracy, but the continued fractions are, shall I say, <i>unorthodox<\/i>.  Let's see more examples.<\/p><h4>pi<a name=\"c49542f6-f4e9-4ff2-aa17-81a92a7bb8c9\"><\/a><\/h4><p>Let's generate increasingly accurate rational approximations to $\\pi$. With two output arguments, <tt>rat<\/tt> unwinds the continued fraction to produce two integers whose ratio has the same value.  The accuracy of <tt>rat<\/tt> is determined by an optional tolerance, <tt>rat(x,tol)<\/tt>. The default tolerance is <tt>1.0e-6*norm(x)<\/tt>.<\/p><p>Requesting low accuracy produces a familiar approximation of $\\pi$.<\/p><pre class=\"codeinput\">  form = <span class=\"string\">' tol = %6.1e \\n r = %s \\n   = %d\/%d\\n'<\/span>;\r\n\r\n  tol = 1.0e-2;\r\n  r = rat(pi,tol);\r\n  [n,d] = rat(pi,tol);\r\n  fprintf(form,tol,r,n,d)\r\n<\/pre><pre class=\"codeoutput\"> tol = 1.0e-02 \r\n r = 3 + 1\/(7) \r\n   = 22\/7\r\n<\/pre><p>The default tolerance produces a less familiar, but elegant approximation.<\/p><pre class=\"codeinput\">  tol = 1.0e-6;\r\n  r = rat(pi,tol);\r\n  [n,d] = rat(pi,tol);\r\n  fprintf(form,tol,r,n,d)\r\n<\/pre><pre class=\"codeoutput\"> tol = 1.0e-06 \r\n r = 3 + 1\/(7 + 1\/(16)) \r\n   = 355\/113\r\n<\/pre><p>Somewhat higher accuracy produces a negative term.<\/p><pre class=\"codeinput\">  tol = 1.0e-8;\r\n  r = rat(pi,tol);\r\n  [n,d] = rat(pi,tol);\r\n  fprintf(form,tol,r,n,d)\r\n<\/pre><pre class=\"codeoutput\"> tol = 1.0e-08 \r\n r = 3 + 1\/(7 + 1\/(16 + 1\/(-294))) \r\n   = 104348\/33215\r\n<\/pre><p>Another negative term.<\/p><pre class=\"codeinput\">  tol = 1.0e-11;\r\n  r = rat(pi,tol);\r\n  [n,d] = rat(pi,tol);\r\n  fprintf(form,tol,r,n,d)\r\n<\/pre><pre class=\"codeoutput\"> tol = 1.0e-11 \r\n r = 3 + 1\/(7 + 1\/(16 + 1\/(-294 + 1\/(3 + 1\/(-4))))) \r\n   = 1146408\/364913\r\n<\/pre><h4>Latex<a name=\"e573bf01-d66c-4be1-97bc-07403ccab1af\"><\/a><\/h4><p>If we're careful with the curly braces, Latex and MathJax can typeset that last continued fraction and produce a graphic for this post.<\/p><p>{{1}\\over{{7+ {{1}\\over{{16+ {{1}\\over{{-294+ {{1}\\over{{3+ {{1}\\over{-4}}}}}}}}}}}}}}<\/p><p>$${{1}\\over{{7+\r\n{{1}\\over{{16+\r\n{{1}\\over{{-294+\r\n{{1}\\over{{3+\r\n{{1}\\over{-4}}}}}}}}}}}}}}$$<\/p><h4>ratp<a name=\"fa827dc1-b57f-4047-b464-2e9da72d5992\"><\/a><\/h4><p>I have another function, <tt>ratp<\/tt>, that is <tt>rat<\/tt> with <tt>round<\/tt> replaced by <tt>floor<\/tt> so that it produces proper, but longer, continued fractions with positive terms. Let's see how it compares to <tt>rat<\/tt>. Since the MATLAB quantity <tt>pi<\/tt> that approximates $\\pi$ is rational, we can set <tt>tol<\/tt> to zero without fear of an infinite loop.<\/p><pre class=\"codeinput\">  form = <span class=\"string\">' tol = %6.1e \\n r = %s \\n p = %s \\n\\n'<\/span>;\r\n  <span class=\"keyword\">for<\/span> tol = 10.^[-2 -6 -8 -11 -inf]\r\n      fprintf(form,tol,rat(pi,tol),ratp(pi,tol))\r\n  <span class=\"keyword\">end<\/span>\r\n<\/pre><pre class=\"codeoutput\"> tol = 1.0e-02 \r\n r = 3 + 1\/(7) \r\n p = 3 + 1\/(7) \r\n\r\n tol = 1.0e-06 \r\n r = 3 + 1\/(7 + 1\/(16)) \r\n p = 3 + 1\/(7 + 1\/(15 + 1\/(1))) \r\n\r\n tol = 1.0e-08 \r\n r = 3 + 1\/(7 + 1\/(16 + 1\/(-294))) \r\n p = 3 + 1\/(7 + 1\/(15 + 1\/(1 + 1\/(292)))) \r\n\r\n tol = 1.0e-11 \r\n r = 3 + 1\/(7 + 1\/(16 + 1\/(-294 + 1\/(3 + 1\/(-4))))) \r\n p = 3 + 1\/(7 + 1\/(15 + 1\/(1 + 1\/(292 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(2)))))))) \r\n\r\n tol = 0.0e+00 \r\n r = 3 + 1\/(7 + 1\/(16 + 1\/(-294 + 1\/(3 + 1\/(-4 + 1\/(5 + 1\/(-15))))))) \r\n p = 3 + 1\/(7 + 1\/(15 + 1\/(1 + 1\/(292 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(2 + 1\/(1 + 1\/(3 + 1\/(1 + 1\/(14)))))))))))) \r\n\r\n<\/pre><h4>phi<a name=\"bacc2a0b-42f1-420e-aaca-a2c421865a04\"><\/a><\/h4><p>The world's most irrational number, as measured by the length of continued fraction approximations, is $\\phi$, the golden ratio. (Proof: solve $x = 1 + 1\/x$.) The coefficients in the proper continued fraction are all ones, thereby achieving the slowest possible convergence.  For the same accuracy, <tt>ratp(phi,tol)<\/tt> requires about twice as many terms as <tt>rat(phi,tol)<\/tt>.<\/p><pre class=\"codeinput\">   format <span class=\"string\">short<\/span>\r\n   phi = (1 + sqrt(5))\/2\r\n   tol = 1.0e-4\r\n   r = rat(phi)\r\n   p = ratp(phi)\r\n<\/pre><pre class=\"codeoutput\">\r\nphi =\r\n\r\n    1.6180\r\n\r\n\r\ntol =\r\n\r\n   1.0000e-04\r\n\r\n\r\nr =\r\n\r\n    '2 + 1\/(-3 + 1\/(3 + 1\/(-3 + 1\/(3 + 1\/(-3 + 1\/(3 + 1\/(-3)))))))'\r\n\r\n\r\np =\r\n\r\n    '1 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(1 + 1\/(1))))))))))))))'\r\n\r\n<\/pre><pre class=\"codeinput\">   tol = 0;\r\n   lengthr = length(rat(phi,tol))\r\n   lengthp = length(ratp(phi,tol))\r\n<\/pre><pre class=\"codeoutput\">\r\nlengthr =\r\n\r\n   154\r\n\r\n\r\nlengthp =\r\n\r\n   297\r\n\r\n<\/pre><h4>DIY<a name=\"894597b5-ee09-4be7-9bfb-4c89ad35d439\"><\/a><\/h4><p>(For my international readers, DIY stands for \"Do It Yourself\".) Create your own <tt>ratp<\/tt> by making a one-word edit in a copy of <tt>toolbox\\matlab\\specfun\\rat.m<\/tt>.  Compare the two functions on <tt>sqrt(2)<\/tt>. Warning: all is not what appears at first glance.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_b8ab911766214dcb8c1c85a4df89c69f() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='b8ab911766214dcb8c1c85a4df89c69f ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b8ab911766214dcb8c1c85a4df89c69f';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2019 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_b8ab911766214dcb8c1c85a4df89c69f()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2018b<br><\/p><\/div><!--\r\nb8ab911766214dcb8c1c85a4df89c69f ##### SOURCE BEGIN #####\r\n%% Continued Fractions and Function \"rat\"\r\n% Let me tell you about MATLAB's controversial function |rat|.\r\n\r\n%% Classic |rat|\r\n% The function |rat| is older than MathWorks.  It was one of the 73\r\n% functions in my original Fortran MATLAB.  Here is the help entry from\r\n% 40 years ago.\r\n%\r\n%   <>\r\n%   help rat\r\n%\r\n%    RAT   An experimental  function  which  attempts  to  remove  the\r\n%          roundoff   error  from  results  that  should  be  \"simple\"\r\n%          rational numbers.\r\n%          RAT(X) approximates each  element  of   X  by  a  continued\r\n%          fraction of the form\r\n%\r\n%                    a\/b = d1 + 1\/(d2 + 1\/(d3 + ... + 1\/dk))\r\n%\r\n%          with k <= len, integer di and abs(di) <= max .  The default\r\n%          values of the parameters are len = 5 and max = 100.\r\n%          RAT(len,max) changes the default values.  Increasing either\r\n%          len or max increases the number of possible fractions.\r\n%          <A,B> = RAT(X) produces integer matrices A and B so that\r\n%\r\n%                    A .\/ B  =  RAT(X)\r\n%\r\n%          Some examples:\r\n%\r\n%                long\r\n%                T = hilb(6), X = inv(T)\r\n%                <A,B> = rat(X)\r\n%                H = A .\/ B, S = inv(H)\r\n%\r\n%                short e\r\n%                d = 1:8,  e = ones(d),  A = abs(d'*e - e'*d)\r\n%                X = inv(A)\r\n%                rat(X)\r\n%                display(ans)\r\n\r\n%% An example\r\n% Let's try that second example with modern MATLAB and |format rat|.\r\n\r\n  format short\r\n  d = 1:6\r\n  e = ones(size(d)),\r\n  A = abs(d'*e - e'*d)\r\n  \r\n  format short e\r\n  X = inv(A)\r\n  format rat\r\n  X\r\n  \r\n%%\r\n% Which version of |X| do you prefer?  The first, printed with a floating\r\n% point format so the roundoff errors are readily apparent.\r\n% Or the second, printed with a rational format that seeks to disguise\r\n% the roundoff.  For this example, almost everybody would vote for the\r\n% rational format.\r\n\r\n%% Don't try to hide it\r\n% But I have learned that it is better to explain floating point\r\n% arithmetic than it is to try to hide it.  Besides, most important\r\n% MATLAB calculations involve larger matrices, and eigenvalues,\r\n% differential equations and signal processing.  They don't have\r\n% such tidy results.\r\n\r\n%%\r\n% So, we don't have much use for |rat| and |format rat| today,\r\n% but they're still there and, as I said, they're moderately controversial.\r\n\r\n%% The controversial algorithm\r\n% The core of |rat| generates continued fractions by repeatedly\r\n% subtracting off the integer part and taking the reciprocal of\r\n% what is left.  If there is nothing left to reciprocate, the\r\n% process terminates because the input is a rational number.  If the\r\n% input is irrational, the process does not terminate.\r\n%\r\n% The controversary comes from what is meant by integer part.  To\r\n% generate proper continued fractions, integer part should be |floor|. \r\n% That always leaves a positive fraction to reciprocate.  But 40 years\r\n% ago, I got clever and used |round| instead of |floor|.  That means\r\n% it may take fewer terms to obtain a specified accuracy, but\r\n% the continued fractions are, shall I say, _unorthodox_.  Let's see\r\n% more examples.\r\n\r\n%% pi\r\n% Let's generate increasingly accurate rational approximations to $\\pi$.\r\n% With two output arguments, |rat| unwinds the continued fraction\r\n% to produce two integers whose ratio has the same value.  The accuracy\r\n% of |rat| is determined by an optional tolerance, |rat(x,tol)|.\r\n% The default tolerance is |1.0e-6*norm(x)|.\r\n\r\n%%\r\n% Requesting low accuracy produces a familiar approximation of $\\pi$. \r\n\r\n  form = ' tol = %6.1e \\n r = %s \\n   = %d\/%d\\n';\r\n\r\n  tol = 1.0e-2;\r\n  r = rat(pi,tol);\r\n  [n,d] = rat(pi,tol);\r\n  fprintf(form,tol,r,n,d)\r\n  \r\n%%\r\n% The default tolerance produces a less familiar, but elegant approximation.\r\n\r\n  tol = 1.0e-6;\r\n  r = rat(pi,tol);\r\n  [n,d] = rat(pi,tol);\r\n  fprintf(form,tol,r,n,d)\r\n  \r\n%%\r\n% Somewhat higher accuracy produces a negative term.\r\n\r\n  tol = 1.0e-8;\r\n  r = rat(pi,tol);\r\n  [n,d] = rat(pi,tol);\r\n  fprintf(form,tol,r,n,d)\r\n  \r\n%% \r\n% Another negative term.\r\n\r\n  tol = 1.0e-11;\r\n  r = rat(pi,tol);\r\n  [n,d] = rat(pi,tol);\r\n  fprintf(form,tol,r,n,d)  \r\n  \r\n%% Latex\r\n% If we're careful with the curly braces, Latex and MathJax can typeset\r\n% that last continued fraction and produce a graphic for this post.\r\n%\r\n% {{1}\\over{{7+\r\n% {{1}\\over{{16+\r\n% {{1}\\over{{-294+\r\n% {{1}\\over{{3+\r\n% {{1}\\over{-4}}}}}}}}}}}}}}\r\n\r\n%%\r\n% $${{1}\\over{{7+\r\n% {{1}\\over{{16+\r\n% {{1}\\over{{-294+\r\n% {{1}\\over{{3+\r\n% {{1}\\over{-4}}}}}}}}}}}}}}$$\r\n\r\n%% ratp\r\n% I have another function, |ratp|, that is |rat| with |round| replaced by\r\n% |floor| so that it produces proper, but longer, continued fractions\r\n% with positive terms. Let's see how it compares to |rat|.\r\n% Since the MATLAB quantity |pi| that approximates $\\pi$ is rational,\r\n% we can set |tol| to zero without fear of an infinite loop.\r\n\r\n  form = ' tol = %6.1e \\n r = %s \\n p = %s \\n\\n';\r\n  for tol = 10.^[-2 -6 -8 -11 -inf]\r\n      fprintf(form,tol,rat(pi,tol),ratp(pi,tol))\r\n  end\r\n  \r\n%% phi\r\n% The world's most irrational number, as measured by the length of\r\n% continued fraction approximations, is $\\phi$, the golden ratio.\r\n% (Proof: solve $x = 1 + 1\/x$.)\r\n% The coefficients in the proper continued fraction are all ones,\r\n% thereby achieving the slowest possible convergence.  For the same\r\n% accuracy, |ratp(phi,tol)| requires about twice as many terms\r\n% as |rat(phi,tol)|.\r\n\r\n   format short \r\n   phi = (1 + sqrt(5))\/2\r\n   tol = 1.0e-4\r\n   r = rat(phi)\r\n   p = ratp(phi)\r\n   \r\n%%\r\n   tol = 0;\r\n   lengthr = length(rat(phi,tol))\r\n   lengthp = length(ratp(phi,tol))\r\n   \r\n%% DIY\r\n% (For my international readers, DIY stands for \"Do It Yourself\".)\r\n% Create your own |ratp| by making a one-word edit in a copy of\r\n% |toolbox\\matlab\\specfun\\rat.m|.  Compare the two functions on |sqrt(2)|.\r\n% Warning: all is not what appears at first glance.\r\n##### SOURCE END ##### b8ab911766214dcb8c1c85a4df89c69f\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/pi_approx_2.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><!--introduction--><p>Let me tell you about MATLAB's controversial function <tt>rat<\/tt>.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/cleve\/2019\/05\/13\/continued-fractions-and-function-rat\/\">read more >><\/a><\/p>","protected":false},"author":78,"featured_media":4895,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,4,16,7],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/4867"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/comments?post=4867"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/4867\/revisions"}],"predecessor-version":[{"id":4885,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/4867\/revisions\/4885"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media\/4895"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media?parent=4867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/categories?post=4867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/tags?post=4867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}