How to plot Ramanujan's continued fraction in Mathematica?

The built-in function ContinuedFractionK can be used to generate an approximation to R[q] good enough for plotting purposes.

 r[q_, n_] = q^(1/5) ContinuedFractionK[q^i, 1, {i, 0, n}];
 r[q, 4]

rq4

A very reasonable plot can be made with

Plot[r[q, 20], {q, 0, 3}]

plot


...and now, the answer I promised to write.

As I noted in the comments, there is in fact an explicit formula for the RRCF in terms of built-in Mathematica functions, thanks to the deep theory of modular forms:

$$\mathcal{R}(q)=\sqrt[5]{q}\frac{\left(q;q^5\right)_\infty \left(q^4;q^5\right)_\infty}{\left(q^2;q^5\right)_\infty \left(q^3;q^5\right)_\infty}$$

Here, $\left(a;q\right)_\infty$ is the $q$-Pochhammer symbol,

$$\left(a;q\right)_\infty=\prod_{j=0}^\infty \left(1-a q^j\right)$$

which is built-in as QPochhammer[a, q]. Thus, here is the definition of the RRCF in Mathematica:

RogersRamanujanR[q_] :=
     q^(1/5) (QPochhammer[q, q^5] QPochhammer[q^4, q^5])/
             (QPochhammer[q^2, q^5] QPochhammer[q^3, q^5])

The other answers have already shown you a plot of the RRCF on the real line, but it looks so boring and flat in that domain. The complexities of this function become apparent when you consider it as a function of complex $q$. In particular, the unit circle is a natural boundary of analyticity for the RRCF, and thus it does not make sense to evaluate it for $|q| > 1$ (though the truncations of the continued fraction exhibit interesting properties for $|q| > 1$; see Trott's article for more details).

Here, then, are density plots of the real and imaginary plots of the RRCF over the unit disk:

With[{ε = 1*^-4}, GraphicsRow[
     {DensityPlot[Re[RogersRamanujanR[x + I y]],
                  {x, -1 + ε, 1 - ε}, {y, -1 + ε, 1 - ε},
                  ColorFunction -> "ThermometerColors", MaxRecursion -> 0,
                  PlotPoints -> 135, RegionFunction -> (Norm[{#1, #2}] < 1 &)], 
      DensityPlot[Im[RogersRamanujanR[x + I y]],
                  {x, -1 + ε, 1 - ε}, {y, -1 + ε, 1 - ε},
                  ColorFunction -> "ThermometerColors", MaxRecursion -> 0,
                  PlotPoints -> 135, RegionFunction -> (Norm[{#1, #2}] < 1 &)]}]]

real and imaginary parts of the RRCF

Noteworthy features include the fence of singularities along the unit circle, and the branch cut along the negative real axis (inherited from the $\sqrt[5]{q}$ factor).


In his answer, m_goldberg shows how to use ContinuedFractionK[] to evaluate convergents of the RRCF. This approach, however, is unwieldy to use for numerical evaluation, since in this case you do not know in advance how many terms of the CF are needed to reach convergence. One can instead use specially-adapted forward evaluation algorithms, which provide a running check on the convergence. Here is a routine for evaluating the RRCF, which uses Steed's algorithm:

rrcf[q_?InexactNumberQ, n_Integer: 500] := 
    Module[{eps = 10^(-Precision[q] - 1), d, f, h, k, z},
           f = h = d = z = k = 1;
           While[
                 z *= q; d = 1/(1 + z d);
                 h *= d - 1; f += h; k++;
                 k < n && Abs[h] > Abs[f] eps];
           q^(1/5) f]

Some quick tests show that rrcf[] is slightly faster than RogersRamanujanR[] for values not too near the singularities of the RRCF.


n = 5;    
(q^(1/5)/Fold[HoldForm @ Evaluate[1 + q^#2/#1] &, 1, Reverse @ Range @ n])