|
-
Oct 2nd, 2013, 11:19 PM
#9
Re: This interest rate, how ?
 Originally Posted by Niya
Hope he's still active.
I am, just intermittently.
Atenk is right, inasmuch as the problem can't in general be solved algebraically (and standard special functions don't seem to be enough). Nonetheless, the function is very well-behaved, so something as simple as Newton's Method can solve it easily. The following is a Python script illustrating the technique. If you need it explained or translated, please ask.
Code:
A = 1000
P = 30
N = 56
def f(I):
return P*(1-(1+I)**(-N))/I - A
def f_prime(I):
return ((-1 + (1+I)**(-1-N) * (1+I+I*N)) * P)/(I*I)
def newt(I,n):
for i in range(n):
if f_prime(I) == 0:
return I
I = I - f(I)/f_prime(I)
return I
print(newt(0.0001, 5)) # prints 0.020223511346654406
Some notes:
* You can choose to iterate more or fewer steps, or to iterate until a desired precision (or until too many steps have passed).
* I haven't checked the stability of this initial value. You should check this method with many varied inputs to make sure it works as desired in all cases. For instance, if your initial value for I is 0.5, the algorithm doesn't find the root and returns something enormous.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|