Re: Self-teaching snag
- To: mathgroup at smc.vnet.net
- Subject: [mg74597] Re: [mg74556] Self-teaching snag
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Tue, 27 Mar 2007 04:08:26 -0500 (EST)
- Reply-to: hanlonr at cox.net
To speed recursions up, add memory to the function definition so that it doesn't repeat prior recursions. Clear[charge1]; charge1[0]=1; charge1[day_Integer?Positive]:= charge1[day]=0.95charge1[day-1]; charge1[20] 0.358486 charge1[35] 0.166083 However, the recursion can be avoided with Clear[charge2,rate]; charge2[day_,rate_:0.95]:=rate^day; Off[Solve::ifun]; d=day/.Solve[charge2[day]==0.15,day][[1]] 36.9857 Plot[charge2[day],{day,0,40}, Epilog->{Red, AbsolutePointSize[4], Point/@Table[{n,charge1[n]},{n,0,40,4}], Blue,AbsoluteDashing[{5, 10}], Line[{{0,0.15},{d,0.15},{d,0}}]}, ImageSize->500]; If the result for charge2 is not obvious it can be obtained from RSolve Clear[charge,day,rate]; charge[day]/.RSolve[{charge[day]==rate* charge[day-1],charge[0]==1},charge[day],day][[1]] rate^day Bob Hanlon ---- 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 >