MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Self-teaching snag

  • To: mathgroup at smc.vnet.net
  • Subject: [mg74572] Re: [mg74556] Self-teaching snag
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Tue, 27 Mar 2007 03:54:56 -0500 (EST)
  • Organization: Mathematics & Statistics, Univ. of Mass./Amherst
  • References: <200703260704.CAA11373@smc.vnet.net>
  • Reply-to: murray at math.umass.edu

The trouble is that each and every time Mathematica calculates 
charge[day] for some particular value of day, it must recurse all the 
way back the initial value.  That is, once a value of charge is 
calculated, Mathematica forgets it.

Look at the following, for example:

   charge[0]=1.0 (* 100% *);
   charge[day_]:=(charge[day-1]-(0.05*charge[day-1]));
   charge[20]
0.358486

   ?charge   (* what does Mathematica know about charge? *)
Global`charge
charge[0]=1.
charge[day_]:=charge[day-1]-0.05 charge[day-1]

Indeed, Mathematica knows nothing more about charge than the original 
definitions, even though it calculated charge[20] -- and hence in the 
process had to calculate charge[19], charge[18], ..., charge[2], charge[1].

Now force Mathematica to "remember" any value of charge already 
calculated -- in technical terms, "cache" the calculated values -- by 
changing the definition to the following:

   charge[0]=1.0 (*100%*);
   charge[day_] := charge[day] = (charge[day-1]-(0.05*charge[day-1]));

Watch what happens now when, say, you calculate charge[4] (to keep the 
output here short):

   charge[4]
0.814506

   ?charge
Global`charge
charge[0]=1.
charge[1]=0.95
charge[2]=0.9025
charge[3]=0.857375
charge[4]=0.814506


Todd Allen wrote:
> Hi All,
> 
>    I am trying to refresh my skills in basic problem
> solving using Mathematica, but am running into some
> difficulties which are beginning to make me suspicious
> of Mathematica itself.  (I probably should be
> suspicious of my own brain...but you know how that is
> :-)
> 
> Here is the scenario:  I have written a basic function
> to tell me what percentage of battery power will
> remain in a battery after x number of days, provided
> that we start with a full charge and lose 5% of that
> charge per day.
> 
> If you execute the following code in Mathematica
> (V5.1):
> 
> charge[0]=1.0 (* 100% *);
> charge[day_]:=(charge[day-1]-(0.05*charge[day-1]));
> charge[20]
> 
> I receive an output of 0.358486 for my query at the 20
> day mark.....so, no problem so far.
> 
> However, when I try to ask for the output at
> charge[35], mathematica seems to enter an endless
> calculation.  I've let the computer run for as long as
> 5 minutes without getting an answer.  Is there
> something wrong with my function, my version of
> Mathematica or something else I haven't considered?
> 
> 
> Additionally,
> 
> When I try the following:
> 
> In[145]:=
> Solve[charge[day]==0.15,day];
> 
> Mathematica gives me the error:
> "$RecursionLimit::reclim: Recursion depth of 256
> exceeded."
> 
> I am trying to ask Mathematica to tell my how many
> days it takes to reduce the battery power to 15
> percent, but I must be messing something up??
> 
> If anyone has any pointers, I'd certainly appreciate
> it, because I am a little stuck right now.
> 
> Best regards,
> Todd Allen
> 
> 
>  
> ____________________________________________________________________________________
> We won't tell. Get more on shows you hate to love 
> (and love to hate): Yahoo! TV's Guilty Pleasures list.
> http://tv.yahoo.com/collections/265 
> 

-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305


  • Prev by Date: Re: Self-teaching snag
  • Next by Date: Re: Self-teaching snag
  • Previous by thread: Re: Self-teaching snag
  • Next by thread: Re: Self-teaching snag