# An infinite continued fraction of $x^n$

I coded in SageMath to compute the 50th convergent of $f(x)$ and plot for integers $x=1,\dots,50$:

def f(x, n_terms):
result = []
for i in range(n_terms):
result.extend([x**i, x**i])
return continued_fraction( + result).n()

list_plot(([f(x,50) for x in range(1,50)]), title='$f(x)=\\dfrac{x}{x+\\dfrac{x^2}{x^2+\\dfrac{x^3}{x^3+\\ddots}}}$')


My question is to make a continuous plot of $f(x)$. I have difficulty in that continued_fraction only support integer values of $x$.

edit retag close merge delete

Your function f cannot be evaluated at non-integer values x since continued_fraction() expects a list with integer elements. Hence, f (as defined) is not a continuous function and cannot be plotted as such. Do you mean a continuous interpolation of f from its values at integer points or something else?

Yes, the built-in function continued_fraction() expects a list of integers, but I want to compute $$f(x)=\cfrac{x}{x+\cfrac{x^2}{x^2+\cfrac{x^3}{x^3+\cdots}}}$$ $f$ is a continuous function for real number $x>1$. Does SageMath have built-in function to compute continued fractions of real numbers?

Sort by » oldest newest most voted Your generalized continued fraction can be reduced to the conventional one: $[0;1,1,x,x,x^2,x^2,\dots]$ Correspondingly, it can be computed for arbitrary $x$ via continuants, and then plotted as in the following code:

def g(x, n_terms):
X = sum( ([x^i,x^i] for i in range(n_terms)), [])
return continuant(+X) / continuant(X)

import functools
plot( functools.partial(g,n_terms=50), (1,50) )

more

The code throws error:

verbose 0 (3935: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 198 points.
verbose 0 (3935: plot.py, generate_plot_points) Last error message: 'Unable to compute f(50.0)'


It's still plotting, isn't it? This is what I see in Sagecell: https://sagecell.sagemath.org/?q=vbtqxm The issue is in computing the function for large arguments as I understand.