       Re: Re: Self-teaching snag

```Dear Todd,

Your question solicited a lot of responses, including mine below that
was not entirely correct. While everyone noted the problem with the
efficiency of recursive definitions, I wanted to add simply that even
the case of trying to solve for the time when the battery is at .95
(obviously after 1 day) fails because of the problem with machine
precision that I suggested in my original reply.

In:=charge=1.0 (*100%*);
charge[day_]:=(charge[day-1]-(0.05*charge[day-1]));
In:=Solve[charge[day]\[Equal]0.15,day]
From In:=\!\(\*
RowBox[{\(\$RecursionLimit::"reclim"\), \(\(:\)\(\ \)\), "\<\"Recursion \
depth of \\!\\(256\\) exceeded.
\\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", \
ButtonData:>\\\"\$RecursionLimit::reclim\\\"]\\)\"\>"}]\)
From In:=
\!\(\*

Out=\$Aborted

You have already been given numerous ways to get the answer you wanted.

Leigh

leigh pascoe wrote:
> 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=1.0 (* 100% *);
>> charge[day_]:=(charge[day-1]-(0.05*charge[day-1]));
>> charge
>>
>> 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, 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:=
>> 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
>>
>>
>>
>>
>>
>>
> This is not exactly an answer to your question, but recursive
> definitions are notoriously inefficient. However your basic problem is
> that you are mixing a function that is defined for integers and trying
> to solve an equation whose solution is not an exact multiple of days.
> Similarly when you try to find charge you are probably suffering
> from rounding errors due to machine precision and never arriving at the
> root of your recursion (charge). For example if you try to evaluate
> charge[30.00000000000000001] Ma will never arrive at a value by the
> recursive search.
>
> Your problem is easily soluble if you formulate it directly as a
> continuous variable as follows.
>
> charge[x]:=.95^x;
> charge
> for the first part,
>
> or
>
> Solve[ch[x]\[Equal].15,x]
>
> for the second.
>
> with a pencil and paper x=Log[.15]/Log[.95]=36.9857, which is the answer
> returned by Solve.
>
> I hope this helps.
>
> Leigh
>
>
>
>
>
>

```

• Prev by Date: Re: Re: Definite Integration in Mathematica
• Next by Date: Re: Re: Self-teaching snag
• Previous by thread: Re: Self-teaching snag
• Next by thread: Re: Self-teaching snag