I'm coding a 'Future Value of Annuity Due' calculator, that allows a user to find an unknown in the formula. The formula is fv = (1 + i) * pp * ((1 + i)**n - 1) / i
; where fv
is future value, pp
is periodic payment, i
is interest rate and n
is number of periods. As an example, assuming pp = 100
, i = .2735 (27.35%)
, and n = 11
, one obtains fv = 6187.56
. Without knowing i
, I can interpolate the rate to be say 25%, and would like to use Newton-Raphson iteration to get a more exact answer. However, my code below is off, as it's diverging (it seems to work for small values of i
, i.e. 5%).
fv = 11807.795
pp = 1000
n = 10
i = .03
def newton_raphson_method(fv,pp,i,n):
newton_raphson_i = i
for num in range(1,20):
newton_raphson_i = i - (1+i)*(pp*(1+i)**n - pp-fv*i) / ((n +1)*pp*(1+i)**n - fv)
i = newton_raphson_i
print(i)
i = round(i,11)
print('')
print ('The newton interest rate is ' + str("%.9f" % (i * 100)) + '%')
print('')