Ask Your Question
1

Plot a function involving lowest terms

asked 2013-07-22 07:33:41 +0200

ensaba gravatar image

updated 2013-07-22 09:43:15 +0200

Hi,

How do I use sage to plot the following function?

$$ f(x)= \left[ \begin {array} {cc} 0, & x \text{ irrational, } 0 \lt x \lt 1 \\ \frac{1}{q},& x = \frac{p}{q} \text{ in lowest term, } 0 < x < 1.\end{array} \right.$$

I actually wrote a function that would generate the following sequence: $$ \frac{1}{n}, \dots , \frac{n-1}{n}$$ for a given $n$.

Then I created a list of lists that contains the above sequence for each n. After flattening that list, I call the set function on it to remove duplicates. Then I use that to creat a list (x,y) tuple to plot the function using scatter plot.

I was wondering if there is a simpler way of doing this.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2013-07-22 08:17:44 +0200

tmonteil gravatar image

updated 2013-07-22 18:20:20 +0200

I modify my answer according to your precision. You can do:

sage: npoints = 10000
sage: f = lambda x: 1/(RR(x).nearby_rational(max_error=1/npoints).denominator())
sage: plot(f, (x,0,1), plot_points=npoints)

(the picture with npoints = 100 look quite urbanized)

To understand what the .nearby_rational() method does on elements of RR, you can do:

sage: a = 0.1
sage: a.nearby_rational?

From a matematical point of view, you might be interested by the "Stern–Brocot tree" and "Farey sequences".

edit flag offensive delete link more

Comments

@tmonteil Thank you for your reply. I have reworded my question make my end objective clear.

ensaba gravatar imageensaba ( 2013-07-22 09:44:50 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-07-22 07:33:41 +0200

Seen: 406 times

Last updated: Jul 22 '13