plotting a multi-precision function

8 views
Skip to first unread message

Dweebe...@gmail.com

unread,
Feb 7, 2008, 1:58:29 PM2/7/08
to sage-newbie
Hi,
I've been looking for a way to plot the following function:

Sum n ->1 to 1000 of sin(x^n)

over the range 0, 2*Pi

This 'quest' :-) has led me to Sage, which seems extremely powerful,
but also has a bit of a learning curve. I was able to get a simple Pi
graph, but trying to graph my target function generates errors. I was
trying to do something like (don't have the exact code in front of
me):

def sinie(x): for n in range(1,1000): sin(x^n)
a = plot(sinie, 0.0, 2*pi)
show(a)

I know this isn't correct on several points, but I don't know exactly
how to fix it.

First I need to sum my sin function ... something like:
def sinie(x): r = 0; for n in range(1,1000): r += sin(x^n); return
r;
What is the syntax for such a function?

Second sin(x^1000) is going to be a pretty big number (outside of the
precision of a 64 bit real). I saw ways to create a "real field" of
an arbitrary number of bits, but I could not figure out how to tell
Sage to just apply that to all it's calculations.

Can anyone guide me in the right direction or provide a 3-4 line
example of what I need?

Thanks,
DL

PS: Bonus number theory question (not really related to Sage) ... the
above function could be done using (64 bit) doubles if the angle part
of x^n could be easily calculated. sin(x) = sin(t + 2*Pi*k) =
sin(t) -->x = t + 2*Pi*k --> t = x - 2*Pi*k (t is x Mod 2*Pi) ...
is their any way to determine sin(x^n) without raising x^n (and
exceeding the range of double), to directly figure out what the angle
(0.0 ... 2*Pi range) would be? Sorry if I haven't made the above
clear.

Carl Witty

unread,
Feb 8, 2008, 1:47:26 PM2/8/08
to sage-newbie
On Feb 7, 10:58 am, "DweeberlyL...@gmail.com"
<DweeberlyL...@gmail.com> wrote:
> Hi,
> I've been looking for a way to plot the following function:
>
> Sum n ->1 to 1000 of sin(x^n)
>
> over the range 0, 2*Pi

It is impossible to plot this function; you would need to use around
10^800 samples to see all the wiggles.

With a totally huge number of samples, like a million, you might be
able to see the effects of the first 8 or so terms in the series...
except that the effects of the other 992 terms would act like
essentially random noise, and would totally overwhelm the first 8
terms (at least toward the right-hand edge of the graph).

Carl Witty

Dweebe...@gmail.com

unread,
Feb 8, 2008, 2:24:00 PM2/8/08
to sage-newbie

Thank you for replying. I see what you are saying and that might be
the heart of the errors I'm receiving (I'm going to play with it more
tonight). If I increment the plot (x values) by 0.001 (course) or
reduce the function from 1->1000 to 1->3 (knowing that I lose a lot of
details), how might I write a Sage program to do this ... my best
guess is something like this (don't have access to SAGE here to test
this):

maxie = 3
def sinie(x): sum(for n in range(1,maxie): sin(x^n))
p = plot(x,0.0, 2*pi, 0.001)
show(p)

Once I can learn how to plot this then I can change the range on the
function and the plot function to better explore my target function.

Dweebe...@gmail.com

unread,
Feb 8, 2008, 7:30:17 PM2/8/08
to sage-newbie
Ok, I'm getting a little closer ... here is the program (it was easier
once I realized the syntax was python):

maxie = 1000
def sinie(x):
rv=0.0
for n in range(1,maxie):
rv += sin(x^n)
return rv
p = plot(sinie, 0.0, pi/2)
show(p)

This works until I increase the plot range to pi, where it gives me
the following error:
File "/home/notebook/sage_notebook/worksheets/admin/4/code/38.py",
line 8, in sinie
rv += sin(x**n)
OverflowError: (34, 'Numerical result out of range')

I believe this is because I am exceeding the range of a double, but
I'm still not sure how to change the numeric precision so that this
will not happen :-(

Suggestions?

Thanks,



On Feb 8, 2:24 pm, "DweeberlyL...@gmail.com" <DweeberlyL...@gmail.com>
wrote:
> > Carl Witty- Hide quoted text -
>
> - Show quoted text -

Carl Witty

unread,
Feb 8, 2008, 11:33:58 PM2/8/08
to sage-newbie
On Feb 8, 4:30 pm, "DweeberlyL...@gmail.com" <DweeberlyL...@gmail.com>
wrote:
> Ok, I'm getting a little closer ... here is the program (it was easier
> once I realized the syntax was python):
>
> maxie = 1000
> def sinie(x):
> rv=0.0
> for n in range(1,maxie):
> rv += sin(x^n)
> return rv
> p = plot(sinie, 0.0, pi/2)
> show(p)
>
> This works until I increase the plot range to pi, where it gives me
> the following error:
> File "/home/notebook/sage_notebook/worksheets/admin/4/code/38.py",
> line 8, in sinie
> rv += sin(x**n)
> OverflowError: (34, 'Numerical result out of range')
>
> I believe this is because I am exceeding the range of a double, but
> I'm still not sure how to change the numeric precision so that this
> will not happen :-(
>
> Suggestions?

Here is some code that will plot samples from this curve:

maxie = 1000
RF = RealField(maxie*3 + 20)
def sinie(x):
rfx = RF(x)
rfxn = RF(1)
rv=float(0.0)
for i in (1..maxie):
rfxn = rfxn*rfx
rv = rv + sin(rfxn)
return rv

plot(sinie, 0, 2*pi)

With this code, sinie will give reasonably accurate results on the
arguments you pass it. I've tried this with maxie=3, 20, and 100, and
got plausible-looking plots. Doing a plot with maxie=1000 is quite
slow, and my plot has not terminated yet.

Let me emphasize again that THE REAL FUNCTION LOOKS NOTHING LIKE THE
PLOTS YOU WILL GET WITH THIS METHOD. For x>1.2 or so, you might as
well just plot random numbers.

You can make things faster, at the cost of having sinie return
incorrect values, by changing the "RF =" line to "RF =
RealField(53)". But it doesn't really matter that sinie returns
incorrect values, since they might as well be random anyway...

Carl

Dweebe...@gmail.com

unread,
Feb 11, 2008, 10:47:08 AM2/11/08
to sage-newbie
Thanks, that's exactly what I needed. I can "zoom-in" on parts of
interest to see approximations of this rapidly changing function, I
realize that as x gets bigger it will be more and more difficult to
extract useful info from the ploy approximation.
Thanks again!
> Carl- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages