Re: Self-teaching snag

• To: mathgroup at smc.vnet.net
• Subject: [mg74584] Re: Self-teaching snag
• From: jesse.woodroffe at gmail.com
• Date: Tue, 27 Mar 2007 04:01:26 -0500 (EST)
• References: <eu7re0\$b6p\$1@smc.vnet.net>

```Todd,

Recursive call needs to be made with care, because they can quickly
chew up your system's resources. I'm not certain precisely how Mathematica's
handling your function, but the existence of two recursive calls on
the RHS means that (for any non-small) value of "day" you've got to
launch a whole lot of processes. The quickest fix for this problem
would be to replace

charge[day_]:=charge[day-1]-(0.05*charge[day-1])

with the more compact

charge[day_]:=0.95*charge[day-1]

As an aside, if you intend to use this function multiple times,it
might pay to use dynamic assigment,

charge[day_]:=charge[day]=0.95*charge[day-1];

This way, you keep a record of your past calls for future use.

There is a problem with solving your recursion for a particular final
charge, such as 0.15, since it is a discrete process and there is no
I'd recommend instead using the NestWhile/NestWhileList structure,
which allows you to effectively recurse a function while monitoring a
termination condition. For instance, a function that tells you how
many days until a certain charge level is passed might be

daysUntilCharge[finalCharge_] :=Module[{chargeRecord, decreaseDaily =
0=2E95, initialCharge = 1.0},
chargeRecord = NestWhileList[decreaseDaily*# &, initialCharge, # >
finalCharge &];
Dimensions[chargeRecord][[1]] - 1 (*Initial charge occupies
position 1*)
]

Using this function, we find

In[126]:=daysUntilCharge[0.15]
Out[126]=37

If you print out the list of values, you find that the charge at this
point is 0.14989 whereas the previous day had 0.15779 - 0.15, the
desired value, falls somewhere in between.

Hope that helps (and that I didn't make too many off-by-one errors!)

Jesse Woodroffe

On Mar 26, 2:06 am, Todd Allen <genesplice... 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?
>
>
> 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
>
> _________________________________________________________________________=