Re: Plotting a Function - more questions
- To: mathgroup at smc.vnet.net
- Subject: [mg71759] Re: Plotting a Function - more questions
- From: "Jean-Marc Gulliet" <jeanmarc.gulliet at gmail.com>
- Date: Tue, 28 Nov 2006 06:04:13 -0500 (EST)
- References: <20061127165228.76340.qmail@web51502.mail.yahoo.com>
On 11/27/06, Craig Reed <tharkun860 at yahoo.com> wrote: > On 26 Nov 2006, you wrote in > comp.soft-sys.math.mathematica: > > > Timing[rng = Range[1, 100000]; > > ListPlot[Rest[FoldList[Plus, 0, > > (Boole[DigitCount[#1, 10, 3] > 0] & ) /@ > rng]]/ > > rng]; ] > > > > thank you for this piece of code. > > If I may, several questions: > > 1. I'm trying to get the 'x' scale to be a log scale > instead of linear. I have found that changing > 'ListPlot' to 'LogListPlot' doesn't produce a graph. > Can you explain why? You must load a specific add-on to be able to use LogListPlot. See below for an example. > 2. For my further understanding, can you explain just > how your code works? I've been looking key words up > in the Help Browser, but it's been my experience that > explanations from humans make more sense and/or are > more complete. Below, I have rewritten the code in a more manageable and easy to understand format. (At least, I hope so :-) I have also added some commentaries at the end. In[1]:= << "Graphics`Graphics`" mySearch[lowerBound_Integer, upperBound_Integer] := Module[{myValues, myTests, runningCount, myResult}, myValues = Range[lowerBound, upperBound]; myTests = (Boole[DigitCount[#1, 10, 3] > 0] & ) /@ myValues; runningCount = Rest[FoldList[Plus, 0, myTests]]; myResult = runningCount / myValues; myResult = DeleteCases[myResult, 0] ] In[3]:= Timing[lst = mySearch[1, 100000]; ][[1]] Out[3]= 7.453 Second In[4]:= DiplayTogetherArray[{ListPlot[lst], LogListPlot[lst]}]; In[5]:= myValues = Range[20, 30] Out[5]= {20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30} In[6]:= myTests = (Boole[DigitCount[#1, 10, 3] > 0] & ) /@ myValues Out[6]= {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1} (* FoldList returns the starting value in addition to the list of results. Therefore, we use the function Rest to get all the elements but the first. In our example from 20 to 30 (inclusive) we have 11 elements of interest. FoldLisr returns a list with 12 elements. The first 0 is discarded and runningCount has now the correct length. *) In[7]:= runningCount = Rest[FoldList[Plus, 0, myTests]] Out[7]= {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2} (* Mathematica works on whole lists. So {1, 2, 3} / {4, 5, 6} yields the list {1/4, 2/5, 1/3} (Note that 3/6 is automaticaly simplified to 1/3) *) In[8]:= myResult = runningCount / myValues Out[8]= 1 1 1 1 1 1 1 1 {0, 0, 0, --, --, --, --, --, --, --, --} 23 24 25 26 27 28 29 15 (* Finally we remove the first few elements equal to zero (really needed for when using LogListPlot *) In[9]:= myResult = DeleteCases[myResult, 0] Out[9]= 1 1 1 1 1 1 1 1 {--, --, --, --, --, --, --, --} 23 24 25 26 27 28 29 15 Regards, Jean-Marc