Loading [MathJax]/jax/output/HTML-CSS/jax.js
Ask Your Question
1

Plot a function involving lowest terms

asked 11 years ago

ensaba gravatar image

updated 11 years ago

Hi,

How do I use sage to plot the following function?

f(x)=[0,x irrational, 0<x<11q,x=pq in lowest term, 0<x<1.

I actually wrote a function that would generate the following sequence: 1n,,n1n 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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 11 years ago

tmonteil gravatar image

updated 11 years ago

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".

Preview: (hide)
link

Comments

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

ensaba gravatar imageensaba ( 11 years ago )

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: 11 years ago

Seen: 824 times

Last updated: Jul 22 '13