ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 19 Nov 2023 12:32:40 +0100An infinite continued fraction of $x^n$https://ask.sagemath.org/question/74375/an-infinite-continued-fraction-of-xn/ I coded in SageMath to compute the 50<sup>th</sup> 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([0] + 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$.Sat, 18 Nov 2023 18:40:32 +0100https://ask.sagemath.org/question/74375/an-infinite-continued-fraction-of-xn/Comment by hbghlyj for <p>I coded in SageMath to compute the 50<sup>th</sup> convergent of $f(x)$ and plot for integers $x=1,\dots,50$:</p>
<pre><code>def f(x, n_terms):
result = []
for i in range(n_terms):
result.extend([x**i, x**i])
return continued_fraction([0] + 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}}}$')
</code></pre>
<p>My question is to make a continuous plot of $f(x)$.
I have difficulty in that <code>continued_fraction</code> only support integer values of $x$.</p>
https://ask.sagemath.org/question/74375/an-infinite-continued-fraction-of-xn/?comment=74380#post-id-74380Yes, 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?Sun, 19 Nov 2023 00:50:53 +0100https://ask.sagemath.org/question/74375/an-infinite-continued-fraction-of-xn/?comment=74380#post-id-74380Comment by Max Alekseyev for <p>I coded in SageMath to compute the 50<sup>th</sup> convergent of $f(x)$ and plot for integers $x=1,\dots,50$:</p>
<pre><code>def f(x, n_terms):
result = []
for i in range(n_terms):
result.extend([x**i, x**i])
return continued_fraction([0] + 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}}}$')
</code></pre>
<p>My question is to make a continuous plot of $f(x)$.
I have difficulty in that <code>continued_fraction</code> only support integer values of $x$.</p>
https://ask.sagemath.org/question/74375/an-infinite-continued-fraction-of-xn/?comment=74378#post-id-74378Your 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?Sat, 18 Nov 2023 21:52:25 +0100https://ask.sagemath.org/question/74375/an-infinite-continued-fraction-of-xn/?comment=74378#post-id-74378Answer by Max Alekseyev for <p>I coded in SageMath to compute the 50<sup>th</sup> convergent of $f(x)$ and plot for integers $x=1,\dots,50$:</p>
<pre><code>def f(x, n_terms):
result = []
for i in range(n_terms):
result.extend([x**i, x**i])
return continued_fraction([0] + 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}}}$')
</code></pre>
<p>My question is to make a continuous plot of $f(x)$.
I have difficulty in that <code>continued_fraction</code> only support integer values of $x$.</p>
https://ask.sagemath.org/question/74375/an-infinite-continued-fraction-of-xn/?answer=74383#post-id-74383Your 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([0]+X) / continuant(X)
import functools
plot( functools.partial(g,n_terms=50), (1,50) )
Sun, 19 Nov 2023 05:35:14 +0100https://ask.sagemath.org/question/74375/an-infinite-continued-fraction-of-xn/?answer=74383#post-id-74383Comment by Max Alekseyev for <p>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:</p>
<pre><code>def g(x, n_terms):
X = sum( ([x^i,x^i] for i in range(n_terms)), [])
return continuant([0]+X) / continuant(X)
import functools
plot( functools.partial(g,n_terms=50), (1,50) )
</code></pre>
https://ask.sagemath.org/question/74375/an-infinite-continued-fraction-of-xn/?comment=74386#post-id-74386It'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.Sun, 19 Nov 2023 12:32:40 +0100https://ask.sagemath.org/question/74375/an-infinite-continued-fraction-of-xn/?comment=74386#post-id-74386Comment by hbghlyj for <p>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:</p>
<pre><code>def g(x, n_terms):
X = sum( ([x^i,x^i] for i in range(n_terms)), [])
return continuant([0]+X) / continuant(X)
import functools
plot( functools.partial(g,n_terms=50), (1,50) )
</code></pre>
https://ask.sagemath.org/question/74375/an-infinite-continued-fraction-of-xn/?comment=74385#post-id-74385The 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)'Sun, 19 Nov 2023 11:21:45 +0100https://ask.sagemath.org/question/74375/an-infinite-continued-fraction-of-xn/?comment=74385#post-id-74385