Re: Plotting a function -
- To: mathgroup at smc.vnet.net
- Subject: [mg71650] Re: Plotting a function -
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 26 Nov 2006 03:48:32 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <ek990u$k88$1@smc.vnet.net>
Craig Reed wrote:
> Hi -
>
> I'm trying to get Mathematica 5.2 to graph a function which is the ratio of
> integers which have a '3' in them. Done in Exce3l, the graph of the first
> 32,000 data points has a fractal look to it, especially when done with a
> log scale.
>
> What I've tried is the following
>
>
> f[x_] := Boole[DigitCount[x, 10, 3]]
> g[x_] := Sum[f, {i, x}]/x
> Plot[g, {x, 1, 100}]
>
>
> I get 3 errors of "g is not a michine-size real number at" followed by 3
> real numbers:
> 1.000004125
> 5.016125.....
> 9.39607.....
>
> What am I doing wrong? or perhaps what I should ask is, "Is there a better
> way?"
>
Craig,
The corrected version of your code I gave you in my previous email is
very slow indeed. You will find below a functional code that is much faster.
In[1]:=
f[x_] := Boole[DigitCount[x, 10, 3] > 0]
g[x_] := Sum[f[i], {i, x}]/x
Plot[Evaluate[g[x]], {x, 1, 100}];
In[4]:=
Timing[Plot[Evaluate[g[x]], {x, 1, 100}]; ]
Out[4]=
{0.609 Second,Null}
In[5]:=
Timing[Plot[Evaluate[g[x]], {x, 1, 1000}]; ]
Out[5]=
{23.282 Second,Null}
In[23]:=
Timing[rng = Range[1, 100];
ListPlot[Rest[FoldList[Plus, 0,
(Boole[DigitCount[#1, 10, 3] > 0] & ) /@ rng]]/
rng]; ]
Out[23]=
{0.015 Second,Null}
In[24]:=
Timing[rng = Range[1, 1000];
ListPlot[Rest[FoldList[Plus, 0,
(Boole[DigitCount[#1, 10, 3] > 0] & ) /@ rng]]/
rng]; ]
Out[24]=
{0.078 Second,Null}
In[25]:=
Timing[rng = Range[1, 10000];
ListPlot[Rest[FoldList[Plus, 0,
(Boole[DigitCount[#1, 10, 3] > 0] & ) /@ rng]]/
rng]; ]
Out[25]=
{1.141 Second,Null}
In[26]:=
Timing[rng = Range[1, 100000];
ListPlot[Rest[FoldList[Plus, 0,
(Boole[DigitCount[#1, 10, 3] > 0] & ) /@ rng]]/
rng]; ]
Out[26]=
{7.859 Second,Null}
Regards,
Jean-Marc