[Date Index]
[Thread Index]
[Author Index]
Re: Self-teaching snag
*To*: mathgroup at smc.vnet.net
*Subject*: [mg74580] Re: Self-teaching snag
*From*: "Szabolcs" <szhorvat at gmail.com>
*Date*: Tue, 27 Mar 2007 03:59:16 -0500 (EST)
*References*: <eu7re0$b6p$1@smc.vnet.net>
On Mar 26, 9:06 am, 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.
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.
In[22]:=
Table[charge[n],{n,1,50}]
Out[22]=
{0.95,0.9025,0.857375,0.814506,0.773781,0.735092,0.698337,0.66342,0.630249,0.\
598737,0.5688,0.54036,0.513342,0.487675,0.463291,0.440127,0.41812,0.397214,0.\
377354,0.358486,0.340562,0.323534,0.307357,0.291989,0.27739,0.26352,0.250344,\
0.237827,0.225936,0.214639,0.203907,0.193711,0.184026,0.174825,0.166083,0.\
157779,0.14989,0.142396,0.135276,0.128512,0.122087,0.115982,0.110183,0.104674,\
0.0994403,0.0944682,0.0897448,0.0852576,0.0809947,0.076945}
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**
| |