Re: Self-teaching snag

*To*: mathgroup at smc.vnet.net*Subject*: [mg74576] Re: [mg74556] Self-teaching snag*From*: Stern <nycstern at gmail.com>*Date*: Tue, 27 Mar 2007 03:57:06 -0500 (EST)*References*: <200703260704.CAA11373@smc.vnet.net>

Your method, though it works, never refers to previously computed values when calculating new values. As a result, it recomputes the earlier steps over and over again. There are many ways to compute something like this in mathematica that use recursion more efficiently. Consider the following, for example: betterway[day_]:=FoldList[(.95*#1) &, 1, Range[day]] The gap in performance between this method and yours grows as your day target increases. On my machine, this solution is effectively instantaneous past day 50000. Of course, for the problem you have expressed, you don't need a recursive solution at all; you could just solve betterway2[day_] := .95^day which is effectively instantaneous and uses no memory to speak of, almost regardless of how big your day count is. Of course, as expressed above, it reports only the final charge, not the charge on each day getting there. That can be fixed in a variety of ways, if you want, including: betterway3[day_] := .95^Range[day] Of the methods shown, betterway2 is the most suited to solving for a given charge level, as it reports only a single number for its output. Solve[betterway2[day] == 0.15, day] works just fine. Cheers, Michael Stern On 3/26/07, Todd Allen <genesplicer28 at yahoo.com> 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 > >

**References**:**Self-teaching snag***From:*Todd Allen <genesplicer28@yahoo.com>