Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: Self-teaching snag

  • To: mathgroup at
  • Subject: [mg74580] Re: Self-teaching snag
  • From: "Szabolcs" <szhorvat at>
  • Date: Tue, 27 Mar 2007 03:59:16 -0500 (EST)
  • References: <eu7re0$b6p$>

On Mar 26, 9:06 am, Todd Allen <genesplicer28 at> 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, no problem so far.
> However, when I try to ask for the output at
> charge[35], mathematica seems to enter an endless
> calculation.

Here Mathematica evaluates the two charge[day-1] *before* evaluating
Plus (i.e. adding them). There are two charge[ ] expressions, so with
every recursion step, the number of evaluations is doubled. For
day==35 you get 2^35 == 34359738368 a very large number of evaluations
(which takes a very long time).

If you use charge[day_] := 0.95 charge[day-1], the computation will be
much faster.

> 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."

Here Mathematica tries to evaluate charge[day], but day is a symbol
(not a number), so it never reaches 0, and the evaluation will never
end. (It will go down as day, day-1, day-2 etc.)

> 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??

Your definition of the charge[ ] function works only for integer
numbers (for a non-integer day value, you will never get 0, no matter
how many times you subtract 1). So there might not even be an integer
'day' number that gives you a charge of 0.15. You can check this by
looking at all some charge[day] values for small 'day' values.



You may want to look at RSolve if you want to get a closed expression
for your charge[] function using Mathematica. But this problem is best
described by a differential equation (if you want to work with a
continuous day variable, that is)

  • Prev by Date: Re: Self-teaching snag
  • Next by Date: Re: Self-teaching snag
  • Previous by thread: RE: how to pass a document to word
  • Next by thread: Re: Self-teaching snag