Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a complicated function that calculates the total value of payments from an account into which deposits are made annually. I'd like to solve the function for a growth rate numerically, but NSolve and FindRoot run for hours without returning a solution. This is the function:

    CalcDonation[seed_, growthrate_, reinvestrate_, annualcontrib_, horizon_] :=
     Module[{balance = 0, earnings, reinvestamount = 0, donation = 0},
      For[year = 1, year <= horizon , year++,
       (* No annual contribution in first year *)
       balance += If[year == 1, seed, annualcontrib + reinvestamount];
       earnings = balance * growthrate;
       reinvestamount = earnings * reinvestrate;
       donation += earnings *(1 - reinvestrate);
     ];
    donation
   ];

These are the attempts I have made so far:

FindRoot[CalcDonation[5000, g, 0.50, 1200, 36] == 5000 + 36*1200, {g, 0.05}]
NSolve[CalcDonation[5000, g, 0.50, 1200, 36] == 5000 + 36*1200, g, Reals]

Using Manipulate, I can approximate the answer as 0.0656782 (which is how I decided to use 0.05 as the starting point for FindRoot), but is there a way to solve this equation to a certain precision, besides FindRoot and NSolve? If not, how can I use these functions more effectively?

I'm not sure if this question has an answer that I can apply, since the question applies few details about the asker's situation.

share|improve this question
4  
You have the right idea with the linked question. Either changing the function definition to include growthrate_?NumericQ in place of growthrate_, or passing the option Evaluated -> False, will allow FindRoot to obtain the answer very quickly. To find it to a certain precision, use WorkingPrecision -> prec, where prec is your desired precision. In this case it is also better to express 0.5 as 1/2 and 0.05 as 1/20 because the former representations are machine precision numbers, which "poison" the calculation. – Oleksandr R. Dec 10 '12 at 4:43
1  
Question to others: do we have a canonical question for the _?NumericQ issue yet? I wasn't able to find it after a brief search. This question is well written and demonstrates the issue very clearly without any confounding details, so if not, maybe this can be it. If so I will post an answer rather than the above comment which I made on the assumption that this will eventually be closed as a duplicate and deleted when OP indicates his problem has been solved. – Oleksandr R. Dec 10 '12 at 5:20
@OleksandrR. What are constructs like _?NumericQ called? A brief search of the documentation doesn't suggest to me why your solution works, which is perhaps why I glossed over the reference to it in the linked question. – John Bensin Dec 10 '12 at 5:34
1  
@JohnBensin Try a ClearAll[] before evaluating – belisarius Dec 10 '12 at 5:53
1  
@JohnBensin they are called pattern tests. The reason it works is because if given non-numerical input your CalcDonation continues indefinitely, and with Evaluated -> True (the default) then it will be called with g in place of a numeric value before FindRoot has the opportunity to act on it. And belisarius is right--you probably didn't clear the existing definition before redefining it. If you had, the result should have been returned essentially immediately. – Oleksandr R. Dec 10 '12 at 5:54
show 3 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.